diff --git a/README.md b/README.md index 8285bd8..3fc8c8b 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,8 @@ This is a fork against the repository [muratguzel/letsrate](https://github.com/m ## TODO 1. Write RSpec tests for this Gem -3. Add option to show the number of users who gave rates -4. Add a share helper to Facebook, Twitter -5. Force refresh after rating when ***disable_after_rate*** and ***imdb_avg*** options is set to true. +2. Add a share helper to Facebook, Twitter +3. Force refresh after rating when ***disable_after_rate*** and ***imdb_avg*** options is set to true. ## Detailed view of the new features @@ -77,7 +76,7 @@ rails g ratyrate user # => user is the model generated by devise ``` This generator will create Rate and RatingCache models, -db/migrations, +db/migrations, and link to your user model. ### Database Migration @@ -179,6 +178,11 @@ Speed : <%= rating_for @car, 'speed', cancel_on: 'cancel-on2.png' %> ```erb Speed : <%= rating_for @car, 'speed', cancel_off: 'cancel-off2.png' %> ``` +11- To show the number of voters **(default is false)** +```erb +Speed : <%= rating_for @car, 'speed', num_of_voters: true %> +``` + ### Other Helpers You can use the *rating_for_user* helper method to show the star rating for the user. diff --git a/lib/generators/ratyrate/templates/jquery.raty.js.erb b/lib/generators/ratyrate/templates/jquery.raty.js.erb index 0d7e491..04aeee4 100644 --- a/lib/generators/ratyrate/templates/jquery.raty.js.erb +++ b/lib/generators/ratyrate/templates/jquery.raty.js.erb @@ -754,7 +754,8 @@ targetKeep : false, targetScore : undefined, targetText : '', - targetType : 'hint' + targetType : 'hint', + numberOfVoters: false }; })(jQuery); diff --git a/lib/ratyrate/helpers.rb b/lib/ratyrate/helpers.rb index 8eade55..be7d2c9 100644 --- a/lib/ratyrate/helpers.rb +++ b/lib/ratyrate/helpers.rb @@ -1,6 +1,5 @@ module Helpers def rating_for(rateable_obj, dimension=nil, options={}) - cached_average = rateable_obj.average dimension avg = cached_average ? cached_average.avg : 0 @@ -26,6 +25,7 @@ def rating_for(rateable_obj, dimension=nil, options={}) targetFormat = options[:targetFormat] || '{score}' targetScore = options[:targetScore] || '' readOnly = options[:readonly] || false + num_of_voters = options[:num_of_voters] || false disable_after_rate = options[:disable_after_rate] && true disable_after_rate = true if disable_after_rate == nil @@ -38,6 +38,10 @@ def rating_for(rateable_obj, dimension=nil, options={}) end end + if num_of_voters + num_of_voters = Rate.where(:rateable_id => rateable_obj.id).count + end + if options[:imdb_avg] && readOnly content_tag :div, '', :style => "background-image:url('#{image_path('mid-star.png')}');width:61px;height:57px;margin-top:10px;" do content_tag :p, avg, :style => "position:relative;font-size:.8rem;text-align:center;line-height:60px;" @@ -67,7 +71,9 @@ def rating_for(rateable_obj, dimension=nil, options={}) "data-target-text" => targetText, "data-target-type" => targetType, "data-target-format" => targetFormat, - "data-target-score" => targetScore + "data-target-score" => targetScore, + "data-number-of-voters" => num_of_voters + end end @@ -108,6 +114,7 @@ def rating_for_user(rateable_obj, rating_user, dimension = nil, options = {}) targetFormat = options[:targetFormat] || '{score}' targetScore = options[:targetScore] || '' readOnly = options[:readonly] || false + num_of_voters = options[:num_of_voters] || false disable_after_rate = options[:disable_after_rate] || false @@ -115,6 +122,10 @@ def rating_for_user(rateable_obj, rating_user, dimension = nil, options = {}) readOnly = rating_user.present? ? !rateable_obj.can_rate?(rating_user, dimension) : true end + if num_of_voters + num_of_voters = Rate.where(:rateable_id => rateable_obj.id).count + end + content_tag :div, '', "data-dimension" => dimension, :class => "star", "data-rating" => stars, "data-id" => rateable_obj.id, "data-classname" => rateable_obj.class.name == rateable_obj.class.base_class.name ? rateable_obj.class.name : rateable_obj.class.base_class.name, "data-disable-after-rate" => disable_after_rate, @@ -138,7 +149,8 @@ def rating_for_user(rateable_obj, rating_user, dimension = nil, options = {}) "data-target" => target, "data-target-text" => targetText, "data-target-format" => targetFormat, - "data-target-score" => targetScore + "data-target-score" => targetScore, + "data-number-of-voters" => num_of_voters end end