ajaxful_rating 2.1.3 → 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,5 @@
1
1
  == 2.1.0 July 25, 2009
2
2
  * AjaxfulRating is now available as a gem
3
+
4
+ == 2.1.4 January 25, 2010
5
+ * Added option for showing current_user's rate instead of global average
data/README.textile CHANGED
@@ -43,10 +43,10 @@ Also this generator copies the necesary images, styles, etc.
43
43
  Example:
44
44
  _I suppose you have generated already an authenticated model..._
45
45
 
46
- <pre><code>
47
- script/generate authenticated user sessions
48
- script/generate ajaxful_rating user
49
- </code></pre>
46
+ <pre>
47
+ script/generate authenticated user sessions
48
+ script/generate ajaxful_rating user
49
+ </pre>
50
50
 
51
51
  So this call will create a Rate model and will link it to your User model.
52
52
 
@@ -60,20 +60,20 @@ customise this call:
60
60
  * @:dimensions@ Array of dimensions. Allows to rate the model on various specs,
61
61
  like for example: a car could be rated for its speed, beauty or price.
62
62
 
63
- <pre><code>
64
- class Car < ActiveRecord::Base
65
- ajaxful_rateable :stars => 10, :dimensions => [:speed, :beauty, :price]
66
- end
67
- </code></pre>
63
+ <pre>
64
+ class Car < ActiveRecord::Base
65
+ ajaxful_rateable :stars => 10, :dimensions => [:speed, :beauty, :price]
66
+ end
67
+ </pre>
68
68
 
69
69
  Then you need to add a call @ajaxful_rater@ in the user model. This includes a simple line so
70
70
  you can add it _by your own_ as well (@has_many :rates@).
71
71
 
72
- <pre><code>
73
- class User < ActiveRecord::Base
74
- ajaxful_rater
75
- end
76
- </code></pre>
72
+ <pre>
73
+ class User < ActiveRecord::Base
74
+ ajaxful_rater
75
+ end
76
+ </pre>
77
77
 
78
78
  Finally, as a mere recomendation to make it even easier, modify your routes to
79
79
  map a rate action:
@@ -87,54 +87,66 @@ It tries to call @current_user@ method as the rater instance. You can pass @:sta
87
87
  as the second param to display only the static stars (not clickables).
88
88
  And also you can pass the dimension you want to show the ratings for.
89
89
 
90
- <pre><code>
91
- #show.html.erb
92
- <%= ratings_for @article %>
93
-
94
- #To display static stars:
95
- <%= ratings_for @article, :static %>
90
+ <pre>
91
+ #show.html.erb
92
+ <%= ratings_for @article %>
93
+
94
+ #To display static stars:
95
+ <%= ratings_for @article, :static %>
96
96
 
97
- #To display the ratings for a dimension:
98
- <%= ratings_for @article, :dimension => :speed %>
99
- </code></pre>
97
+ #To display the ratings for a dimension:
98
+ <%= ratings_for @article, :dimension => :speed %>
99
+ </pre>
100
100
 
101
101
  Or you can specify a custom user instance by passing it as parameter.
102
102
 
103
- <pre><code>
104
- <%= ratings_for @article, @user %>
105
- </code></pre>
106
-
103
+ <pre>
104
+ <%= ratings_for @article, @user %>
105
+ </pre>
106
+
107
+ By default ratings_for will display the average user rating. If you would like it to
108
+ display the rating for the current_user, then set the :show_user_rating parameter to true.
109
+ For example:
110
+
111
+ <pre>
112
+ # To display the rating for the current user (current_user):
113
+ <%= ratings_for @article, :show_user_rating => true %>
114
+
115
+ # To display the rating for the user specified by @user:
116
+ <%= ratings_for @article, @user, :show_user_rating => true %>
117
+ </pre>
118
+
107
119
  There's a condition here, if you didn't add the route @rate@ to your resource
108
120
  (as shown above) or you named it different, you'll need to pass the url to the
109
121
  correct action in your controller:
110
122
 
111
- <pre><code>
112
- <%= ratings_for @article, :remote_options => {:url => your_rate_path(@article)} %>
113
- </code></pre>
123
+ <pre>
124
+ <%= ratings_for @article, :remote_options => {:url => your_rate_path(@article)} %>
125
+ </pre>
114
126
 
