module SelfRateable::Core::InstanceMethods::Stars

Public Instance Methods

rate(rater, stars) click to toggle source
# File lib/self_rateable/core/instance_methods.rb, line 36
def rate(rater, stars)
 rater.present? ? rate = ratings.where(rater_id: rater.id).first : raise('Rater object is incorrect.')
 stars = 5 if stars > 5
 stars = 0 if stars < 1 
 if rated_by_rater?(rater)
 # set new star rating if already rated
         rate.rating = stars.to_i
         rate.save!
 else
 #create new record if not yet rated
         ratings << SelfRateable::Rating.new(rating: stars.to_i, rater_id: rater.try(:id))
 end
 self.save!
end
rated_by_rater?(rater) click to toggle source
# File lib/self_rateable/core/instance_methods.rb, line 32
def rated_by_rater?(rater)
  ratings.where(:rater_id => rater.id).count > 0
end
stars() click to toggle source
# File lib/self_rateable/core/instance_methods.rb, line 28
   def stars
  ratings.average(:rating).to_f
end