Module: ActiveMetadata::Persistence::Attachment::InstanceMethods
- Defined in:
- lib/active_metadata/persistence/attachment.rb
Instance Method Summary collapse
- #attachments_for(field, order_by = "updated_at DESC") ⇒ Object
- #count_attachments_for(field) ⇒ Object
- #delete_attachment(id) ⇒ Object
- #find_attachment_by_id(id) ⇒ Object
- #has_attachments_for(field) ⇒ Object
- #save_attachment_for(field, file, starred = false, group = nil) ⇒ Object
- #star_attachment(id) ⇒ Object
-
#starred_attachments ⇒ Object
return all the starred notes for the current model and for any field datas does not come from cache.
- #starred_attachments_for(field) ⇒ Object
- #unstar_attachment(id) ⇒ Object
-
#update_attachment(id, newfile, *args) ⇒ Object
Update an attachment replacing the old file Fires a notification.
Instance Method Details
#attachments_for(field, order_by = "updated_at DESC") ⇒ Object
24 25 26 27 28 |
# File 'lib/active_metadata/persistence/attachment.rb', line 24 def (field, order_by="updated_at DESC") Rails.cache.fetch((field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].try(:minutes)) do field, nil, order_by end end |
#count_attachments_for(field) ⇒ Object
68 69 70 |
# File 'lib/active_metadata/persistence/attachment.rb', line 68 def field (field).size end |
#delete_attachment(id) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/active_metadata/persistence/attachment.rb', line 30 def (id) a = ActiveMetadata::Attachment.find(id) filename, created_by = a.attach.original_filename, a.created_by a.destroy a.label self.send(:send_notification, a.label, filename, "", :delete_attachment_message, created_by) end |
#find_attachment_by_id(id) ⇒ Object
72 73 74 |
# File 'lib/active_metadata/persistence/attachment.rb', line 72 def (id) ActiveMetadata::Attachment.find(id) end |
#has_attachments_for(field) ⇒ Object
64 65 66 |
# File 'lib/active_metadata/persistence/attachment.rb', line 64 def field (field).size == 0 ? false : true end |
#save_attachment_for(field, file, starred = false, group = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_metadata/persistence/attachment.rb', line 9 def (field, file, starred=false, group=nil) file.content_type = MIME::Types.type_for(file.original_filename)[0] = ActiveMetadata::Attachment.create!( :model_class => , :model_id => , :label => field, :attach => file, :starred => !!starred, :created_by => current_user_id, :group => group) field self.send(:send_notification, field, "", .attach.original_filename, :new_attachment_message, current_user_id) end |
#star_attachment(id) ⇒ Object
86 87 88 89 |
# File 'lib/active_metadata/persistence/attachment.rb', line 86 def (id) n = ActiveMetadata::Attachment.find(id) id, n.attach, starred: true end |
#starred_attachments ⇒ Object
return all the starred notes for the current model and for any field datas does not come from cache
78 79 80 |
# File 'lib/active_metadata/persistence/attachment.rb', line 78 def nil, true end |
#starred_attachments_for(field) ⇒ Object
82 83 84 |
# File 'lib/active_metadata/persistence/attachment.rb', line 82 def (field) field, true end |
#unstar_attachment(id) ⇒ Object
91 92 93 94 |
# File 'lib/active_metadata/persistence/attachment.rb', line 91 def (id) n = ActiveMetadata::Attachment.find(id) id, n.attach, starred: false end |
#update_attachment(id, newfile, *args) ⇒ Object
Update an attachment replacing the old file Fires a notification. When is used to star or unstar fires a notification of the correct type The cache is reloaded any time an operation is completed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_metadata/persistence/attachment.rb', line 42 def (id, newfile, *args) = args.last.is_a?(Hash) ? args.last : {} [:message_type] ||= :update_attachment_message a = ActiveMetadata::Attachment.find(id) old_filename = a.attach.original_filename a.attach = newfile a.updated_by = current_user_id #mass assign starred inly if provided unless [:starred].nil? a[:starred] = [:starred] [:message_type] = [:starred] ? :star_attachment_message : :unstar_attachment_message end a.save! new_filename = a.attach.original_filename a.label self.send(:send_notification, a.label, old_filename, new_filename, [:message_type], current_user_id) end |