Class: MnModel::Note

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mn_model/note.rb

Instance Method Summary collapse

Instance Method Details

#all_entries_with_data(options = {}) ⇒ Object



43
44
45
46
# File 'lib/mn_model/note.rb', line 43

def all_entries_with_data(options={})
  #entries_with_date = get_entries_with_data_by_sql(entries)
  entries_with_date = get_entries_with_data_by_cal(items_with_field_name)
end

#create_entry_with_data(attributes = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mn_model/note.rb', line 11

def create_entry_with_data(attributes={})
  entry_with_data = {"data" => Hash.new}

  #transaction do
  #end
  entry = Entry.create note_id: id
  entry_with_data.merge! entry.serializable_hash

  fields.each do |f|
    #if attributes[f.name].present?
    unless attributes[f.name].nil?
      item = entry.items.create field_id: f.id, content: attributes[f.name]
      entry_with_data["data"].merge!(f.name => item.content)
    end
  end

  return entry_with_data
end

#find_entry_with_data(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mn_model/note.rb', line 30

def find_entry_with_data(options={})
  entry = Entry.find options["entry_id"]
  entry_with_data = {"data" => Hash.new}.merge! entry.serializable_hash

  fields.each do |f|
    item = Item.where(entry_id: entry.id, field_id: f.id).first
    #entry_with_data["data"].merge!(f.name => item.try(:content))
    entry_with_data["data"].merge!(f.name => item.content) if item
  end

  return entry_with_data
end

#items_with_field_nameObject



49
50
51
# File 'lib/mn_model/note.rb', line 49

def items_with_field_name
  items.select("items.*, fields.name as field_name")
end