Module: Shrine::Plugins::Mongoid::AttachmentMethods

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

Instance Method Summary collapse

Instance Method Details

#included(model) ⇒ Object



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
48
49
50
51
52
# File 'lib/shrine/plugins/mongoid.rb', line 19

def included(model)
  super

  return unless model < ::Mongoid::Document

  name = @name

  if shrine_class.opts[:mongoid][:validations]
    # add validation plugin integration
    model.validate do
      send(:"#{name}_attacher").send(:mongoid_validate)
    end
  end

  if shrine_class.opts[:mongoid][:callbacks]
    model.before_save do
      send(:"#{name}_attacher").send(:mongoid_before_save)
    end

    model.after_save do
      send(:"#{name}_attacher").send(:mongoid_after_save)
    end

    model.after_destroy do
      send(:"#{name}_attacher").send(:mongoid_after_destroy)
    end
  end

  define_method :reload do |*args|
    result = super(*args)
    instance_variable_set(:"@#{name}_attacher", nil)
    result
  end
end