Module: Refile::Sequel::Attachment

Includes:
Attachment
Defined in:
lib/refile/sequel/attachment.rb

Instance Method Summary collapse

Instance Method Details

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



8
9
10
11
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
43
# File 'lib/refile/sequel/attachment.rb', line 8

def attachment(name, raise_errors: false, **options)
  super # includes a module

  attachment_module = ancestors[1]
  attacher = "#{name}_attacher"

  attachment_module.class_eval do
    define_method(:validate) do
      super()
      if send(attacher).present?
        send(attacher).valid?
        send(attacher).errors.each do |error|
          errors.add(name, error)
        end
      end
    end

    define_method(:before_save) do
      super()
      send(attacher).store!
    end

    define_method(:after_destroy) do
      super()
      send(attacher).delete!
    end

    # Sequel::Model#update internally calls #save_changes, which saves
    # the record only if columns have changed. So, if you were to do
    # `User[23].update(avatar: new_image)`, the attachment wouldn't get
    # updated, because changing of "avatar_id" column happens only in
    # `before_save`, which doesn't get triggered. This forces each record
    # to always save itself, thus triggering all hooks.
    define_method(:save_changes) { save }
  end
end