Sha256: 1e678c0d3efa5ec487727124fe0dd01ccc5f2f0895ff91a5da81a39e0d9d6762

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

class C2::Informant::Bucket
  include Mongoid::Document
  
  field :label
  field :description
  
  field :method_name
  
  embedded_in :locus, :class_name => 'C2::Informant::Locus'
  
  delegate :count, :to => :entries
  delegate :elements, :entry_label, :sanitized, :to => :locus
  
  validate :method_name, :presences => true, :unique => true
  
  def label
    self[:label] || self.method_name.titleize
  end
    
  def entries
    return _parent.klass.all unless method_name && _parent.klass.respond_to?(method_name)
    _parent.klass.try(method_name)
  end
  
  def entries_page(page=1,per=10)
    entries.offset((page - 1) * per).limit(per).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_label] = entry[entry_label] 
      data
    end
  end

  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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
c2-0.1.7 app/models/c2/informant/bucket.rb
c2-0.1.6 app/models/c2/informant/bucket.rb
c2-0.1.5 app/models/c2/informant/bucket.rb
c2-0.1.4 app/models/c2/informant/bucket.rb
c2-0.1.3 app/models/c2/informant/bucket.rb
c2-0.1.2 app/models/c2/informant/bucket.rb
c2-0.1.1 app/models/c2/informant/bucket.rb
c2-0.1.0 app/models/c2/informant/bucket.rb