ajaxful_rating 2.2.9.1 → 3.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ pkg
2
+ doc
3
+ .DS_Store
4
+ *.gem
data/CHANGELOG CHANGED
@@ -1,21 +1,3 @@
1
- == 2.2.8.1 Jul 02, 2010
2
- * Call html_safe only when it's available.
3
-
4
- == 2.2.8 Jun 14, 2010
5
- * Fix html for <li> marking it as html_safe when using rails_xss plugin
6
-
7
- == 2.2.7 Jun 12, 2010
8
- * Fix generated html marking it as html_safe when using rails_xss plugin
9
-
10
- == 2.2.6 May 27, 2010
11
- * Fix wrapper_dom_id with false values for keys
12
-
13
- == 2.2.5 May 21, 2010
14
- * Fix wrapper_dom_id with empty values
15
-
16
- == 2.2.4 April 15, 2010
17
- * Fix wrapper_dom_id join options
18
-
19
1
  == 2.2.3 March 10, 2010
20
2
  * Fix validation of current_user to show linked stars
21
3
 
@@ -0,0 +1,33 @@
1
+ == 2.2.3 March 10, 2010
2
+ * Fix validation of current_user to show linked stars
3
+
4
+ == 2.2.2 March 9, 2010
5
+ * Fix bug for jRuby
6
+ * Fixed user self relational model to be rater and rateable.
7
+
8
+ == 2.2.1 March 8, 2010
9
+ * Changed :force_dynamic for its opposite :force_static.
10
+ * Fixed I18n texts.
11
+ * Fixed missed quotes for Rate belongs_to association with User class
12
+ * Detailed wrapper DOM id with passed hash.
13
+
14
+ == 2.2.0 March 7, 2010
15
+ * Improved model methods and options.
16
+ * Works now with inherited models.
17
+ * Removed HTML and CSS options for helper for sake of simpleness.
18
+ * Renamed helper option :small_stars to :small.
19
+ * Changed whole helper approach. It should work the same as before.
20
+ (Except for the renamed and removed options)
21
+ * Removed :force_dynamic, you should exclude the helper from cache since it uses
22
+ authorization.
23
+ * Fixed opened bugs so far.
24
+
25
+ == 2.1.5 January 28, 2010
26
+ * Renamed options method name to avoid collapse.
27
+ * Removed CSS duplication.
28
+
29
+ == 2.1.4 January 25, 2010
30
+ * Added option for showing current_user's rate instead of global average
31
+
32
+ == 2.1.0 July 25, 2009
33
+ * AjaxfulRating is now available as a gem
data/Manifest CHANGED
@@ -2,7 +2,6 @@ CHANGELOG
2
2
  Manifest
3
3
  README.textile
4
4
  Rakefile
5
- ajaxful_rating.gemspec
6
5
  generators/ajaxful_rating/USAGE
7
6
  generators/ajaxful_rating/ajaxful_rating_generator.rb
8
7
  generators/ajaxful_rating/templates/images/star.png
@@ -64,7 +64,8 @@ class Car < ActiveRecord::Base
64
64
  end
65
65
  </pre>
66
66
 
67
- Then you need to add a call @ajaxful_rater@ in the user model to make your @User@ model able to rate objects.
67
+ Then you need to add a call @ajaxful_rater@ in the user model. This includes a simple line so
68
+ you can add it _by your own_ as well (@has_many :rates@).
68
69
 
69
70
  <pre>
70
71
  class User < ActiveRecord::Base
@@ -121,13 +122,9 @@ correct action in your controller:
121
122
  <%= ratings_for @article, :remote_options => {:url => your_rate_path(@article)} %>
122
123
  </pre>
123
124
 
124
- h3. Important!
125
-
126
125
  *To display the stars properly you need to add a call in the head of your layout, which will generate the
127
126
  required CSS style for the list. Also don't forget to include the javascripts.*
128
127
 
129
- It's also important to note that this call MUST be within your head tags in your layout, as for now it seems to doesn't work with the @content_for@ tag.
130
-
131
128
  <pre>
132
129
  #within the head tags of your layout...
133
130
  <%= javascript_include_tag :defaults %>
data/Rakefile CHANGED
@@ -1,14 +1,21 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
- require 'echoe'
4
3
 
5
- Echoe.new('ajaxful_rating', '2.2.9.1') do |p|
6
- p.description = "Provides a simple way to add rating functionality to your application."
7
- p.url = "http://github.com/edgarjs/ajaxful-rating"
8
- p.author = "Edgar J. Suarez"
9
- p.email = "edgar.js@gmail.com"
10
- p.ignore_pattern = ["tmp/*", "script/*"]
11
- p.development_dependencies = []
4
+ begin
5
+ require 'jeweler'
6
+
7
+ Jeweler::Tasks.new do |spec|
8
+ spec.name = "ajaxful_rating"
9
+ spec.version = "3.0.0.beta1"
10
+ spec.summary = "Provides a simple way to add rating functionality to your application."
11
+ spec.description = "Provides a simple way to add rating functionality to your application."
12
+ spec.homepage = "http://github.com/edgarjs/ajaxful-rating"
13
+ spec.authors = ["Edgar J. Suarez", "Denis Odorcic"]
14
+ spec.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com"]
15
+ spec.has_rdoc = true
16
+ spec.files.exclude 'init.rb'
17
+ end
18
+
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: gem install jeweler"
12
21
  end
13
-
14
- Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -1,29 +1,74 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
1
4
  # -*- encoding: utf-8 -*-
2
5
 
3
6
  Gem::Specification.new do |s|
4
- s.name = "ajaxful_rating"
5
- s.version = "2.2.9.1"
7
+ s.name = %q{ajaxful_rating}
8
+ s.version = "3.0.0.beta1"
6
9
 
7
- s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Edgar J. Suarez"]
9
- s.date = "2012-06-29"
10
- s.description = "Provides a simple way to add rating functionality to your application."
11
- s.email = "edgar.js@gmail.com"
12
- s.extra_rdoc_files = ["CHANGELOG", "README.textile", "lib/ajaxful_rating.rb", "lib/axr/css_builder.rb", "lib/axr/errors.rb", "lib/axr/helpers.rb", "lib/axr/locale.rb", "lib/axr/model.rb", "lib/axr/stars_builder.rb"]
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/axr/css_builder.rb", "lib/axr/errors.rb", "lib/axr/helpers.rb", "lib/axr/locale.rb", "lib/axr/model.rb", "lib/axr/stars_builder.rb"]
14
- s.homepage = "http://github.com/edgarjs/ajaxful-rating"
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ajaxful_rating", "--main", "README.textile"]
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Edgar J. Suarez", "Denis Odorcic"]
12
+ s.date = %q{2010-05-03}
13
+ s.description = %q{Provides a simple way to add rating functionality to your application.}
14
+ s.email = ["edgar.js@gmail.com", "denis.odorcic@gmail.com"]
15
+ s.extra_rdoc_files = [
16
+ "ChangeLog",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ "CHANGELOG",
22
+ "Manifest",
23
+ "README.textile",
24
+ "Rakefile",
25
+ "ajaxful_rating.gemspec",
26
+ "lib/ajaxful_rating.rb",
27
+ "lib/axr/css_builder.rb",
28
+ "lib/axr/errors.rb",
29
+ "lib/axr/helpers.rb",
30
+ "lib/axr/locale.rb",
31
+ "lib/axr/model.rb",
32
+ "lib/axr/stars_builder.rb",
33
+ "lib/generators/ajaxful_rating/USAGE",
34
+ "lib/generators/ajaxful_rating/ajaxful_rating_generator.rb",
35
+ "lib/generators/ajaxful_rating/templates/images/star.png",
36
+ "lib/generators/ajaxful_rating/templates/images/star_small.png",
37
+ "lib/generators/ajaxful_rating/templates/migration.rb",
38
+ "lib/generators/ajaxful_rating/templates/model.rb",
39
+ "lib/generators/ajaxful_rating/templates/style.css",
40
+ "test/fixtures/car.rb",
41
+ "test/fixtures/cars.yml",
42
+ "test/fixtures/rate.rb",
43
+ "test/fixtures/rates.yml",
44
+ "test/fixtures/schema.rb",
45
+ "test/fixtures/user.rb",
46
+ "test/fixtures/users.yml",
47
+ "test/lib/model_test.rb",
48
+ "test/test_helper.rb"
49
+ ]
50
+ s.homepage = %q{http://github.com/edgarjs/ajaxful-rating}
51
+ s.rdoc_options = ["--charset=UTF-8"]
16
52
  s.require_paths = ["lib"]
17
- s.rubyforge_project = "ajaxful_rating"
18
- s.rubygems_version = "1.8.21"
19
- s.summary = "Provides a simple way to add rating functionality to your application."
53
+ s.rubygems_version = %q{1.3.6}
54
+ s.summary = %q{Provides a simple way to add rating functionality to your application.}
55
+ s.test_files = [
56
+ "test/fixtures/car.rb",
57
+ "test/fixtures/rate.rb",
58
+ "test/fixtures/schema.rb",
59
+ "test/fixtures/user.rb",
60
+ "test/lib/model_test.rb",
61
+ "test/test_helper.rb"
62
+ ]
20
63
 
21
64
  if s.respond_to? :specification_version then
65
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
66
  s.specification_version = 3
23
67
 
24
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
68
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
69
  else
26
70
  end
27
71
  else
28
72
  end
29
73
  end
74
+
@@ -20,7 +20,6 @@ module AjaxfulRating # :nodoc:
20
20
  # * <tt>:show_user_rating</tt> Set to true if you want to display only the current user's rating, instead of the global average.
21
21
  # * <tt>:dimension</tt> The dimension to show the ratings for.
22
22
  # * <tt>:force_static</tt> Force static stars even when you're passing a user instance.
23
- # * <tt>:to_nearest</tt> Allow stars to be set and averaged to nearest whatever. eg. 0.5 for the nearest half-star
24
23
  #
25
24
  # Example:
26
25
  # <%= ratings_for @article, :wrap => false %> # => Will produce something like:
@@ -70,16 +69,17 @@ module AjaxfulRating # :nodoc:
70
69
  #
71
70
  # ajaxful_rating:
72
71
  # helper:
73
- # global_average: "Global rating average: %{value} out of %{max}"
74
- # user_rating: "Your rating: %{value} out of %{max}"
75
- # hover: "Rate %{value} out of %{max}" def ratings_for(*args)
72
+ # global_average: "Global rating average: {{value}} out of {{max}}"
73
+ # user_rating: "Your rating: {{value}} out of {{max}}"
74
+ # hover: "Rate {{value}} out of {{max}}" def ratings_for(*args)
76
75
  def ratings_for(*args)
77
76
  @axr_css ||= CSSBuilder.new
78
- options = args.extract_options!.symbolize_keys.slice(:small, :remote_options,
79
- :wrap, :show_user_rating, :dimension, :force_static, :current_user, :disable_remote, :to_nearest)
77
+ options = args.extract_options!.to_hash.symbolize_keys.slice(:small, :remote_options,
78
+ :wrap, :show_user_rating, :dimension, :force_static, :current_user)
79
+
80
80
  remote_options = options.delete(:remote_options) || {}
81
81
  rateable = args.shift
82
- user = args.shift || (respond_to?(:current_user) ? current_user : raise(NoUserSpecified))
82
+ user = args.shift || (respond_to?(:current_user) ? current_user : raise(Errors::NoUserSpecified))
83
83
  StarsBuilder.new(rateable, user, self, @axr_css, options, remote_options).render
84
84
  end
85
85
  end
@@ -4,16 +4,15 @@ module AjaxfulRating # :nodoc:
4
4
  #
5
5
  # ajaxful_rating:
6
6
  # helper:
7
- # global_average: "Global rating average: %{value} out of %{max}"
8
- # user_rating: "Your rating: %{value} out of %{max}"
9
- # hover: "Rate %{value} out of %{max}"
7
+ # global_average: "Global rating average: {{value}} out of {{max}}"
8
+ # user_rating: "Your rating: {{value}} out of {{max}}"
9
+ # hover: "Rate {{value}} out of {{max}}"
10
10
  module Locale
11
11
 
12
12
  DEFAULTS = {
13
- :user_rating => "Your rating: %{value} out of %{max}",
14
- :global_average => "Global rating average: %{value} out of %{max}",
15
- :hover => "Rate %{value} out of %{max}",
16
- :no_ratings => "Not yet rated"
13
+ :user_rating => "Your rating: {{value}} out of {{max}}",
14
+ :global_average => "Global rating average: {{value}} out of {{max}}",
15
+ :hover => "Rate {{value}} out of {{max}}"
17
16
  }
18
17
 
19
18
  def i18n(key, value = nil)
@@ -22,7 +21,6 @@ module AjaxfulRating # :nodoc:
22
21
  else
23
22
  key.to_sym
24
23
  end
25
- key = :no_ratings if key == :user_rating && options[:show_user_rating] && ((value || show_value) == 0)
26
24
  default = DEFAULTS[key]
27
25
  key = "ajaxful_rating.helper.#{key}"
28
26
  I18n.t(key, :value => (value || show_value),
@@ -12,18 +12,16 @@ module AjaxfulRating # :nodoc:
12
12
  # * <tt>:stars</tt> Max number of stars that can be submitted.
13
13
  # * <tt>:allow_update</tt> Set to true if you want users to be able to update their votes.
14
14
  # * <tt>:cache_column</tt> Name of the column for storing the cached rating average.
15
- # * <tt>:cache_column</tt> Name of the column for storing the cached rating average.
16
- # * <tt>:to_nearest</tt> Allow stars to be set and averaged to nearest whatever . eg. 0.5 for the nearest half-star. May be overridden in the ratings_for
17
15
  #
18
16
  # Example:
19
17
  # class Article < ActiveRecord::Base
20
18
  # ajaxful_rateable :stars => 10, :cache_column => :custom_column
21
19
  # end
22
20
  def ajaxful_rateable(options = {})
23
- has_many :rates_without_dimension, :as => :rateable, options.merge(:class_name => 'Rate'),
21
+ has_many :rates_without_dimension, :as => :rateable, :class_name => 'Rate',
24
22
  :dependent => :destroy, :conditions => {:dimension => nil}
25
23
  has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
26
-
24
+
27
25
  options[:dimensions].each do |dimension|
28
26
  has_many "#{dimension}_rates", :dependent => :destroy,
29
27
  :conditions => {:dimension => dimension.to_s}, :class_name => 'Rate', :as => :rateable
@@ -38,12 +36,12 @@ module AjaxfulRating # :nodoc:
38
36
  :cache_column => :rating_average
39
37
  }
40
38
  end
41
-
39
+
42
40
  alias_method :ajaxful_rating_options, :axr_config
43
41
  end
44
-
42
+
45
43
  axr_config.update(options)
46
-
44
+
47
45
  include AjaxfulRating::InstanceMethods
48
46
  extend AjaxfulRating::SingletonMethods
49
47
  end
@@ -56,7 +54,7 @@ module AjaxfulRating # :nodoc:
56
54
 
57
55
  # Instance methods for the rateable object.
58
56
  module InstanceMethods
59
-
57
+
60
58
  # Proxy for axr_config singleton method.
61
59
  def axr_config
62
60
  self.class.axr_config
@@ -73,7 +71,7 @@ module AjaxfulRating # :nodoc:
73
71
  # end
74
72
  def rate(stars, user, dimension = nil)
75
73
  return false if (stars.to_i > self.class.max_stars)
76
- raise AlreadyRatedError if (!self.class.axr_config[:allow_update] && rated_by?(user, dimension))
74
+ raise Errors::AlreadyRatedError if (!self.class.axr_config[:allow_update] && rated_by?(user, dimension))
77
75
 
78
76
  rate = if self.class.axr_config[:allow_update] && rated_by?(user, dimension)
79
77
  rate_by(user, dimension)
@@ -86,19 +84,21 @@ module AjaxfulRating # :nodoc:
86
84
  rate.save!
87
85
  self.update_cached_average(dimension)
88
86
  end
89
-
87
+
90
88
  # Builds the DOM id attribute for the wrapper in view.
91
89
  def wrapper_dom_id(options = {})
92
- options = options.symbolize_keys.slice(:small, :dimension)
93
- options = options.select { |k, v| v.present? or (v == false) }.map do |k, v|
90
+ options = options.to_hash.symbolize_keys.slice(:small, :dimension)
91
+ options = options.map do |k, v|
94
92
  if k == :dimension
95
93
  v.to_s
96
94
  else
97
95
  v.to_s == 'true' ? k.to_s : "no-#{k}"
98
96
  end
99
97
  end
100
- options.unshift("ajaxful_rating")
101
- ApplicationController.helpers.dom_id(self, options.sort.join('_'))
98
+ options = options.delete_if { |x| x.empty? }
99
+ prefix = "ajaxful_rating"
100
+ prefix << "_#{options.sort.join('_')}" unless options.empty?
101
+ ApplicationController.helpers.dom_id(self, prefix)
102
102
  end
103
103
 
104
104
  # Returns an array with the users that have rated this object for the
@@ -108,13 +108,13 @@ module AjaxfulRating # :nodoc:
108
108
  def raters(dimension = nil)
109
109
  sql = "SELECT DISTINCT u.* FROM #{self.class.user_class.table_name} u "\
110
110
  "INNER JOIN rates r ON u.id = r.rater_id WHERE "
111
-
111
+
112
112
  sql << self.class.send(:sanitize_sql_for_conditions, {
113
113
  :rateable_id => id,
114
114
  :rateable_type => self.class.base_class.name,
115
115
  :dimension => (dimension.to_s if dimension)
116
116
  }, 'r')
117
-
117
+
118
118
  self.class.user_class.find_by_sql(sql)
119
119
  end
120
120
 
@@ -127,7 +127,7 @@ module AjaxfulRating # :nodoc:
127
127
  def rated_by?(user, dimension = nil)
128
128
  !rate_by(user, dimension).nil?
129
129
  end
130
-
130
+
131
131
  # Returns whether or not the user can rate this object.
132
132
  # Based on if the user has already rated the object or the
133
133
  # :allow_update option is enabled.
@@ -148,20 +148,11 @@ module AjaxfulRating # :nodoc:
148
148
  # Rating average for the object.
149
149
  #
150
150
  # Pass false as param to force the calculation if you are caching it.
151
- def rate_average(cached = true, dimension = nil, to_nearest = nil)
152
- avg = if cached && self.class.caching_average?(dimension, to_nearest)
153
- send(caching_column_name(dimension, to_nearest)).to_f
151
+ def rate_average(cached = true, dimension = nil)
152
+ avg = if cached && self.class.caching_average?(dimension)
153
+ send(caching_column_name(dimension)).to_f
154
154
  else
155
- avg = self.rates_sum(dimension).to_f / self.total_rates(dimension).to_f
156
- avg.nan? ? 0.0 : avg
157
- if avg > 0
158
- to_nearest ||= axr_config[:to_nearest]
159
- if to_nearest
160
- notches = 1/to_nearest.to_f
161
- avg = ((avg * notches).round)/notches
162
- end
163
- end
164
- avg
155
+ self.rates_sum(dimension).to_f / self.total_rates(dimension).to_f
165
156
  end
166
157
  avg.nan? ? 0.0 : avg
167
158
  end
@@ -179,20 +170,20 @@ module AjaxfulRating # :nodoc:
179
170
  end
180
171
 
181
172
  # Returns the name of the cache column for the passed dimension.
182
- def caching_column_name(dimension = nil, to_nearest = nil)
183
- self.class.caching_column_name(dimension, to_nearest)
173
+ def caching_column_name(dimension = nil)
174
+ self.class.caching_column_name(dimension)
184
175
  end
185
176
 
186
177
  # Updates the cached average column in the rateable model.
187
- def update_cached_average(dimension = nil, to_nearest = nil)
188
- if self.class.caching_average?(dimension, to_nearest)
189
- update_attribute caching_column_name(dimension, to_nearest), self.rate_average(false, dimension, to_nearest)
178
+ def update_cached_average(dimension = nil)
179
+ if self.class.caching_average?(dimension)
180
+ update_attribute caching_column_name(dimension), self.rate_average(false, dimension)
190
181
  end
191
182
  end
192
183
  end
193
184
 
194
185
  module SingletonMethods
195
-
186
+
196
187
  # Maximum value accepted when rating the model. Default is 5.
197
188
  #
198
189
  # Change it by passing the :stars option to +ajaxful_rateable+
@@ -202,16 +193,11 @@ module AjaxfulRating # :nodoc:
202
193
  axr_config[:stars]
203
194
  end
204
195
 
205
- # Allow stars to be set and averaged to nearest whatever. eg. 0.5 for the nearest half-star
206
- def to_nearest
207
- axr_config[:to_nearest] || 1
208
- end
209
-
210
196
  # Name of the class for the user model.
211
197
  def user_class_name
212
198
  Rate.reflect_on_association(:rater).options[:class_name]
213
199
  end
214
-
200
+
215
201
  # Gets the user's class
216
202
  def user_class
217
203
  user_class_name.constantize
@@ -241,13 +227,13 @@ module AjaxfulRating # :nodoc:
241
227
  def find_statement(attr_name, attr_value, dimension = nil)
242
228
  sql = "SELECT DISTINCT r2.* FROM rates r1 INNER JOIN "\
243
229
  "#{self.base_class.table_name} r2 ON r1.rateable_id = r2.id WHERE "
244
-
230
+
245
231
  sql << sanitize_sql_for_conditions({
246
232
  :rateable_type => self.base_class.name,
247
233
  attr_name => attr_value,
248
234
  :dimension => (dimension.to_s if dimension)
249
235
  }, 'r1')
250
-
236
+
251
237
  find_by_sql(sql)
252
238
  end
253
239
 
@@ -262,15 +248,14 @@ module AjaxfulRating # :nodoc:
262
248
  #
263
249
  # ajaxful_rateable :cache_column => :my_custom_column
264
250
  #
265
- def caching_average?(dimension = nil, to_nearest = nil)
266
- column_names.include?(caching_column_name(dimension, to_nearest))
251
+ def caching_average?(dimension = nil)
252
+ column_names.include?(caching_column_name(dimension))
267
253
  end
268
254
 
269
255
  # Returns the name of the cache column for the passed dimension.
270
- def caching_column_name(dimension = nil, to_nearest = nil)
256
+ def caching_column_name(dimension = nil)
271
257
  name = axr_config[:cache_column].to_s
272
258
  name += "_#{dimension.to_s.underscore}" unless dimension.blank?
273
- name += "_#{to_nearest.to_s.gsub('.', '_').underscore}" unless to_nearest.blank?
274
259
  name
275
260
  end
276
261
  end
@@ -30,7 +30,6 @@ module AjaxfulRating # :nodoc:
30
30
  :wrap => true,
31
31
  :small => false,
32
32
  :show_user_rating => false,
33
- :disable_remote => false,
34
33
  :force_static => false,
35
34
  :current_user => (@template.current_user if @template.respond_to?(:current_user))
36
35
  }.merge(options)
@@ -38,7 +37,7 @@ module AjaxfulRating # :nodoc:
38
37
  @options[:small] = @options[:small].to_s == 'true'
39
38
  @options[:show_user_rating] = @options[:show_user_rating].to_s == 'true'
40
39
  @options[:wrap] = @options[:wrap].to_s == 'true'
41
-
40
+
42
41
  @remote_options = {
43
42
  :url => nil,
44
43
  :method => :post
@@ -50,7 +49,7 @@ module AjaxfulRating # :nodoc:
50
49
  if @template.respond_to?(url)
51
50
  @remote_options[:url] = @template.send(url, rateable)
52
51
  else
53
- raise(MissingRateRoute)
52
+ raise(Errors::MissingRateRoute)
54
53
  end
55
54
  end
56
55
  end
@@ -65,31 +64,29 @@ module AjaxfulRating # :nodoc:
65
64
 
66
65
  stars << @template.content_tag(:li, i18n(:current), :class => "show-value",
67
66
  :style => "width: #{width}%")
68
- stars += (1..rateable.class.max_stars/rateable.class.to_nearest).map do |i|
69
- star_tag(i*rateable.class.to_nearest)
67
+ stars += (1..rateable.class.max_stars).map do |i|
68
+ star_tag(i)
70
69
  end
71
- # When using rails_xss plugin, it needs to render as HTML
72
- stars = "".respond_to?(:html_safe) ? stars.join.html_safe : stars.join
73
- @template.content_tag(:ul, stars, :class => "ajaxful-rating#{' small' if options[:small]}")
70
+ @template.content_tag(:ul, stars.join.html_safe, :class => "ajaxful-rating#{' small' if options[:small]}")
74
71
  end
75
72
 
76
73
  def star_tag(value)
77
74
  already_rated = rateable.rated_by?(user, options[:dimension]) if user
78
- css_class = "stars-%s" % [ value.to_s.gsub('.', '_') ]
75
+ css_class = "stars-#{value}"
79
76
  @css_builder.rule(".ajaxful-rating .#{css_class}", {
80
77
  :width => "#{(value / rateable.class.max_stars.to_f) * 100}%",
81
- :zIndex => "%.f" % ( (rateable.class.max_stars/rateable.class.to_nearest) + 2 - (value/rateable.class.to_nearest))
78
+ :zIndex => (rateable.class.max_stars + 2 - value).to_s
82
79
  })
80
+
83
81
  @template.content_tag(:li) do
84
- if !options[:force_static] && (user && options[:current_user] == user &&
85
- (!already_rated || rateable.axr_config[:allow_update]))
82
+ if !options[:force_static] && (user && options[:current_user] == user && (!already_rated || rateable.axr_config[:allow_update]))
86
83
  link_star_tag(value, css_class)
87
84
  else
88
85
  @template.content_tag(:span, show_value, :class => css_class, :title => i18n(:current))
89
86
  end
90
87
  end
91
88
  end
92
-
89
+
93
90
  def link_star_tag(value, css_class)
94
91
  query = {
95
92
  :stars => value,
@@ -97,24 +94,21 @@ module AjaxfulRating # :nodoc:
97
94
  :small => options[:small],
98
95
  :show_user_rating => options[:show_user_rating]
99
96
  }.to_query
100
- config = {
101
- :html => {
102
- :class => css_class,
103
- :title => i18n(:hover, value)
104
- },
105
- :url => "#{remote_options[:url]}",
106
- :with => "'#{query}'"
97
+
98
+ options = {
99
+ :class => css_class,
100
+ :title => i18n(:hover, value),
101
+ :method => remote_options[:method] || :post,
102
+ :remote => true
107
103
  }
108
- if options[:disable_remote]
109
- config[:url] += "?" + query
110
- @template.link_to(value, config[:url], config[:html])
111
- else
112
- @template.link_to_remote(value, remote_options.merge(config))
113
- end
104
+
105
+ href = "#{remote_options[:url]}?#{query}"
106
+
107
+ @template.link_to(value, href, options)
114
108
  end
115
-
109
+
116
110
  def wrapper_tag
117
- @template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper#{' show-user-rating' if @options[:show_user_rating]}",
111
+ @template.content_tag(:div, ratings_tag, :class => "ajaxful-rating-wrapper",
118
112
  :id => rateable.wrapper_dom_id(options))
119
113
  end
120
114
  end
@@ -2,4 +2,4 @@ Description:
2
2
  Generates a model called Rate linked to a user model named as the passed parameter.
3
3
 
4
4
  Example:
5
- ./script/generate ajaxful_rating User
5
+ rails generate ajaxful_rating User
@@ -0,0 +1,38 @@
1
+ require 'rails/generators/migration'
2
+ class AjaxfulRatingGenerator < Rails::Generators::NamedBase
3
+ include Rails::Generators::Migration
4
+
5
+ def self.source_root
6
+ @_axr_root ||= File.expand_path("../templates", __FILE__)
7
+ end
8
+
9
+ def create_model_files
10
+ model_file = File.join('app/models', "#{file_path}.rb")
11
+ raise "User model (#{model_file}) must exits." unless File.exists?(model_file)
12
+ class_collisions 'Rate'
13
+ template 'model.rb', File.join('app/models', class_path, "rate.rb")
14
+ end
15
+
16
+ def create_migration
17
+ migration_template 'migration.rb', "db/migrate/create_rates.rb"
18
+ end
19
+
20
+ def create_layout
21
+ copy_file 'images/star.png', 'public/images/ajaxful_rating/star.png'
22
+ copy_file 'images/star_small.png', 'public/images/ajaxful_rating/star_small.png'
23
+ copy_file 'style.css', 'public/stylesheets/ajaxful_rating.css'
24
+ end
25
+
26
+ private
27
+
28
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
29
+ # Implement the required interface for Rails::Generators::Migration.
30
+ def self.next_migration_number(dirname) #:nodoc:
31
+ if ActiveRecord::Base.timestamped_migrations
32
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
33
+ else
34
+ "%.3d" % (current_migration_number(dirname) + 1)
35
+ end
36
+ end
37
+
38
+ end
@@ -3,7 +3,7 @@ class CreateRates < ActiveRecord::Migration
3
3
  create_table :rates do |t|
4
4
  t.belongs_to :rater
5
5
  t.belongs_to :rateable, :polymorphic => true
6
- t.float :stars, :null => false
6
+ t.integer :stars, :null => false
7
7
  t.string :dimension
8
8
  t.timestamps
9
9
  end
@@ -0,0 +1,3 @@
1
+ class Car < ActiveRecord::Base
2
+ ajaxful_rateable :stars => 10, :dimensions => [:speed, :reliability, :price]
3
+ end
@@ -0,0 +1,7 @@
1
+ audi:
2
+ id: 1
3
+ name: Audi
4
+
5
+ porsche:
6
+ id: 2
7
+ name: Infinity
@@ -0,0 +1,6 @@
1
+ class Rate < ActiveRecord::Base
2
+ belongs_to :rater, :class_name => "User"
3
+ belongs_to :rateable, :polymorphic => true
4
+
5
+ attr_accessible :rate, :dimension
6
+ end
@@ -0,0 +1,41 @@
1
+ rate_audi_denis:
2
+ rater_id: 1 # Denis
3
+ rateable_id: 1 # Audi
4
+ rateable_type: Car
5
+ stars: 6
6
+ dimension:
7
+
8
+ rate_audi_denis_speed:
9
+ rater_id: 1 # Denis
10
+ rateable_id: 1 # Audi
11
+ rateable_type: Car
12
+ stars: 7
13
+ dimension: speed
14
+
15
+ rate_audi_denis_reliability:
16
+ rater_id: 1 # Denis
17
+ rateable_id: 1 # Audi
18
+ rateable_type: Car
19
+ stars: 5
20
+ dimension: reliability
21
+
22
+ rate_audi_john:
23
+ rater_id: 2 # John
24
+ rateable_id: 1 # Audi
25
+ rateable_type: Car
26
+ stars: 8
27
+ dimension:
28
+
29
+ rate_audi_john_speed:
30
+ rater_id: 2 # John
31
+ rateable_id: 1 # Audi
32
+ rateable_type: Car
33
+ stars: 8
34
+ dimension: speed
35
+
36
+ rate_audi_john_reliability:
37
+ rater_id: 2 # John
38
+ rateable_id: 2 # Audi
39
+ rateable_type: Car
40
+ stars: 5
41
+ dimension: reliability
@@ -0,0 +1,22 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table "cars", :force => true do |t|
3
+ t.string "name"
4
+ t.datetime "created_at"
5
+ t.datetime "updated_at"
6
+ end
7
+
8
+ create_table "rates", :force => true do |t|
9
+ t.integer "rater_id"
10
+ t.integer "rateable_id"
11
+ t.string "rateable_type"
12
+ t.integer "stars", :null => false
13
+ t.string "dimension"
14
+ end
15
+
16
+ add_index "rates", ["rateable_id", "rateable_type"], :name => "index_rates_on_rateable_id_and_rateable_type"
17
+ add_index "rates", ["rater_id"], :name => "index_rates_on_rater_id"
18
+
19
+ create_table "users", :force => true do |t|
20
+ t.string "name"
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ ajaxful_rater
3
+ end
@@ -0,0 +1,7 @@
1
+ denis:
2
+ id: 1
3
+ name: Denis Odorcic
4
+
5
+ john:
6
+ id: 2
7
+ name: John Doe
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class ModelTest < ActiveSupport::TestCase
4
+ include ActiveRecord::TestFixtures
5
+
6
+ def setup
7
+ @audi = Car.find_by_name("Audi")
8
+ @infinity = Car.find_by_name("Infinity")
9
+
10
+ @denis = User.find_by_name("Denis Odorcic")
11
+ end
12
+
13
+ def test_find_statement
14
+ assert_equal Car.find_statement(:stars, 7).size, 0
15
+ assert_equal Car.find_statement(:stars, 8).size, 1
16
+ assert_equal Car.find_statement(:stars, 8, :speed).size, 1
17
+ assert_equal Car.find_statement(:stars, 5, :reliability).size, 2
18
+ end
19
+
20
+ def test_rate_higher_than_max_stars
21
+ assert_equal Car.max_stars, 10
22
+ assert !@audi.rate(15, User.first)
23
+ end
24
+
25
+ def test_already_rated_error
26
+ Car.axr_config[:allow_update] = false
27
+ assert @audi.rated_by?(@denis)
28
+ assert_raise AjaxfulRating::Errors::AlreadyRatedError do
29
+ @audi.rate(4, @denis)
30
+ end
31
+ end
32
+
33
+ def test_already_rated_and_allowed_to_update
34
+ assert @audi.rated_by?(@denis)
35
+ stars = @audi.rate_by(@denis).stars
36
+
37
+ assert_no_difference 'Rate.count' do
38
+ @audi.rate(1, @denis)
39
+ end
40
+ assert_equal @audi.rate_by(@denis).stars, 1
41
+ assert_not_equal @audi.rate_by(@denis).stars, stars
42
+ end
43
+
44
+ def test_new_rating
45
+ assert_difference 'Rate.count', 1 do
46
+ @audi.rate(5, @denis, :price)
47
+ end
48
+ end
49
+
50
+ def test_raters
51
+ assert_equal @audi.raters.size, 2
52
+ assert_difference 'Rate.count', 1 do
53
+ @audi.rate(3, User.create(:name => "Bob"))
54
+ end
55
+ assert_equal @audi.raters.size, 3
56
+ end
57
+
58
+ end
@@ -0,0 +1,25 @@
1
+ $:.unshift File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'active_record'
6
+ require 'active_record/fixtures'
7
+ require 'action_controller'
8
+ require 'ajaxful_rating'
9
+
10
+ AXR_FIXTURES_PATH = File.join(File.dirname(__FILE__), 'fixtures')
11
+
12
+ # For transactional fixtures to work in tests, configurations in AR::Base has to be set to something
13
+ ActiveRecord::Base.configurations = {:epic => 'fail'}
14
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
15
+
16
+ # Add fixtures to load path
17
+ dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
18
+ dep.load_paths.unshift AXR_FIXTURES_PATH
19
+
20
+ ActiveRecord::Base.silence do
21
+ ActiveRecord::Migration.verbose = false
22
+ load File.join(AXR_FIXTURES_PATH, 'schema.rb')
23
+ end
24
+
25
+ Fixtures.create_fixtures(AXR_FIXTURES_PATH, ActiveRecord::Base.connection.tables)
metadata CHANGED
@@ -1,54 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ajaxful_rating
3
3
  version: !ruby/object:Gem::Version
4
- hash: 89
5
- prerelease:
4
+ prerelease: true
6
5
  segments:
7
- - 2
8
- - 2
9
- - 9
10
- - 1
11
- version: 2.2.9.1
6
+ - 3
7
+ - 0
8
+ - 0
9
+ - beta1
10
+ version: 3.0.0.beta1
12
11
  platform: ruby
13
12
  authors:
14
13
  - Edgar J. Suarez
14
+ - Denis Odorcic
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-06-29 00:00:00 Z
19
+ date: 2010-05-03 00:00:00 -05:00
20
+ default_executable:
20
21
  dependencies: []
21
22
 
22
23
  description: Provides a simple way to add rating functionality to your application.
23
- email: edgar.js@gmail.com
24
+ email:
25
+ - edgar.js@gmail.com
26
+ - denis.odorcic@gmail.com
24
27
  executables: []
25
28
 
26
29
  extensions: []
27
30
 
28
31
  extra_rdoc_files:
29
- - CHANGELOG
32
+ - ChangeLog
30
33
  - README.textile
31
- - lib/ajaxful_rating.rb
32
- - lib/axr/css_builder.rb
33
- - lib/axr/errors.rb
34
- - lib/axr/helpers.rb
35
- - lib/axr/locale.rb
36
- - lib/axr/model.rb
37
- - lib/axr/stars_builder.rb
38
34
  files:
35
+ - .gitignore
39
36
  - CHANGELOG
40
37
  - Manifest
41
38
  - README.textile
42
39
  - Rakefile
43
40
  - ajaxful_rating.gemspec
44
- - generators/ajaxful_rating/USAGE
45
- - generators/ajaxful_rating/ajaxful_rating_generator.rb
46
- - generators/ajaxful_rating/templates/images/star.png
47
- - generators/ajaxful_rating/templates/images/star_small.png
48
- - generators/ajaxful_rating/templates/migration.rb
49
- - generators/ajaxful_rating/templates/model.rb
50
- - generators/ajaxful_rating/templates/style.css
51
- - init.rb
52
41
  - lib/ajaxful_rating.rb
53
42
  - lib/axr/css_builder.rb
54
43
  - lib/axr/errors.rb
@@ -56,44 +45,59 @@ files:
56
45
  - lib/axr/locale.rb
57
46
  - lib/axr/model.rb
58
47
  - lib/axr/stars_builder.rb
48
+ - lib/generators/ajaxful_rating/USAGE
49
+ - lib/generators/ajaxful_rating/ajaxful_rating_generator.rb
50
+ - lib/generators/ajaxful_rating/templates/images/star.png
51
+ - lib/generators/ajaxful_rating/templates/images/star_small.png
52
+ - lib/generators/ajaxful_rating/templates/migration.rb
53
+ - lib/generators/ajaxful_rating/templates/model.rb
54
+ - lib/generators/ajaxful_rating/templates/style.css
55
+ - test/fixtures/car.rb
56
+ - test/fixtures/cars.yml
57
+ - test/fixtures/rate.rb
58
+ - test/fixtures/rates.yml
59
+ - test/fixtures/schema.rb
60
+ - test/fixtures/user.rb
61
+ - test/fixtures/users.yml
62
+ - test/lib/model_test.rb
63
+ - test/test_helper.rb
64
+ - ChangeLog
65
+ has_rdoc: true
59
66
  homepage: http://github.com/edgarjs/ajaxful-rating
60
67
  licenses: []
61
68
 
62
69
  post_install_message:
63
70
  rdoc_options:
64
- - --line-numbers
65
- - --inline-source
66
- - --title
67
- - Ajaxful_rating
68
- - --main
69
- - README.textile
71
+ - --charset=UTF-8
70
72
  require_paths:
71
73
  - lib
72
74
  required_ruby_version: !ruby/object:Gem::Requirement
73
- none: false
74
75
  requirements:
75
76
  - - ">="
76
77
  - !ruby/object:Gem::Version
77
- hash: 3
78
78
  segments:
79
79
  - 0
80
80
  version: "0"
81
81
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
82
  requirements:
84
- - - ">="
83
+ - - ">"
85
84
  - !ruby/object:Gem::Version
86
- hash: 11
87
85
  segments:
88
86
  - 1
89
- - 2
90
- version: "1.2"
87
+ - 3
88
+ - 1
89
+ version: 1.3.1
91
90
  requirements: []
92
91
 
93
- rubyforge_project: ajaxful_rating
94
- rubygems_version: 1.8.21
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.6
95
94
  signing_key:
96
95
  specification_version: 3
97
96
  summary: Provides a simple way to add rating functionality to your application.
98
- test_files: []
99
-
97
+ test_files:
98
+ - test/fixtures/car.rb
99
+ - test/fixtures/rate.rb
100
+ - test/fixtures/schema.rb
101
+ - test/fixtures/user.rb
102
+ - test/lib/model_test.rb
103
+ - test/test_helper.rb
@@ -1,30 +0,0 @@
1
- class AjaxfulRatingGenerator < Rails::Generator::NamedBase
2
- def initialize(runtime_args, runtime_options = {})
3
- super
4
-
5
- # if there's no user model
6
- model_file = File.join('app/models', "#{file_path}.rb")
7
- raise "User model (#{model_file}) must exits." unless File.exists?(model_file)
8
- end
9
-
10
- def manifest
11
- record do |m|
12
- m.class_collisions 'Rate'
13
- m.template 'model.rb', File.join('app/models', 'rate.rb')
14
- m.migration_template 'migration.rb', 'db/migrate',
15
- :migration_file_name => 'create_rates'
16
-
17
- # style
18
- m.directory 'public/images/ajaxful_rating'
19
- m.file 'images/star.png', 'public/images/ajaxful_rating/star.png'
20
- m.file 'images/star_small.png', 'public/images/ajaxful_rating/star_small.png'
21
- m.file 'style.css', 'public/stylesheets/ajaxful_rating.css'
22
- end
23
- end
24
-
25
- protected
26
-
27
- def banner
28
- "Usage: #{$0} ajaxful_rating UserModelName"
29
- end
30
- end
data/init.rb DELETED
@@ -1,6 +0,0 @@
1
- puts "------------------------------------------"
2
- puts "IMPORTANT: AjaxfulRating has been updated to v2.2.x and some options changed."\
3
- "Please read the changelog at http://github.com/edgarjs/ajaxful-rating/blob/master/CHANGELOG"
4
- puts "------------------------------------------"
5
-
6
- require 'ajaxful_rating'