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 =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#average_ratingObject

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



33
34
35
# File 'lib/app/models/rails_rateable.rb', line 33

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

#average_rating_percentObject

Returns the average rating in percent.



43
44
45
46
# File 'lib/app/models/rails_rateable.rb', line 43

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

#average_rating_roundObject

Rounds the average rating value.



38
39
40
# File 'lib/app/models/rails_rateable.rb', line 38

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.



27
28
29
30
# File 'lib/app/models/rails_rateable.rb', line 27

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)


54
55
56
# File 'lib/app/models/rails_rateable.rb', line 54

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.



59
60
61
62
# File 'lib/app/models/rails_rateable.rb', line 59

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.



49
50
51
# File 'lib/app/models/rails_rateable.rb', line 49

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