Skip to content

Commit

Permalink
Make tally query Postgres friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael McQuinn authored and Brady Bouchard committed Nov 28, 2010
1 parent 848bb60 commit ce806bd
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/acts_as_voteable.rb
Expand Up @@ -30,21 +30,31 @@ module SingletonMethods
# :at_most - Item may not have more than X votes
def tally(*args)
options = args.extract_options!

# Use the explicit SQL statement throughout for Postgresql compatibility.
vote_count = "COUNT(#{Vote.table_name}.voteable_id)"

t = self.where("#{Vote.table_name}.voteable_type = '#{self.name}'")

# We join so that you can order by columns on the voteable model.
t = t.joins("LEFT OUTER JOIN #{Vote.table_name} ON #{self.table_name}.#{self.primary_key} = #{Vote.table_name}.voteable_id")
t = t.having("vote_count > 0")
t = t.group("#{Vote.table_name}.voteable_id")

t = t.having("#{vote_count} > 0")
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 >= ?", options[:at_least]]) if options[:at_least]
t = t.having(["vote_count <= ?", options[:at_most]]) if options[:at_most]
t = options[:order] ? t.order(options[:order]) : t.order("#{vote_count} DESC")
t = t.having(["#{vote_count} >= ?", options[:at_least]]) if options[:at_least]
t = t.having(["#{vote_count} <= ?", options[:at_most]]) if options[:at_most]
t.select("#{self.table_name}.*, COUNT(#{Vote.table_name}.voteable_id) AS vote_count")
end

def column_names_for_tally
column_names.map { |column| "#{self.table_name}.#{column}" }.join(', ')
end

end

module InstanceMethods
Expand Down Expand Up @@ -82,4 +92,4 @@ def voted_by?(voter)

end
end
end
end

0 comments on commit ce806bd

Please sign in to comment.