Class: C2::Informant::Bucket

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

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



40
41
42
43
44
45
# File 'app/models/c2/informant/bucket.rb', line 40

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

#entriesObject



20
21
22
23
# File 'app/models/c2/informant/bucket.rb', line 20

def entries
  return _parent.klass.all unless method_name && _parent.klass.respond_to?(method_name)
  _parent.klass.try(method_name)
end

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/c2/informant/bucket.rb', line 25

def entries_page(page=1,per=10)
  # entries.offset((page - 1) * per).limit(per).map do |entry|
  entries.map do |entry|
    data = elements.enabled.inject({}) do |memo, element|
      memo[element.name] = entry.send(element.name) if entry.respond_to?(element.name)
      memo
    end
    data['_id'] = entry['_id']
    data['created_at'] = entry['created_at']
    data['updated_at'] = entry['updated_at']
    data[] = entry[] 
    data
  end
end

#labelObject



16
17
18
# File 'app/models/c2/informant/bucket.rb', line 16

def label
  self[:label] || self.method_name.titleize
end