Module: ActsAsMongoRateable::InstanceMethods
- Defined in:
- lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb
Instance Method Summary collapse
- #average_rating ⇒ Object
- #bayesian_rating(stats = nil) ⇒ Object
- #delete_all_ratings ⇒ Object
- #delete_ratings_by_user(user) ⇒ Object
- #rate(value, user = nil, weight = 1) ⇒ Object
-
#rated_by_user?(user) ⇒ Boolean
returns the Rating object found if user has rated this project, else returns nil.
Instance Method Details
#average_rating ⇒ Object
34 35 36 |
# File 'lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb', line 34 def ['average'] end |
#bayesian_rating(stats = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb', line 38 def (stats=nil) return 0 if ['count'] == 0 stats ||= self.class.all({:select => 'id, rating_stats'}) system_counts = stats.map{ |p| [ p.id.to_s, p.['count'] ] } = stats.map{|p| p.['average'] || 0 }.sum / stats.size.to_f avg_num_votes = system_counts.inject(0){|sum, r| sum += r.to_a.flatten[1] } / system_counts.size.to_f = ['average'] || 0 my_count = ['count'] ( (avg_num_votes * ) + (my_count * ) ) / (avg_num_votes + my_count) end |
#delete_all_ratings ⇒ Object
30 31 32 |
# File 'lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb', line 30 def .delete_all end |
#delete_ratings_by_user(user) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb', line 49 def (user) return false unless user return 0 if .blank? .delete_all(:user_id => user.id.to_s) self.reload end |
#rate(value, user = nil, weight = 1) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb', line 56 def rate(value, user = nil, weight = 1) (user) (value) r = Rating.new({ :value => value, :user_id => user.id, :rateable_id => self.id, :rateable_class => self.class.to_s, :weight => weight }) self. << r self.reload r end |
#rated_by_user?(user) ⇒ Boolean
returns the Rating object found if user has rated this project, else returns nil
72 73 74 75 |
# File 'lib/acts_as_mongo_rateable/acts_as_mongo_rateable.rb', line 72 def rated_by_user?(user) return false unless user .detect{ |r| r.user_id == user.id} end |