Module: Shrine::Plugins::Persistence

Defined in:
lib/shrine/plugins/_persistence.rb

Overview

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

plugin :_persistence, plugin: MyPlugin

Defined Under Namespace

Modules: AttacherMethods

Class Method Summary collapse

Class Method Details

.configure(uploader, plugin:) ⇒ Object

Using #<name>_persist, #<name>_reload, and #<name>?, defines the following methods for a persistence plugin:

* Attacher#persist
* Attacher#atomic_persist
* Attacher#atomic_promote


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shrine/plugins/_persistence.rb', line 21

def self.configure(uploader, plugin:)
  plugin_name = plugin.to_s.split("::").last.downcase

  plugin::AttacherMethods.module_eval do
    define_method :atomic_promote do |**options, &block|
      return super(**options, &block) unless send(:"#{plugin_name}?")

      abstract_atomic_promote(
        reload:  method(:"#{plugin_name}_reload"),
        persist: method(:"#{plugin_name}_persist"),
        **options, &block
      )
    end

    define_method :atomic_persist do |*args, **options, &block|
      return super(*args, **options, &block) unless send(:"#{plugin_name}?")

      abstract_atomic_persist(
        *args,
        reload:  method(:"#{plugin_name}_reload"),
        persist: method(:"#{plugin_name}_persist"),
        **options, &block
      )
    end

    define_method :persist do
      return super() unless send(:"#{plugin_name}?")

      send(:"#{plugin_name}_persist")
    end

    define_method :hash_attribute? do
      return super() unless send(:"#{plugin_name}?")

      respond_to?(:"#{plugin_name}_hash_attribute?", true) &&
      send(:"#{plugin_name}_hash_attribute?")
    end
    private :hash_attribute?
  end
end

.load_dependencies(uploader) ⇒ Object



10
11
12
13
# File 'lib/shrine/plugins/_persistence.rb', line 10

def self.load_dependencies(uploader, *)
  uploader.plugin :atomic_helpers
  uploader.plugin :entity
end