seems_rateable 1.0.13 → 2.0.0
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.
- checksums.yaml +4 -4
- data/.gitignore +20 -0
- data/Gemfile +3 -0
- data/README.md +41 -61
- data/Rakefile +8 -2
- data/app/assets/javascripts/seems_rateable/index.js +1 -0
- data/{lib/generators/seems_rateable/install/templates → app/assets/javascripts/seems_rateable}/jRating.js.erb +225 -225
- data/app/assets/stylesheets/seems_rateable/{application.css → index.css.erb} +2 -2
- data/app/controllers/seems_rateable/application_controller.rb +1 -3
- data/app/controllers/seems_rateable/rates_controller.rb +31 -0
- data/app/models/seems_rateable/rate.rb +2 -2
- data/bin/rails +8 -0
- data/config/routes.rb +1 -1
- data/lib/generators/seems_rateable/install/install_generator.rb +34 -28
- data/lib/generators/seems_rateable/install/templates/initializer.rb +5 -2
- data/lib/generators/seems_rateable/install/templates/rateable.js.erb +12 -22
- data/lib/generators/seems_rateable/install/templates/rates_migration.rb +6 -9
- data/lib/generators/seems_rateable/manifest_finder.rb +25 -0
- data/lib/generators/seems_rateable/migration_helpers.rb +21 -0
- data/lib/generators/seems_rateable/uninstall/templates/drop_seems_rateable_rates_table.rb +9 -0
- data/lib/generators/seems_rateable/uninstall/uninstall_generator.rb +50 -0
- data/lib/generators/seems_rateable/uninstall_old/templates/drop_seems_rateable_cached_ratings_table.rb +9 -0
- data/lib/generators/seems_rateable/uninstall_old/templates/drop_seems_rateable_rates_table.rb +9 -0
- data/lib/generators/seems_rateable/uninstall_old/uninstall_old_generator.rb +48 -0
- data/lib/seems_rateable.rb +22 -7
- data/lib/seems_rateable/builder.rb +35 -0
- data/lib/seems_rateable/builder/html_options.rb +37 -0
- data/lib/seems_rateable/configuration.rb +23 -0
- data/lib/seems_rateable/engine.rb +12 -7
- data/lib/seems_rateable/errors.rb +3 -17
- data/lib/seems_rateable/helpers/action_view_extension.rb +18 -0
- data/lib/seems_rateable/helpers/current_rater.rb +15 -0
- data/lib/seems_rateable/models/active_record_extension.rb +23 -0
- data/lib/seems_rateable/models/active_record_extension/rateable.rb +26 -0
- data/lib/seems_rateable/models/active_record_extension/rater.rb +11 -0
- data/lib/seems_rateable/rating.rb +19 -0
- data/lib/seems_rateable/routes.rb +1 -1
- data/lib/seems_rateable/version.rb +1 -1
- data/seems_rateable.gemspec +32 -0
- data/spec/builder_spec.rb +53 -0
- data/spec/controllers/rates_controller_spec.rb +56 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/{app/assets/javascripts/seems_rateable → spec/dummy/app/assets/javascripts}/application.js +4 -3
- data/spec/dummy/app/assets/javascripts/posts.js +2 -0
- data/spec/dummy/app/assets/javascripts/rateable/rateable.js.erb +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +19 -0
- data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/posts_controller.rb +58 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/models/user.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +26 -0
- data/spec/dummy/app/views/posts/_form.html.erb +21 -0
- data/spec/dummy/app/views/posts/edit.html.erb +6 -0
- data/spec/dummy/app/views/posts/index.html.erb +28 -0
- data/spec/dummy/app/views/posts/new.html.erb +5 -0
- data/spec/dummy/app/views/posts/show.html.erb +9 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +42 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/devise.rb +254 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/seems_rateable.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/devise.en.yml +59 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +8 -0
- data/spec/dummy/db/migrate/20140506144141_create_posts.rb +9 -0
- data/spec/dummy/db/migrate/20140506144841_devise_create_users.rb +42 -0
- data/spec/dummy/db/migrate/20140520170316_create_seems_rateable_rates.rb +15 -0
- data/spec/dummy/db/schema.rb +54 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/posts.rb +5 -0
- data/spec/factories/rate.rb +7 -0
- data/spec/factories/users.rb +7 -0
- data/spec/features/rating_spec.rb +63 -0
- data/spec/helpers/action_view_extension_spec.rb +59 -0
- data/spec/models/active_record_extension_spec.rb +105 -0
- data/spec/models/rate_spec.rb +6 -0
- data/spec/rating_spec.rb +70 -0
- data/spec/spec_helper.rb +45 -0
- data/spec/support/dummies.rb +19 -0
- metadata +263 -25
- data/app/controllers/seems_rateable/ratings_controller.rb +0 -13
- data/app/models/seems_rateable/cached_rating.rb +0 -5
- data/lib/generators/seems_rateable/install/templates/cached_ratings_migration.rb +0 -17
- data/lib/seems_rateable/helpers.rb +0 -27
- data/lib/seems_rateable/model.rb +0 -91
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b5c6c41b54dc14c601e4f35661dcba8c29cc944d
|
|
4
|
+
data.tar.gz: dba8a7b572f328759c514d5a240230d4d8d0de61
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 324447036712396cd2c77ad6d84f543c647b8ffee420aa68c1eee20fee34ca16f130aa514e6d06d8da6d82cdb7693954306750fc0452f7038efd60a84b8bec59
|
|
7
|
+
data.tar.gz: ad5e2d9e980a32d119018373ceaef79416186539691251c296e986419246602c6de405b8e655d46831e1b492b78487d2dcc555f8d1ee2415237b1eaa06960a0d
|
data/.gitignore
ADDED
|
@@ -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
data/README.md
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
# SeemsRateable
|
|
2
2
|
|
|
3
|
-
Star rating gem for Rails application using jQuery plugin
|
|
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
|
-
|
|
7
|
+
[Demo](http://seemsrateable.herokuapp.com/) application, requires to sign up before rating.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
### Important! If you are upgrading from 1.x.x please do accordingly :
|
|
10
10
|
|
|
11
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
### Prepare
|
|
37
|
+
And run :
|
|
32
38
|
|
|
33
|
-
|
|
39
|
+
$ rake db:migrate
|
|
34
40
|
|
|
35
|
-
|
|
36
|
-
//= require_directory ./rateable
|
|
41
|
+
## Prepare
|
|
37
42
|
|
|
38
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
Don't forget to run
|
|
46
|
+
Post < ActiveRecord::Base
|
|
47
|
+
seems_rateable # :quality, :speed, :effectiveness
|
|
48
|
+
end
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
|
|
51
|
+
To track user's given rates add `seems_rateable_rater` to your rater model.
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
53
|
+
User < ActiveRecord::Base
|
|
54
|
+
seems_rateable_rater
|
|
55
|
+
end
|
|
50
56
|
|
|
51
|
-
|
|
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
|
-
|
|
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
|
-
|
|
61
|
+
#show.html.erb
|
|
61
62
|
|
|
62
|
-
|
|
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
|
-
|
|
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
|
-
|
|
67
|
+
You can specify these options :
|
|
83
68
|
|
|
84
|
-
|
|
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
|
-
|
|
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
|
-
|
|
76
|
+
## Configuration
|
|
89
77
|
|
|
90
|
-
|
|
78
|
+
In *config/initializers/seems_rateable.rb* you can configure these options :
|
|
91
79
|
|
|
92
|
-
|
|
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.
|
|
5
|
-
@Date :
|
|
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.
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
top:-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
$(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
.css(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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);
|