module Paperweight::Hooks::RecordHook

Overrides the `has_attached_file` method from `paperclip` so that `paperweight` can add extras when the macro is called.

Public Instance Methods

has_attached_file(name, options = {}) click to toggle source

rubocop:disable Naming/PredicateName

Calls superclass method
# File lib/paperweight/hooks.rb, line 10
def has_attached_file(name, options = {})
  after_download = options.delete(:after_download)
  super

  name = AttachmentName.new(name)
  attr_reader name.url

  define_paperweight_setter_for(name)
  define_paperweight_after_commit_for(name)
  define_method(name.after_download, &after_download) if after_download
end

Private Instance Methods

define_paperweight_after_commit_for(name) click to toggle source
# File lib/paperweight/hooks.rb, line 37
def define_paperweight_after_commit_for(name)
  after_commit if: name.url do
    PostProcessJob.perform_later(self, name.name.to_s)
  end
end
define_paperweight_setter_for(name) click to toggle source

rubocop:enable Naming/PredicateName

# File lib/paperweight/hooks.rb, line 25
def define_paperweight_setter_for(name)
  define_method(name.url_eq) do |value|
    instance_variable_set(name.url_attr, value)
    self[name.processing] = value

    return unless value

    self[name.updated_at] = Time.now
    self.updated_at = Time.now
  end
end