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.4.4 app/models/concerns/redde/with_photo.rb
redde-0.4.3 app/models/concerns/redde/with_photo.rb
redde-0.4.2 app/models/concerns/redde/with_photo.rb
redde-0.4.1 app/models/concerns/redde/with_photo.rb
redde-0.4.0 app/models/concerns/redde/with_photo.rb
redde-0.3.39 app/models/concerns/redde/with_photo.rb
redde-0.3.38 app/models/concerns/redde/with_photo.rb
redde-0.3.37 app/models/concerns/redde/with_photo.rb
redde-0.3.36 app/models/concerns/redde/with_photo.rb
redde-0.3.35 app/models/concerns/redde/with_photo.rb
redde-0.3.34 app/models/concerns/redde/with_photo.rb
redde-0.3.33 app/models/concerns/redde/with_photo.rb
redde-0.3.32 app/models/concerns/redde/with_photo.rb
redde-0.3.31 app/models/concerns/redde/with_photo.rb
redde-0.3.30 app/models/concerns/redde/with_photo.rb
redde-0.3.29 app/models/concerns/redde/with_photo.rb
redde-0.3.28 app/models/concerns/redde/with_photo.rb
redde-0.3.27 app/models/concerns/redde/with_photo.rb
redde-0.3.26 app/models/concerns/redde/with_photo.rb
redde-0.3.25 app/models/concerns/redde/with_photo.rb