Created
October 30, 2008 14:19
-
-
Save rumblex/21009 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rating(*args) | |
options = args.extract_options! | |
category = options.delete(:category) | |
as_float = options.delete(:as_float) | |
certified = options.delete(:certified) | |
user_or_ip = options.delete(:user) || options.delete(:ip) | |
if user_or_ip && Rating.instance_methods.include?('user') | |
options[:conditions] = ["user_id = ?", user_or_ip.id] | |
elsif user_or_ip | |
options[:conditions] = ["ip = ?", user_or_ip] | |
end | |
unless category.nil? | |
if options[:conditions] | |
options[:conditions][0] += " AND category = ?" | |
options[:conditions] << category.to_s | |
else | |
options[:conditions] = ["category = ?", category.to_s] | |
end | |
end | |
selected_ratings = certified ? ratings : ratings.certified | |
if (rating = selected_ratings.average(:value, options)) | |
as_float ? rating : rating.round | |
else | |
as_float ? 0.0 : Integer(0) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment