module Shrine::Plugins::Sequel::AttachmentMethods

  1. lib/shrine/plugins/sequel.rb

Methods

Public Instance

  1. included

Public Instance methods

included(model)
[show source]
   # File lib/shrine/plugins/sequel.rb
25 def included(model)
26   super
27 
28   return unless model < ::Sequel::Model
29 
30   name = @name
31 
32   if shrine_class.opts[:sequel][:validations]
33     define_method :validate do
34       super()
35       send(:"#{name}_attacher").send(:sequel_validate)
36     end
37   end
38 
39   if shrine_class.opts[:sequel][:hooks]
40     define_method :before_save do
41       super()
42       if send(:"#{name}_attacher").changed?
43         send(:"#{name}_attacher").send(:sequel_before_save)
44       end
45     end
46 
47     define_method :after_save do
48       super()
49       if send(:"#{name}_attacher").changed?
50         send(:"#{name}_attacher").send(:sequel_after_save)
51       end
52     end
53 
54     define_method :after_destroy do
55       super()
56       if send(:"#{name}_attacher").attached?
57         send(:"#{name}_attacher").send(:sequel_after_destroy)
58       end
59     end
60   end
61 
62   # reload the attacher on record reload
63   define_method :_refresh do |*args|
64     result = super(*args)
65     send(:"#{name}_attacher").reload if instance_variable_defined?(:"@#{name}_attacher")
66     result
67   end
68   private :_refresh
69 end