Module: Refile::ActiveRecord::Attachment

Includes:
Refile::Attachment
Defined in:
lib/refile/attachment/active_record.rb

Instance Method Summary collapse

Instance Method Details

#attachment(name, raise_errors: false, **options) ⇒ Object

Attachment method which hooks into ActiveRecord models


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/refile/attachment/active_record.rb', line 9

def attachment(name, raise_errors: false, **options)
  super

  attacher = "#{name}_attacher"

  validate do
    if send(attacher).present?
      send(attacher).valid?
      errors = send(attacher).errors
      errors.each do |error|
        self.errors.add(name, error)
      end
    end
  end

  before_save do
    send(attacher).store!
  end

  after_destroy do
    send(attacher).delete!
  end
end