class Image < ApplicationRecord belongs_to :user has_many :image_tags, dependent: :destroy has_many :tags, through: :image_tags has_one_attached :file validates :title, presence: true validates :file, presence: true, on: :create # Status for review process enum :status, { pending: 0, approved: 1, rejected: 2 }, default: :pending # Scopes for filtering scope :pending, -> { where(status: :pending) } scope :approved, -> { where(status: :approved) } scope :rejected, -> { where(status: :rejected) } ransacker :status, formatter: ->(value) { statuses[value] } def self.ransackable_attributes(auth_object = nil) [ "created_at", "id", "id_value", "status", "title", "updated_at", "user_id" ] end def self.ransackable_associations(auth_object = nil) [ "image_tags", "tags", "user" ] end # Returns the URL for the attached file def file_url file.attached? ? Rails.application.routes.url_helpers.rails_blob_path(file, only_path: true) : nil end # Returns the URL for a thumbnail version of the attached file def thumbnail_url # return nil unless file.attached? && file.content_type.start_with?("image/") # variant = file.variant(resize_to_limit: [300, 300]).processed # Rails.application.routes.url_helpers.rails_blob_path(variant, only_path: true) end # Returns the URL for a medium-sized version of the attached file def medium_url # return nil unless file.attached? && file.content_type.start_with?("image/") # variant = file.variant(resize_to_limit: [600, 600]).processed # Rails.application.routes.url_helpers.rails_blob_path(variant, only_path: true) end # Method to set tags by IDs def set_tags_by_ids(tag_ids) return if tag_ids.nil? # Clear existing tags if needed tags.clear # Add tags by ID tag_ids.each do |id| tag = Tag.find_by(id: id) tags << tag if tag && !tags.include?(tag) end end end