Permalink
Browse files

Rateit module refactoring

  • Loading branch information...
1 parent daccdd7 commit 736df24c57882974fa0dba9e1964ee42bb2d8df8 @muratguzel committed Sep 4, 2012
Showing with 72 additions and 76 deletions.
  1. +72 −76 lib/rateit/model.rb
View
@@ -1,19 +1,88 @@
module Rateit
+
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def rate(stars, user_id, dimension=nil)
+ if can_rate? user_id, dimension
+ rates(dimension).build do |r|
+ r.stars = stars
+ r.rater_id = user_id
+ r.save!
+ end
+ update_rate_average(stars, dimension)
+ else
+ raise "User has already rated."
+ end
+ end
+
+ def update_rate_average(stars, dimension=nil)
+ if average(dimension).nil?
+ RatingCache.create do |avg|
+ avg.cacheable_id = self.id
+ avg.cacheable_type = self.class.name
+ avg.avg = stars
+ avg.qty = 1
+ avg.dimension = dimension
+ avg.save!
+ end
+ else
+ a = average(dimension)
+ a.avg = (a.avg + stars) / (a.qty+1)
+ a.qty = a.qty + 1
+ a.save!
+ end
+ end
+
+ def average(dimension=nil)
+ if dimension.nil?
+ self.send "rate_average_without_dimension"
+ else
+ self.send "#{dimension}_average"
+ end
+ end
+
+ def can_rate?(user_id, dimension=nil)
+ val = self.connection.select_value("select count(*) as cnt from rates where rateable_id=#{self.id} and rateable_type='#{self.class.name}' and rater_id=#{user_id} and dimension='#{dimension}'").to_i
+ if val == 0
+ true
+ else
+ false
+ end
+ end
+
+ def rates(dimension=nil)
+ if dimension.nil?
+ self.send "rates_without_dimension"
+ else
+ self.send "#{dimension}_rates"
+ end
+ end
+
+ def raters(dimension=nil)
+ if dimension.nil?
+ self.send "raters_without_dimension"
+ else
+ self.send "#{dimension}_raters"
+ end
+ end
+
module ClassMethods
def rateit_rater
has_many :ratings_given, :class_name => "Rate", :foreign_key => :rater_id
end
- def rateit_rateable(options = {})
+ def rateit_rateable(*options)
has_many :rates_without_dimension, :as => :rateable, :class_name => "Rate", :dependent => :destroy, :conditions => {:dimension => nil}
has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
has_one :rate_average_without_dimension, :as => :cacheable, :class_name => "RatingCache",
:dependent => :destroy, :conditions => {:dimension => nil}
- options[:dimensions].each do |dimension|
+ options.each do |dimension|
has_many "#{dimension}_rates", :dependent => :destroy,
:conditions => {:dimension => dimension.to_s},
:class_name => "Rate",
@@ -23,83 +92,10 @@ def rateit_rateable(options = {})
has_one "#{dimension}_average", :as => :cacheable, :class_name => "RatingCache",
:dependent => :destroy, :conditions => {:dimension => dimension.to_s}
- end if options[:dimensions].is_a?(Array)
+ end
end
-
end
-
- module InstanceMethods
-
- def rate(stars, user_id, dimension=nil)
- if can_rate? user_id, dimension
- rates(dimension).build do |r|
- r.stars = stars
- r.rater_id = user_id
- r.save!
- end
- update_rate_average(stars, dimension)
- else
- raise "User has already rated."
- end
- end
- def update_rate_average(stars, dimension=nil)
- if average(dimension).nil?
- RatingCache.create do |avg|
- avg.cacheable_id = self.id
- avg.cacheable_type = self.class.name
- avg.avg = stars
- avg.qty = 1
- avg.dimension = dimension
- avg.save!
- end
- else
- a = average(dimension)
- a.avg = (a.avg + stars) / (a.qty+1)
- a.qty = a.qty + 1
- a.save!
- end
- end
-
- def average(dimension=nil)
- if dimension.nil?
- self.send "rate_average_without_dimension"
- else
- self.send "#{dimension}_average"
- end
- end
-
- def can_rate?(user_id, dimension=nil)
- val = self.connection.select_value("select count(*) as cnt from rates where rateable_id=#{self.id} and rateable_type='#{self.class.name}' and rater_id=#{user_id} and dimension='#{dimension}'").to_i
- if val == 0
- true
- else
- false
- end
- end
-
- def rates(dimension=nil)
- if dimension.nil?
- self.send "rates_without_dimension"
- else
- self.send "#{dimension}_rates"
- end
- end
-
- def raters(dimension=nil)
- if dimension.nil?
- self.send "raters_without_dimension"
- else
- self.send "#{dimension}_raters"
- end
- end
-
- end
-
- def self.included(receiver)
- receiver.extend ClassMethods
- receiver.send :include, InstanceMethods
- end
end
class ActiveRecord::Base

0 comments on commit 736df24

Please sign in to comment.