Sha256: 3d78711a20c2b88efb7b55d99adf88b0316140841baf5fc8f90ed8799478b604
Contents?: true
Size: 1.35 KB
Versions: 1
Compression:
Stored size: 1.35 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(Punch.arel_table[:punchable_type], Punch.arel_table[:punchable_id], arel_table.primary_key) 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(arel_table.join(Punch.arel_table, Arel::Nodes::OuterJoin).on(Punch.arel_table[:punchable_id].eq(arel_table.primary_key).and(Punch.arel_table[:punchable_type].eq(self.name))).join_sources.first) query = query.group(arel_table.primary_key) 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
punching_bag-0.3.4 | lib/punching_bag/acts_as_punchable.rb |