module Shrine::Plugins::AtomicHelpers::AttacherClassMethods

Public Instance Methods

retrieve(model: nil, entity: nil, name:, file:, **options) click to toggle source

Retrieves the attacher from the given entity/model and verifies that the attachment hasn’t changed. It raises ‘Shrine::AttachmentChanged` exception if the attached file doesn’t match.

Shrine::Attacher.retrieve(model: photo, name: :image, file: file_data)
#=> #<ImageUploader::Attacher>
# File lib/shrine/plugins/atomic_helpers.rb, line 17
def retrieve(model: nil, entity: nil, name:, file:, **options)
  fail ArgumentError, "either :model or :entity is required" unless model || entity

  record = model || entity

  attacher   = record.send(:"#{name}_attacher", **options) if record.respond_to?(:"#{name}_attacher")
  attacher ||= from_model(record, name, **options) if model
  attacher ||= from_entity(record, name, **options) if entity

  if attacher.file != attacher.uploaded_file(file)
    fail Shrine::AttachmentChanged, "attachment has changed"
  end

  attacher
end