Module: AjaxfulRating::Helper
- Included in:
- ActionView::Base
- Defined in:
- lib/ajaxful_rating_helper.rb
Defined Under Namespace
Classes: MissingRateRoute
Instance Method Summary collapse
-
#ajaxful_rating_style ⇒ Object
Call this method <strong>within head tags</strong> of the main layout to yield the dynamic styles.
-
#ratings_for(rateable, *args) ⇒ Object
Generates the stars list to submit a rate.
Instance Method Details
#ajaxful_rating_style ⇒ Object
Call this method <strong>within head tags</strong> of the main layout to yield the dynamic styles. It will include the necessary stlyesheet and output the dynamic CSS.
Example:
<head>
<%= ajaxful_rating_style %>
</head>
95 96 97 98 |
# File 'lib/ajaxful_rating_helper.rb', line 95 def stylesheet_link_tag('ajaxful_rating') + content_tag(:style, ajaxful_styles, :type => 'text/css') unless ajaxful_styles.blank? end |
#ratings_for(rateable, *args) ⇒ Object
Generates the stars list to submit a rate.
It accepts the next options:
-
:class
CSS class for the ul. Default is ‘ajaxful-rating’. -
:link_class_prefix
Prefix for the li a CSS class. Default is ‘stars’. -
:small_stars
Set this param to true to display smaller images. Default is false. -
:small_star_class
CSS class for the list when using small images. Default is ‘small-stars’. -
:html
Hash of options to customise the ul tag. -
:remote_options
Hash of options for the link_to_remote function.
Default is => :post, :url => rate_rateablemodel_path(rateable).
-
:wrap
Whether the star list is wrapped within a div tag or not. This is useful when page updating. Default is true.
Example:
<%= ratings_for @article %>
# => Will produce something like:
<ul class="ajaxful-rating">
<li class="current-rating" style="width: 60%;">Currently 3/5 stars</li>
<li><%= link_to_remote 1, :url => rate_article_path(@article, :stars => 1), :method => :post, :html => {:class => 'stars-1', :title => '1 star out of 5'} %></li>
<li><%= link_to_remote 2, :url => rate_article_path(@article, :stars => 2), :method => :post, :html => {:class => 'stars-2', :title => '2 stars out of 5'} %></li>
<li><%= link_to_remote 3, :url => rate_article_path(@article, :stars => 3), :method => :post, :html => {:class => 'stars-3', :title => '3 stars out of 5'} %></li>
<li><%= link_to_remote 4, :url => rate_article_path(@article, :stars => 4), :method => :post, :html => {:class => 'stars-4', :title => '4 stars out of 5'} %></li>
<li><%= link_to_remote 5, :url => rate_article_path(@article, :stars => 5), :method => :post, :html => {:class => 'stars-5', :title => '5 stars out of 5'} %></li>
</ul>
It will try to use the method current_user
as the user instance. You can specify a custom instance in the second parameter or pass :static
to leave the list of stars static.
Example:
<%= ratings_for @article, @user, :small_stars => true %>
# => Will use @user instead <tt>current_user</tt>
<%= ratings_for @article, :static, :small_stars => true %>
# => Will produce a static list of stars showing the current rating average for @article.
The user passed here will not be the one who submits the rate. It will be used only for the display behavior of the stars. Like for example, if there is a user logged in or if the current logged in user is able to submit a rate depending on the configuration (accepts update of rates, etc).
So to actually set the user who will rate the model you need to do it in your controller:
# controller
def rate
@article = Article.find(params[:id])
@article.rate(params[:stars], current_user) # or any user instance
# update page, etc.
end
I18n:
You can translate the title of the images (the tool tip that shows when the mouse is over) and the ‘Currently x/x stars’ string by setting these keys on your translation hash:
ajaxful_rating:
stars:
current_average: "Current rating: {{average}}/{{max}}"
title:
one: 1 star out of {{total}}
other: "{{count}} stars out of {{total}}"
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ajaxful_rating_helper.rb', line 67 def (rateable, *args) user = (rateable, *args) ajaxful_styles << %Q( .#{[:class]} { width: #{rateable.class.max_rate_value * 25}px; } .#{[:small_star_class]} { width: #{rateable.class.max_rate_value * 10}px; } ) width = (rateable.rate_average(true, [:dimension]) / rateable.class.max_rate_value.to_f) * 100 ul = content_tag(:ul, [:html]) do Range.new(1, rateable.class.max_rate_value).collect do |i| build_star rateable, user, i end.insert(0, content_tag(:li, current_average(rateable), :class => 'current-rating', :style => "width:#{width}%")) end if [:wrap] content_tag(:div, ul, :class => 'ajaxful-rating-wrapper', :id => "ajaxful-rating-#{![:dimension].blank? ? "#{[:dimension]}-" : ''}#{rateable.class.name.downcase}-#{rateable.id}") else ul end end |