Class: Shrine::Plugins::Hanami::RepositoryMethods

Inherits:
Module
  • Object
show all
Defined in:
lib/shrine/plugins/hanami.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ RepositoryMethods

Returns a new instance of RepositoryMethods.



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
61
62
63
# File 'lib/shrine/plugins/hanami.rb', line 34

def initialize(name)
  module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def create(entity)
      save_#{name}_attachment(entity) { super }
    end
    def update(entity)
      save_#{name}_attachment(entity) { super }
    end
    def persist(entity)
      save_#{name}_attachment(entity) { super }
    end
    def delete(entity)
      delete_#{name}_attachment(entity) { super }
    end

    private
    def save_#{name}_attachment(entity)
      entity.#{name}_attacher.save

      entity.#{name}_attacher.replace
      entity.#{name}_attacher._promote
      yield
    end

    def delete_#{name}_attachment(entity)
      yield
      entity.#{name}_attacher.destroy
    end
  RUBY
end