Module: ActiveMetadata::Persistence::Attachment::InstanceMethods

Defined in:
lib/active_metadata/persistence/attachment.rb

Instance Method Summary collapse

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 attachments_for(field, order_by="updated_at DESC")
  Rails.cache.fetch(attachments_cache_key(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].try(:minutes)) do
    fetch_attachments_for field, nil, order_by
  end
end

#count_attachments_for(field) ⇒ Object



68
69
70
# File 'lib/active_metadata/persistence/attachment.rb', line 68

def count_attachments_for field
  attachments_for(field).size
end

#delete_attachment(id) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/active_metadata/persistence/attachment.rb', line 30

def delete_attachment(id)
  a = ActiveMetadata::Attachment.find(id)
  filename, created_by = a.attach.original_filename, a.created_by
  a.destroy
  reload_attachments_cache_for 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 find_attachment_by_id(id)
  ActiveMetadata::Attachment.find(id)
end

#has_attachments_for(field) ⇒ Object



64
65
66
# File 'lib/active_metadata/persistence/attachment.rb', line 64

def has_attachments_for field
  attachments_for(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 save_attachment_for(field, file, starred=false, group=nil)
  file.content_type = MIME::Types.type_for(file.original_filename)[0]
  attachment = ActiveMetadata::Attachment.create!(
          :model_class => ,
          :model_id => ,
          :label => field,
          :attach => file,
          :starred => !!starred,
          :created_by => current_user_id,
          :group => group)

  reload_attachments_cache_for field
  self.send(:send_notification, field, "", attachment.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 star_attachment(id)
  n = ActiveMetadata::Attachment.find(id)
  update_attachment id, n.attach, starred: true
end

#starred_attachmentsObject

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 starred_attachments
  fetch_attachments_for nil, true
end

#starred_attachments_for(field) ⇒ Object



82
83
84
# File 'lib/active_metadata/persistence/attachment.rb', line 82

def starred_attachments_for(field)
  fetch_attachments_for field, true
end

#unstar_attachment(id) ⇒ Object



91
92
93
94
# File 'lib/active_metadata/persistence/attachment.rb', line 91

def unstar_attachment(id)
  n = ActiveMetadata::Attachment.find(id)
  update_attachment 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 update_attachment(id, newfile, *args)
  options = args.last.is_a?(Hash) ? args.last : {}
  options[: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 options[:starred].nil?
    a[:starred] = options[:starred]
    options[:message_type] = options[:starred] ? :star_attachment_message : :unstar_attachment_message
  end

  a.save!
  new_filename = a.attach.original_filename
  reload_attachments_cache_for a.label

  self.send(:send_notification, a.label, old_filename, new_filename, options[:message_type], current_user_id)
end