Class: ActsRateable::Count

Inherits:
ArRating
  • Object
show all
Defined in:
lib/acts_rateable/count.rb

Class Method Summary collapse

Class Method Details

.data_for(author) ⇒ Object



31
32
33
34
35
36
# File 'lib/acts_rateable/count.rb', line 31

def self.data_for(author)
  local     = values_for(author)
  global    = get_totals(author)
  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(author) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/acts_rateable/count.rb', line 15

def self.get_totals(author)
  sql = "SELECT COUNT(*) total_ratings, SUM(value) rating_sum, AVG(value) rating_avg, "+
        "(SELECT COUNT(DISTINCT author_id) FROM ar_rates WHERE author_type = '#{author.class.base_class.name}') rated_count, "+
        "((SELECT COUNT(*) from ar_rates WHERE author_type = '#{author.class.base_class.name}') / (SELECT COUNT(DISTINCT author_id) FROM ar_rates WHERE author_type = '#{author.class.base_class.name}')) avg_num_ratings "+
        "FROM ar_rates WHERE author_type = '#{author.class.base_class.name}'"
  #  RETURNS = { "total_ratings"=>"", "rating_sum"=>"", "rating_avg"=>"", "rated_count"=>"", "avg_num_ratings"=>"" }
  ActsRateable::Rate.connection.execute(sql).first
end

.values_for(author) ⇒ Object



24
25
26
27
28
29
# File 'lib/acts_rateable/count.rb', line 24

def self.values_for(author)    
  sql =   "SELECT COUNT(*) total_ratings, COALESCE(SUM(value),0) rating_sum, COALESCE(AVG(value),0) rating_avg "+
          "FROM ar_rates WHERE author_type = '#{author.class.base_class.name}' and author_id = '#{author.id}'"
  # RETURNS = {"total_ratings"=>"", "rating_sum"=>"", "rating_avg"=>""}
  ActsRateable::Rate.connection.execute(sql).first
end