module Shrine::Plugins::AtomicHelpers::AttacherMethods

  1. lib/shrine/plugins/atomic_helpers.rb

Public Instance methods

abstract_atomic_persist(original_file = file, reload:, persist:)

Reloads the record to check whether the attachment has changed. If it hasn’t, it persists the record. Otherwise it raises Shrine::AttachmentChanged exception.

attacher.abstract_atomic_persist(
  reload:  reload_strategy,
  persist: persist_strategy,
)

This more convenient to use with concrete persistence plugins, which provide defaults for reloading and persistence.

[show source]
   # File lib/shrine/plugins/atomic_helpers.rb
71 def abstract_atomic_persist(original_file = file, reload:, persist:)
72   abstract_reload(reload) do |attacher|
73     if attacher && attacher.file != original_file
74       fail Shrine::AttachmentChanged, "attachment has changed"
75     end
76 
77     yield attacher if block_given?
78 
79     abstract_persist(persist)
80   end
81 end
abstract_atomic_promote(reload:, persist:, **options, &block)

Like promote, but additionally persists the promoted file atomically. You need to specify :reload and :persist strategies when calling the method:

attacher.abstract_atomic_promote(
  reload:  reload_strategy,
  persist: persist_strategy,
)

This more convenient to use with concrete persistence plugins, which provide defaults for reloading and persistence.

[show source]
   # File lib/shrine/plugins/atomic_helpers.rb
46 def abstract_atomic_promote(reload:, persist:, **options, &block)
47   original_file = file
48 
49   result = promote(**options)
50 
51   begin
52     abstract_atomic_persist(original_file, reload: reload, persist: persist, &block)
53     result
54   rescue Shrine::AttachmentChanged
55     destroy_attached
56     raise
57   end
58 end
file_data()

Return only needed main file data, without the metadata. This allows you to avoid bloating your background job payload when you have derivatives or lots of metadata, by only sending data you need for atomic persitence.

attacher.file_data #=> { "id" => "abc123.jpg", "storage" => "store" }
[show source]
   # File lib/shrine/plugins/atomic_helpers.rb
89 def file_data
90   file!.data.reject { |key, _| key == "metadata" }
91 end