module Shrine::Plugins::AtomicHelpers::AttacherMethods
Public Instance Methods
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.
# File lib/shrine/plugins/atomic_helpers.rb, line 71 def abstract_atomic_persist(original_file = file, reload:, persist:) abstract_reload(reload) do |attacher| if attacher && attacher.file != original_file fail Shrine::AttachmentChanged, "attachment has changed" end yield attacher if block_given? abstract_persist(persist) end end
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.
# File lib/shrine/plugins/atomic_helpers.rb, line 46 def abstract_atomic_promote(reload:, persist:, **options, &block) original_file = file result = promote(**options) begin abstract_atomic_persist(original_file, reload: reload, persist: persist, &block) result rescue Shrine::AttachmentChanged destroy_attached raise end end
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" }
# File lib/shrine/plugins/atomic_helpers.rb, line 89 def file_data file!.data.reject { |key, _| key == "metadata" } end
Protected Instance Methods
Calls the persist strategy.
# File lib/shrine/plugins/atomic_helpers.rb, line 109 def abstract_persist(strategy) return if strategy == false strategy.call end
Calls the reload strategy and yields a reloaded attacher from the reloaded record.
# File lib/shrine/plugins/atomic_helpers.rb, line 97 def abstract_reload(strategy) return yield if strategy == false strategy.call do |record| reloaded_attacher = dup reloaded_attacher.load_entity(record, name) yield reloaded_attacher end end