seems_rateable 1.0.13 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +20 -0
  3. data/Gemfile +3 -0
  4. data/README.md +41 -61
  5. data/Rakefile +8 -2
  6. data/app/assets/javascripts/seems_rateable/index.js +1 -0
  7. data/{lib/generators/seems_rateable/install/templates → app/assets/javascripts/seems_rateable}/jRating.js.erb +225 -225
  8. data/app/assets/stylesheets/seems_rateable/{application.css → index.css.erb} +2 -2
  9. data/app/controllers/seems_rateable/application_controller.rb +1 -3
  10. data/app/controllers/seems_rateable/rates_controller.rb +31 -0
  11. data/app/models/seems_rateable/rate.rb +2 -2
  12. data/bin/rails +8 -0
  13. data/config/routes.rb +1 -1
  14. data/lib/generators/seems_rateable/install/install_generator.rb +34 -28
  15. data/lib/generators/seems_rateable/install/templates/initializer.rb +5 -2
  16. data/lib/generators/seems_rateable/install/templates/rateable.js.erb +12 -22
  17. data/lib/generators/seems_rateable/install/templates/rates_migration.rb +6 -9
  18. data/lib/generators/seems_rateable/manifest_finder.rb +25 -0
  19. data/lib/generators/seems_rateable/migration_helpers.rb +21 -0
  20. data/lib/generators/seems_rateable/uninstall/templates/drop_seems_rateable_rates_table.rb +9 -0
  21. data/lib/generators/seems_rateable/uninstall/uninstall_generator.rb +50 -0
  22. data/lib/generators/seems_rateable/uninstall_old/templates/drop_seems_rateable_cached_ratings_table.rb +9 -0
  23. data/lib/generators/seems_rateable/uninstall_old/templates/drop_seems_rateable_rates_table.rb +9 -0
  24. data/lib/generators/seems_rateable/uninstall_old/uninstall_old_generator.rb +48 -0
  25. data/lib/seems_rateable.rb +22 -7
  26. data/lib/seems_rateable/builder.rb +35 -0
  27. data/lib/seems_rateable/builder/html_options.rb +37 -0
  28. data/lib/seems_rateable/configuration.rb +23 -0
  29. data/lib/seems_rateable/engine.rb +12 -7
  30. data/lib/seems_rateable/errors.rb +3 -17
  31. data/lib/seems_rateable/helpers/action_view_extension.rb +18 -0
  32. data/lib/seems_rateable/helpers/current_rater.rb +15 -0
  33. data/lib/seems_rateable/models/active_record_extension.rb +23 -0
  34. data/lib/seems_rateable/models/active_record_extension/rateable.rb +26 -0
  35. data/lib/seems_rateable/models/active_record_extension/rater.rb +11 -0
  36. data/lib/seems_rateable/rating.rb +19 -0
  37. data/lib/seems_rateable/routes.rb +1 -1
  38. data/lib/seems_rateable/version.rb +1 -1
  39. data/seems_rateable.gemspec +32 -0
  40. data/spec/builder_spec.rb +53 -0
  41. data/spec/controllers/rates_controller_spec.rb +56 -0
  42. data/spec/dummy/README.rdoc +28 -0
  43. data/spec/dummy/Rakefile +6 -0
  44. data/{app/assets/javascripts/seems_rateable → spec/dummy/app/assets/javascripts}/application.js +4 -3
  45. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  46. data/spec/dummy/app/assets/javascripts/rateable/rateable.js.erb +15 -0
  47. data/spec/dummy/app/assets/stylesheets/application.css +19 -0
  48. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  49. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  50. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  51. data/spec/dummy/app/controllers/posts_controller.rb +58 -0
  52. data/spec/dummy/app/models/.keep +0 -0
  53. data/spec/dummy/app/models/concerns/.keep +0 -0
  54. data/spec/dummy/app/models/post.rb +3 -0
  55. data/spec/dummy/app/models/user.rb +5 -0
  56. data/spec/dummy/app/views/layouts/application.html.erb +26 -0
  57. data/spec/dummy/app/views/posts/_form.html.erb +21 -0
  58. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  59. data/spec/dummy/app/views/posts/index.html.erb +28 -0
  60. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  61. data/spec/dummy/app/views/posts/show.html.erb +9 -0
  62. data/spec/dummy/bin/bundle +3 -0
  63. data/spec/dummy/bin/rails +4 -0
  64. data/spec/dummy/bin/rake +4 -0
  65. data/spec/dummy/config.ru +4 -0
  66. data/spec/dummy/config/application.rb +42 -0
  67. data/spec/dummy/config/boot.rb +5 -0
  68. data/spec/dummy/config/database.yml +25 -0
  69. data/spec/dummy/config/environment.rb +5 -0
  70. data/spec/dummy/config/environments/development.rb +29 -0
  71. data/spec/dummy/config/environments/production.rb +80 -0
  72. data/spec/dummy/config/environments/test.rb +36 -0
  73. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  74. data/spec/dummy/config/initializers/devise.rb +254 -0
  75. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  76. data/spec/dummy/config/initializers/inflections.rb +16 -0
  77. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  78. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  79. data/spec/dummy/config/initializers/seems_rateable.rb +4 -0
  80. data/spec/dummy/config/initializers/session_store.rb +3 -0
  81. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  82. data/spec/dummy/config/locales/devise.en.yml +59 -0
  83. data/spec/dummy/config/locales/en.yml +23 -0
  84. data/spec/dummy/config/routes.rb +8 -0
  85. data/spec/dummy/db/migrate/20140506144141_create_posts.rb +9 -0
  86. data/spec/dummy/db/migrate/20140506144841_devise_create_users.rb +42 -0
  87. data/spec/dummy/db/migrate/20140520170316_create_seems_rateable_rates.rb +15 -0
  88. data/spec/dummy/db/schema.rb +54 -0
  89. data/spec/dummy/lib/assets/.keep +0 -0
  90. data/spec/dummy/public/404.html +58 -0
  91. data/spec/dummy/public/422.html +58 -0
  92. data/spec/dummy/public/500.html +57 -0
  93. data/spec/dummy/public/favicon.ico +0 -0
  94. data/spec/factories/posts.rb +5 -0
  95. data/spec/factories/rate.rb +7 -0
  96. data/spec/factories/users.rb +7 -0
  97. data/spec/features/rating_spec.rb +63 -0
  98. data/spec/helpers/action_view_extension_spec.rb +59 -0
  99. data/spec/models/active_record_extension_spec.rb +105 -0
  100. data/spec/models/rate_spec.rb +6 -0
  101. data/spec/rating_spec.rb +70 -0
  102. data/spec/spec_helper.rb +45 -0
  103. data/spec/support/dummies.rb +19 -0
  104. metadata +263 -25
  105. data/app/controllers/seems_rateable/ratings_controller.rb +0 -13
  106. data/app/models/seems_rateable/cached_rating.rb +0 -5
  107. data/lib/generators/seems_rateable/install/templates/cached_ratings_migration.rb +0 -17
  108. data/lib/seems_rateable/helpers.rb +0 -27
  109. data/lib/seems_rateable/model.rb +0 -91
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5da25869fc6cb9fd1c221b6f6e4a91863e05b199
4
- data.tar.gz: e6a95e44392d188f812290df55b638441b4b094e
3
+ metadata.gz: b5c6c41b54dc14c601e4f35661dcba8c29cc944d
4
+ data.tar.gz: dba8a7b572f328759c514d5a240230d4d8d0de61
5
5
  SHA512:
