module Mongoid::Rateable
Public Instance Methods
previous_rating()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 172 def previous_rating read_attribute(:rating_previous) end
rate(mark, rater = nil, weight = 1)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 103 def rate(mark, rater = nil, weight = 1) case rater when Array rater.each{|rater| rate(mark, rater, weight)} else if !rater unless respond_to?(:default_rater) raise ArgumentError, "No rater argument and no default_rater specified" end rater = default_rater end validate_rater!(rater) validate_rating!(mark) unrate_without_rating_update(rater) total_mark = mark.to_i*weight.to_i self.rates += total_mark self.rating_marks.new(:rater_id => rater.id, :mark => mark, :rater_class => rater.class.to_s, :weight => weight) self.weighted_rate_count += weight update_rating end end
rate_and_save(mark, rater, weight = 1)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 135 def rate_and_save(mark, rater, weight = 1) case rater when Array rater.each{|rater| rate_and_save(mark, rater, weight)} else rate(mark, rater, weight) save end end
rate_count()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 186 def rate_count self.rating_marks.size end
rate_weight()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 190 def rate_weight check_weighted_rate_count read_attribute(:weighted_rate_count) end
rated?()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 155 def rated? rate_count != 0 end
rated_by?(rater)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 159 def rated_by?(rater) case rater when Array rater.each{|rater| rated_by(mark, rater, weight)} else self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).count == 1 end end
rating()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 168 def rating read_attribute(:rating) end
rating_delta()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 176 def rating_delta read_attribute(:rating_delta) end
unrate(rater)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 125 def unrate(rater) case rater when Array rater.each{|rater| unrate(mark, rater, weight)} else unrate_without_rating_update(rater) update_rating end end
unrate_and_save(rater)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 145 def unrate_and_save(rater) case rater when Array rater.each{|rater| unrate_and_save(mark, rater, weight)} else unrate(rater) save end end
unweighted_rating()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 180 def unweighted_rating return nil if self.rating_marks.empty? total_sum = self.rating_marks.map(&:mark).sum return total_sum.to_f/self.rating_marks.size end
user_mark(rater)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 195 def user_mark(rater) r = self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).first r ? r.mark : nil end
user_marks(raters)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 200 def user_marks(raters) if raters.map{|x| x.class}.uniq.count > 1 raise ArgumentError, "Raters all must be of same class." return end r = self.rating_marks.in(:rater_id => raters.map(&:id), :rater_class => raters.first.class.to_s) r ? r.inject(Hash.new(0)) { |h, e| h[e.rater_id] = e.mark ; h } : nil end
Protected Instance Methods
check_weighted_rate_count()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 243 def check_weighted_rate_count #migration from old version wrc = read_attribute(:weighted_rate_count).to_i if (wrc==0 && rate_count!=0) write_attribute(:weighted_rate_count, self.rating_marks.size) end end
unrate_without_rating_update(rater)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 223 def unrate_without_rating_update(rater) rmark = self.rating_marks.where(:rater_id => rater.id, :rater_class => rater.class.to_s).first return unless rmark weight = (rmark.weight ||= 1) total_mark = rmark.mark.to_i*weight.to_i self.rates -= total_mark self.weighted_rate_count -= weight rmark.delete end
update_rating()
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 234 def update_rating check_weighted_rate_count write_attribute(:rating_previous, self.rating) rt = (self.rates.to_f / self.weighted_rate_count.to_f) unless self.rating_marks.blank? write_attribute(:rating, rt) delta = (self.rating && self.previous_rating) ? rating-previous_rating : 0.0 write_attribute(:rating_delta, delta) end
validate_rater!(rater)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 211 def validate_rater!(rater) unless self.class.valid_rater_class?(rater.class) raise ArgumentError, "Not a valid rater: #{rater.class}, must be of one of #{self.class.rater_classes}" end end
validate_rating!(value)
click to toggle source
# File lib/mongoid_rateable/rateable.rb, line 217 def validate_rating!(value) if !self.class.in_rating_range?(value) raise ArgumentError, "Rating not in range #{self.class.rating_range}. Rating provided was #{value}." end end