Module: Bulldog::HasAttachment::InstanceMethods
- Defined in:
- lib/bulldog/has_attachment.rb
Class Method Summary collapse
Instance Method Summary collapse
- #attachment_reflection_for(name) ⇒ Object
- #destroy_attachments ⇒ Object
- #process_attachment(name, event, *args) ⇒ Object
- #save_attachments ⇒ Object
- #update_attachment_timestamps ⇒ Object
Class Method Details
.included(base) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bulldog/has_attachment.rb', line 24 def self.included(base) base.instance_variable_set(:@attachment_reflections, {}) # We need to store the attachment changes ourselves, since # they're unavailable in an after_save. base.before_save :store_original_attachments base.after_save :save_attachments base.after_save :clear_original_attachments base.before_save :update_attachment_timestamps base.after_destroy :destroy_attachments # Force initialization of attachments, as #destroy will freeze # the attributes afterwards. base.before_destroy :initialize_remaining_attachments %w[validation save create update].each do |event| base.send("before_#{event}", "process_attachments_for_before_#{event}") base.send("after_#{event}", "process_attachments_for_after_#{event}") end end |
Instance Method Details
#attachment_reflection_for(name) ⇒ Object
76 77 78 |
# File 'lib/bulldog/has_attachment.rb', line 76 def (name) self.class.[name] end |
#destroy_attachments ⇒ Object
54 55 56 57 58 |
# File 'lib/bulldog/has_attachment.rb', line 54 def .each do |name, reflection| (name).destroy end end |
#process_attachment(name, event, *args) ⇒ Object
70 71 72 73 74 |
# File 'lib/bulldog/has_attachment.rb', line 70 def (name, event, *args) reflection = [name] or raise ArgumentError, "no such attachment: #{name}" (name).process(event, *args) end |
#save_attachments ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/bulldog/has_attachment.rb', line 46 def .each do |name, reflection| = @original_attachments[name] and .destroy (name).save end end |
#update_attachment_timestamps ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/bulldog/has_attachment.rb', line 60 def .each do |name, reflection| next unless send("#{name}_changed?") setter = "#{name}_updated_at=" if respond_to?(setter) send(setter, Time.now) end end end |