lib/acts_as_voteable.rb
# Calculate the vote counts for all voteables of my type.
# This method returns all voteables (even without any votes) by default.
# The vote count for each voteable is available as #vote_count.
# This returns an Arel relation, so you can add conditions as you like chained on to
# this method call.
# i.e. Posts.tally.where('votes.created_at > ?', 2.days.ago)
Calculate the vote counts for all voteables of my type.
This method returns all voteables (even without any votes) by default.
The vote count for each voteable is available as #vote_count.
This returns an Arel relation, so you can add conditions as you like chained on to
this method call.
Calculate the vote counts for all voteables of my type . This method returns all voteables ( even without any votes ) by default . The vote count for each voteable is available as #vote_count . This returns an Arel relation so you can add conditions as you like chained on to this method call .
def tally(*args)
        options = args.extract_options!
        vote_count = "COUNT(#{Vote.table_name}.voteable_id)"
        t = self.joins("LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.#{self.primary_key} = #{Vote.table_name}.voteable_id")
        t = t.group("#{Vote.table_name}.voteable_id, #{column_names_for_tally}")
        t = t.limit(options[:limit]) if options[:limit]
        t = t.where("#{Vote.table_name}.created_at >= ?", options[:start_at]) if options[:start_at]
        t = t.where("#{Vote.table_name}.created_at <= ?", options[:end_at]) if options[:end_at]
        t = t.where(options[:conditions]) if options[:conditions]
        t = options[:order] ? t.order(options[:order]) : t.order("#{vote_count} DESC")
        t = t.having([
            "#{vote_count} >= 0",
            (options[:at_least] ? "#{vote_count} >= #{sanitize(options[:at_least])}" : nil),
            (options[:at_most] ? "#{vote_count} <= #{sanitize(options[:at_most])}" : nil)
            ].compact.join(' AND '))
        t.select("#{self.table_name}.*, COUNT(#{Vote.table_name}.voteable_id) AS vote_count")
      end
def tally ( * args ) options = args . extract_options! vote_count = "COUNT(#{Vote.table_name}.voteable_id)" t = self . joins ( "LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.#{self.primary_key} = #{Vote.table_name}.voteable_id" ) t = t . group ( "#{Vote.table_name}.voteable_id, #{column_names_for_tally}" ) t = t . limit ( options [ :limit ] ) if options [ :limit ] t = t . where ( "#{Vote.table_name}.created_at >= ?" , options [ :start_at ] ) if options [ :start_at ] t = t . where ( "#{Vote.table_name}.created_at <= ?" , options [ :end_at ] ) if options [ :end_at ] t = t . where ( options [ :conditions ] ) if options [ :conditions ] t = options [ :order ] ? t . order ( options [ :order ] ) : t . order ( "#{vote_count} DESC" ) t = t . having ( [ "#{vote_count} >= 0" , ( options [ :at_least ] ? "#{vote_count} >= #{sanitize(options[:at_least])}" : nil ) , ( options [ :at_most ] ? "#{vote_count} <= #{sanitize(options[:at_most])}" : nil ) ] . compact . join ( ' AND ' ) ) t . select ( "#{self.table_name}.*, COUNT(#{Vote.table_name}.voteable_id) AS vote_count" ) end
Calculate the vote counts for all voteables of my type.
Calculate the vote counts for all voteables of my type .
# Use the explicit SQL statement throughout for Postgresql compatibility. # Column commented out to support showing 0 votes for models.  This line won't work as # if there are no votes for a model, then it can't filter by this where clause, so the # results come out as null, instead of just counting it as 0. # t = self.where("#{Vote.table_name}.voteable_type = '#{self.name}'") # We join so that you can order by columns on the voteable model. # I haven't been able to confirm this bug yet, but Arel (2.0.7) currently blows up # with multiple 'having' clauses. So we hack them all into one for now. # If you have a more elegant solution, a pull request on Github would be greatly appreciated. # t = t.having("#{vote_count} > 0") # t = t.having(["#{vote_count} >= ?", options[:at_least]]) if options[:at_least] # t = t.having(["#{vote_count} <= ?", options[:at_most]]) if options[:at_most]
args
<not_specific>