seems_rateable 1.0.6 → 1.0.7
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 +8 -8
- data/{LICENSE.txt → MIT-LICENSE} +2 -4
- data/README.md +43 -54
- data/Rakefile +29 -4
- data/{lib/generators/seems_rateable/templates/images → app/assets/images/seems_rateable}/bg_jRatingInfos.png +0 -0
- data/{lib/generators/seems_rateable/templates/images → app/assets/images/seems_rateable}/small.png +0 -0
- data/{lib/generators/seems_rateable/templates/images → app/assets/images/seems_rateable}/stars.png +0 -0
- data/app/assets/javascripts/seems_rateable/application.js +15 -0
- data/{lib/generators/seems_rateable/templates/javascripts → app/assets/javascripts/seems_rateable}/jRating.jquery.js.erb +3 -3
- data/app/assets/stylesheets/seems_rateable/application.css +13 -0
- data/{lib/generators/seems_rateable/templates/stylesheets → app/assets/stylesheets/seems_rateable}/jRating.jquery.css +4 -4
- data/app/controllers/seems_rateable/application_controller.rb +4 -0
- data/app/controllers/seems_rateable/ratings_controller.rb +17 -0
- data/app/helpers/seems_rateable/application_helper.rb +4 -0
- data/app/helpers/seems_rateable/ratings_helper.rb +4 -0
- data/app/models/seems_rateable/cached_rating.rb +5 -0
- data/app/models/seems_rateable/rate.rb +6 -0
- data/app/views/layouts/seems_rateable/application.html.erb +14 -0
- data/config/routes.rb +3 -0
- data/lib/generators/seems_rateable/install/install_generator.rb +39 -0
- data/lib/generators/seems_rateable/{templates/cached_rating_migration.rb → install/templates/cached_ratings_migration.rb} +2 -2
- data/lib/generators/seems_rateable/install/templates/initializer.rb +4 -0
- data/lib/generators/seems_rateable/install/templates/jRating.js.erb +225 -0
- data/lib/generators/seems_rateable/install/templates/rateable.js.erb +25 -0
- data/lib/generators/seems_rateable/{templates/rate_migration.rb → install/templates/rates_migration.rb} +2 -5
- data/lib/seems_rateable/engine.rb +17 -0
- data/lib/seems_rateable/errors.rb +21 -0
- data/lib/seems_rateable/helpers.rb +27 -23
- data/lib/seems_rateable/model.rb +111 -0
- data/lib/seems_rateable/routes.rb +7 -0
- data/lib/seems_rateable/version.rb +1 -1
- data/lib/seems_rateable.rb +11 -3
- data/lib/tasks/seems_rateable_tasks.rake +4 -0
- metadata +83 -33
- data/.gitignore +0 -19
- data/Gemfile +0 -9
- data/lib/generators/seems_rateable/USAGE +0 -0
- data/lib/generators/seems_rateable/seems_rateable_generator.rb +0 -65
- data/lib/generators/seems_rateable/templates/cached_model.rb +0 -4
- data/lib/generators/seems_rateable/templates/controller.rb +0 -15
- data/lib/generators/seems_rateable/templates/javascripts/rateable.jquery.js.erb +0 -25
- data/lib/generators/seems_rateable/templates/model.rb +0 -7
- data/lib/generators/seems_rateable_destroy_generator.rb +0 -19
- data/lib/seems_rateable/record.rb +0 -117
- data/seems_rateable.gemspec +0 -25
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NjkxYmI5OTY4NDFmN2MzMDllYTIwZmVhYjZiNjhhMzkwMDVmNTRiNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTkyNGVkZTZiNGZlNzVmYTE2NDQ2ZWJhZmM3YmY4NzgwZGVmMmE0Yw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
M2FmYjc3NWNjNzI1YWVhNzFlNmYxMDNjZmFjOTM3ZGQzOWNkNjk3M2EzNjlj
|
10
|
+
ZmNiZmZkMDMwNzBiYjcyYmFlNGZhZjhhYzlmYzU2NDg5YTcwODBkYTA0ZmU0
|
11
|
+
OTVmOTk5YTNlYmU0NjZmYTZjZTQ4YWZmMzA2ZjczZWZiYmRiNzc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OWIzNmJkYjI5OGMwYjNmNDU2YTFmMjlkZmE0OWNmYjMzMDBmYTUyNWY4ODFk
|
14
|
+
N2U1YTY5OWMxYjRhYjViZTYwNzMzYjdmNjMzYzYwYmVkOWUxZmYyNDQ4NzAx
|
15
|
+
MGUwMzcwMzJjOWMzOTUzZmExMDFmZGE3NjhkNTYzNzc1YmE0NDY=
|
data/{LICENSE.txt → MIT-LICENSE}
RENAMED
@@ -1,6 +1,4 @@
|
|
1
|
-
Copyright
|
2
|
-
|
3
|
-
MIT License
|
1
|
+
Copyright 2013 YOURNAME
|
4
2
|
|
5
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
6
4
|
a copy of this software and associated documentation files (the
|
@@ -19,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Star rating gem for Rails application using jQuery plugin <a href="http://www.my
|
|
4
4
|
|
5
5
|
## Demo
|
6
6
|
|
7
|
-
<a href="http://rateable.herokuapp.com/">Demo</a> application, requires to sign up before rating
|
7
|
+
<a href="http://rateable.herokuapp.com/">Demo</a> application, requires to sign up before rating
|
8
8
|
|
9
9
|
## Instructions
|
10
10
|
|
@@ -24,58 +24,51 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
### Generation
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
Generator
|
30
|
-
The generator creates necessary model, controller and asset files. It also creates route and migration files that are already being migrated
|
27
|
+
$ rails generate seems_rateable:install
|
28
|
+
|
29
|
+
Generator creates migration files, javascript files and initializer
|
31
30
|
|
32
31
|
### Prepare
|
33
|
-
|
34
|
-
Include Javascript files adding these lines to application.js
|
35
|
-
|
36
|
-
#application.js
|
37
|
-
//= require rateable/jRating.jquery
|
38
|
-
//= require rateable/rateable.jquery
|
39
|
-
|
40
|
-
Include CSS file adding <code><%= seems_rateable_style %></code> to your layaut head tag
|
41
|
-
|
42
|
-
Also make sure you have an existing <code>current_user</code> object. If not, add something like this to your application controller
|
43
|
-
|
44
|
-
#application_controller.rb
|
45
|
-
helper_method :current_user
|
46
|
-
private
|
47
|
-
def current_user
|
48
|
-
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
49
|
-
end
|
50
|
-
|
51
|
-
To prepare model be rateable add <code>seems_rateable</code> to your model file. You can also pass a hash of options to
|
52
|
-
customize the functionality
|
53
32
|
|
54
|
-
|
55
|
-
<li><code>:dimensions</code> Array of dimensions e.g. <code>:dimensions => [:quality, :price, :performance]</code></li>
|
56
|
-
<li><code>:allow_update</code> Allowing user to re-rate his own ratings, default set to false e.g <code>:allow_update=> true</code>
|
57
|
-
</ul>
|
33
|
+
Require javascript files by adding this line to application.js
|
58
34
|
|
59
|
-
|
60
|
-
|
61
|
-
end
|
35
|
+
#application.js
|
36
|
+
//= require_directory ./rateable
|
62
37
|
|
63
|
-
|
64
|
-
<ul>
|
65
|
-
<li><code>@computer.rates_without_dimension</code> returns array of ratings given to the object</li>
|
66
|
-
<li><code>@computer.raters_without_dimension</code> returns array of users that rated the object</li>
|
67
|
-
<li><code>@computer.rate_average_without_dimension</code> returns cached database object containing average rate and quantity of given ratings of the object</li>
|
68
|
-
</ul>
|
38
|
+
Add <code>seems_rateable</code> to routes.rb file
|
69
39
|
|
70
|
-
|
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
|
45
|
+
$ rake db:migrate
|
46
|
+
|
47
|
+
To prepare model add <code> seems_rateable </code> to your model. You can also pass a hash of options to
|
48
|
+
customize the functionality
|
71
49
|
|
72
50
|
<ul>
|
73
|
-
<li><code
|
74
|
-
<li><code
|
75
|
-
<li><code>@computer.dimension_average</code> e.g <code>@computer.performance_average</code></li>
|
51
|
+
<li><code>:dimensions</code>Array of dimensions e.g <code>:dimensions => [:quality, :quantity]</code> </li>
|
52
|
+
<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>
|
76
53
|
</ul>
|
77
54
|
|
78
|
-
To
|
55
|
+
To access object's rates use <code>rates</code> method, to get dimension rates pass an argument eg :
|
56
|
+
$ @object.rates
|
57
|
+
$ @object.rates(:quality)
|
58
|
+
$ @object.rates(:quantity)
|
59
|
+
|
60
|
+
This also applies to cached average rating e.g
|
61
|
+
$ @object.average
|
62
|
+
$ @object.average(:quality)
|
63
|
+
$ @object.average(:quantity)
|
64
|
+
|
65
|
+
And to object's raters e.g
|
66
|
+
$ @object.raters
|
67
|
+
$ @object.raters(:quality)
|
68
|
+
$ @object.raters(:quantity)
|
69
|
+
|
70
|
+
To track user's given ratings add <code>seems_rateable_rater</code> to your rater model.
|
71
|
+
If your rater class is not "User"(e.g "Client" or "Customer") change configuration in initializer generated by this engine
|
79
72
|
Now you can access user's ratings by <code>@user.ratings_given</code>
|
80
73
|
|
81
74
|
### Usage
|
@@ -84,26 +77,22 @@ To display star rating use helper method <code>rating_for</code> in your view
|
|
84
77
|
|
85
78
|
#index.html.erb
|
86
79
|
|
87
|
-
rating_for @
|
80
|
+
rating_for @post
|
88
81
|
|
89
|
-
rating_for @
|
82
|
+
rating_for @post, :dimension => :quality, :class => 'post', :id => 'list'
|
90
83
|
|
91
|
-
rating_for @
|
92
|
-
|
84
|
+
rating_for @post, :static => true
|
85
|
+
|
93
86
|
You can specify these options :
|
94
87
|
<ul>
|
95
88
|
<li><code>:dimension</code>The dimension of the object</li>
|
96
89
|
<li><code>:static</code>Set to true to display static star rating, default false</li>
|
90
|
+
<li><code>:class</code>Class of the div, default set to 'rateable'</li>
|
97
91
|
<li><code>:id</code>ID of the div e.g <code>:id => "info"</code>, default nil</li>
|
98
92
|
</ul>
|
99
93
|
|
100
|
-
To edit the javascript options locate rateable.jquery.js file in /
|
101
|
-
The javascript options are explained directly in the file
|
102
|
-
|
103
|
-
### Removal
|
104
|
-
|
105
|
-
To remove gem files simply execute <code>rails g seems_rateable_destroy</code> in terminal.
|
106
|
-
Neither migration files nor database tables will be destroyed.
|
94
|
+
To edit the javascript options locate rateable.jquery.js file in /app/assets/javascripts/rateable/.
|
95
|
+
The javascript options are explained directly in the file
|
107
96
|
|
108
97
|
## Contributing
|
109
98
|
|
data/Rakefile
CHANGED
@@ -1,7 +1,32 @@
|
|
1
|
-
|
2
|
-
require
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
3
6
|
|
4
|
-
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'SeemsRateable'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
Bundler::GemHelper.install_tasks
|
20
|
+
|
21
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
22
|
+
load 'rails/tasks/engine.rake'
|
23
|
+
|
24
|
+
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
|
25
|
+
require 'rspec/core'
|
26
|
+
require 'rspec/core/rake_task'
|
5
27
|
|
6
28
|
task :default => :spec
|
7
|
-
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
File without changes
|
data/{lib/generators/seems_rateable/templates/images → app/assets/images/seems_rateable}/small.png
RENAMED
File without changes
|
data/{lib/generators/seems_rateable/templates/images → app/assets/images/seems_rateable}/stars.png
RENAMED
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
|
13
|
+
//= require_tree .
|
14
|
+
|
15
|
+
|
@@ -12,9 +12,9 @@
|
|
12
12
|
$.fn.jRating = function(op) {
|
13
13
|
var defaults = {
|
14
14
|
/** String vars **/
|
15
|
-
bigStarsPath : '<%= image_path "
|
16
|
-
smallStarsPath : '<%= image_path "
|
17
|
-
path : '<%=
|
15
|
+
bigStarsPath : '<%= image_path "seems_rateable/stars.png" %>', // path of the icon stars.png
|
16
|
+
smallStarsPath : '<%= image_path "seems_rateable/small.png" %>', // path of the icon small.png
|
17
|
+
path : '<%= SeemsRateable::Engine.routes.url_helpers.ratings_path %>',
|
18
18
|
type : 'big', // can be set to 'small' or 'big'
|
19
19
|
|
20
20
|
/** Boolean vars **/
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_dependency "seems_rateable/application_controller"
|
2
|
+
|
3
|
+
module SeemsRateable
|
4
|
+
class RatingsController < ::ApplicationController
|
5
|
+
def create
|
6
|
+
raise NoCurrentUserInstanceError unless current_user
|
7
|
+
|
8
|
+
obj = params[:kls].classify.constantize.find(params[:idBox])
|
9
|
+
begin
|
10
|
+
obj.rate(params[:rate].to_i, current_user.id, params[:dimension])
|
11
|
+
render :json => true
|
12
|
+
rescue Errors::AlreadyRatedError
|
13
|
+
render :json => {:error => true}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>SeemsRateable</title>
|
5
|
+
<%= stylesheet_link_tag "seems_rateable/application", media: "all" %>
|
6
|
+
<%= javascript_include_tag "seems_rateable/application" %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module SeemsRateable
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < ::Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def self.next_migration_number(path)
|
11
|
+
unless @prev_migration_nr
|
12
|
+
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
13
|
+
else
|
14
|
+
@prev_migration_nr += 1
|
15
|
+
end
|
16
|
+
@prev_migration_nr.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "generating migration files"
|
20
|
+
def copy_migrations
|
21
|
+
migration_template "rates_migration.rb", "db/migrate/create_seems_rateable_rates.rb"
|
22
|
+
migration_template "cached_ratings_migration.rb", "db/migrate/create_seems_rateable_cached_ratings.rb"
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "generating initializer"
|
26
|
+
def copy_initializer
|
27
|
+
template "initializer.rb", "config/initializers/seems_rateable.rb"
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "generating javascript files"
|
31
|
+
def copy_javascript_asset
|
32
|
+
Dir.mkdir "app/assets/javascripts/rateable" unless File.directory?("app/assets/javascripts/rateable")
|
33
|
+
copy_file "rateable.js.erb", "app/assets/javascripts/rateable/rateable.js.erb" unless File.exists?("app/assets/javascripts/rateable/rateable.js.erb")
|
34
|
+
copy_file "jRating.js.erb", "app/assets/javascripts/rateable/jRating.js.erb" unless File.exists?("app/assets/javascripts/rateable/jRating.js.erb")
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
class
|
1
|
+
class CreateSeemsRateableCachedRatings < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
|
-
create_table :
|
3
|
+
create_table :seems_rateable_cached_ratings do |t|
|
4
4
|
t.belongs_to :cacheable, :polymorphic => true
|
5
5
|
t.float :avg, :null => false
|
6
6
|
t.integer :cnt, :null => false
|
@@ -0,0 +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 : '<%= image_path "seems_rateable/stars.png" %>', // path of the icon stars.png
|
16
|
+
smallStarsPath : '<%= image_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);
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$(document).ready(function(){
|
2
|
+
$(".rateable").jRating({
|
3
|
+
//default options displayed below ->
|
4
|
+
|
5
|
+
rateMax: 5, //Maximal rate
|
6
|
+
length : 5, //Number of stars
|
7
|
+
//decimalLength : 0, //Number of decimals in the rate
|
8
|
+
//type : 'big', //Big or small
|
9
|
+
//step : true, //If set to true, filling of the stars is done star by star (step by step).
|
10
|
+
//isDisabled: false, //Set true to display static rating
|
11
|
+
//showRateInfo:false, //Rate info panel, set true to display
|
12
|
+
//rateInfosX : 45, //In pixel - Absolute left position of the information box during mousemove.
|
13
|
+
//rateInfosY : 5, //In pixel - Absolute top position of the information box during mousemove.
|
14
|
+
path : '<%= SeemsRateable::Engine.routes.url_helpers.ratings_path %>',
|
15
|
+
onSuccess : function(element, rate){
|
16
|
+
//something like ->
|
17
|
+
//alert('success');
|
18
|
+
$('<span class="text-success"><small style="display:inline-block;">Thanks for rating!</small></span>').insertAfter(element)
|
19
|
+
},
|
20
|
+
onError : function(element, rate) {
|
21
|
+
$('<span class="text-error"><small style="display:inline-block;">You have already rated!</small></span>').insertAfter(element)
|
22
|
+
}
|
23
|
+
});
|
24
|
+
|
25
|
+
});
|