Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the option to generate number of voters #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/generators/ratyrate/templates/jquery.raty.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,8 @@
targetKeep : false,
targetScore : undefined,
targetText : '',
targetType : 'hint'
targetType : 'hint',
numberOfVoters: false
};

})(jQuery);
18 changes: 15 additions & 3 deletions lib/ratyrate/helpers.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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;"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -108,13 +114,18 @@ 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

if disable_after_rate
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,
Expand All @@ -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
Expand Down