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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e273b87f76a38f6c21e25fa78863bcc11f6233ca
4
- data.tar.gz: 93b78f7f92bf44079c01aba93cb64304b46421cb
3
+ metadata.gz: 75166b76cc03e894fbb7d2d63cd807acdafc4816
4
+ data.tar.gz: d8353a22a2e5cfa5ddba077445e4900d3dd4afe3
5
5
  SHA512:
6
- metadata.gz: 0112a624f4a7734ec39753748b0775e5759a99ab8ab30fa5805470ccae382d44d3ee706cf0b478851b2792955125268256a5af60e8979d53db4ae0daf96d6ccc
7
- data.tar.gz: 1691a6b51152d14b098cb8f856881795e7428359d744905430b2b732a0e2179a6ed804a20ba929b7a3a4a1e1733f41543adc2b1ee163c623ed8722874344b21d
6
+ metadata.gz: f6c2b82a4cee9ac55e02150db1142bc49d4f0c1cf00d7fa06e5c4f8e389b21cc090b5f5289d11ae240038cb140ccae116a6e4b1e82e468775fe0d55e9380b0f2
7
+ data.tar.gz: 9a250d496faf719be8e82823536e4ea25774f5ba8c688274e3640261d864f30d1384c9b0310d6453a4c088f8ad742a0ef1a5ba864e8543808efc90632f79ddbb
@@ -20,43 +20,43 @@
20
20
 
21
21
 
22
22
  .jRatingAverage {
23
- background-color:#f62929;
24
- position:relative;
25
- top:0;
26
- left:0;
27
- z-index:2;
28
- height:100%;
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
- background-color:#FFD400; /* bgcolor of the stars*/
32
- position:relative;
33
- top:0;
34
- left:0;
35
- z-index:2;
36
- height:100%;
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
- position:relative;
42
- left:0;
43
- z-index:3;
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
- 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;
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
- color:#c9c9c9;
61
- font-size:14px;
60
+ color:#c9c9c9;
61
+ font-size:14px;
62
62
  }
@@ -1,7 +1,7 @@
1
1
  module SeemsRateable
2
- class ApplicationController < ::ApplicationController
3
- rescue_from SeemsRateable::Errors::AlreadyRatedError do |exception|
4
- render :json => {:error => true}
5
- end
6
- end
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
- 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
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,5 +1,5 @@
1
1
  module SeemsRateable
2
- class CachedRating < ActiveRecord::Base
3
- belongs_to :cacheable, :polymorphic => true
4
- end
2
+ class CachedRating < ActiveRecord::Base
3
+ belongs_to :cacheable, :polymorphic => true
4
+ end
5
5
  end
@@ -1,6 +1,6 @@
1
1
  module SeemsRateable
2
- class Rate < ActiveRecord::Base
3
- belongs_to :rater, :class_name => SeemsRateable::Engine.config.owner_class
4
- belongs_to :rateable, :polymorphic => true
5
- end
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
@@ -1,3 +1,3 @@
1
1
  SeemsRateable::Engine.routes.draw do
2
- resources :ratings, :only => :create
2
+ resources :ratings, :only => :create
3
3
  end
@@ -2,41 +2,41 @@ require 'rails/generators/migration'
2
2
  require 'fileutils'
3
3
 
4
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
- 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
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
- 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
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
- def self.down
15
- drop_table :cached_ratings
16
- end
14
+ def self.down
15
+ drop_table :cached_ratings
16
+ end
17
17
  end
@@ -1,5 +1,5 @@
1
1
  begin
2
- require 'rails'
2
+ require 'rails'
3
3
  rescue LoadError
4
4
  end
5
5
 
@@ -1,16 +1,16 @@
1
1
  module SeemsRateable
2
- class Engine < ::Rails::Engine
3
- isolate_namespace SeemsRateable
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SeemsRateable
4
4
 
5
- config.generators do |g|
6
- g.test_framework :rspec, :fixture => false
7
- g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
- end
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
- 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
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
- 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
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
- 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
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
@@ -1,112 +1,112 @@
1
1
  require 'active_support/concern'
2
2
 
3
3
  module SeemsRateable
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
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
- 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
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
@@ -1,7 +1,7 @@
1
1
  module SeemsRateable
2
- module Routes
3
- def seems_rateable
4
- mount SeemsRateable::Engine => '/rateable', :as => :rateable
5
- end
6
- end
2
+ module Routes
3
+ def seems_rateable
4
+ mount SeemsRateable::Engine => '/rateable', :as => :rateable
5
+ end
6
+ end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module SeemsRateable
2
- VERSION = "1.0.11"
2
+ VERSION = "1.0.12"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seems_rateable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.11
4
+ version: 1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Toth