seems_rateable 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/stylesheets/seems_rateable/application.css +27 -27
- data/app/controllers/seems_rateable/application_controller.rb +5 -5
- data/app/controllers/seems_rateable/ratings_controller.rb +9 -9
- 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 +37 -37
- data/lib/generators/seems_rateable/install/templates/cached_ratings_migration.rb +14 -14
- data/lib/seems_rateable.rb +1 -1
- data/lib/seems_rateable/engine.rb +12 -12
- data/lib/seems_rateable/errors.rb +19 -19
- data/lib/seems_rateable/helpers.rb +25 -25
- data/lib/seems_rateable/model.rb +106 -106
- data/lib/seems_rateable/routes.rb +5 -5
- data/lib/seems_rateable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75166b76cc03e894fbb7d2d63cd807acdafc4816
|
4
|
+
data.tar.gz: d8353a22a2e5cfa5ddba077445e4900d3dd4afe3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6c2b82a4cee9ac55e02150db1142bc49d4f0c1cf00d7fa06e5c4f8e389b21cc090b5f5289d11ae240038cb140ccae116a6e4b1e82e468775fe0d55e9380b0f2
|
7
|
+
data.tar.gz: 9a250d496faf719be8e82823536e4ea25774f5ba8c688274e3640261d864f30d1384c9b0310d6453a4c088f8ad742a0ef1a5ba864e8543808efc90632f79ddbb
|
@@ -20,43 +20,43 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
.jRatingAverage {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
background-color:#f62929;
|
24
|
+
position:relative;
|
25
|
+
top:0;
|
26
|
+
left:0;
|
27
|
+
z-index:2;
|
28
|
+
height:100%;
|
29
29
|
}
|
30
30
|
.jRatingColor {
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
background-color:#FFD400; /* bgcolor of the stars*/
|
32
|
+
position:relative;
|
33
|
+
top:0;
|
34
|
+
left:0;
|
35
|
+
z-index:2;
|
36
|
+
height:100%;
|
37
37
|
}
|
38
38
|
|
39
39
|
/** Div containing the stars **/
|
40
40
|
.jStar {
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
position:relative;
|
42
|
+
left:0;
|
43
|
+
z-index:3;
|
44
44
|
}
|
45
45
|
|
46
46
|
/** P containing the rate informations **/
|
47
47
|
p.jRatingInfos {
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
48
|
+
position: absolute;
|
49
|
+
z-index:9999;
|
50
|
+
background: transparent url('bg_jRatingInfos.png') no-repeat;
|
51
|
+
color: #CACACA;
|
52
|
+
display: none;
|
53
|
+
width: 91px;
|
54
|
+
height: 29px;
|
55
|
+
font-size:16px;
|
56
|
+
text-align:center;
|
57
|
+
padding-top:5px;
|
58
58
|
}
|
59
59
|
p.jRatingInfos span.maxRate {
|
60
|
-
|
61
|
-
|
60
|
+
color:#c9c9c9;
|
61
|
+
font-size:14px;
|
62
62
|
}
|
@@ -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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
+
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
+
def routegen
|
20
|
+
route("seems_rateable")
|
21
|
+
end
|
22
|
+
|
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
|
+
|
29
|
+
desc "generating initializer"
|
30
|
+
def copy_initializer
|
31
|
+
template "initializer.rb", "config/initializers/seems_rateable.rb"
|
32
|
+
end
|
33
|
+
|
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
|
-
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace SeemsRateable
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
config.generators do |g|
|
6
|
+
g.test_framework :rspec, :fixture => false
|
7
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
+
|
9
|
+
class NoCurrentUserInstanceError < StandardError
|
10
|
+
def to_s
|
11
|
+
"User instance current_user is not available."
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class AlreadyRatedError < StandardError
|
16
|
+
def to_s
|
17
|
+
"User has already rated an object."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
21
|
end
|
@@ -1,27 +1,27 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
2
|
+
module Helpers
|
3
|
+
def rating_for(obj, opts={})
|
4
|
+
raise Errors::InvalidRateableObjectError unless obj.class.respond_to?(:rateable?)
|
5
|
+
|
6
|
+
options = {
|
7
|
+
:dimension => nil,
|
8
|
+
:static => false,
|
9
|
+
:class => 'rateable',
|
10
|
+
:id => nil
|
11
|
+
}.update(opts)
|
12
|
+
|
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
|
+
|
18
|
+
def seems_rateable_stylesheet
|
19
|
+
stylesheet_link_tag "seems_rateable/application", media: "all", "data-turbolinks-track" => true
|
20
|
+
end
|
21
|
+
|
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,112 +1,112 @@
|
|
1
1
|
require 'active_support/concern'
|
2
2
|
|
3
3
|
module SeemsRateable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
4
|
+
module Model
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
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
|
21
|
+
|
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
|
38
|
+
|
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
|
47
|
+
|
48
|
+
|
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
|
56
|
+
|
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
|
64
|
+
|
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
|
72
|
+
|
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
|
77
|
+
|
78
|
+
def can_update?
|
79
|
+
self.class.can_update?
|
80
|
+
end
|
81
81
|
|
82
82
|
module ClassMethods
|
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
|
-
|
111
|
-
|
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
|
112
112
|
end
|