module Mongoid::Utils::Hitable::ClassMethods
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
Public Instance Methods
popular_by(param, options = {})
click to toggle source
# File lib/mongoid/utils/hitable.rb, line 33 def popular_by(param, options = {}) 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 => options[:limit].to_i } if options.key?(:limit) ids = Hit.collection.aggregate(filter).collect { |raw| raw[:_id] } where(:id.in => ids) end