Module: Shrine::Plugins::Sequel::AttachmentMethods

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

Instance Method Summary collapse

Instance Method Details

#included(model) ⇒ Object


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
61
62
63
64
65
66
67
68
69
# File 'lib/shrine/plugins/sequel.rb', line 25

def included(model)
  super

  return unless model < ::Sequel::Model

  name = @name

  if shrine_class.opts[:sequel][:validations]
    define_method :validate do
      super()
      send(:"#{name}_attacher").send(:sequel_validate)
    end
  end

  if shrine_class.opts[:sequel][:hooks]
    define_method :before_save do
      super()
      if send(:"#{name}_attacher").changed?
        send(:"#{name}_attacher").send(:sequel_before_save)
      end
    end

    define_method :after_save do
      super()
      if send(:"#{name}_attacher").changed?
        send(:"#{name}_attacher").send(:sequel_after_save)
      end
    end

    define_method :after_destroy do
      super()
      if send(:"#{name}_attacher").attached?
        send(:"#{name}_attacher").send(:sequel_after_destroy)
      end
    end
  end

  # reload the attacher on record reload
  define_method :_refresh do |*args|
    result = super(*args)
    send(:"#{name}_attacher").reload if instance_variable_defined?(:"@#{name}_attacher")
    result
  end
  private :_refresh
end