module Shrine::Plugins::AtomicHelpers::AttacherClassMethods

  1. lib/shrine/plugins/atomic_helpers.rb

Methods

Public Instance

  1. retrieve

Public Instance methods

retrieve(model: nil, entity: nil, name:, file:, **options)

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>
[show source]
   # File lib/shrine/plugins/atomic_helpers.rb
17 def retrieve(model: nil, entity: nil, name:, file:, **options)
18   fail ArgumentError, "either :model or :entity is required" unless model || entity
19 
20   record = model || entity
21 
22   attacher   = record.send(:"#{name}_attacher", **options) if record.respond_to?(:"#{name}_attacher")
23   attacher ||= from_model(record, name, **options) if model
24   attacher ||= from_entity(record, name, **options) if entity
25 
26   if attacher.file != attacher.uploaded_file(file)
27     fail Shrine::AttachmentChanged, "attachment has changed"
28   end
29 
30   attacher
31 end