Class: ActsRateable::Rating
- Defined in:
- lib/acts_rateable/rating.rb
Class Method Summary collapse
- .data_for(resource) ⇒ Object
-
.get_totals(resource) ⇒ Object
RETURNS = { “total_ratings”=>“”, “rating_sum”=>“”, “rating_avg”=>“”, “rated_count”=>“”, “avg_num_ratings”=>“” }.
-
.values_for(resource) ⇒ Object
RETURNS = “rating_sum”=>“”, “rating_avg”=>“”.
Class Method Details
.data_for(resource) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/acts_rateable/rating.rb', line 31 def self.data_for(resource) local = values_for(resource) global = get_totals(resource) estimate = (local['total_ratings'].to_f / (local['total_ratings'].to_f+global['avg_num_ratings'].to_f)) * local['rating_avg'].to_f + (global['avg_num_ratings'].to_f / (local['total_ratings'].to_f+global['avg_num_ratings'].to_f)) *global['rating_avg'].to_f return { 'global' => global, 'local' => local.merge!({ 'estimate' => estimate }) } end |
.get_totals(resource) ⇒ Object
RETURNS = { “total_ratings”=>“”, “rating_sum”=>“”, “rating_avg”=>“”, “rated_count”=>“”, “avg_num_ratings”=>“” }
16 17 18 19 20 21 22 |
# File 'lib/acts_rateable/rating.rb', line 16 def self.get_totals(resource) sql = "SELECT COUNT(*) total_ratings, SUM(value) rating_sum, AVG(value) rating_avg, "+ "(SELECT COUNT(DISTINCT resource_id) FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}') rated_count, "+ "((SELECT COUNT(*) from ar_rates WHERE resource_type = '#{resource.class.base_class.name}') / (SELECT COUNT(DISTINCT resource_id) FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}')) avg_num_ratings "+ "FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}'" ActsRateable::Rate.connection.execute(sql).first end |
.values_for(resource) ⇒ Object
RETURNS = “rating_sum”=>“”, “rating_avg”=>“”
25 26 27 28 29 |
# File 'lib/acts_rateable/rating.rb', line 25 def self.values_for(resource) sql = "SELECT COUNT(*) total_ratings, COALESCE(SUM(value),0) rating_sum, COALESCE(AVG(value),0) rating_avg "+ "FROM ar_rates WHERE resource_type = '#{resource.class.base_class.name}' and resource_id = '#{resource.id}'" ActsRateable::Rate.connection.execute(sql).first end |