Module: ActiveRecord::Acts::Rateable::InstanceMethods

Defined in:
lib/acts_as_rateable.rb

Instance Method Summary collapse

Instance Method Details

#average_ratingObject

Returns the average rating. Calculation based on the already given scores.



45
46
47
# File 'lib/acts_as_rateable.rb', line 45

def average_rating
  rating && rating.average_rating || 0.0
end

#average_rating_percentObject

Returns the average rating in percent.



55
56
57
58
# File 'lib/acts_as_rateable.rb', line 55

def average_rating_percent
  f = 100 / max_rating.to_f
  average_rating * f
end

#average_rating_roundObject

Rounds the average rating value.



50
51
52
# File 'lib/acts_as_rateable.rb', line 50

def average_rating_round
  average_rating.round
end

#rate_it(score, user) ⇒ Object

Rates the object by a given score. A user object should be passed to the method.



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

def rate_it(score, user)
  create_rating unless rating
  rating.rate(score, user)
end

#rated_by?(user) ⇒ Boolean

Checks whether a user rated the object or not.

Returns:

  • (Boolean)


66
67
68
# File 'lib/acts_as_rateable.rb', line 66

def rated_by?(user)
  rating && rating.user_ratings.exists?(:user_id => user)
end

#rating_by(user) ⇒ Object

Returns the rating a specific user has given the object.



71
72
73
74
# File 'lib/acts_as_rateable.rb', line 71

def rating_by(user)
  user_rating = rating && rating.user_ratings.find_by_user_id(user.id)
  user_rating ? user_rating.score : nil
end

#ratings_countObject

Returns the number of ratings.



61
62
63
# File 'lib/acts_as_rateable.rb', line 61

def ratings_count
  rating && rating.ratings_count || 0
end