Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/helpers/rating_engine/ratings_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module RatingEngine
module RatingsHelper
def star_rating_for(rateable, params={})
enabled, show_score = params[:enabled].nil? ? true : params[:enabled], (params[:show_score] || true)
enabled = params[:enabled].nil? ? true : params[:enabled]
show_score = params[:show_score] || true
current_score = show_score ? score_in_pixels(rateable.average_rating) : 0

render :partial => 'rating_engine/ratings/star_rating',
Expand All @@ -10,15 +11,21 @@ def star_rating_for(rateable, params={})
end

def score_in_pixels(current_score)
((current_score * 125.0)/RatingEngine.max_score).round
(current_score * RatingEngine.star_length).round
end

def rating_url(rateable)
if rateable.rated_by?(controller.current_user)
if current_user.nil?
""
elsif rateable.rated_by?(controller.current_user)
eval("#{rateable.class.to_s.downcase}_rating_path(#{rateable.id})")
else
eval("#{rateable.class.to_s.downcase}_ratings_path(#{rateable.id})")
end
end

def star_left_padding(score)
score_in_pixels(score) - RatingEngine.star_length
end
end
end
end
4 changes: 2 additions & 2 deletions app/views/rating_engine/ratings/_star_rating.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<% if enabled %>
<% (RatingEngine.min_score..RatingEngine.max_score).each do |score| %>
<li>
<a href="#" title='<%= score %> stars out of <%= RatingEngine.max_score %>' style='left:<%= ((score) * 25) - 25%>px'>
<a href="#" title='<%= score %> stars out of <%= RatingEngine.max_score %>' style='left:<%= star_left_padding(score) %>px'>
<%= score %>
</a>
</li>
<% end %>
<% end %>
</ul>
</div>
</div>
5 changes: 4 additions & 1 deletion lib/rating_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module RatingEngine
mattr_accessor :min_score
@@min_score = 1

mattr_accessor :star_length
@@star_length = 25

def self.setup
yield self
end
Expand All @@ -22,4 +25,4 @@ def self.setup
require 'rating_engine/engine'
end

end
end