Module: Juixe::Acts::Voteable::SingletonMethods
- Defined in:
- lib/acts_as_voteable.rb
Overview
This module contains class methods
Instance Method Summary collapse
-
#options_for_tally(options = {}) ⇒ Object
Options: :start_at - Restrict the votes to those created after a certain time :end_at - Restrict the votes to those created before a certain time :conditions - A piece of SQL conditions to add to the query :limit - The maximum number of voteables to return :order - A piece of SQL to order by.
-
#tally(options = {}) ⇒ Object
Calculate the vote counts for all voteables of my type.
Instance Method Details
#options_for_tally(options = {}) ⇒ Object
Options:
:start_at - Restrict the votes to those created after a certain time
:end_at - Restrict the votes to those created before a certain time
:conditions - A piece of SQL conditions to add to the query
:limit - The maximum number of voteables to return
:order - A piece of SQL to order by. Eg 'votes.count desc' or 'voteable.created_at desc'
:at_least - Item must have at least X votes
:at_most - Item may not have more than X votes
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/acts_as_voteable.rb', line 36 def ( = {}) .assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit scope = scope(:find) start_at = sanitize_sql(["#{Vote.table_name}.created_at >= ?", .delete(:start_at)]) if [:start_at] end_at = sanitize_sql(["#{Vote.table_name}.created_at <= ?", .delete(:end_at)]) if [:end_at] type_and_context = "#{Vote.table_name}.voteable_type = #{quote_value(base_class.name)}" conditions = [ type_and_context, [:conditions], start_at, end_at ] conditions = conditions.compact.join(' AND ') conditions = merge_conditions(conditions, scope[:conditions]) if scope joins = ["LEFT OUTER JOIN #{Vote.table_name} ON #{table_name}.#{primary_key} = #{Vote.table_name}.voteable_id"] joins << scope[:joins] if scope && scope[:joins] at_least = sanitize_sql(["COUNT(#{Vote.table_name}.id) >= ?", .delete(:at_least)]) if [:at_least] at_most = sanitize_sql(["COUNT(#{Vote.table_name}.id) <= ?", .delete(:at_most)]) if [:at_most] having = [at_least, at_most].compact.join(' AND ') group_by = "#{Vote.table_name}.voteable_id HAVING COUNT(#{Vote.table_name}.id) > 0" group_by << " AND #{having}" unless having.blank? { :select => "#{table_name}.*, COUNT(#{Vote.table_name}.id) AS count", :joins => joins.join(" "), :conditions => conditions, :group => group_by }.update() end |
#tally(options = {}) ⇒ Object
Calculate the vote counts for all voteables of my type.
23 24 25 |
# File 'lib/acts_as_voteable.rb', line 23 def tally( = {}) find(:all, (.merge({:order =>"count DESC" }))) end |