6
- metadata.gz: a77a2cf678894a71dc0c5f0fa985be61d5f4d506d7fec15afec417a1409cf1fc1de4b31dce4e96ab161f84ef1d34a69b12e642af2fbbd907eca542af72ec5930
7
- data.tar.gz: bd098828cb0a4900c887f9fbe5c6f79c4939501f87b3ce1ee35e3ec3af12ef57c32c136bb4a760749f7f05cc8678c2082f1bab0cdc2c14ce43304e5d2bf4239a
6
+ metadata.gz: 324447036712396cd2c77ad6d84f543c647b8ffee420aa68c1eee20fee34ca16f130aa514e6d06d8da6d82cdb7693954306750fc0452f7038efd60a84b8bec59
7
+ data.tar.gz: ad5e2d9e980a32d119018373ceaef79416186539691251c296e986419246602c6de405b8e655d46831e1b492b78487d2dcc555f8d1ee2415237b1eaa06960a0d
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ tmp
15
+ .project
16
+ .rvmrc
17
+ .rspec
18
+ spec/dummy/log/*
19
+ spec/dummy/db/*.sqlite3
20
+ spec/dummy/tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md CHANGED
@@ -1,14 +1,20 @@
1
1
  # SeemsRateable
2
2
 
3
- Star rating gem for Rails application using jQuery plugin <a href="http://www.myjqueryplugins.com/jquery-plugin/jrating">jRating</a>
3
+ Star rating gem for Rails application using jQuery plugin [jRating](http://www.myjqueryplugins.com/jquery-plugin/jrating)
4
4
 
5
5
  ## Demo
6
6
 
7
- <a href="http://rateable.herokuapp.com/">Demo</a> application, requires to sign up before rating
7
+ [Demo](http://seemsrateable.herokuapp.com/) application, requires to sign up before rating.
8
8
 
9
- ## Instructions
9
+ ### Important! If you are upgrading from 1.x.x please do accordingly :
10
10
 
11
- ### Installation
11
+ Run :
12
+
13
+ $ rails g seems_rateable:uninstall_old
14
+
15
+ Then follow as instructed.
16
+
17
+ ## Installation
12
18
 
13
19
  Add this line to your application's Gemfile:
14
20
 
@@ -24,83 +30,57 @@ Or install it yourself as:
24
30
 
25
31
  ### Generation
26
32
 
33
+ Generate required files :
34
+
27
35
  $ rails generate seems_rateable:install
28
36
 
29
- Generator creates migration files, javascript files and initializer
30
-
31
- ### Prepare
37
+ And run :
32
38
 
33
- Require javascript files by adding this line to application.js
39
+ $ rake db:migrate
34
40
 
35
- #application.js
36
- //= require_directory ./rateable
41
+ ## Prepare
37
42
 
38
- Add <code>seems_rateable</code> to routes.rb file
43
+ To prepare model add `seems_rateable` to your rateable model file. You can also pass a hash of dimensions
44
+ e.g `:quality, :price`
39
45
 
40
- Include stylesheet adding <code><%= seems_rateable_stylesheet %></code> to your layout header
41
-
42
- Also make sure you have an existing <code>current_user</code> helper method
43
-
44
- Don't forget to run
46
+ Post < ActiveRecord::Base
47
+ seems_rateable # :quality, :speed, :effectiveness
48
+ end
45
49
 
46
- $ rake db:migrate
50
+
51
+ To track user's given rates add `seems_rateable_rater` to your rater model.
47
52
 
48
- To prepare model add <code> seems_rateable </code> to your rateable model file. You can also pass a hash of options to
49
- customize the functionality
53
+ User < ActiveRecord::Base
54
+ seems_rateable_rater
55
+ end
50
56
 
51
- <ul>
52
- <li><code>:dimensions</code>Array of dimensions e.g <code>:dimensions => [:quality, :quantity]</code> </li>
53
- <li><code>:allow_update</code>Allowing user to re-rate his own ratings, default set to false e.g <code>:allow_update=> true</code></li>
54
- </ul>
57
+ ## Usage
55
58
 
56
- class Post < ActiveRecord::Base
57
- seems_rateable :allow_update => true, :dimensions => [:quality, :length]
58
- end
59
+ To display star rating use helper method `rating_for` in your view
59
60
 
60
- To access object's rates use <code>rates</code> method, to get dimension rates pass an argument eg :
61
+ #show.html.erb
61
62
 
62
- @object.rates
63
- @object.rates(:quality)
64
- @object.rates(:quantity)
65
-
66
- This also applies to cached average rating e.g
63
+ rating_for @post
67
64
 
68
- @object.average
69
- @object.average(:quality)
70
- @object.average(:quantity)
71
-
72
- And to object's raters e.g
73
-
74
- @object.raters
75
- @object.raters(:quality)
76
- @object.raters(:quantity)
77
-
78
- To track user's given ratings add <code>seems_rateable_rater</code> to your rater model.
79
- If your rater class is not "User"(e.g "Client" or "Customer") change configuration in initializer generated by this engine.
80
- Now you can access user's ratings by <code>@user.ratings_given</code>
65
+ rating_for @post, dimension: :quality, disabled: true, html_options
81
66
 
82
- ### Usage
67
+ You can specify these options :
83
68
 
84
- To display star rating use helper method <code>rating_for</code> in your view
69
+ * `dimension` The dimension of the rating object
70
+ * `disabled` Set to true to display static star rating meaning *no* user would be allowed to rate
71
+ * `html_options` HTML options of the selector
85
72
 
86
- #index.html.erb
73
+ To edit the javascript options locate rateable.js file in `app/assets/javascripts/rateable/`.
74
+ The javascript options are explained directly in the file.
87
75
 
88
- rating_for @post
76
+ ## Configuration
89
77
 
90
- rating_for @post, :dimension => :quality, :class => 'post', :id => 'list'
78
+ In *config/initializers/seems_rateable.rb* you can configure these options :
91
79
 
92
- rating_for @post, :static => true
80
+ * **rate\_owner_class** - Usually `User`, name of the model you put `seems_rateable_rater`
81
+ * **current\_rater_method** - The method representing a rater instance, `current_user` by default
82
+ * **default\_selector_class** - Default HTML class of the rating div
93
83
 
94
- You can specify these options :
95
- <ul>
96
- <li><code>:dimension</code>The dimension of the object</li>
97
- <li><code>:static</code>Set to true to display static star rating, default false</li>
98
- <li><code>:class</code>Class of the div, default set to 'rateable'</li>
99
- <li><code>:id</code>ID of the div e.g <code>:id => "info"</code>, default nil</li>
100
- </ul>
101
-
102
- To edit the javascript options locate rateable.js file in /app/assets/javascripts/rateable/.
103
- The javascript options are explained directly in the file
104
84
 
105
85
  ## Contributing
106
86
 
data/Rakefile CHANGED
@@ -1,11 +1,13 @@
1
+ require 'rspec/core'
2
+ require 'rspec/core/rake_task'
3
+ require 'rdoc/task'
4
+
1
5
  begin
2
6
  require 'bundler/setup'
3
7
  rescue LoadError
4
8
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
9
  end
6
10
 
7
- require 'rdoc/task'
8
-
9
11
  RDoc::Task.new(:rdoc) do |rdoc|
10
12
  rdoc.rdoc_dir = 'rdoc'
11
13
  rdoc.title = 'SeemsRateable'
@@ -15,3 +17,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
15
17
  end
16
18
 
17
19
  Bundler::GemHelper.install_tasks
20
+
21
+ RSpec::Core::RakeTask.new(:spec)
22
+
23
+ task default: :spec
@@ -0,0 +1 @@
1
+ //= require_tree
@@ -1,225 +1,225 @@
1
- /************************************************************************
2
- *************************************************************************
3
- @Name : jRating - jQuery Plugin
4
- @Revison : 3.0
5
- @Date : 28/01/2013
6
- @Author: ALPIXEL - (www.myjqueryplugins.com - www.alpixel.fr)
7
- @License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
8
-
9
- **************************************************************************
10
- *************************************************************************/
11
- (function($) {
12
- $.fn.jRating = function(op) {
13
- var defaults = {
14
- /** String vars **/
15
- bigStarsPath : '<%= asset_path "seems_rateable/stars.png" %>', // path of the icon stars.png
16
- smallStarsPath : '<%= asset_path "seems_rateable/small.png" %>', // path of the icon small.png
17
- path : '<%= SeemsRateable::Engine.routes.url_helpers.ratings_path %>',
18
- type : 'big', // can be set to 'small' or 'big'
19
-
20
- /** Boolean vars **/
21
- step:false, // if true, mouseover binded star by star,
22
- isDisabled:false,
23
- showRateInfo: false,
24
- canRateAgain : false,
25
-
26
- /** Integer vars **/
27
- length:5, // number of star to display
28
- decimalLength : 0, // number of decimals.. Max 3, but you can complete the function 'getNote'
29
- rateMax : 20, // maximal rate - integer from 0 to 9999 (or more)
30
- rateInfosX : -45, // relative position in X axis of the info box when mouseover
31
- rateInfosY : 5, // relative position in Y axis of the info box when mouseover
32
- nbRates : 1,
33
-
34
- /** Functions **/
35
- onSuccess : null,
36
- onError : null
37
- };
38
-
39
- if(this.length>0)
40
- return this.each(function() {
41
- /*vars*/
42
- var opts = $.extend(defaults, op),
43
- newWidth = 0,
44
- starWidth = 0,
45
- starHeight = 0,
46
- bgPath = '',
47
- hasRated = false,
48
- globalWidth = 0,
49
- nbOfRates = opts.nbRates;
50
-
51
- if($(this).hasClass('jDisabled') || opts.isDisabled)
52
- var jDisabled = true;
53
- else
54
- var jDisabled = false;
55
-
56
- getStarWidth();
57
- $(this).height(starHeight);
58
-
59
-
60
-
61
- var average = parseFloat($(this).attr('data-average')), // get the average of all rates
62
- idBox = parseInt($(this).attr('data-id')), // get the id of the box
63
- kls = $(this).attr('data-kls'),
64
- dimension = $(this).attr('data-dimension'),
65
- widthRatingContainer = starWidth*opts.length, // Width of the Container
66
- widthColor = average/opts.rateMax*widthRatingContainer, // Width of the color Container
67
- quotient =
68
- $('<div>',
69
- {
70
- 'class' : 'jRatingColor',
71
- css:{
72
- width:widthColor
73
- }
74
- }).appendTo($(this)),
75
-
76
- average =
77
- $('<div>',
78
- {
79
- 'class' : 'jRatingAverage',
80
- css:{
81
- width:0,
82
- top:- starHeight
83
- }
84
- }).appendTo($(this)),
85
-
86
- jstar =
87
- $('<div>',
88
- {
89
- 'class' : 'jStar',
90
- css:{
91
- width:widthRatingContainer,
92
- height:starHeight,
93
- top:- (starHeight*2),
94
- background: 'url('+bgPath+') repeat-x'
95
- }
96
- }).appendTo($(this));
97
-
98
- $(this).css({width: widthRatingContainer,overflow:'hidden',zIndex:1,position:'relative'});
99
-
100
- if(!jDisabled)
101
- $(this).unbind().bind({
102
- mouseenter : function(e){
103
- var realOffsetLeft = findRealLeft(this);
104
- var relativeX = e.pageX - realOffsetLeft;
105
- if (opts.showRateInfo)
106
- var tooltip =
107
- $('<p>',{
108
- 'class' : 'jRatingInfos',
109
- html : getNote(relativeX)+' <span class="maxRate">/ '+opts.rateMax+'</span>',
110
- css : {
111
- top: (e.pageY + opts.rateInfosY),
112
- left: (e.pageX + opts.rateInfosX)
113
- }
114
- }).appendTo('body').show();
115
- },
116
- mouseover : function(e){
117
- $(this).css('cursor','pointer');
118
- },
119
- mouseout : function(){
120
- $(this).css('cursor','default');
121
- if(hasRated) average.width(globalWidth);
122
- else average.width(0);
123
- },
124
- mousemove : function(e){
125
- var realOffsetLeft = findRealLeft(this);
126
- var relativeX = e.pageX - realOffsetLeft;
127
- if(opts.step) newWidth = Math.floor(relativeX/starWidth)*starWidth + starWidth;
128
- else newWidth = relativeX;
129
- average.width(newWidth);
130
- if (opts.showRateInfo)
131
- $("p.jRatingInfos")
132
- .css({
133
- left: (e.pageX + opts.rateInfosX)
134
- })
135
- .html(getNote(newWidth) +' <span class="maxRate">/ '+opts.rateMax+'</span>');
136
- },
137
- mouseleave : function(){
138
- $("p.jRatingInfos").remove();
139
- },
140
- click : function(e){
141
- var element = this;
142
-
143
- /*set vars*/
144
- hasRated = true;
145
- globalWidth = newWidth;
146
- nbOfRates--;
147
-
148
- if(!opts.canRateAgain || parseInt(nbOfRates) <= 0) $(this).unbind().css('cursor','default').addClass('jDisabled');
149
-
150
- if (opts.showRateInfo) $("p.jRatingInfos").fadeOut('fast',function(){$(this).remove();});
151
- e.preventDefault();
152
- var rate = getNote(newWidth);
153
- average.width(newWidth);
154
-
155
-
156
- $.post(defaults.path,
157
- {
158
- idBox : idBox,
159
- rate : rate,
160
- kls : kls,
161
- dimension : dimension
162
- /** action : 'rating' **/
163
- },
164
- function(data) {
165
- if(!data.error)
166
- {
167
- /** Here you can display an alert box,
168
- or use the jNotify Plugin :) http://www.myqjqueryplugins.com/jNotify
169
- exemple : */
170
- if(opts.onSuccess) opts.onSuccess( element, rate );
171
- }
172
- else
173
- {
174
-
175
- /** Here you can display an alert box,
176
- or use the jNotify Plugin :) http://www.myqjqueryplugins.com/jNotify
177
- exemple : */
178
- if(opts.onError) opts.onError( element, rate );
179
- }
180
- },
181
- 'json'
182
- );
183
- }
184
- });
185
-
186
- function getNote(relativeX) {
187
- var noteBrut = parseFloat((relativeX*100/widthRatingContainer)*opts.rateMax/100);
188
- switch(opts.decimalLength) {
189
- case 1 :
190
- var note = Math.round(noteBrut*10)/10;
191
- break;
192
- case 2 :
193
- var note = Math.round(noteBrut*100)/100;
194
- break;
195
- case 3 :
196
- var note = Math.round(noteBrut*1000)/1000;
197
- break;
198
- default :
199
- var note = Math.round(noteBrut*1)/1;
200
- }
201
- return note;
202
- };
203
-
204
- function getStarWidth(){
205
- switch(opts.type) {
206
- case 'small' :
207
- starWidth = 12; // width of the picture small.png
208
- starHeight = 10; // height of the picture small.png
209
- bgPath = opts.smallStarsPath;
210
- break;
211
- default :
212
- starWidth = 23; // width of the picture stars.png
213
- starHeight = 20; // height of the picture stars.png
214
- bgPath = opts.bigStarsPath;
215
- }
216
- };
217
-
218
- function findRealLeft(obj) {
219
- if( !obj ) return 0;
220
- return obj.offsetLeft + findRealLeft( obj.offsetParent );
221
- };
222
- });
223
-
224
- }
225
- })(jQuery);
1
+ /************************************************************************
2
+ *************************************************************************
3
+ @Name : jRating - jQuery Plugin
4
+ @Revison : 3.1
5
+ @Date : 13/08/2013
6
+ @Author: ALPIXEL - (www.myjqueryplugins.com - www.alpixel.fr)
7
+ @License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
8
+
9
+ **************************************************************************
10
+ *************************************************************************/
11
+ (function($) {
12
+ $.fn.jRating = function(op) {
13
+ var defaults = {
14
+ /** String vars **/
15
+ bigStarsPath : '<%= asset_path "seems_rateable/stars.png" %>', // path of the icon stars.png
16
+ smallStarsPath : '<%= asset_path "seems_rateable/small.png" %>', // path of the icon small.png
17
+ path : '<%= SeemsRateable::Engine.routes.url_helpers.rates_path %>',
18
+ type : 'big', // can be set to 'small' or 'big'
19
+
20
+ /** Boolean vars **/
21
+ step: false, // if true, mouseover binded star by star,
22
+ isDisabled: false, // if true, user could not rate
23
+ showRateInfo: false, // show rates informations when cursor moves onto the plugin
24
+ canRateAgain : false, // if true, the user could rates {nbRates} times with jRating.. Default, 1 time
25
+ sendRequest: true, // send values to server
26
+
27
+ /** Integer vars **/
28
+ length:5, // number of star to display
29
+ decimalLength : 0, // number of decimals.
30
+ rateMax : 5, // maximal rate - integer from 0 to 9999 (or more)
31
+ rateInfosX : -45, // relative position in X axis of the info box when mouseover
32
+ rateInfosY : 5, // relative position in Y axis of the info box when mouseover
33
+ nbRates : 1,
34
+
35
+ /** Functions **/
36
+ onSuccess : function(element, data) {
37
+ var rateable = $(element).clone();
38
+ rateable.children().remove();
39
+ rateable.attr("data-average", data.average);
40
+ rateable.jRating(this);
41
+ $(element).replaceWith(rateable);
42
+ },
43
+
44
+ onError : function(element, data) {
45
+ $('<span>An error occurred</span>').insertAfter(element)
46
+ },
47
+
48
+ onClick: null // Fires when clicking on a star
49
+ };
50
+
51
+ if(this.length>0)
52
+ return this.each(function() {
53
+ /*vars*/
54
+ var opts = $.extend(defaults, op),
55
+ newWidth = 0,
56
+ starWidth = 0,
57
+ starHeight = 0,
58
+ bgPath = '',
59
+ hasRated = false,
60
+ globalWidth = 0,
61
+ nbOfRates = opts.nbRates;
62
+
63
+ if($(this).hasClass('jDisabled') || opts.isDisabled)
64
+ var jDisabled = true;
65
+ else
66
+ var jDisabled = false;
67
+
68
+ getStarWidth();
69
+ $(this).height(starHeight);
70
+
71
+ var average = parseFloat($(this).attr('data-average')), // get the average of all rates
72
+ rateable_id = parseInt($(this).attr('data-rateable-id')),
73
+ rateable_type = $(this).attr('data-rateable-type'),
74
+ dimension = $(this).attr('data-dimension'),
75
+ widthRatingContainer = starWidth*opts.length, // Width of the Container
76
+ widthColor = average/opts.rateMax*widthRatingContainer, // Width of the color Container
77
+
78
+ quotient =
79
+ $('<div>',
80
+ {
81
+ 'class' : 'jRatingColor',
82
+ css:{
83
+ width:widthColor
84
+ }
85
+ }).appendTo($(this)),
86
+
87
+ average =
88
+ $('<div>',
89
+ {
90
+ 'class' : 'jRatingAverage',
91
+ css:{
92
+ width:0,
93
+ top:- starHeight
94
+ }
95
+ }).appendTo($(this)),
96
+
97
+ jstar =
98
+ $('<div>',
99
+ {
100
+ 'class' : 'jStar',
101
+ css:{
102
+ width:widthRatingContainer,
103
+ height:starHeight,
104
+ top:- (starHeight*2),
105
+ background: 'url('+bgPath+') repeat-x'
106
+ }
107
+ }).appendTo($(this));
108
+
109
+
110
+ $(this).css({width: widthRatingContainer,overflow:'hidden',zIndex:1,position:'relative'});
111
+
112
+ if(!jDisabled)
113
+ $(this).unbind().bind({
114
+ mouseenter : function(e){
115
+ var realOffsetLeft = findRealLeft(this);
116
+ var relativeX = e.pageX - realOffsetLeft;
117
+ if (opts.showRateInfo)
118
+ var tooltip =
119
+ $('<p>',{
120
+ 'class' : 'jRatingInfos',
121
+ html : getNote(relativeX)+' <span class="maxRate">/ '+opts.rateMax+'</span>',
122
+ css : {
123
+ top: (e.pageY + opts.rateInfosY),
124
+ left: (e.pageX + opts.rateInfosX)
125
+ }
126
+ }).appendTo('body').show();
127
+ },
128
+ mouseover : function(e){
129
+ $(this).css('cursor','pointer');
130
+ },
131
+ mouseout : function(){
132
+ $(this).css('cursor','default');
133
+ if(hasRated) average.width(globalWidth);
134
+ else average.width(0);
135
+ },
136
+ mousemove : function(e){
137
+ var realOffsetLeft = findRealLeft(this);
138
+ var relativeX = e.pageX - realOffsetLeft;
139
+ if(opts.step) newWidth = Math.floor(relativeX/starWidth)*starWidth + starWidth;
140
+ else newWidth = relativeX;
141
+ average.width(newWidth);
142
+ if (opts.showRateInfo)
143
+ $("p.jRatingInfos")
144
+ .css({
145
+ left: (e.pageX + opts.rateInfosX)
146
+ })
147
+ .html(getNote(newWidth) +' <span class="maxRate">/ '+opts.rateMax+'</span>');
148
+ },
149
+ mouseleave : function(){
150
+ $("p.jRatingInfos").remove();
151
+ },
152
+ click : function(e){
153
+ var element = this;
154
+
155
+ /*set vars*/
156
+ hasRated = true;
157
+ globalWidth = newWidth;
158
+ nbOfRates--;
159
+
160
+ if(!opts.canRateAgain || parseInt(nbOfRates) <= 0) $(this).unbind().css('cursor','default').addClass('jDisabled');
161
+
162
+ if (opts.showRateInfo) $("p.jRatingInfos").fadeOut('fast',function(){$(this).remove();});
163
+ e.preventDefault();
164
+ var stars = getNote(newWidth);
165
+ average.width(newWidth);
166
+
167
+ if(opts.onClick) opts.onClick( element, rate );
168
+
169
+ if(opts.sendRequest) {
170
+ $.post(opts.path,
171
+ {
172
+ rate: {
173
+ rateable_id : rateable_id,
174
+ rateable_type : rateable_type,
175
+ stars : stars,
176
+ dimension : dimension
177
+ }
178
+ },
179
+
180
+ function(data) {
181
+ if(!data.error)
182
+ {
183
+ if(opts.onSuccess) opts.onSuccess(element, data);
184
+ }
185
+ else
186
+ {
187
+ if(opts.onError) opts.onError(element, data);
188
+ }
189
+ },
190
+ 'json'
191
+ );
192
+ }
193
+
194
+ }
195
+ });
196
+
197
+ function getNote(relativeX) {
198
+ var noteBrut = parseFloat((relativeX*100/widthRatingContainer)*parseInt(opts.rateMax)/100);
199
+ var dec=Math.pow(10,parseInt(opts.decimalLength));
200
+ var note = Math.round(noteBrut*dec)/dec;
201
+ return note;
202
+ };
203
+
204
+ function getStarWidth(){
205
+ switch(opts.type) {
206
+ case 'small' :
207
+ starWidth = 12; // width of the picture small.png
208
+ starHeight = 10; // height of the picture small.png
209
+ bgPath = opts.smallStarsPath;
210
+ break;
211
+ default :
212
+ starWidth = 23; // width of the picture stars.png
213
+ starHeight = 20; // height of the picture stars.png
214
+ bgPath = opts.bigStarsPath;
215
+ }
216
+ };
217
+
218
+ function findRealLeft(obj) {
219
+ if( !obj ) return 0;
220
+ return obj.offsetLeft + findRealLeft( obj.offsetParent );
221
+ };
222
+ });
223
+
224
+ }
225
+ })(jQuery);