acts_as_nps_rateable 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDE0YTQ5NWUwNjdkMjFmM2YyYzM3ZjdjYzU5MTk5OTQ0ODM5ODM4Mg==
5
+ data.tar.gz: !binary |-
6
+ Yjk5MmEyMjE1ODVmZDJmMDk3YTYxM2VmNTM3MTAxNzA5NzdkNTNlMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OGI3YmVlNmNmM2I2Mzc1NDczMjJmYmYwMTUwMGRkM2E0ZjMxNTRkY2MxMGI5
10
+ NmI5YmYzN2NhYmQ2OGQwZmIzY2U3ODgxNGZkMjk0MjhiYTc3YmNmODkxYTcz
11
+ MTg0NDBhN2ExNzQ4ZGZjZjA3OWVkZmUxZjJkMDMyZmIyNDNkN2Y=
12
+ data.tar.gz: !binary |-
13
+ ZTU4YzNmY2M2ZmJjODY4NmNhYWUyYWUxNjUxOTM1OGFjMWI3OTFlOGZhYTY2
14
+ OGExY2I2ZTkyZDlmNTkwMDRkMmE4MjFjOTZiNDhkMWMwZWIyMWIzMzQ3N2Y2
15
+ NjljOGY3MTU5YzVjMmE5ZWE0MTU4YzBkYjkyYjU2ODUyYTM4MTE=
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ acts_as_nps_rateable
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p194
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_nps_rateable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Shahbaz Javeed
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ActsAsNpsRateable
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_nps_rateable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_nps_rateable
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_nps_rateable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'acts_as_nps_rateable'
8
+ gem.version = ActsAsNpsRateable::VERSION
9
+ gem.authors = ['Shahbaz Javeed']
10
+ gem.email = ['sjaveed@gmail.com']
11
+ gem.description = %q{Rails gem that provides Net Promoter Score (NPS) ratings and analysis for ActiveRecord models. It can be used as a regular 0 to 10 rating scale and you can just ignore the NPS analysis methods.}
12
+ gem.summary = %q{Net Promoter Score ratings and analysis for ActiveRecord models}
13
+ gem.homepage = 'https://github.com/sjaveed/acts_as_nps_rateable'
14
+
15
+ gem.add_dependency 'activerecord', '~> 3.0'
16
+ gem.add_dependency 'rails', '~> 3.0'
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ['lib']
22
+ end
@@ -0,0 +1,8 @@
1
+ require File.join(File.dirname(__FILE__), "acts_as_nps_rateable/railtie")
2
+
3
+ module ActsAsNpsRateable
4
+ autoload :Hook, File.join(File.dirname(__FILE__), "acts_as_nps_rateable/hook")
5
+ autoload :InstanceMethods, File.join(File.dirname(__FILE__), "acts_as_nps_rateable/instance_methods")
6
+ end
7
+
8
+ require 'nps_rating'
@@ -0,0 +1,10 @@
1
+ # This is the hook that implements the acts_as_nps_rateable method
2
+ # and includes all acts_as_nps_rateable instance methods as needed
3
+
4
+ module ActsAsNpsRateable::Hook
5
+ def acts_as_nps_rateable
6
+ has_many :nps_ratings, :as => :nps_rateable, class_name: 'ActsAsNpsRateable::NpsRating', :dependent => :destroy
7
+
8
+ include ActsAsNpsRateable::InstanceMethods
9
+ end
10
+ end
@@ -0,0 +1,63 @@
1
+ # These are all the instance methods that acts_as_nps_rateable includes into an ActiveModel
2
+
3
+ module ActsAsNpsRateable::InstanceMethods
4
+ # rate this rateable ensuring there's at most only one rating per user per rateable
5
+ def rate(score, user)
6
+ ActiveRecord::Base.transaction do
7
+ nps_ratings.where(user_id: user.id).delete_all if user.present?
8
+ nps_ratings.create(score: score.to_i, user_id: user.id)
9
+ end
10
+ end
11
+
12
+ # the average of all ratings for this rateable
13
+ def average_rating
14
+ nps_ratings.average(:score)
15
+ end
16
+
17
+ # a check to see if the specified user has already rated this rateable
18
+ def rated_by?(user)
19
+ return unless user.present?
20
+
21
+ nps_ratings.where(user_id: user.id).size > 0
22
+ end
23
+
24
+ #
25
+ # Net Promoter Score Calculations: http://www.checkmarket.com/2011/06/net-promoter-score/
26
+ #
27
+
28
+ def promoters ratings = nil
29
+ if ratings.nil?
30
+ nps_ratings.promoters.size
31
+ else
32
+ ratings.promoters.size
33
+ end
34
+ end
35
+
36
+ def passives ratings = nil
37
+ if ratings.nil?
38
+ nps_ratings.passives.size
39
+ else
40
+ ratings.passives.size
41
+ end
42
+ end
43
+
44
+ def detractors ratings = nil
45
+ if ratings.nil?
46
+ nps_ratings.detractors.size
47
+ else
48
+ ratings.detractors.size
49
+ end
50
+ end
51
+
52
+ def net_promoter_score ratings = nil
53
+ total_ratings = ratings.nil? ? nps_ratings.size : ratings.size
54
+ return 0 if total_ratings == 0
55
+
56
+ if ratings.nil?
57
+ (promoters - detractors) * 100 / total_ratings
58
+ else
59
+ (promoters(ratings) - detractors(ratings)) * 100 / total_ratings
60
+ end
61
+ end
62
+
63
+ end
@@ -0,0 +1,8 @@
1
+ module ActsAsNpsRateable
2
+ class Railtie < Rails::Railtie
3
+ # Extend ActiveRecord::Base here and include ActsAsNpsRateable instance methods
4
+ config.to_prepare do
5
+ ActiveRecord::Base.send(:extend, ActsAsNpsRateable::Hook)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsNpsRateable
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module ActsAsNpsRateable
4
+ class InstallGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def self.next_migration_number(path)
10
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
11
+ end
12
+
13
+ def create_migration
14
+ migration_template '01-migration.rb', 'db/migrate/acts_as_nps_rateable_migration'
15
+ migration_template '02-migration.rb', 'db/migrate/acts_as_nps_rateable_migration_upgrade_0_0_2'
16
+ end
17
+
18
+ def install_models
19
+ #template 'nps_rating.rb', 'app/models/nps_rating.rb'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class ActsAsNpsRateableMigration < ActiveRecord::Migration
2
+ def change
3
+ create_table :nps_ratings do |t|
4
+ t.integer :score
5
+ t.integer :nps_rateable_id
6
+ t.string :nps_rateable_type
7
+ t.integer :user_id
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_index :nps_ratings, [:nps_rateable_type, :nps_rateable_id, :user_id], unique: true, name: "acts_as_nps_rateable_unique_index"
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ class ActsAsNpsRateableMigration < ActiveRecord::Migration
2
+ def change
3
+ add_column :nps_ratings, :comments, :text
4
+ end
5
+ end
data/lib/nps_rating.rb ADDED
@@ -0,0 +1,41 @@
1
+ module ActsAsNpsRateable
2
+ class NpsRating < ActiveRecord::Base
3
+ self.table_name = 'nps_ratings'
4
+
5
+ belongs_to :nps_rateable, polymorphic: true
6
+ belongs_to :user
7
+
8
+ validates_presence_of :score
9
+ validates_numericality_of :score, only_integer: true
10
+ validates_inclusion_of :score, in: (0..10).to_a, message: 'must be between 0 and 10'
11
+
12
+ validates_presence_of :nps_rateable_id
13
+ validates_presence_of :nps_rateable_type
14
+
15
+ validates_presence_of :user_id
16
+ validates_uniqueness_of :user_id, scope: [:nps_rateable_type, :nps_rateable_id], message: 'has already rated'
17
+
18
+ before_save :remove_blank_comments
19
+
20
+ scope :promoters, where(score: [9, 10])
21
+ scope :passives, where(score: [7, 8])
22
+ scope :detractors, where('score <= 6')
23
+ scope :with_comments, where('comments IS NOT NULL')
24
+
25
+ def self.calculate_for relevant_ratings
26
+ total_ratings = relevant_ratings.size
27
+ return 0 if total_ratings == 0
28
+
29
+ promoters = relevant_ratings.promoters.size
30
+ detractors = relevant_ratings.detractors.size
31
+
32
+ ((promoters - detractors) * 100.0 / total_ratings).round
33
+ end
34
+
35
+ private
36
+
37
+ def remove_blank_comments
38
+ write_attribute(:comments, nil) if comments.blank?
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_nps_rateable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Shahbaz Javeed
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Rails gem that provides Net Promoter Score (NPS) ratings and analysis
42
+ for ActiveRecord models. It can be used as a regular 0 to 10 rating scale and you
43
+ can just ignore the NPS analysis methods.
44
+ email:
45
+ - sjaveed@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - .gitignore
51
+ - .ruby-gemset
52
+ - .ruby-version
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - acts_as_nps_rateable.gemspec
58
+ - lib/acts_as_nps_rateable.rb
59
+ - lib/acts_as_nps_rateable/hook.rb
60
+ - lib/acts_as_nps_rateable/instance_methods.rb
61
+ - lib/acts_as_nps_rateable/railtie.rb
62
+ - lib/acts_as_nps_rateable/version.rb
63
+ - lib/generators/acts_as_nps_rateable/install_generator.rb
64
+ - lib/generators/acts_as_nps_rateable/templates/01-migration.rb
65
+ - lib/generators/acts_as_nps_rateable/templates/02-migration.rb
66
+ - lib/nps_rating.rb
67
+ homepage: https://github.com/sjaveed/acts_as_nps_rateable
68
+ licenses: []
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.0.6
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Net Promoter Score ratings and analysis for ActiveRecord models
90
+ test_files: []