Sha256: 9759b5e2f1956fe49a9a9f44d2b67794e763ba33800eae4b82ce4a2a95e40c7f

Contents?: true

Size: 665 Bytes

Versions: 45

Compression:

Stored size: 665 Bytes

Contents

module Redde::WithPhoto
  extend ActiveSupport::Concern

  QUERY = 'imageable_id = :id AND imageable_type = :type OR token in (:tokens)'.freeze

  included do
    attr_accessor :photo_tokens
    has_many :photos, class_name: 'Redde::Photo', dependent: :destroy, as: :imageable
    after_create :assign_photos
  end

  def all_photos
    Redde::Photo.where(QUERY, id: id, type: self.class.name, tokens: tokens)
  end

  def assign_photos
    Redde::Photo.where(token: tokens).update_all(imageable_attributes)
  end

  def imageable_attributes
    { imageable_id: id, imageable_type: self.class.name, token: nil }
  end

  def tokens
    photo_tokens || []
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
redde-0.3.24 app/models/concerns/redde/with_photo.rb
redde-0.3.23 app/models/concerns/redde/with_photo.rb
redde-0.3.22 app/models/concerns/redde/with_photo.rb
redde-0.3.21 app/models/concerns/redde/with_photo.rb
redde-0.3.20 app/models/concerns/redde/with_photo.rb
redde-0.3.19 app/models/concerns/redde/with_photo.rb
redde-0.3.18 app/models/concerns/redde/with_photo.rb
redde-0.3.17 app/models/concerns/redde/with_photo.rb
redde-0.3.16 app/models/concerns/redde/with_photo.rb
redde-0.3.15 app/models/concerns/redde/with_photo.rb
redde-0.3.14 app/models/concerns/redde/with_photo.rb
redde-0.3.13 app/models/concerns/redde/with_photo.rb
redde-0.3.12 app/models/concerns/redde/with_photo.rb
redde-0.3.11 app/models/concerns/redde/with_photo.rb
redde-0.3.10 app/models/concerns/redde/with_photo.rb
redde-0.3.9 app/models/concerns/redde/with_photo.rb
redde-0.3.8 app/models/concerns/redde/with_photo.rb
redde-0.3.7 app/models/concerns/redde/with_photo.rb
redde-0.3.6 app/models/concerns/redde/with_photo.rb
redde-0.3.5 app/models/concerns/redde/with_photo.rb