module Shrine::Plugins::Persistence

  1. lib/shrine/plugins/_persistence.rb

Helper plugin that defines persistence methods on the attacher according to convention.

plugin :_persistence, plugin: MyPlugin

Methods

Public Class

  1. configure
  2. load_dependencies

Public Class methods

configure(uploader, plugin:)

Using persist, #<name>reload, and

  • Attacher#persist

  • Attacher#atomic_persist

  • Attacher#atomic_promote

[show source]
   # File lib/shrine/plugins/_persistence.rb
21 def self.configure(uploader, plugin:)
22   plugin_name = plugin.to_s.split("::").last.downcase
23 
24   plugin::AttacherMethods.module_eval do
25     define_method :atomic_promote do |**options, &block|
26       return super(**options, &block) unless send(:"#{plugin_name}?")
27 
28       abstract_atomic_promote(
29         reload:  method(:"#{plugin_name}_reload"),
30         persist: method(:"#{plugin_name}_persist"),
31         **options, &block
32       )
33     end
34 
35     define_method :atomic_persist do |*args, **options, &block|
36       return super(*args, **options, &block) unless send(:"#{plugin_name}?")
37 
38       abstract_atomic_persist(
39         *args,
40         reload:  method(:"#{plugin_name}_reload"),
41         persist: method(:"#{plugin_name}_persist"),
42         **options, &block
43       )
44     end
45 
46     define_method :persist do
47       return super() unless send(:"#{plugin_name}?")
48 
49       send(:"#{plugin_name}_persist")
50     end
51 
52     define_method :hash_attribute? do
53       return super() unless send(:"#{plugin_name}?")
54 
55       respond_to?(:"#{plugin_name}_hash_attribute?", true) &&
56       send(:"#{plugin_name}_hash_attribute?")
57     end
58     private :hash_attribute?
59   end
60 end
load_dependencies(uploader, *)
[show source]
   # File lib/shrine/plugins/_persistence.rb
10 def self.load_dependencies(uploader, *)
11   uploader.plugin :atomic_helpers
12   uploader.plugin :entity
13 end