115
127
  *To display the stars properly you need to add a call in the head of your layout, which will generate the
116
128
  required CSS style for the list. Also don't forget to include the javascripts.*
117
129
 
118
- <pre><code>
119
- #within the head tags of your layout...
120
- <%= javascript_include_tag :defaults %>
121
- <%= ajaxful_rating_style %>
122
- </code></pre>
130
+ <pre>
131
+ #within the head tags of your layout...
132
+ <%= javascript_include_tag :defaults %>
133
+ <%= ajaxful_rating_style %>
134
+ </pre>
123
135
 
124
136
  When a user submits a rating it will call the action in your controller, for
125
137
  example (if you added the @rate@ route):
126
138
 
127
- <pre><code>
128
- def rate
129
- @article = Article.find(params[:id])
130
- @article.rate(params[:stars], current_user, params[:dimension])
131
- id = "ajaxful-rating-#{!params[:dimension].blank? ? "#{params[:dimension]}-" : ''}article-#{@article.id}"
132
- render :update do |page|
133
- page.replace_html id, ratings_for(@article, :wrap => false, :dimension => params[:dimension])
134
- page.visual_effect :highlight, id
135
- end
139
+ <pre>
140
+ def rate
141
+ @article = Article.find(params[:id])
142
+ @article.rate(params[:stars], current_user, params[:dimension])
143
+ id = "ajaxful-rating-#{!params[:dimension].blank? ? "#{params[:dimension]}-" : ''}article-#{@article.id}"
144
+ render :update do |page|
145
+ page.replace_html id, ratings_for(@article, :wrap => false, :dimension => params[:dimension])
146
+ page.visual_effect :highlight, id
136
147
  end
137
- </code></pre>
148
+ end
149
+ </pre>
138
150
 
139
151
  There are some more options for this helper, please see the rdoc for details.
140
152
 
@@ -145,11 +157,11 @@ corresponding rates for the dimension you want.
145
157
 
146
158
  For example, you defined these dimensions:
147
159
 
148
- <pre><code>
149
- class Car < ActiveRecord::Base
150
- ajaxful_rateable :dimensions => [:speed, :beauty, :price]
151
- end
152
- </code></pre>
160
+ <pre>
161
+ class Car < ActiveRecord::Base
162
+ ajaxful_rateable :dimensions => [:speed, :beauty, :price]
163
+ end
164
+ </pre>
153
165
 
154
166
  And hence you can call @car.rates(:price)@ for the price rates or @car.rates(:speed)@ for the speed ratings and so on.
155
167
 
@@ -158,39 +170,39 @@ h3. Namespaces
158
170
  If you use the plugin inside a namespace you’ll need to specify the rating url which should points to
159
171
  a controller inside a namespace. Your files should be like these:
160
172
 
161
- <pre><code>
162
- routes.rb:
163
- map.namespace :admin do |admin|
164
- admin.resources :articles, :member => {:rate => :post}
165
- end
173
+ <pre>
174
+ routes.rb:
175
+ map.namespace :admin do |admin|
176
+ admin.resources :articles, :member => {:rate => :post}
177
+ end
166
178
 
167
- views/admin/articles/show.html.erb
168
- <%= ratings_for @article, :remote_options => {:url => rate_admin_article_path(@article)} %>
169
- </code></pre>
179
+ views/admin/articles/show.html.erb
180
+ <%= ratings_for @article, :remote_options => {:url => rate_admin_article_path(@article)} %>
181
+ </pre>
170
182
 
171
183
  h3. Cache
172
184
 
173
185
  To cache the model's rating average add a column named @rating_average@ to your model table:
174
186
 
175
- <pre><code>
176
- class AddRatingAverageToArticles < ActiveRecord::Migration
177
- def self.up
178
- add_column :articles, :rating_average, :decimal, :default => 0
179
- end
180
-
181
- def self.down
182
- remove_column :articles, :rating_average
183
- end
187
+ <pre>
188
+ class AddRatingAverageToArticles < ActiveRecord::Migration
189
+ def self.up
190
+ add_column :articles, :rating_average, :decimal, :default => 0, :precision => 6, :scale => 2
184
191
  end
185
- </code></pre>
192
+
193
+ def self.down
194
+ remove_column :articles, :rating_average
195
+ end
196
+ end
197
+ </pre>
186
198
 
187
199
  If you want to customise the name of the cache column just pass it in the options hash:
188
200
 
189
- <pre><code>
190
- class Article < ActiveRecord::Base
191
- ajaxful_rateable :cache_column => :my_cached_rating
192
- end
193
- </code></pre>
201
+ <pre>
202
+ class Article < ActiveRecord::Base
203
+ ajaxful_rateable :cache_column => :my_cached_rating
204
+ end
205
+ </pre>
194
206
 
195
207
  To use caching with dimensions, make sure you have a cache column defined for each dimension you want cached.
196
208
  So if you want to cache the @spelling@ dimension, you’ll need to have a column called @rating_average_spelling@ on the articles table.
data/Rakefile CHANGED
@@ -2,11 +2,11 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('ajaxful_rating', '2.1.3') do |p|
5
+ Echoe.new('ajaxful_rating', '2.1.4') do |p|
6
6
  p.description = "Provides a simple way to add rating functionality to your application."
7
7
  p.url = "http://github.com/edgarjs/ajaxful-rating"
8
8
  p.author = "Edgar J. Suarez"
9
- p.email = "e@dgar.org"
9
+ p.email = "edgar.js@gmail.com"
10
10
  p.ignore_pattern = ["tmp/*", "script/*"]
11
11
  p.development_dependencies = []
12
12
  end
@@ -2,13 +2,13 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{ajaxful_rating}
5
- s.version = "2.1.3"
5
+ s.version = "2.1.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Edgar J. Suarez"]
9
- s.date = %q{2009-09-09}
9
+ s.date = %q{2010-01-25}
10
10
  s.description = %q{Provides a simple way to add rating functionality to your application.}
11
- s.email = %q{e@dgar.org}
11
+ s.email = %q{edgar.js@gmail.com}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "README.textile", "lib/ajaxful_rating.rb", "lib/ajaxful_rating_helper.rb", "lib/ajaxful_rating_model.rb"]
13
13
  s.files = ["CHANGELOG", "Manifest", "README.textile", "Rakefile", "ajaxful_rating.gemspec", "generators/ajaxful_rating/USAGE", "generators/ajaxful_rating/ajaxful_rating_generator.rb", "generators/ajaxful_rating/templates/images/star.png", "generators/ajaxful_rating/templates/images/star_small.png", "generators/ajaxful_rating/templates/migration.rb", "generators/ajaxful_rating/templates/model.rb", "generators/ajaxful_rating/templates/style.css", "init.rb", "lib/ajaxful_rating.rb", "lib/ajaxful_rating_helper.rb", "lib/ajaxful_rating_model.rb"]
