Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add possibility to rerate #23

Open
wants to merge 2 commits into from

7 participants

@robban

Add the possibility to change ones rating. The new rating replaces the old and the average is recalculated.

@mpakus

if I try to rerate I see 500 code with internal error message "User has already rated.app/controllers/rater_controller.rb, line 7"

@robban

This is not merged yet, so try the add_possibility_to_rerate branch on robban/letsrate and see if it fixes your problems.

@mpakus

cool, thanx a lot!

@andreslucena

Related with #4

@MaRoe

As I miss a vote button i have to beg with a comment: please merge those changes :) :+1:

@mjc
mjc commented

:+1:

@willviles

Merge this please!

@igorsokolov

+1 for this change to be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 18 deletions.
  1. +11 −18 lib/letsrate/model.rb
View
29 lib/letsrate/model.rb
@@ -8,29 +8,22 @@ def rate(stars, user_id, dimension=nil)
r.stars = stars
r.rater_id = user_id
r.save!
- end
- update_rate_average(stars, dimension)
+ end
else
- raise "User has already rated."
+ previous_rate = rates(dimension).where(:rater_id => user_id).first
+ previous_rate.stars = stars
+ previous_rate.save!
end
+ update_rate_average(stars, dimension)
end
def update_rate_average(stars, dimension=nil)
- if average(dimension).nil?
- RatingCache.create do |avg|
- avg.cacheable_id = self.id
- avg.cacheable_type = self.class.name
- avg.avg = stars
- avg.qty = 1
- avg.dimension = dimension
- avg.save!
- end
- else
- a = average(dimension)
- a.avg = (a.avg*a.qty + stars) / (a.qty+1)
- a.qty = a.qty + 1
- a.save!
- end
+ rating_cache_properties = {:cacheable_id => self.id,:cacheable_type => self.class.name,:dimension => dimension}
+ rating_cache = RatingCache.where(rating_cache_properties).first || RatingCache.new(rating_cache_properties)
+ all_rates = Rate.where(:rateable_id => self.id, :rateable_type => self.class.name, :dimension => dimension)
+ rating_cache.qty = all_rates.count
+ rating_cache.avg = all_rates.map(&:stars).sum.to_f / rating_cache.qty
+ rating_cache.save!
end
def average(dimension=nil)
Something went wrong with that request. Please try again.