Class: C2::Informant::Locus

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/c2/informant/locus.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



103
104
105
106
107
108
# File 'app/models/c2/informant/locus.rb', line 103

def as_json(options={})
  cleaned = super((options || {}).merge({
    :methods => [:count, :buckets, :label, :singular_label, :entry_label, :entries_page, :entry_form_builder]
  })).map {|k,v| [k.to_s, (v.is_a?(Numeric) ? v.to_s : v)]}
  Hash[cleaned]
end

#entriesObject



40
41
42
# File 'app/models/c2/informant/locus.rb', line 40

def entries
  klass.all
end

#entries_as_json(entries) ⇒ Object



61
62
63
# File 'app/models/c2/informant/locus.rb', line 61

def entries_as_json(entries)
  entries.map { |entry| self.entry_as_json(entry) }
end

#entries_page(page = 1, per = 10) ⇒ Object



44
45
46
# File 'app/models/c2/informant/locus.rb', line 44

def entries_page(page=1,per=10)
  entries_as_json(entries)
end

#entry_as_json(entry) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/c2/informant/locus.rb', line 48

def entry_as_json(entry)
  data = self.elements.inject({}) do |memo, element|
    memo[element.name] = entry.send(element.name) if entry.respond_to?(element.name)
    memo
  end
  data['_id'] = entry['_id']
  data[] = entry.send() if  && entry.respond_to?()
  data['created_at'] = entry['created_at']
  data['updated_at'] = entry['updated_at']
  data['errors'] = entry.errors
  data
end

#entry_form_builderObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/c2/informant/locus.rb', line 76

def entry_form_builder
  {
    'action' => '#',
    'elements' => [
      {
        'type' => 'div',
        'id' => 'entry-fields',
        'class' => 'fields',
        'elements' => elements.enabled.as_json(:except => ['_id', 'tag'])
      },
      {
        'type' => 'div',
        'class' => 'actions',
        'elements' => [
          {'type' => 'submit', 'class' => 'button', 'value' => 'Save'},
          {
            'type' => 'a',
            'class' => 'cancel flip-trigger',
            'href' => "#",
            'html' => 'Cancel'
          }
        ]
      }
    ]
  }
end

#entry_labelObject



27
28
29
30
# File 'app/models/c2/informant/locus.rb', line 27

def 
  return self[:entry_label_cache] || '' unless self.class_name
  self[:entry_label_cache] ||= ([:c2_label, :entry_label, :to_label, :label, :title, :name, :email, :subject].map(&:to_s) & klass.instance_methods).first
end

#entry_label=(value) ⇒ Object



32
33
34
# File 'app/models/c2/informant/locus.rb', line 32

def (value)
  self[:entry_label_cache] = value
end

#hash_pathObject



72
73
74
# File 'app/models/c2/informant/locus.rb', line 72

def hash_path
  '#/locus/' + self.id.to_s
end

#klassObject



36
37
38
# File 'app/models/c2/informant/locus.rb', line 36

def klass
  @klass ||= self.class_name.classify.constantize
end

#labelObject



18
19
20
21
# File 'app/models/c2/informant/locus.rb', line 18

def label
  return self[:label_cache] || '' unless self.class_name
  self[:label_cache] ||= self.class_name.pluralize.titleize
end

#sanitized(params) ⇒ Object



65
66
67
68
69
70
# File 'app/models/c2/informant/locus.rb', line 65

def sanitized(params)
  self.elements.enabled.map(&:name).inject({}) do |memo, field|
    memo[field] = params[field]
    memo
  end        
end

#singular_labelObject



23
24
25
# File 'app/models/c2/informant/locus.rb', line 23

def singular_label
  label.to_s.singularize
end