seems_rateable 1.0.10 → 1.0.11
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/README.md +6 -6
- data/app/controllers/seems_rateable/application_controller.rb +5 -5
- data/app/controllers/seems_rateable/ratings_controller.rb +8 -8
- data/app/models/seems_rateable/cached_rating.rb +3 -3
- data/app/models/seems_rateable/rate.rb +4 -4
- data/config/routes.rb +1 -1
- data/lib/generators/seems_rateable/install/install_generator.rb +32 -32
- data/lib/generators/seems_rateable/install/templates/cached_ratings_migration.rb +14 -14
- data/lib/generators/seems_rateable/install/templates/rateable.js.erb +1 -1
- data/lib/generators/seems_rateable/install/templates/rates_migration.rb +1 -1
- data/lib/seems_rateable.rb +3 -1
- data/lib/seems_rateable/engine.rb +14 -14
- data/lib/seems_rateable/errors.rb +16 -16
- data/lib/seems_rateable/helpers.rb +21 -21
- data/lib/seems_rateable/model.rb +99 -98
- data/lib/seems_rateable/routes.rb +5 -5
- data/lib/seems_rateable/version.rb +1 -1
- metadata +2 -6
- data/app/helpers/seems_rateable/application_helper.rb +0 -4
- data/app/helpers/seems_rateable/ratings_helper.rb +0 -4
- data/app/views/layouts/seems_rateable/application.html.erb +0 -14
- data/lib/tasks/seems_rateable_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e273b87f76a38f6c21e25fa78863bcc11f6233ca
|
4
|
+
data.tar.gz: 93b78f7f92bf44079c01aba93cb64304b46421cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0112a624f4a7734ec39753748b0775e5759a99ab8ab30fa5805470ccae382d44d3ee706cf0b478851b2792955125268256a5af60e8979d53db4ae0daf96d6ccc
|
7
|
+
data.tar.gz: 1691a6b51152d14b098cb8f856881795e7428359d744905430b2b732a0e2179a6ed804a20ba929b7a3a4a1e1733f41543adc2b1ee163c623ed8722874344b21d
|
data/README.md
CHANGED
@@ -45,7 +45,7 @@ Don't forget to run
|
|
45
45
|
|
46
46
|
$ rake db:migrate
|
47
47
|
|
48
|
-
To prepare model add <code> seems_rateable </code> to your rateable model file. You can also pass a hash of options to
|
48
|
+
To prepare model add <code> seems_rateable </code> to your rateable model file. You can also pass a hash of options to
|
49
49
|
customize the functionality
|
50
50
|
|
51
51
|
<ul>
|
@@ -76,7 +76,7 @@ And to object's raters e.g
|
|
76
76
|
@object.raters(:quantity)
|
77
77
|
|
78
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.
|
79
|
+
If your rater class is not "User"(e.g "Client" or "Customer") change configuration in initializer generated by this engine.
|
80
80
|
Now you can access user's ratings by <code>@user.ratings_given</code>
|
81
81
|
|
82
82
|
### Usage
|
@@ -84,13 +84,13 @@ Now you can access user's ratings by <code>@user.ratings_given</code>
|
|
84
84
|
To display star rating use helper method <code>rating_for</code> in your view
|
85
85
|
|
86
86
|
#index.html.erb
|
87
|
-
|
87
|
+
|
88
88
|
rating_for @post
|
89
|
-
|
89
|
+
|
90
90
|
rating_for @post, :dimension => :quality, :class => 'post', :id => 'list'
|
91
|
-
|
91
|
+
|
92
92
|
rating_for @post, :static => true
|
93
|
-
|
93
|
+
|
94
94
|
You can specify these options :
|
95
95
|
<ul>
|
96
96
|
<li><code>:dimension</code>The dimension of the object</li>
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
class ApplicationController < ::ApplicationController
|
3
|
+
rescue_from SeemsRateable::Errors::AlreadyRatedError do |exception|
|
4
|
+
render :json => {:error => true}
|
5
|
+
end
|
6
|
+
end
|
7
7
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require_dependency "seems_rateable/application_controller"
|
2
2
|
|
3
3
|
module SeemsRateable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
class RatingsController < ApplicationController
|
5
|
+
def create
|
6
|
+
raise NoCurrentUserInstanceError unless current_user
|
7
|
+
obj = params[:kls].classify.constantize.find(params[:idBox])
|
8
|
+
obj.rate(params[:rate].to_i, current_user.id, params[:dimension])
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
render :json => true
|
11
|
+
end
|
12
|
+
end
|
13
13
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class Rate < ActiveRecord::Base
|
3
|
+
belongs_to :rater, :class_name => SeemsRateable::Engine.config.owner_class
|
4
|
+
belongs_to :rateable, :polymorphic => true
|
5
|
+
end
|
6
6
|
end
|
data/config/routes.rb
CHANGED
@@ -2,41 +2,41 @@ require 'rails/generators/migration'
|
|
2
2
|
require 'fileutils'
|
3
3
|
|
4
4
|
module SeemsRateable
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < ::Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
def routegen
|
20
|
+
route("seems_rateable")
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
23
|
+
desc "generating migration files"
|
24
|
+
def copy_migrations
|
25
|
+
migration_template "rates_migration.rb", "db/migrate/create_seems_rateable_rates.rb"
|
26
|
+
migration_template "cached_ratings_migration.rb", "db/migrate/create_seems_rateable_cached_ratings.rb"
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
desc "generating initializer"
|
30
|
+
def copy_initializer
|
31
|
+
template "initializer.rb", "config/initializers/seems_rateable.rb"
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
34
|
+
desc "generating javascript files"
|
35
|
+
def copy_javascript_asset
|
36
|
+
Dir.mkdir "app/assets/javascripts/rateable" unless File.directory?("app/assets/javascripts/rateable")
|
37
|
+
copy_file "rateable.js.erb", "app/assets/javascripts/rateable/rateable.js.erb" unless File.exists?("app/assets/javascripts/rateable/rateable.js.erb")
|
38
|
+
copy_file "jRating.js.erb", "app/assets/javascripts/rateable/jRating.js.erb" unless File.exists?("app/assets/javascripts/rateable/jRating.js.erb")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
42
|
end
|
@@ -1,17 +1,17 @@
|
|
1
1
|
class CreateSeemsRateableCachedRatings < ActiveRecord::Migration
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
2
|
+
def self.up
|
3
|
+
create_table :seems_rateable_cached_ratings do |t|
|
4
|
+
t.belongs_to :cacheable, :polymorphic => true
|
5
|
+
t.float :avg, :null => false
|
6
|
+
t.integer :cnt, :null => false
|
7
|
+
t.string :dimension
|
8
|
+
t.integer :cacheable_id, :limit => 8
|
9
|
+
t.string :cacheable_type
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def self.down
|
15
|
+
drop_table :cached_ratings
|
16
|
+
end
|
17
17
|
end
|
data/lib/seems_rateable.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace SeemsRateable
|
4
|
+
|
5
|
+
config.generators do |g|
|
6
|
+
g.test_framework :rspec, :fixture => false
|
7
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer :seems_rateable do
|
11
|
+
ActiveRecord::Base.send :include, SeemsRateable::Model
|
12
|
+
ActionView::Base.send :include, SeemsRateable::Helpers
|
13
|
+
ActionDispatch::Routing::Mapper.send :include, SeemsRateable::Routes
|
14
|
+
end
|
15
|
+
end
|
16
16
|
end
|
@@ -1,21 +1,21 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
module Errors
|
3
|
+
class InvalidRateableObjectError < StandardError
|
4
|
+
def to_s
|
5
|
+
"Stated object is not rateable. Add 'seems_rateable' to your object's class model."
|
6
|
+
end
|
7
|
+
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
class NoCurrentUserInstanceError < StandardError
|
10
|
+
def to_s
|
11
|
+
"User instance current_user is not available."
|
12
|
+
end
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
class AlreadyRatedError < StandardError
|
16
|
+
def to_s
|
17
|
+
"User has already rated an object."
|
18
|
+
end
|
19
|
+
end
|
18
20
|
end
|
19
|
-
end
|
20
|
-
end
|
21
21
|
end
|
@@ -1,27 +1,27 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
module Helpers
|
3
|
+
def rating_for(obj, opts={})
|
4
|
+
raise Errors::InvalidRateableObjectError unless obj.class.respond_to?(:rateable?)
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
options = {
|
7
|
+
:dimension => nil,
|
8
|
+
:static => false,
|
9
|
+
:class => 'rateable',
|
10
|
+
:id => nil
|
11
|
+
}.update(opts)
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
content_tag :div, "", "data-average" => obj.average(options[:dimension]) ? obj.average(options[:dimension]).avg : 0, :id => options[:id],
|
14
|
+
:class => "#{options[:class]}#{jdisabled?(options[:static])}",
|
15
|
+
"data-id" => obj.id, "data-kls" => obj.class.name, "data-dimension" => options[:dimension]
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def seems_rateable_stylesheet
|
19
|
+
stylesheet_link_tag "seems_rateable/application", media: "all", "data-turbolinks-track" => true
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
private
|
23
|
+
def jdisabled?(option)
|
24
|
+
" jDisabled" if option || !current_user
|
25
|
+
end
|
26
|
+
end
|
27
27
|
end
|
data/lib/seems_rateable/model.rb
CHANGED
@@ -1,111 +1,112 @@
|
|
1
1
|
require 'active_support/concern'
|
2
|
+
|
2
3
|
module SeemsRateable
|
3
|
-
|
4
|
-
|
4
|
+
module Model
|
5
|
+
extend ActiveSupport::Concern
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
7
|
+
def rate(stars, user_id, dimension=nil)
|
8
|
+
if !has_rated?(user_id, dimension)
|
9
|
+
self.rates.create do |r|
|
10
|
+
r.stars = stars
|
11
|
+
r.rater_id = user_id
|
12
|
+
r.dimension = dimension
|
13
|
+
end
|
14
|
+
update_overall_average_rating(stars, dimension)
|
15
|
+
elsif has_rated?(user_id, dimension) && can_update?
|
16
|
+
update_users_rating(stars, user_id, dimension)
|
17
|
+
else
|
18
|
+
raise Errors::AlreadyRatedError
|
19
|
+
end
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
22
|
+
def update_overall_average_rating(stars, dimension=nil)
|
23
|
+
if average(dimension).nil?
|
24
|
+
CachedRating.create do |r|
|
25
|
+
r.avg = stars
|
26
|
+
r.dimension = dimension
|
27
|
+
r.cacheable_id = self.id
|
28
|
+
r.cacheable_type = self.class.name
|
29
|
+
r.cnt = 1
|
30
|
+
end
|
31
|
+
else
|
32
|
+
r = average(dimension)
|
33
|
+
r.avg = (r.avg * r.cnt + stars) / (r.cnt+1)
|
34
|
+
r.cnt += 1
|
35
|
+
r.save!
|
36
|
+
end
|
37
|
+
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
def update_users_rating(stars, user_id, dimension=nil)
|
40
|
+
obj = rates(dimension).where(:rater_id => user_id).first
|
41
|
+
current_record = average(dimension)
|
42
|
+
current_record.avg = (current_record.avg*current_record.cnt - obj.stars + stars) / (current_record.cnt)
|
43
|
+
current_record.save!
|
44
|
+
obj.stars = stars
|
45
|
+
obj.save!
|
46
|
+
end
|
45
47
|
|
46
48
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
49
|
+
def average(dimension=nil)
|
50
|
+
if dimension.nil?
|
51
|
+
self.send "rate_average_without_dimension"
|
52
|
+
else
|
53
|
+
self.send "#{dimension}_average"
|
54
|
+
end
|
55
|
+
end
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
def rates(dimension=nil)
|
58
|
+
if dimension.nil?
|
59
|
+
self.send "rates_without_dimension"
|
60
|
+
else
|
61
|
+
self.send "#{dimension}_rates"
|
62
|
+
end
|
63
|
+
end
|
62
64
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
65
|
+
def raters(dimension=nil)
|
66
|
+
if dimension.nil?
|
67
|
+
self.send "raters_without_dimension"
|
68
|
+
else
|
69
|
+
self.send "#{dimension}_raters"
|
70
|
+
end
|
71
|
+
end
|
70
72
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
def has_rated?(user_id, dimension=nil)
|
74
|
+
record = self.rates(dimension).where(:rater_id => user_id)
|
75
|
+
record.empty? ? false : true
|
76
|
+
end
|
75
77
|
|
76
|
-
|
77
|
-
|
78
|
-
|
78
|
+
def can_update?
|
79
|
+
self.class.can_update?
|
80
|
+
end
|
79
81
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
end
|
82
|
+
module ClassMethods
|
83
|
+
def seems_rateable(opts={})
|
84
|
+
has_many :rates_without_dimension, -> { where(dimension: nil) }, :as => :rateable, :class_name => SeemsRateable::Rate, :dependent => :destroy
|
85
|
+
has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
|
86
|
+
has_one :rate_average_without_dimension, -> { where(dimension: nil) }, :as => :cacheable, :class_name => SeemsRateable::CachedRating, :dependent => :destroy
|
87
|
+
|
88
|
+
@permission = opts[:allow_update] ? true : false
|
89
|
+
|
90
|
+
def self.can_update?
|
91
|
+
@permission
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.rateable?
|
95
|
+
true
|
96
|
+
end
|
97
|
+
|
98
|
+
if opts[:dimensions].is_a?(Array)
|
99
|
+
opts[:dimensions].each do |dimension|
|
100
|
+
has_many :"#{dimension}_rates", -> { where(dimension: dimension.to_s) }, :dependent => :destroy, :class_name => SeemsRateable::Rate, :as => :rateable
|
101
|
+
has_many :"#{dimension}_raters", :through => :"#{dimension}_rates", :source => :rater
|
102
|
+
has_one :"#{dimension}_average", -> { where(dimension: dimension.to_s) }, :as => :cacheable, :class_name => SeemsRateable::CachedRating, :dependent => :destroy
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def seems_rateable_rater
|
108
|
+
has_many :ratings_given, :class_name => SeemsRateable::Rate, :foreign_key => :rater_id
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
111
112
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seems_rateable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Toth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -101,7 +101,6 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
- app/views/layouts/seems_rateable/application.html.erb
|
105
104
|
- app/assets/stylesheets/seems_rateable/application.css
|
106
105
|
- app/assets/javascripts/seems_rateable/application.js
|
107
106
|
- app/assets/images/seems_rateable/stars.png
|
@@ -109,8 +108,6 @@ files:
|
|
109
108
|
- app/assets/images/seems_rateable/bg_jRatingInfos.png
|
110
109
|
- app/controllers/seems_rateable/ratings_controller.rb
|
111
110
|
- app/controllers/seems_rateable/application_controller.rb
|
112
|
-
- app/helpers/seems_rateable/ratings_helper.rb
|
113
|
-
- app/helpers/seems_rateable/application_helper.rb
|
114
111
|
- app/models/seems_rateable/rate.rb
|
115
112
|
- app/models/seems_rateable/cached_rating.rb
|
116
113
|
- config/routes.rb
|
@@ -127,7 +124,6 @@ files:
|
|
127
124
|
- lib/seems_rateable/errors.rb
|
128
125
|
- lib/seems_rateable/engine.rb
|
129
126
|
- lib/seems_rateable/model.rb
|
130
|
-
- lib/tasks/seems_rateable_tasks.rake
|
131
127
|
- MIT-LICENSE
|
132
128
|
- Rakefile
|
133
129
|
- README.md
|
@@ -1,14 +0,0 @@
|
|
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>
|