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

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

Instance Method Summary collapse

Instance Method Details

#included(model) ⇒ Object



12
13
14
15
16
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
# File 'lib/shrine/plugins/mongoid.rb', line 12

def included(model)
  super

  return unless model < ::Mongoid::Document

  if shrine_class.opts[:mongoid_validations]
    model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      validate do
        #{@name}_attacher.errors.each do |message|
          errors.add(:#{@name}, message)
        end
      end
    RUBY
  end

  if shrine_class.opts[:mongoid_callbacks]
    model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      before_save do
        #{@name}_attacher.save if #{@name}_attacher.attached?
      end

      after_save do
        #{@name}_attacher.finalize if #{@name}_attacher.attached?
      end

      after_destroy do
        #{@name}_attacher.destroy
      end
    RUBY
  end
end