Sha256: b65239da7ecbe366a797dddc57daf58a82d94e76e099e20ddc73ccb78579dcea

Contents?: true

Size: 626 Bytes

Versions: 8

Compression:

Stored size: 626 Bytes

Contents

module Redde::WithPhoto
  extend ActiveSupport::Concern

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

  included do
    attr_accessor :photo_tokens
    has_many :photos, dependent: :destroy, as: :imageable
    after_create :assign_photos
  end

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

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

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

  def tokens
    photo_tokens || []
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
redde-0.1.18 lib/redde/concerns/with_photo.rb
redde-0.1.17 lib/redde/concerns/with_photo.rb
redde-0.1.16 lib/redde/concerns/with_photo.rb
redde-0.1.15 lib/redde/concerns/with_photo.rb
redde-0.1.14 lib/redde/concerns/with_photo.rb
redde-0.1.13 lib/redde/concerns/with_photo.rb
redde-0.1.12 lib/redde/concerns/with_photo.rb
redde-0.1.11 lib/redde/concerns/with_photo.rb