Module: IsRateable::ActsAsRateable::LocalInstanceMethods

Defined in:
lib/is_rateable/acts_as_rateable.rb

Instance Method Summary collapse

Instance Method Details

#add_rating(options = {}) ⇒ Object

Easily add a new rating to the object by calling Object.add_rating(score: 5, rater: user) Pass in the entire rater object in, rather than the id.



40
41
42
# File 'lib/is_rateable/acts_as_rateable.rb', line 40

def add_rating(options = {})
  Rating.create(score: options[:score], rater: options[:rater], ratee: self)
end

#any_ratings?Boolean

Has this object been rated before?

Returns:

  • (Boolean)


29
30
31
# File 'lib/is_rateable/acts_as_rateable.rb', line 29

def any_ratings?
  ratee_ratings.any?
end

#average_ratingObject

Find the Average rating for the ratee. Return 0.0 if they have not been rated yet.



23
24
25
26
# File 'lib/is_rateable/acts_as_rateable.rb', line 23

def average_rating
  return IsRateable.default_rating unless enough_ratings_for_average?
  any_ratings? ? ((ratee_ratings.average(:score) * 10).round / 10.0) : 0.0
end

#enough_ratings_for_average?Boolean

Has the object received enough ratings to show the average?

Returns:

  • (Boolean)


34
35
36
# File 'lib/is_rateable/acts_as_rateable.rb', line 34

def enough_ratings_for_average?
  any_ratings? && ratee_ratings.length > IsRateable.minimum_ratings_for_average
end