Module: Mongoid::Utils::Hitable::ClassMethods
- Defined in:
- lib/mongoid/utils/hitable.rb
Overview
def hit_count(options={})
options.reverse_merge!(filter: :session_hash, start_date: nil, end_date: Time.now)
hts = options[:start_date].blank? ? hits : hits.between(created_at: options[:start_date]..options[:end_date])
distinct = options[:filter] != :all
distinct ? hts.where(options[:filter].ne => nil).distinct(options[:filter]).count : hts.count
end
Instance Method Summary collapse
Instance Method Details
#popular_by(param, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mongoid/utils/hitable.rb', line 33 def popular_by(param, = {}) range = case param when 'day'; Time.now.at_beginning_of_day..Time.now when 'week'; Time.now.at_beginning_of_week..Time.now when 'month'; Time.now.at_beginning_of_month..Time.now when 'year'; Time.now.at_beginning_of_year..Time.now end filter = [] filter << { :$match => {hitable_type: self.name, _id: {:$gte => BSON::ObjectId.from_time(range.first), :$lte => BSON::ObjectId.from_time(range.last)} } } filter << { :$group => {_id: '$hitable_id', count: {:$sum => 1}} } # filter << { :$sort => {count: -1} } filter << { :$limit => [:limit].to_i } if .key?(:limit) ids = Hit.collection.aggregate(filter).collect { |raw| raw[:_id] } where(:id.in => ids) end |