17
18
19
20
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
|
# File 'lib/shrine/plugins/activerecord.rb', line 17
def included(model)
super
return unless model < ::ActiveRecord::Base
name = attachment_name
if shrine_class.opts[:activerecord_validations]
model.validate do
send("#{name}_attacher").errors.each do |message|
errors.add(name, *message)
end
end
end
if shrine_class.opts[:activerecord_callbacks]
model.before_save do
attacher = send("#{name}_attacher")
attacher.save if attacher.changed?
end
model.after_commit on: [:create, :update] do
attacher = send("#{name}_attacher")
attacher.finalize if attacher.changed?
end
model.after_commit on: [:destroy] do
send("#{name}_attacher").destroy
end
end
end
|