Module: RailsRateable

Extended by:
ActiveSupport::Concern
Defined in:
lib/rails_rateable/version.rb,
lib/app/models/rails_rateable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"1.0.0"

Instance Method Summary collapse

Instance Method Details

#average_ratingObject

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



31
32
33
# File 'lib/app/models/rails_rateable.rb', line 31

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

#average_rating_percentObject

Returns the average rating in percent.



41
42
43
44
# File 'lib/app/models/rails_rateable.rb', line 41

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

#average_rating_roundObject

Rounds the average rating value.



36
37
38
# File 'lib/app/models/rails_rateable.rb', line 36

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.



25
26
27
28
# File 'lib/app/models/rails_rateable.rb', line 25

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)


52
53
54
# File 'lib/app/models/rails_rateable.rb', line 52

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.



57
58
59
60
# File 'lib/app/models/rails_rateable.rb', line 57

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.



47
48
49
# File 'lib/app/models/rails_rateable.rb', line 47

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