Permalink
Browse files

Merge pull request #20 from denislavski/master

Added option 'disable_after_rate' which makes the stars readOnly for the current user after successful rate
  • Loading branch information...
2 parents 642277c + 5f336a2 commit 10471deef72b00815720e5c3899e2e00e2674015 @muratguzel committed Mar 27, 2013
Showing with 58 additions and 42 deletions.
  1. +36 −27 lib/generators/letsrate/templates/letsrate.js
  2. +22 −15 lib/letsrate/helpers.rb
@@ -1,27 +1,36 @@
-$.fn.raty.defaults.path = "/assets";
-$.fn.raty.defaults.half_show = true;
-
-$(function(){
- $(".star").raty({
- score: function(){
- return $(this).attr('data-rating')
- },
- number: function() {
- return $(this).attr('data-star-count')
- },
- click: function(score, evt) {
- $.post('<%= Rails.application.class.routes.url_helpers.rate_path %>',
- {
- score: score,
- dimension: $(this).attr('data-dimension'),
- id: $(this).attr('data-id'),
- klass: $(this).attr('data-classname')
- },
- function(data) {
- if(data) {
- // success code goes here ...
- }
- });
- }
- });
-});
+$.fn.raty.defaults.path = "/assets";
+$.fn.raty.defaults.half_show = true;
+
+$(function(){
+ $(".star").each(function() {
+ var $readonly = ($(this).attr('data-readonly') == 'true');
+ $(this).raty({
+ score: function(){
+ return $(this).attr('data-rating')
+ },
+ number: function() {
+ return $(this).attr('data-star-count')
+ },
+ readOnly: $readonly,
+ click: function(score, evt) {
+ var _this = this;
+ $.post('<%= Rails.application.class.routes.url_helpers.rate_path %>',
+ {
+ score: score,
+ dimension: $(this).attr('data-dimension'),
+ id: $(this).attr('data-id'),
+ klass: $(this).attr('data-classname')
+ },
+ function(data) {
+ if(data) {
+ // success code goes here ...
+
+ if ($(_this).attr('data-disable-after-rate') == 'true') {
+ $(_this).raty('set', { readOnly: true, score: score });
+ }
+ }
+ });
+ }
+ });
+ });
+});
@@ -1,29 +1,36 @@
-module Helpers
- def rating_for(rateable_obj, dimension=nil, options={})
-
+module Helpers
+ def rating_for(rateable_obj, dimension=nil, options={})
+
if dimension.nil?
klass = rateable_obj.average
- else
- klass = rateable_obj.average "#{dimension}"
+ else
+ klass = rateable_obj.average "#{dimension}"
end
-
+
if klass.nil?
avg = 0
else
avg = klass.avg
end
-
+
star = options[:star] || 5
-
- content_tag :div, "", "data-dimension" => dimension, :class => "star", "data-rating" => avg,
- "data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name,
- "data-star-count" => star
-
-
+
+ disable_after_rate = options[:disable_after_rate] || false
+
+ readonly = false
+ if disable_after_rate
+ readonly = current_user.present? ? !rateable_obj.can_rate?(current_user.id, dimension) : true
+ end
+
+ content_tag :div, '', "data-dimension" => dimension, :class => "star", "data-rating" => avg,
+ "data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name,
+ "data-disable-after-rate" => disable_after_rate,
+ "data-readonly" => readonly,
+ "data-star-count" => star
end
-
+
end
class ActionView::Base
include Helpers
-end
+end

0 comments on commit 10471de

Please sign in to comment.