devise_user_metering 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 235ac1530efa4f108ed266bc062097c61c1eb5ba
4
+ data.tar.gz: 9f2705da414110def3942b74846971a48c1ff1ea
5
+ SHA512:
6
+ metadata.gz: 3154a5e58bafcd8ecabf4717d0dacdf174f92ae60c3a8e386cbe45001890b42d6060586b1fd60cc37487527d7549f49c524c64e3f2f2d0897d91ce9737939f9c
7
+ data.tar.gz: 52ca71e1d748802a99da12e7cf17a4517cfcb55b3558d4d735e8635143ed81afe727a6908c795a3c6ef3aa81a2a1d155d0ef0508721d4e08f8aa5ece8e85541b
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'rails', ">= 3.0.4"
7
+ gem 'devise'
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "shoulda", ">= 0"
13
+ gem "rdoc", "~> 3.12"
14
+ gem "bundler", "~> 1.0"
15
+ gem "jeweler", "~> 2.0.1"
16
+ gem "simplecov", ">= 0"
17
+ gem "timecop"
18
+ gem 'test-unit'
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,175 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (4.2.3)
5
+ actionpack (= 4.2.3)
6
+ actionview (= 4.2.3)
7
+ activejob (= 4.2.3)
8
+ mail (~> 2.5, >= 2.5.4)
9
+ rails-dom-testing (~> 1.0, >= 1.0.5)
10
+ actionpack (4.2.3)
11
+ actionview (= 4.2.3)
12
+ activesupport (= 4.2.3)
13
+ rack (~> 1.6)
14
+ rack-test (~> 0.6.2)
15
+ rails-dom-testing (~> 1.0, >= 1.0.5)
16
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
17
+ actionview (4.2.3)
18
+ activesupport (= 4.2.3)
19
+ builder (~> 3.1)
20
+ erubis (~> 2.7.0)
21
+ rails-dom-testing (~> 1.0, >= 1.0.5)
22
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
23
+ activejob (4.2.3)
24
+ activesupport (= 4.2.3)
25
+ globalid (>= 0.3.0)
26
+ activemodel (4.2.3)
27
+ activesupport (= 4.2.3)
28
+ builder (~> 3.1)
29
+ activerecord (4.2.3)
30
+ activemodel (= 4.2.3)
31
+ activesupport (= 4.2.3)
32
+ arel (~> 6.0)
33
+ activesupport (4.2.3)
34
+ i18n (~> 0.7)
35
+ json (~> 1.7, >= 1.7.7)
36
+ minitest (~> 5.1)
37
+ thread_safe (~> 0.3, >= 0.3.4)
38
+ tzinfo (~> 1.1)
39
+ addressable (2.3.8)
40
+ arel (6.0.2)
41
+ bcrypt (3.1.10)
42
+ builder (3.2.2)
43
+ descendants_tracker (0.0.4)
44
+ thread_safe (~> 0.3, >= 0.3.1)
45
+ devise (3.5.1)
46
+ bcrypt (~> 3.0)
47
+ orm_adapter (~> 0.1)
48
+ railties (>= 3.2.6, < 5)
49
+ responders
50
+ thread_safe (~> 0.1)
51
+ warden (~> 1.2.3)
52
+ docile (1.1.5)
53
+ erubis (2.7.0)
54
+ faraday (0.9.1)
55
+ multipart-post (>= 1.2, < 3)
56
+ git (1.2.9.1)
57
+ github_api (0.12.3)
58
+ addressable (~> 2.3)
59
+ descendants_tracker (~> 0.0.4)
60
+ faraday (~> 0.8, < 0.10)
61
+ hashie (>= 3.3)
62
+ multi_json (>= 1.7.5, < 2.0)
63
+ nokogiri (~> 1.6.3)
64
+ oauth2
65
+ globalid (0.3.5)
66
+ activesupport (>= 4.1.0)
67
+ hashie (3.4.2)
68
+ highline (1.7.2)
69
+ i18n (0.7.0)
70
+ jeweler (2.0.1)
71
+ builder
72
+ bundler (>= 1.0)
73
+ git (>= 1.2.5)
74
+ github_api
75
+ highline (>= 1.6.15)
76
+ nokogiri (>= 1.5.10)
77
+ rake
78
+ rdoc
79
+ json (1.8.3)
80
+ jwt (1.5.1)
81
+ loofah (2.0.2)
82
+ nokogiri (>= 1.5.9)
83
+ mail (2.6.3)
84
+ mime-types (>= 1.16, < 3)
85
+ mime-types (2.6.1)
86
+ mini_portile (0.6.2)
87
+ minitest (5.7.0)
88
+ multi_json (1.11.2)
89
+ multi_xml (0.5.5)
90
+ multipart-post (2.0.0)
91
+ nokogiri (1.6.6.2)
92
+ mini_portile (~> 0.6.0)
93
+ oauth2 (1.0.0)
94
+ faraday (>= 0.8, < 0.10)
95
+ jwt (~> 1.0)
96
+ multi_json (~> 1.3)
97
+ multi_xml (~> 0.5)
98
+ rack (~> 1.2)
99
+ orm_adapter (0.5.0)
100
+ power_assert (0.2.2)
101
+ rack (1.6.4)
102
+ rack-test (0.6.3)
103
+ rack (>= 1.0)
104
+ rails (4.2.3)
105
+ actionmailer (= 4.2.3)
106
+ actionpack (= 4.2.3)
107
+ actionview (= 4.2.3)
108
+ activejob (= 4.2.3)
109
+ activemodel (= 4.2.3)
110
+ activerecord (= 4.2.3)
111
+ activesupport (= 4.2.3)
112
+ bundler (>= 1.3.0, < 2.0)
113
+ railties (= 4.2.3)
114
+ sprockets-rails
115
+ rails-deprecated_sanitizer (1.0.3)
116
+ activesupport (>= 4.2.0.alpha)
117
+ rails-dom-testing (1.0.6)
118
+ activesupport (>= 4.2.0.beta, < 5.0)
119
+ nokogiri (~> 1.6.0)
120
+ rails-deprecated_sanitizer (>= 1.0.1)
121
+ rails-html-sanitizer (1.0.2)
122
+ loofah (~> 2.0)
123
+ railties (4.2.3)
124
+ actionpack (= 4.2.3)
125
+ activesupport (= 4.2.3)
126
+ rake (>= 0.8.7)
127
+ thor (>= 0.18.1, < 2.0)
128
+ rake (10.4.2)
129
+ rdoc (3.12.2)
130
+ json (~> 1.4)
131
+ responders (2.1.0)
132
+ railties (>= 4.2.0, < 5)
133
+ shoulda (3.5.0)
134
+ shoulda-context (~> 1.0, >= 1.0.1)
135
+ shoulda-matchers (>= 1.4.1, < 3.0)
136
+ shoulda-context (1.2.1)
137
+ shoulda-matchers (2.8.0)
138
+ activesupport (>= 3.0.0)
139
+ simplecov (0.10.0)
140
+ docile (~> 1.1.0)
141
+ json (~> 1.8)
142
+ simplecov-html (~> 0.10.0)
143
+ simplecov-html (0.10.0)
144
+ sprockets (3.2.0)
145
+ rack (~> 1.0)
146
+ sprockets-rails (2.3.2)
147
+ actionpack (>= 3.0)
148
+ activesupport (>= 3.0)
149
+ sprockets (>= 2.8, < 4.0)
150
+ test-unit (3.0.8)
151
+ power_assert
152
+ thor (0.19.1)
153
+ thread_safe (0.3.5)
154
+ timecop (0.7.4)
155
+ tzinfo (1.2.2)
156
+ thread_safe (~> 0.1)
157
+ warden (1.2.3)
158
+ rack (>= 1.0)
159
+
160
+ PLATFORMS
161
+ ruby
162
+
163
+ DEPENDENCIES
164
+ bundler (~> 1.0)
165
+ devise
166
+ jeweler (~> 2.0.1)
167
+ rails (>= 3.0.4)
168
+ rdoc (~> 3.12)
169
+ shoulda
170
+ simplecov
171
+ test-unit
172
+ timecop
173
+
174
+ BUNDLED WITH
175
+ 1.10.4
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Mike Ihbe
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = devise_user_metering
2
+
3
+ This is a devise extension that is useful for billing customers based on number of active users.
4
+ It calculates prorated active users in a live system and does not save historical data.
5
+
6
+ == Contributing to devise_user_metering
7
+
8
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
9
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
10
+ * Fork the project.
11
+ * Start a feature/bugfix branch.
12
+ * Commit and push until you are happy with your contribution.
13
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
14
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2015 Mike Ihbe. See LICENSE.txt for
19
+ further details.
20
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ require "#{File.dirname(__FILE__)}/lib/devise_user_metering/version"
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
18
+ gem.name = "devise_user_metering"
19
+ gem.homepage = "http://github.com/MustWin/devise_user_metering"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Add methods to devise User models that account for active time during a month. Useful for SAAS billings}
22
+ gem.description = %Q{Add methods to devise User models that account for active time during a month. Useful for SAAS billings}
23
+ gem.email = "we@mustwin.com"
24
+ gem.authors = ["Mike Ihbe"]
25
+ gem.version = DeviseUserMetering::Version::VERSION
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ desc "Code coverage detail"
38
+ task :simplecov do
39
+ ENV['COVERAGE'] = "true"
40
+ Rake::Task['test'].execute
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "devise_user_metering #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
@@ -0,0 +1,79 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: devise_user_metering 0.0.1 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "devise_user_metering"
9
+ s.version = "0.0.1"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Mike Ihbe"]
14
+ s.date = "2015-07-29"
15
+ s.description = "Add methods to devise User models that account for active time during a month. Useful for SAAS billings"
16
+ s.email = "we@mustwin.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "devise_user_metering.gemspec",
29
+ "lib/devise_user_metering.rb",
30
+ "lib/devise_user_metering/model.rb",
31
+ "lib/devise_user_metering/rails.rb",
32
+ "lib/devise_user_metering/version.rb",
33
+ "lib/generators/devise_user_metering/devise_user_metering_generator.rb",
34
+ "lib/generators/devise_user_metering/templates/migration.rb",
35
+ "test/helper.rb",
36
+ "test/test_devise_user_metering.rb"
37
+ ]
38
+ s.homepage = "http://github.com/MustWin/devise_user_metering"
39
+ s.licenses = ["MIT"]
40
+ s.rubygems_version = "2.4.8"
41
+ s.summary = "Add methods to devise User models that account for active time during a month. Useful for SAAS billings"
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 4
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.4"])
48
+ s.add_runtime_dependency(%q<devise>, [">= 0"])
49
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
50
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
52
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
53
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
54
+ s.add_development_dependency(%q<timecop>, [">= 0"])
55
+ s.add_development_dependency(%q<test-unit>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<rails>, [">= 3.0.4"])
58
+ s.add_dependency(%q<devise>, [">= 0"])
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
61
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
63
+ s.add_dependency(%q<simplecov>, [">= 0"])
64
+ s.add_dependency(%q<timecop>, [">= 0"])
65
+ s.add_dependency(%q<test-unit>, [">= 0"])
66
+ end
67
+ else
68
+ s.add_dependency(%q<rails>, [">= 3.0.4"])
69
+ s.add_dependency(%q<devise>, [">= 0"])
70
+ s.add_dependency(%q<shoulda>, [">= 0"])
71
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
72
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
73
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
74
+ s.add_dependency(%q<simplecov>, [">= 0"])
75
+ s.add_dependency(%q<timecop>, [">= 0"])
76
+ s.add_dependency(%q<test-unit>, [">= 0"])
77
+ end
78
+ end
79
+
@@ -0,0 +1,53 @@
1
+ #require 'devise_user_metering/hooks/user_metering'
2
+ #
3
+ module Devise
4
+ module Models
5
+ module UserMetering
6
+ # This function returns a decimal between 0 and 1 that reflects the amount of the month this user has been 'active'
7
+ def active_proportion_of_month(time)
8
+ month = time.beginning_of_month
9
+ end_month = time.end_of_month
10
+
11
+ if end_month > Time.now
12
+ raise StandardError.new("You can't get meter data for incomplete months")
13
+ end
14
+ if month < self.activated_at.beginning_of_month
15
+ raise StandardError.new("No usage data retained for that month")
16
+ end
17
+
18
+ in_month = ->(time) { (month..end_month).cover?(time) }
19
+ if in_month.call(self.activated_at) || in_month.call(self.deactivated_at)
20
+ if !active && self.deactivated_at < month
21
+ return 0
22
+ end
23
+ month_duration = end_month - month
24
+ remainder = self.active ? [end_month - self.activated_at, 0].max : 0
25
+ (remainder + self.rollover_active_duration) / month_duration
26
+ else
27
+ self.active ? 1 : 0
28
+ end
29
+
30
+ end
31
+
32
+ def activate!
33
+ self.activated_at = Time.new
34
+ self.active = true
35
+ self.save!
36
+ end
37
+
38
+ def deactivate!
39
+ now = Time.new
40
+ self.deactivated_at = now
41
+ self.active = false
42
+ self.rollover_active_duration += now - [self.activated_at, now.beginning_of_month].max
43
+ self.save!
44
+ end
45
+
46
+ def billed!
47
+ self.rollover_active_duration = 0
48
+ self.save!
49
+ end
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,7 @@
1
+ require 'devise_user_metering'
2
+
3
+ module DeviseUserMetering
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+
@@ -0,0 +1,6 @@
1
+ module DeviseUserMetering
2
+ module Version
3
+ VERSION = "0.0.1".freeze
4
+ end
5
+ end
6
+
@@ -0,0 +1,13 @@
1
+ unless defined?(Devise)
2
+ require 'devise'
3
+ end
4
+ require 'devise_user_metering'
5
+ require 'devise_user_metering/model'
6
+
7
+ Devise.add_module :user_metering, :model => 'devise_user_metering/model'
8
+
9
+ module DeviseUserMetering
10
+ end
11
+
12
+ require 'devise_user_metering/rails'
13
+
@@ -0,0 +1,32 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class DeviseLastseenableGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @_devise_source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def self.orm_has_migration?
11
+ Rails::Generators.options[:rails][:orm] == :active_record
12
+ end
13
+
14
+ def self.next_migration_number(dirname)
15
+ if ActiveRecord::Base.timestamped_migrations
16
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
17
+ else
18
+ "%.3d" % (current_migration_number(dirname) + 1)
19
+ end
20
+ end
21
+
22
+ class_option :orm
23
+ class_option :migration, :type => :boolean, :default => orm_has_migration?
24
+
25
+
26
+ def create_migration_file
27
+ migration_template 'migration.rb', "db/migrate/devise_add_user_metering_#{name.downcase}.rb"
28
+ end
29
+
30
+ protected
31
+ end
32
+
@@ -0,0 +1,18 @@
1
+ class DeviseAddLastseenable<%= table_name.camelize.singularize %> < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :<%= table_name %>, :active, :boolean, default: true
4
+ add_column :<%= table_name %>, :activated_at, :datetime
5
+ add_column :<%= table_name %>, :deactivated_at, :datetime
6
+ add_column :<%= table_name %>, :rollover_active_duration, :int
7
+ table_name.classify.find_each do |user|
8
+ user.update_attribute(:activated_at, user.created_at)
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ remove_column :<%= table_name %>, :active, :boolean
14
+ remove_column :<%= table_name %>, :activated_at, :datetime
15
+ remove_column :<%= table_name %>, :rollover_active_duration, :int
16
+ end
17
+ end
18
+
data/test/helper.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'simplecov'
2
+ require 'devise'
3
+ require 'pry-byebug'
4
+
5
+
6
+ module SimpleCov::Configuration
7
+ def clean_filters
8
+ @filters = []
9
+ end
10
+ end
11
+
12
+ SimpleCov.configure do
13
+ clean_filters
14
+ load_adapter 'test_frameworks'
15
+ end
16
+
17
+ ENV["COVERAGE"] && SimpleCov.start do
18
+ add_filter "/.rvm/"
19
+ end
20
+ require 'rubygems'
21
+ require 'bundler'
22
+ begin
23
+ Bundler.setup(:default, :development)
24
+ rescue Bundler::BundlerError => e
25
+ $stderr.puts e.message
26
+ $stderr.puts "Run `bundle install` to install missing gems"
27
+ exit e.status_code
28
+ end
29
+ require 'test/unit'
30
+ require 'shoulda'
31
+ require 'timecop'
32
+
33
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
34
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
35
+ require 'devise_user_metering'
36
+
37
+ class Test::Unit::TestCase
38
+ end
39
+
40
+ Timecop.safe_mode = true
41
+
42
+ class User
43
+ include Devise::Models::UserMetering
44
+ attr_accessor :activated_at, :deactivated_at, :rollover_active_duration, :active
45
+ def save!; end
46
+ end
47
+
48
+ def new_user(opts)
49
+ u = User.new
50
+ opts.each do |k, v|
51
+ u.send("#{k}=", v)
52
+ end
53
+ u
54
+ end
55
+
@@ -0,0 +1,111 @@
1
+ require 'helper'
2
+
3
+ class UserTest < Test::Unit::TestCase
4
+
5
+ ACCEPTABLE_DELTA = 0.00001
6
+
7
+ should "Track users activated before this month as a full month" do
8
+ u = new_user(activated_at: Time.parse("2015-01-01"), active: true, rollover_active_duration: 0)
9
+ Timecop.freeze(Date.parse("2015-03-01")) do
10
+ assert_equal 1, u.active_proportion_of_month(Time.parse("2015-02-01"))
11
+ end
12
+ end
13
+
14
+ should "Track users activated during this month as a partial month" do
15
+ u = new_user(activated_at: Time.parse("2015-01-15"), active: true, rollover_active_duration: 0)
16
+ Timecop.freeze(Date.parse("2015-02-01")) do
17
+ assert_in_delta 17.0/31, u.active_proportion_of_month(Time.parse("2015-01-01")), ACCEPTABLE_DELTA # 15 days of 31 in jan 2015
18
+ end
19
+ end
20
+
21
+ should "Track users activated this month and deactivated this month as a partial month" do
22
+ u = new_user(activated_at: Time.parse("2015-01-10"), active: true, rollover_active_duration: 0)
23
+ Timecop.freeze(Date.parse("2015-01-15")) do
24
+ u.deactivate!
25
+ end
26
+ Timecop.freeze(Date.parse("2015-02-01")) do
27
+ assert_in_delta 5.0/31, u.active_proportion_of_month(Time.parse("2015-01-01")), ACCEPTABLE_DELTA # 15 days of 31 in jan 2015
28
+ end
29
+ end
30
+
31
+ should "Track users activated before this month and deactivated this month as a partial month" do
32
+ u = new_user(activated_at: Time.parse("2015-01-01"), active: true, rollover_active_duration: 0)
33
+ Timecop.freeze(Date.parse("2015-02-15")) do
34
+ u.deactivate!
35
+ end
36
+ Timecop.freeze(Date.parse("2015-03-01")) do
37
+ assert_in_delta 14.0/28, u.active_proportion_of_month(Time.parse("2015-02-01")), ACCEPTABLE_DELTA
38
+ end
39
+ end
40
+
41
+ should "Track users activated before this month, deactivated this month, and reactivated this month as a longer partial month" do
42
+ u = new_user(activated_at: Time.parse("2015-01-01"), active: true, rollover_active_duration: 0)
43
+ Timecop.freeze(Date.parse("2015-02-15")) do
44
+ u.deactivate!
45
+ end
46
+ Timecop.freeze(Date.parse("2015-02-20")) do
47
+ u.activate!
48
+ end
49
+ Timecop.freeze(Date.parse("2015-03-01")) do
50
+ assert_in_delta (15.0+8)/28, u.active_proportion_of_month(Time.parse("2015-02-01")), ACCEPTABLE_DELTA
51
+ end
52
+ end
53
+
54
+ def cycle_activation(u)
55
+ Timecop.freeze(Date.parse("2015-02-05")) do
56
+ u.deactivate!
57
+ end
58
+ Timecop.freeze(Date.parse("2015-02-10")) do
59
+ u.activate!
60
+ end
61
+ Timecop.freeze(Date.parse("2015-02-15")) do
62
+ u.deactivate!
63
+ end
64
+ Timecop.freeze(Date.parse("2015-02-20")) do
65
+ u.activate!
66
+ end
67
+ end
68
+
69
+ should "Track users activated before this month, deactivated and reactivated 2 times as an even longer partial month" do
70
+ u = new_user(activated_at: Time.parse("2015-01-01"), active: true, rollover_active_duration: 0)
71
+ cycle_activation(u)
72
+ Timecop.freeze(Date.parse("2015-03-01")) do
73
+ assert_in_delta (4.0+5+9)/28, u.active_proportion_of_month(Time.parse("2015-02-01")), ACCEPTABLE_DELTA
74
+ end
75
+ end
76
+
77
+ should "Track users activated during this month, deactivated and reactivated 2 times correctly" do
78
+ u = new_user(activated_at: Time.parse("2015-02-03"), active: true, rollover_active_duration: 0)
79
+ cycle_activation(u)
80
+ Timecop.freeze(Date.parse("2015-03-01")) do
81
+ assert_in_delta (2.0+5+9)/28, u.active_proportion_of_month(Time.parse("2015-02-01")), ACCEPTABLE_DELTA
82
+ end
83
+ end
84
+
85
+ should "Billing a user zeroes the rollover" do
86
+ u = new_user(activated_at: Time.parse("2015-01-01"), active: true, rollover_active_duration: 0)
87
+ Timecop.freeze(Date.parse('2015-01-15')) do
88
+ u.deactivate!
89
+ end
90
+ assert u.rollover_active_duration > 0
91
+ u.billed!
92
+ assert_equal 0, u.rollover_active_duration
93
+ end
94
+
95
+ should "Raises if you ask for an out of range month" do
96
+ # We have no information about dates in months before activated_at
97
+ u = new_user(activated_at: Time.parse("2015-02-01"), active: true, rollover_active_duration: 0)
98
+ assert_raises StandardError do
99
+ u.active_proportion_of_month(Time.parse("2015-01-01"))
100
+ end
101
+ end
102
+
103
+ should "Raises if you ask for a month that isn't finished yet" do
104
+ u = new_user(activated_at: Time.parse("2015-02-01"), active: true, rollover_active_duration: 0)
105
+ Timecop.freeze(Date.parse('2015-02-15')) do
106
+ assert_raises StandardError do
107
+ u.active_proportion_of_month(Time.parse("2015-02-01"))
108
+ end
109
+ end
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_user_metering
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Ihbe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.4
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.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: devise
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: shoulda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jeweler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: timecop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: test-unit
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Add methods to devise User models that account for active time during
140
+ a month. Useful for SAAS billings
141
+ email: we@mustwin.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files:
145
+ - LICENSE.txt
146
+ - README.rdoc
147
+ files:
148
+ - ".document"
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - LICENSE.txt
152
+ - README.rdoc
153
+ - Rakefile
154
+ - devise_user_metering.gemspec
155
+ - lib/devise_user_metering.rb
156
+ - lib/devise_user_metering/model.rb
157
+ - lib/devise_user_metering/rails.rb
158
+ - lib/devise_user_metering/version.rb
159
+ - lib/generators/devise_user_metering/devise_user_metering_generator.rb
160
+ - lib/generators/devise_user_metering/templates/migration.rb
161
+ - test/helper.rb
162
+ - test/test_devise_user_metering.rb
163
+ homepage: http://github.com/MustWin/devise_user_metering
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.4.8
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Add methods to devise User models that account for active time during a month.
187
+ Useful for SAAS billings
188
+ test_files: []