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



99
100
101
102
103
104
# File 'app/models/c2/informant/locus.rb', line 99

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



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

def entries
  klass.all
end

#entries_as_json(entries) ⇒ Object



57
58
59
# File 'app/models/c2/informant/locus.rb', line 57

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

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



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

def entries_page(page=1,per=10)
  entries_as_json(entries.offset((page - 1) * per).limit(per))
end

#entry_as_json(entry) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/c2/informant/locus.rb', line 44

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[]
  data['created_at'] = entry['created_at']
  data['updated_at'] = entry['updated_at']
  data['errors'] = entry.errors
  data
end

#entry_form_builderObject



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

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] || '' unless self.class_name
  self[:entry_label] ||= ([:c2_label, :entry_label, :to_label, :label, :title, :name, :email, :subject].map(&:to_s) & klass.instance_methods).first
end

#hash_pathObject



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

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

#klassObject



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

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] || '' unless self.class_name
  self[:label] ||= self.class_name.pluralize.titleize
end

#sanitized(params) ⇒ Object



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

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