Sha256: c98a7453899f71123f36e434b53a9df0063668b398cac289f3dda3c85b6b2260
Contents?: true
Size: 1.22 KB
Versions: 2
Compression:
Stored size: 1.22 KB
Contents
module PunchingBag module ActiveRecord module ClassMethods # Note: this method will only return items if they have 1 or more hits def most_hit(since=nil, limit=5) query = self.scoped.joins(:punches).group(:punchable_type, :punchable_id) query = query.where('punches.average_time >= ?', since) unless since.nil? query.reorder('SUM(punches.hits) DESC').limit(limit) end # Note: this method will return all items with 0 or more hits def sort_by_popularity(dir='DESC') query = self.scoped.joins("LEFT OUTER JOIN punches ON punches.punchable_id = #{self.to_s.tableize}.id AND punches.punchable_type = '#{self.to_s}'") query = query.group(:id) query.reorder("SUM(punches.hits) #{dir}") end end module InstanceMethods def hits(since=nil) self.punches.after(since).sum(:hits) end def punch(request=nil) PunchingBag.punch(self, request) end end end end class ActiveRecord::Base def self.acts_as_punchable extend PunchingBag::ActiveRecord::ClassMethods include PunchingBag::ActiveRecord::InstanceMethods has_many :punches, :as => :punchable, :dependent => :destroy end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
punching_bag-0.3.1 | lib/punching_bag/acts_as_punchable.rb |
punching_bag-0.3.0 | lib/punching_bag/acts_as_punchable.rb |