Module: SelfRateable::Core::InstanceMethods::Stars
- Defined in:
- lib/self_rateable/core/instance_methods.rb
Instance Method Summary collapse
Instance Method Details
#rate(rater, stars) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/self_rateable/core/instance_methods.rb', line 36 def rate(rater, stars) rater.present? ? rate = .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. = stars.to_i rate.save! else #create new record if not yet rated << SelfRateable::Rating.new(rating: stars.to_i, rater_id: rater.try(:id)) end self.save! end |
#rated_by_rater?(rater) ⇒ Boolean
32 33 34 |
# File 'lib/self_rateable/core/instance_methods.rb', line 32 def rated_by_rater?(rater) .where(:rater_id => rater.id).count > 0 end |
#stars ⇒ Object
28 29 30 |
# File 'lib/self_rateable/core/instance_methods.rb', line 28 def stars .average(:rating).to_f end |