Module: AfterAttached::AttachmentCallbacks

Extended by:
ActiveSupport::Concern
Defined in:
lib/after_attached/attachment_callbacks.rb

Instance Method Summary collapse

Instance Method Details

#_run_after_attached_callbacks(attachment) ⇒ Object



67
68
69
# File 'lib/after_attached/attachment_callbacks.rb', line 67

def _run_after_attached_callbacks(attachment)
  _run_attachment_callbacks(:after_attached, attachment)
end

#_run_after_detached_callbacks(attachment) ⇒ Object



75
76
77
# File 'lib/after_attached/attachment_callbacks.rb', line 75

def _run_after_detached_callbacks(attachment)
  _run_attachment_callbacks(:after_detached, attachment)
end

#_run_attachment_callbacks(callback_type, attachment) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/after_attached/attachment_callbacks.rb', line 50

def _run_attachment_callbacks(callback_type, attachment)
  callbacks = self.class._attachment_callbacks[callback_type][attachment.name]
  return unless callbacks

  callbacks.each do |cb|
    invoke_callback(cb, attachment)
  end
rescue StandardError => e
  Rails.logger.error("#{callback_type} callback failed on #{self.class}##{attachment.name}: #{e.message}")
  raise
end

#_run_before_attached_callbacks(attachment) ⇒ Object

Convenience methods for backward compatibility and clarity



63
64
65
# File 'lib/after_attached/attachment_callbacks.rb', line 63

def _run_before_attached_callbacks(attachment)
  _run_attachment_callbacks(:before_attached, attachment)
end

#_run_before_detached_callbacks(attachment) ⇒ Object



71
72
73
# File 'lib/after_attached/attachment_callbacks.rb', line 71

def _run_before_detached_callbacks(attachment)
  _run_attachment_callbacks(:before_detached, attachment)
end

#invoke_callback(callback, attachment) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/after_attached/attachment_callbacks.rb', line 79

def invoke_callback(callback, attachment)
  case callback
  when Symbol, String
    send(callback, attachment)
  else
    instance_exec(attachment, &callback)
  end
end