Class: Rating
- Inherits:
-
Object
- Object
- Rating
- Includes:
- MongoMapper::Document
- Defined in:
- lib/acts_as_mongo_rateable/app/models/rating.rb
Instance Method Summary collapse
-
#find_rated_document ⇒ Object
Various Instance Methods.
- #find_rated_document! ⇒ Object
- #reduce_rating_stats ⇒ Object
- #set_previous_value ⇒ Object
- #set_rating_stats ⇒ Object
- #update_rating_stats ⇒ Object
Instance Method Details
#find_rated_document ⇒ Object
Various Instance Methods
59 60 61 62 |
# File 'lib/acts_as_mongo_rateable/app/models/rating.rb', line 59 def find_rated_document klass = rateable_class.constantize klass.find(rateable_id.to_s) end |
#find_rated_document! ⇒ Object
64 65 66 67 68 |
# File 'lib/acts_as_mongo_rateable/app/models/rating.rb', line 64 def find_rated_document! doc = find_rated_document raise "Associated document not found" if doc.nil? doc end |
#reduce_rating_stats ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/acts_as_mongo_rateable/app/models/rating.rb', line 47 def doc = find_rated_document return if doc.nil? count = (doc.[:count] -= 1) doc.[:total] -= (value * weight) doc.[:sum_of_weights] -= weight doc.[:average] = nil if count == 0 doc.save end |
#set_previous_value ⇒ Object
33 34 35 36 |
# File 'lib/acts_as_mongo_rateable/app/models/rating.rb', line 33 def set_previous_value = Rating.find_by_id(id) previous_value = (.value * weight) if end |
#set_rating_stats ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/acts_as_mongo_rateable/app/models/rating.rb', line 24 def doc = find_rated_document! count = (doc.[:count] += 1) total = (doc.[:total] += ( value * weight )) sow = (doc.[:sum_of_weights] += weight) doc.[:average] = total.to_f / sow doc.save! end |
#update_rating_stats ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/acts_as_mongo_rateable/app/models/rating.rb', line 38 def doc = find_rated_document! value_delta = (value * weight) - (previous_value * weight) total = (doc.[:total] += value_delta) count = doc.[:count] doc.[:average] = total.to_f / count doc.save end |