seems_rateable 1.0.9 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/seems_rateable/application_controller.rb +4 -1
- data/app/controllers/seems_rateable/ratings_controller.rb +8 -12
- 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 +33 -30
- data/lib/generators/seems_rateable/install/templates/jRating.js.erb +2 -2
- data/lib/seems_rateable/engine.rb +12 -13
- data/lib/seems_rateable/errors.rb +16 -16
- data/lib/seems_rateable/helpers.rb +22 -22
- data/lib/seems_rateable/model.rb +96 -96
- data/lib/seems_rateable/routes.rb +4 -4
- data/lib/seems_rateable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e92afbf1216b55fc3629fd9dc830ace41d6f74e5
|
4
|
+
data.tar.gz: 05bcffa91c9f3283436c050b4e4947db993f54d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b0c3a1a4e6eabe2f23b18463a33c122f654bd0449656bc8af3f2d0073878427fdae9016de8b5f47b67a747c302b78da664b61c2fe0ee60bbac509bac70e0544
|
7
|
+
data.tar.gz: 68e21d5a5865df84608105641c80b2434705e2c92fa7e941ec5f6eee854b0cff3334aa9de5a6f1547a3c2f47216d04ee8cf44d459c415af6b2fac13edabde7e1
|
@@ -1,17 +1,13 @@
|
|
1
1
|
require_dependency "seems_rateable/application_controller"
|
2
2
|
|
3
3
|
module SeemsRateable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
render :json => true
|
12
|
-
rescue Errors::AlreadyRatedError
|
13
|
-
render :json => {:error => true}
|
14
|
-
end
|
15
|
-
end
|
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
|
16
11
|
end
|
12
|
+
end
|
17
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,38 +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
|
-
|
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
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
migration_template "cached_ratings_migration.rb", "db/migrate/create_seems_rateable_cached_ratings.rb"
|
23
|
-
end
|
19
|
+
def routegen
|
20
|
+
route("seems_rateable")
|
21
|
+
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
29
|
+
desc "generating initializer"
|
30
|
+
def copy_initializer
|
31
|
+
template "initializer.rb", "config/initializers/seems_rateable.rb"
|
32
|
+
end
|
36
33
|
|
37
|
-
|
38
|
-
|
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
|
39
42
|
end
|
@@ -12,8 +12,8 @@
|
|
12
12
|
$.fn.jRating = function(op) {
|
13
13
|
var defaults = {
|
14
14
|
/** String vars **/
|
15
|
-
bigStarsPath : '<%=
|
16
|
-
smallStarsPath : '<%=
|
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
17
|
path : '<%= SeemsRateable::Engine.routes.url_helpers.ratings_path %>',
|
18
18
|
type : 'big', // can be set to 'small' or 'big'
|
19
19
|
|
@@ -1,17 +1,16 @@
|
|
1
1
|
module SeemsRateable
|
2
|
-
|
3
|
-
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace SeemsRateable
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
5
|
+
config.generators do |g|
|
6
|
+
g.test_framework :rspec, :fixture => false
|
7
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
16
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
|
17
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
|
-
|
18
|
-
end
|
19
|
-
end
|
15
|
+
class AlreadyRatedError < StandardError
|
16
|
+
def to_s
|
17
|
+
"User has already rated an object."
|
20
18
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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,111 +1,111 @@
|
|
1
1
|
require 'active_support/concern'
|
2
2
|
module SeemsRateable
|
3
|
-
|
4
|
-
|
3
|
+
module Model
|
4
|
+
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
6
|
+
def rate(stars, user_id, dimension=nil)
|
7
|
+
if !has_rated?(user_id, dimension)
|
8
|
+
self.rates.create do |r|
|
9
|
+
r.stars = stars
|
10
|
+
r.rater_id = user_id
|
11
|
+
end
|
12
|
+
update_overall_average_rating(stars, dimension)
|
13
|
+
elsif has_rated?(user_id, dimension) && can_update?
|
14
|
+
update_users_rating(stars, user_id, dimension)
|
15
|
+
else
|
16
|
+
raise Errors::AlreadyRatedError
|
17
|
+
end
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
20
|
+
def update_overall_average_rating(stars, dimension=nil)
|
21
|
+
if average(dimension).nil?
|
22
|
+
CachedRating.create do |r|
|
23
|
+
r.avg = stars
|
24
|
+
r.dimension = dimension
|
25
|
+
r.cacheable_id = self.id
|
26
|
+
r.cacheable_type = self.class.name
|
27
|
+
r.cnt = 1
|
28
|
+
end
|
29
|
+
else
|
30
|
+
r = average(dimension)
|
31
|
+
r.avg = (r.avg * r.cnt + stars) / (r.cnt+1)
|
32
|
+
r.cnt += 1
|
33
|
+
r.save!
|
34
|
+
end
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
def update_users_rating(stars, user_id, dimension=nil)
|
38
|
+
obj = rates(dimension).where(:rater_id => user_id).first
|
39
|
+
current_record = average(dimension)
|
40
|
+
current_record.avg = (current_record.avg*current_record.cnt - obj.stars + stars) / (current_record.cnt)
|
41
|
+
current_record.save!
|
42
|
+
obj.stars = stars
|
43
|
+
obj.save!
|
44
|
+
end
|
45
45
|
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
def average(dimension=nil)
|
48
|
+
if dimension.nil?
|
49
|
+
self.send "rate_average_without_dimension"
|
50
|
+
else
|
51
|
+
self.send "#{dimension}_average"
|
52
|
+
end
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
def rates(dimension=nil)
|
56
|
+
if dimension.nil?
|
57
|
+
self.send "rates_without_dimension"
|
58
|
+
else
|
59
|
+
self.send "#{dimension}_rates"
|
60
|
+
end
|
61
|
+
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
63
|
+
def raters(dimension=nil)
|
64
|
+
if dimension.nil?
|
65
|
+
self.send "raters_without_dimension"
|
66
|
+
else
|
67
|
+
self.send "#{dimension}_raters"
|
68
|
+
end
|
69
|
+
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
71
|
+
def has_rated?(user_id, dimension=nil)
|
72
|
+
record = self.rates(dimension).where(:rater_id => user_id)
|
73
|
+
record.empty? ? false : true
|
74
|
+
end
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
76
|
+
def can_update?
|
77
|
+
self.class.can_update?
|
78
|
+
end
|
79
79
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
80
|
+
module ClassMethods
|
81
|
+
def seems_rateable(opts={})
|
82
|
+
#has_many :rates_without_dimension, -> { where(dimension: nil) }, :as => :rateable, :class_name => SeemsRateable::Rate, :dependent => :destroy
|
83
|
+
has_many :rates_without_dimension, :conditions => { dimension: nil }, :as => :rateable, :class_name => SeemsRateable::Rate, :dependent => :destroy
|
84
|
+
has_many :raters_without_dimension, :through => :rates_without_dimension, :source => :rater
|
85
|
+
has_one :rate_average_without_dimension, :conditions => { dimension: nil }, :as => :cacheable, :class_name => SeemsRateable::CachedRating, :dependent => :destroy
|
86
|
+
|
87
|
+
@permission = opts[:allow_update] ? true : false
|
88
|
+
|
89
|
+
def self.can_update?
|
90
|
+
@permission
|
91
|
+
end
|
92
92
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
93
|
+
def self.rateable?
|
94
|
+
true
|
95
|
+
end
|
96
|
+
|
97
|
+
if opts[:dimensions].is_a?(Array)
|
98
|
+
opts[:dimensions].each do |dimension|
|
99
|
+
has_many :"#{dimension}_rates", :conditions => { dimension: dimension.to_s }, :dependent => :destroy, :class_name => SeemsRateable::Rate, :as => :rateable
|
100
|
+
has_many :"#{dimension}_raters", :through => :"#{dimension}_rates", :source => :rater
|
101
|
+
has_one :"#{dimension}_average", :conditions => { dimension: dimension.to_s }, :as => :cacheable, :class_name => SeemsRateable::CachedRating, :dependent => :destroy
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
105
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
106
|
+
def seems_rateable_rater
|
107
|
+
has_many :ratings_given, :class_name => SeemsRateable::Rate, :foreign_key => :rater_id
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
111
|
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.10
|
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-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|