Module: ActiveMetadata::Persistence::Note::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#count_notes_for(field) ⇒ Object



74
75
76
# File 'lib/active_metadata/persistence/note.rb', line 74

def count_notes_for field
  notes_for(field).size
end

#create_note_for(field, note, starred = nil, group = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_metadata/persistence/note.rb', line 9

def create_note_for(field, note, starred=nil, group=nil)
  ActiveMetadata::Note.create!(
      :model_id => ,
      :model_class => ,
      :label => field.to_s,
      :note => note,
      :created_by => current_user_id,
      :starred => !!starred,
      :group => group)

  reload_notes_cache_for field
  self.send(:send_notification, field, "", note, :new_note_message, current_user_id)
end

#create_notes_for(field, notes) ⇒ Object



58
59
60
# File 'lib/active_metadata/persistence/note.rb', line 58

def create_notes_for(field, notes)
  notes.each { |note| create_note_for field, note }
end

#delete_note(id) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/active_metadata/persistence/note.rb', line 62

def delete_note(id)
  note = ActiveMetadata::Note.find(id)
  old_value, created_by = note.note, note.created_by
  note.destroy
  reload_notes_cache_for note.label
  self.send(:send_notification, note.label, old_value, "", :delete_note_message, created_by)
end

#find_note_by_id(id) ⇒ Object



54
55
56
# File 'lib/active_metadata/persistence/note.rb', line 54

def find_note_by_id(id)
  ActiveMetadata::Note.find(id)
end

#has_notes_for(field) ⇒ Object



70
71
72
# File 'lib/active_metadata/persistence/note.rb', line 70

def has_notes_for field
  notes_for(field).size == 0 ? false : true
end

#notes_for(field, order_by = "updated_at DESC") ⇒ Object



48
49
50
51
52
# File 'lib/active_metadata/persistence/note.rb', line 48

def notes_for(field, order_by="updated_at DESC")
  Rails.cache.fetch(notes_cache_key(field), :expires_in => ActiveMetadata::CONFIG['cache_expires_in'].try(:minutes)) do
    fetch_notes_for field, nil, order_by
  end
end

#star_note(id) ⇒ Object

star a note reload the cache calling update



92
93
94
95
# File 'lib/active_metadata/persistence/note.rb', line 92

def star_note(id)
  n = ActiveMetadata::Note.find(id)
  update_note id, n.note, starred: true
end

#starred_notesObject

return all the starred notes for the current model and for any field datas does not come from cache



80
81
82
# File 'lib/active_metadata/persistence/note.rb', line 80

def starred_notes
  fetch_notes_for nil, true
end

#starred_notes_for(field) ⇒ Object

return all the starred notes for a particular field datas does not come from cache



86
87
88
# File 'lib/active_metadata/persistence/note.rb', line 86

def starred_notes_for(field)
  fetch_notes_for field, true
end

#unstar_note(id) ⇒ Object

unstar a note reload the cache calling update



99
100
101
102
# File 'lib/active_metadata/persistence/note.rb', line 99

def unstar_note(id)
  n = ActiveMetadata::Note.find(id)
  update_note id, n.note, starred: false
end

#update_note(id, note, *args) ⇒ Object

Update a note *update the record and recreate the cache *send and update_note_meesage notification *if update is starring or unstarring is sent a message of the correct type



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/active_metadata/persistence/note.rb', line 28

def update_note(id, note, *args)

  options = options = args.last.is_a?(Hash) ? args.last : {}
  options[:message_type] ||= :update_note_message

  n = ActiveMetadata::Note.find(id)
  old_value = n.note
  attributes = {:note => note, :updated_by => current_user_id, :updated_at => Time.now.utc}

  #mass assign starred inly if provided
  unless options[:starred].nil?
    attributes[:starred] = options[:starred]
    options[:message_type] = options[:starred] ? :star_note_message : :unstar_note_message
  end

  n.update_attributes! attributes
  reload_notes_cache_for n.label
  self.send(:send_notification, n.label, old_value, note, options[:message_type], current_user_id)
end