Files
growstuff/app/models/concerns/photo_capable.rb
T
2026-09-18 13:24:31 +09:30

33 lines
853 B
Ruby

# frozen_string_literal: true
module PhotoCapable
extend ActiveSupport::Concern
included do
has_many :photo_associations, as: :photographable, dependent: :delete_all, inverse_of: :photographable
has_many :photos, through: :photo_associations, as: :photographable
scope :has_photos, -> { includes(:photos).where.not(photos: { id: nil }) }
def default_photo
most_liked_photo
end
def thumbnail_url
Rails.cache.fetch("#{cache_key_with_version}/thumbnail_url", expires_in: 8.hours) do
df = default_photo
if df
df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url
elsif respond_to?(:crop) && crop.present?
crop.thumbnail_url
end
end
end
def most_liked_photo
photos.order(likes_count: :desc, created_at: :desc).first
end
end
end