14
14
  s.homepage = %q{http://github.com/edgarjs/ajaxful-rating}
@@ -17,6 +17,7 @@ module AjaxfulRating # :nodoc:
17
17
  # * <tt>:remote_options</tt> Hash of options for the link_to_remote function.
18
18
  # Default is {:method => :post, :url => rate_rateablemodel_path(rateable)}.
19
19
  # * <tt>:wrap</tt> Whether the star list is wrapped within a div tag or not. This is useful when page updating. Default is true.
20
+ # * <tt>:force_dynamic</tt> Whether the star list is always clickable. This is useful for page caching when set to true. Default is false.
20
21
  #
21
22
  # Example:
22
23
  # <%= ratings_for @article %>
@@ -53,6 +54,13 @@ module AjaxfulRating # :nodoc:
53
54
  # # update page, etc.
54
55
  # end
55
56
  #
57
+ # By default ratings_for will render the average rating for all users. If however you would like to display the rating for a single user, then set the :show_user_rating option to true.
58
+ # For example:
59
+ #
60
+ # <%= ratings_for @article, :show_user_rating => true %>
61
+ # Or
62
+ # <%= ratings_for @article, @user, :show_user_rating => true %>
63
+ #
56
64
  # I18n:
57
65
  #
58
66
  # You can translate the title of the images (the tool tip that shows when the mouse is over) and the 'Currently x/x stars'
@@ -66,20 +74,24 @@ module AjaxfulRating # :nodoc:
66
74
  # other: "{{count}} stars out of {{total}}"
67
75
  def ratings_for(rateable, *args)
68
76
  user = extract_options(rateable, *args)
77
+ user_rating = if options[:show_user_rating] == true and rateable.rated_by?(user, options[:dimension])
78
+ rateable.rates(options[:dimension]).find_by_user_id(user).stars
79
+ else
80
+ user_rating = 0
81
+ end
69
82
  ajaxful_styles << %Q(
70
83
  .#{options[:class]} { width: #{rateable.class.max_rate_value * 25}px; }
71
84
  .#{options[:small_star_class]} { width: #{rateable.class.max_rate_value * 10}px; }
72
85
  )
73
- width = (rateable.rate_average(true, options[:dimension]) / rateable.class.max_rate_value.to_f) * 100
86
+ width = ((options[:show_user_rating] == true ? user_rating : rateable.rate_average(true, options[:dimension])) / rateable.class.max_rate_value.to_f) * 100
74
87
  ul = content_tag(:ul, options[:html]) do
75
- Range.new(1, rateable.class.max_rate_value).collect do |i|
88
+ (1..rateable.class.max_rate_value).collect do |i|
76
89
  build_star rateable, user, i
77
- end.insert(0, content_tag(:li, current_average(rateable),
78
- :class => 'current-rating', :style => "width:#{width}%"))
90
+ end.insert(0, content_tag(:li, current_average(rateable), :class => 'current-rating', :style => "width:#{width}%")).join
79
91
  end
80
92
  if options[:wrap]
81
93
  content_tag(:div, ul, :class => 'ajaxful-rating-wrapper', :id => "ajaxful-rating-#{!options[:dimension].blank? ?
82
- "#{options[:dimension]}-" : ''}#{rateable.class.name.downcase}-#{rateable.id}")
94
+ "#{options[:dimension]}-" : ''}#{rateable.class.name.tableize.singularize}-#{rateable.id}")
83
95
  else
84
96
  ul
85
97
  end
@@ -109,7 +121,7 @@ module AjaxfulRating # :nodoc:
109
121
  }
110
122
  )
111
123
  rated = rateable.rated_by?(user, options[:dimension]) if user
112
- star = if user && ((rated && rateable.class.options[:allow_update]) || !rated)
124
+ star = if options[:force_dynamic] || (user && ((rated && rateable.class.options[:allow_update]) || !rated))
113
125
  link_to_remote(i, build_remote_options({:class => a_class, :title => pluralize_title(i, rateable.class.max_rate_value)}, i))
114
126
  else
115
127
  content_tag(:span, i, :class => a_class, :title => current_average(rateable))
@@ -121,6 +133,7 @@ module AjaxfulRating # :nodoc:
121
133
  def options
122
134
  @ajaxful_options ||= {
123
135
  :wrap => true,
136
+ :force_dynamic => false,
124
137
  :class => 'ajaxful-rating',
125
138
  :link_class_prefix => :stars,
126
139
  :small_stars => false,
@@ -155,6 +168,7 @@ module AjaxfulRating # :nodoc:
155
168
 
156
169
  # Extracts the hash options and returns the user instance.
157
170
  def extract_options(rateable, *args)
171
+ options[:show_user_rating] = false # Reset
158
172
  user = if args.first.class.name == rateable.class.user_class_name.classify
159
173
  args.shift
160
174
  elsif args.first != :static
@@ -163,9 +177,9 @@ module AjaxfulRating # :nodoc:
163
177
  config = (!args.empty? && args.last.is_a?(Hash)) ? args.last : {}
164
178
  if config[:remote_options] && config[:remote_options][:url]
165
179
  # we keep the passed url
166
- elsif user
180
+ else#if user
167
181
  config[:remote_options] = {
168
- :url => respond_to?(url = "rate_#{rateable.class.name.downcase}_path") ?
182
+ :url => respond_to?(url = "rate_#{rateable.class.name.tableize.singularize}_path") ?
169
183
  send(url, rateable) : raise(MissingRateRoute)
170
184
  }
171
185
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajaxful_rating
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edgar J. Suarez
@@ -9,12 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-09 00:00:00 -05:00
12
+ date: 2010-01-25 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
16
  description: Provides a simple way to add rating functionality to your application.
17
- email: e@dgar.org
17
+ email: edgar.js@gmail.com
18
18
  executables: []
19
19
 
20
20
  extensions: []