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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0eb8a1e5c55ccaff4c140702bdd45ae130ab3adb
4
- data.tar.gz: 6e6172c3197be41dafd85972be9a6a453bf82e5e
3
+ metadata.gz: e92afbf1216b55fc3629fd9dc830ace41d6f74e5
4
+ data.tar.gz: 05bcffa91c9f3283436c050b4e4947db993f54d6
5
5
  SHA512:
6
- metadata.gz: fcc9344aa5b330b35c220208a910929f3396a8cf579e4a56e63bc9d0c6dff2bb64b371135a23478ad6b0cd9dab1f7de1743eb32d91a0426762d9d1f8cd5a27ca
7
- data.tar.gz: fc8d7526d7726c2b16f07b72f7e2794a6a716f4b945e92313c522812dc6c3c683c49b18db28aaface6fbd4159d4967a9ff654fdcba563b0adace5c40776a50d8
6
+ metadata.gz: 6b0c3a1a4e6eabe2f23b18463a33c122f654bd0449656bc8af3f2d0073878427fdae9016de8b5f47b67a747c302b78da664b61c2fe0ee60bbac509bac70e0544
7
+ data.tar.gz: 68e21d5a5865df84608105641c80b2434705e2c92fa7e941ec5f6eee854b0cff3334aa9de5a6f1547a3c2f47216d04ee8cf44d459c415af6b2fac13edabde7e1
@@ -1,4 +1,7 @@
1
1
  module SeemsRateable
2
- class ApplicationController < ActionController::Base
2
+ class ApplicationController < ::ApplicationController
3
+ rescue_from SeemsRateable::Errors::AlreadyRatedError do |exception|
4
+ render :json => {:error => true}
3
5
  end
6
+ end
4
7
  end
@@ -1,17 +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
-
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
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,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
@@ -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,38 +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
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
- 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
19
+ def routegen
20
+ route("seems_rateable")
21
+ end
24
22
 
25
- desc "generating initializer"
26
- def copy_initializer
27
- template "initializer.rb", "config/initializers/seems_rateable.rb"
28
- end
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
- 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
29
+ desc "generating initializer"
30
+ def copy_initializer
31
+ template "initializer.rb", "config/initializers/seems_rateable.rb"
32
+ end
36
33
 
37
- end
38
- end
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 : '<%= 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
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
- 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
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
- 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
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
- class NoCurrentUserInstanceError < StandardError
10
- def to_s
11
- "User instance current_user is not available."
12
- end
13
- end
9
+ class NoCurrentUserInstanceError < StandardError
10
+ def to_s
11
+ "User instance current_user is not available."
12
+ end
13
+ end
14
14
 
15
- class AlreadyRatedError < StandardError
16
- def to_s
17
- "User has already rated an object."
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
- module Helpers
3
- def rating_for(obj, opts={})
4
- raise Errors::InvalidRateableObjectError unless obj.class.respond_to?(:rateable?)
2
+ module Helpers
3
+ def rating_for(obj, opts={})
4
+ raise Errors::InvalidRateableObjectError unless obj.class.respond_to?(:rateable?)
5
5
 
6
- options = {
7
- :dimension => nil,
8
- :static => false,
9
- :class => 'rateable',
10
- :id => nil
11
- }.update(opts)
6
+ options = {
7
+ :dimension => nil,
8
+ :static => false,
9
+ :class => 'rateable',
10
+ :id => nil
11
+ }.update(opts)
12
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
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
- 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
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,111 +1,111 @@
1
1
  require 'active_support/concern'
2
2
  module SeemsRateable
3
- module Model
4
- extend ActiveSupport::Concern
3
+ module Model
4
+ extend ActiveSupport::Concern
5
5
 
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
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
- 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
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
- 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
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
- 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
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
- 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
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
- 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
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
- def has_rated?(user_id, dimension=nil)
72
- record = self.rates(dimension).where(:rater_id => user_id)
73
- record.empty? ? false : true
74
- end
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
- def can_update?
77
- self.class.can_update?
78
- end
76
+ def can_update?
77
+ self.class.can_update?
78
+ end
79
79
 
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
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
- 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
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
- def seems_rateable_rater
107
- has_many :ratings_given, :class_name => SeemsRateable::Rate, :foreign_key => :rater_id
108
- end
109
- end
110
- end
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
@@ -1,7 +1,7 @@
1
1
  module SeemsRateable
2
- module Routes
3
- def seems_rateable
4
- mount SeemsRateable::Engine => '/rateable', :as => :rateable
5
- end
2
+ module Routes
3
+ def seems_rateable
4
+ mount SeemsRateable::Engine => '/rateable', :as => :rateable
6
5
  end
6
+ end
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module SeemsRateable
2
- VERSION = "1.0.9"
2
+ VERSION = "1.0.10"
3
3
  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.9
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-07-21 00:00:00.000000000 Z
11
+ date: 2013-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails