devise_date_restrictable 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/LICENSE +1 -1
- data/README.textile +48 -11
- data/Rakefile +14 -0
- data/config/locales/en.yml +6 -2
- data/devise_date_restrictable.gemspec +15 -10
- data/lib/devise_date_restrictable.rb +5 -4
- data/lib/devise_date_restrictable/active_record.rb +55 -6
- data/lib/devise_date_restrictable/version.rb +1 -1
- data/lib/generators/devise_date_restrictable/devise_date_restrictable_generator.rb +20 -6
- data/lib/generators/devise_date_restrictable/templates/migration.rb +7 -3
- data/test/orm/active_record.rb +24 -0
- data/test/rails_app/Rakefile +0 -0
- data/test/rails_app/app/models/user.rb +5 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/config/application.rb +27 -0
- data/test/rails_app/config/boot.rb +27 -0
- data/test/rails_app/config/database.yml +7 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/test.rb +27 -0
- data/test/rails_app/config/initializers/devise.rb +9 -0
- data/test/rails_app/config/initializers/migration_class.rb +6 -0
- data/test/rails_app/db/migrate/01_devise_create_users.rb +20 -0
- data/test/test_helper.rb +5 -0
- data/test/tests/restriction_tests.rb +68 -0
- data/test/tests/validation_tests.rb +46 -0
- metadata +60 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1647aeed6dcb44c69b814cb2522eb6316863c840
|
4
|
+
data.tar.gz: 9dffb3dead57f9d239d492af81f01d8fed64e324
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d33055418872176d52b8589df642b610d9e54ae614c8e11a40fc3dc2cadf454aaf1f686922f07fed314db9c713c156b0b262785a3a0370f1b8e88cd69a6d6b7
|
7
|
+
data.tar.gz: 05e8b6e926d56fe8c6e0042ae4e2fc3387b8b655f0bbb1536b26a70c871600b2b87ec660b838ce8ebbf50a85e2e4ec57a9b0ca2a7218fa7d299a255f8f02490c
|
data/.gitignore
CHANGED
data/LICENSE
CHANGED
data/README.textile
CHANGED
@@ -21,26 +21,63 @@ Finally, add @:date_restrictable@ to the devise line of your model, for instance
|
|
21
21
|
|
22
22
|
h2. Usage
|
23
23
|
|
24
|
-
This gem adds two @DATE@ fields to your
|
25
|
-
|
24
|
+
This gem adds two @DATE@ fields to your model: @valid_from@ and @valid_until@. One or both may be specified in order to
|
25
|
+
restrict a user’s ability to log in either before- or after a particular date, or within a given date range.
|
26
|
+
If neither is specified, the user will be able to log in without restriction as before.
|
26
27
|
|
27
|
-
Note that dates are _inclusive_
|
28
|
-
At this time, all restrictions are based on server time—no provision has been made, as yet, for different timezones.
|
28
|
+
Note that dates are _inclusive_, that is:
|
29
29
|
|
30
|
-
|
30
|
+
- if @valid_from@ is specified, the user will be able to log in after 00:00:00 on that date
|
31
|
+
- if @valid_until@ is specified, the user will be able to log in until 23:59:59 on that date
|
31
32
|
|
32
|
-
|
33
|
+
Note that this gem is not timezone-aware, and therefore the server’s local time will be used for everything. I plan to modify this in a later release.
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
This gem also provides basic validation to ensure that if @valid_from@ and @valid_until@ are provided, they make sense
|
36
|
+
chronologically.
|
37
|
+
|
38
|
+
h3. Localisation
|
39
|
+
|
40
|
+
This gem provides two localisable strings:
|
41
|
+
|
42
|
+
*Authentication failure*
|
43
|
+
Shown to the user if their account has been restricted by date. This follows Devise convention, will search the following I18n keys:
|
44
|
+
|
45
|
+
<pre>devise.failure.[model].account_date_restricted
|
46
|
+
devise.failure.account_date_restricted</pre>
|
47
|
+
|
48
|
+
*Date validation failure*
|
49
|
+
Shown if you attempt to set a user’s @valid_from@ to be after @valid_until@. This follows ActiveRecord error convention, and will search the following I18n keys:
|
50
|
+
|
51
|
+
<pre>activerecord.errors.models.[model].attributes.valid_until.must_be_on_or_after
|
52
|
+
activerecord.errors.models.[model].must_be_on_or_after
|
53
|
+
activerecord.errors.messages.must_be_on_or_after
|
54
|
+
errors.attributes.valid_until.must_be_on_or_after
|
55
|
+
errors.messages.must_be_on_or_after</pre>
|
56
|
+
|
57
|
+
The "human-readable":http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models name of the @valid_from@ field is available to these translations as @:field@.)
|
37
58
|
|
38
59
|
h2. Version History
|
39
60
|
|
40
|
-
|
61
|
+
*1.0.0 _(May 23rd, 2018)_*
|
62
|
+
|
63
|
+
* [fix] added ActiveRecord::Migration version number to migration template
|
64
|
+
* added basic date validation (it is no longer possible to punch holes in space/time with this gem, sorry)
|
65
|
+
* added unit tests
|
66
|
+
* general tidying for release.
|
67
|
+
|
68
|
+
*0.0.2 _(June 6th, 2017)_*
|
41
69
|
|
42
70
|
* removing errant debug output
|
43
71
|
|
44
|
-
|
72
|
+
*0.0.1 _(May 28th, 2017)_*
|
45
73
|
|
46
74
|
* initial release
|
75
|
+
|
76
|
+
h2. Contributing
|
77
|
+
|
78
|
+
Go for it! I’ll happily accept any sensible pull requests :)
|
79
|
+
|
80
|
+
h2. Obligatory sales pitch
|
81
|
+
|
82
|
+
When I’m not hacking at random gems, I’m a freelance web developer specialising in all things front-end, based in the beautiful city of Cardiff, UK.
|
83
|
+
I’m usually kept fairly busy with project work, but I’m always on the lookout for new people to do cool stuff with. "Drop me a line":mailto:hello@jonpearse.net – I’d love to hear from you!
|
data/Rakefile
ADDED
data/config/locales/en.yml
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
en:
|
2
2
|
devise:
|
3
3
|
failure:
|
4
|
-
|
5
|
-
|
4
|
+
account_date_restricted: "Your account is not currently active: please contact an administrator for more information"
|
5
|
+
|
6
|
+
errors:
|
7
|
+
attributes:
|
8
|
+
valid_until:
|
9
|
+
must_be_on_or_after: Cannot be before %{field}
|
@@ -2,18 +2,23 @@
|
|
2
2
|
require File.expand_path('../lib/devise_date_restrictable/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
|
6
|
-
gem.
|
7
|
-
gem.
|
8
|
-
gem.
|
9
|
-
gem.
|
10
|
-
gem.
|
5
|
+
|
6
|
+
gem.authors = 'Jon Pearse'
|
7
|
+
gem.email = 'jon@jonpearse.net'
|
8
|
+
gem.description = 'This allows a Devise user’s validity to be restricted by a date range. This allows administrators to set up accounts that are valid to-, or from-, or between specific dates.'
|
9
|
+
gem.summary = 'Devise plugin to restrict access based on a date range.'
|
10
|
+
gem.homepage = 'https://github.com/jonpearse/devise_date_restrictable'
|
11
|
+
gem.license = 'MIT'
|
11
12
|
|
12
13
|
gem.files = `git ls-files`.split($\)
|
13
|
-
gem.name =
|
14
|
-
gem.require_paths = [
|
14
|
+
gem.name = 'devise_date_restrictable'
|
15
|
+
gem.require_paths = [ 'lib' ]
|
15
16
|
gem.version = DeviseDateRestrictable::VERSION
|
16
17
|
|
17
|
-
gem.add_dependency('devise',
|
18
|
-
gem.add_dependency('rails',
|
18
|
+
gem.add_dependency( 'devise', '~> 4.0' )
|
19
|
+
gem.add_dependency( 'rails', '~> 5.1' )
|
20
|
+
|
21
|
+
gem.add_development_dependency( 'bundler', '~> 1.16' )
|
22
|
+
gem.add_development_dependency( 'sqlite3', '~> 1.3', '>= 1.3.13' )
|
23
|
+
|
19
24
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
require "devise_date_restrictable/version"
|
2
2
|
require "devise_date_restrictable/rails"
|
3
3
|
|
4
|
-
unless defined?(Devise)
|
4
|
+
unless defined?( Devise )
|
5
|
+
|
5
6
|
require 'devise'
|
7
|
+
|
6
8
|
end
|
7
9
|
|
8
|
-
Devise.add_module :date_restrictable, model: 'devise_date_restrictable/active_record'
|
10
|
+
Devise.add_module( :date_restrictable, model: 'devise_date_restrictable/active_record' )
|
9
11
|
|
10
|
-
module DeviseDateRestrictable
|
11
|
-
end
|
12
|
+
module DeviseDateRestrictable; end
|
@@ -1,32 +1,81 @@
|
|
1
1
|
module Devise
|
2
2
|
module Models
|
3
|
+
|
4
|
+
# DateRestrictable provides the ability to restrict a user’s access by date. This can be used to limit logging in
|
5
|
+
# either before- or after a certain date, or outside of a given date range.
|
6
|
+
#
|
7
|
+
# Where dates are given they are <em>inclusive</em>, that is:
|
8
|
+
#
|
9
|
+
# - if <tt>valid_from</tt> is specified, the user may log in after 00:00:00 on that date
|
10
|
+
# - if <tt>valid_until</tt> is specified, the user may log in until 23:59:59 on that date
|
11
|
+
#
|
12
|
+
# This module also provides basic validation to ensure that, where both <tt>valid_from</tt> and <tt>valid_until</tt>
|
13
|
+
# are specified, this is done in a sensible chronological order.
|
3
14
|
module DateRestrictable
|
4
15
|
extend ActiveSupport::Concern
|
5
16
|
|
17
|
+
# Add validation onto base model.
|
18
|
+
included do
|
19
|
+
|
20
|
+
validate :date_restrictable_must_be_chronological
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns whether or not the user is currently restricted by date.
|
6
25
|
def date_restricted?
|
26
|
+
|
7
27
|
now = Date.today
|
8
28
|
|
9
29
|
!((valid_from.nil? or now >= valid_from) and (valid_until.nil? or now <= valid_until))
|
30
|
+
|
10
31
|
end
|
11
32
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
33
|
+
# Returns an array of fields required by this module.
|
34
|
+
def self.required_fields( klass )
|
35
|
+
|
36
|
+
%i{ valid_from, valid_until }
|
37
|
+
|
17
38
|
end
|
18
39
|
|
40
|
+
# Hook into lockable: verifies whether the user is locked for any reason.
|
19
41
|
def access_locked?
|
20
|
-
|
42
|
+
|
43
|
+
super && date_restricted?
|
44
|
+
|
21
45
|
end
|
22
46
|
|
47
|
+
# Hook into authenticatable: verifies whether the user is available for authentication.
|
23
48
|
def active_for_authentication?
|
49
|
+
|
24
50
|
super && !date_restricted?
|
51
|
+
|
25
52
|
end
|
26
53
|
|
54
|
+
# Returns an appropriate message should the user be locked out by this module.
|
27
55
|
def inactive_message
|
56
|
+
|
28
57
|
date_restricted? ? :account_date_restricted : super
|
58
|
+
|
29
59
|
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
# Internal validation function to ensure that, when both -from and -to dates are specified, they make
|
64
|
+
# chronological sense.
|
65
|
+
def date_restrictable_must_be_chronological
|
66
|
+
|
67
|
+
# bounce unless we have both dates
|
68
|
+
return if valid_from.blank? or valid_until.blank?
|
69
|
+
|
70
|
+
# otherwise…
|
71
|
+
unless valid_until.to_date >= valid_from.to_date
|
72
|
+
|
73
|
+
field_name = self.class.human_attribute_name( :valid_from )
|
74
|
+
errors.add( :valid_until, :must_be_on_or_after, { field: field_name })
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
30
79
|
end
|
31
80
|
end
|
32
81
|
end
|
@@ -4,14 +4,21 @@ class DeviseDateRestrictableGenerator < Rails::Generators::Base
|
|
4
4
|
include Rails::Generators::Migration
|
5
5
|
|
6
6
|
def self.source_root
|
7
|
-
|
7
|
+
|
8
|
+
@_devise_source_root ||= File.expand_path( '../templates', __FILE__ )
|
9
|
+
|
8
10
|
end
|
9
11
|
|
10
|
-
def self.next_migration_number(dirname)
|
12
|
+
def self.next_migration_number( dirname )
|
13
|
+
|
11
14
|
if ActiveRecord::Base.timestamped_migrations
|
12
|
-
|
15
|
+
|
16
|
+
Time.now.utc.strftime( '%Y%m%d%H%M%S' )
|
17
|
+
|
13
18
|
else
|
14
|
-
|
19
|
+
|
20
|
+
"%.3d" % ( current_migration_number( dirname ) + 1 )
|
21
|
+
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
@@ -24,17 +31,24 @@ class DeviseDateRestrictableGenerator < Rails::Generators::Base
|
|
24
31
|
@model_name = model_name.camelize.singularize
|
25
32
|
|
26
33
|
if columns_exist?
|
34
|
+
|
27
35
|
say "* Date restrictable columns already seem to exist on @{model_name}"
|
36
|
+
|
28
37
|
else
|
38
|
+
|
29
39
|
migration_template 'migration.rb', "db/migrate/devise_add_#{model_name.downcase}_date_restriction_fields.rb"
|
40
|
+
|
30
41
|
end
|
31
42
|
|
32
|
-
puts "
|
43
|
+
puts "\n\nMake sure to add :date_restrictable to the devise line of your #{@model_name} model file!\n\n"
|
44
|
+
|
33
45
|
end
|
34
46
|
|
35
47
|
protected
|
36
48
|
|
37
49
|
def columns_exist?
|
38
|
-
|
50
|
+
|
51
|
+
@model_name.constantize.column_names.include?( 'valid_from' ) and @model_name.constantize.column_names.include?( 'valid_until' )
|
52
|
+
|
39
53
|
end
|
40
54
|
end
|
@@ -1,6 +1,10 @@
|
|
1
|
-
class DeviseAdd<%= @model_name.camelize.singularize %>DateRestrictionFields < ActiveRecord::Migration
|
1
|
+
class DeviseAdd<%= @model_name.camelize.singularize %>DateRestrictionFields < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
|
2
|
+
|
2
3
|
def change
|
3
|
-
|
4
|
-
add_column :<%=@model_name.tableize%>, :
|
4
|
+
|
5
|
+
add_column :<%=@model_name.tableize %>, :valid_from, :date
|
6
|
+
add_column :<%=@model_name.tableize %>, :valid_until, :date
|
7
|
+
|
5
8
|
end
|
9
|
+
|
6
10
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActiveRecord::Migration.verbose = false
|
4
|
+
ActiveRecord::Base.logger = Logger.new(nil)
|
5
|
+
ActiveRecord::Base.include_root_in_json = true
|
6
|
+
|
7
|
+
migrate_path = File.expand_path("../../rails_app/db/migrate/", __FILE__)
|
8
|
+
if Devise::Test.rails52?
|
9
|
+
ActiveRecord::MigrationContext.new(migrate_path).migrate
|
10
|
+
else
|
11
|
+
ActiveRecord::Migrator.migrate(migrate_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
class ActiveSupport::TestCase
|
15
|
+
if Devise::Test.rails5?
|
16
|
+
self.use_transactional_tests = true
|
17
|
+
else
|
18
|
+
# Let `after_commit` work with transactional fixtures, however this is not needed for Rails 5.
|
19
|
+
require 'test_after_commit'
|
20
|
+
self.use_transactional_fixtures = true
|
21
|
+
end
|
22
|
+
|
23
|
+
self.use_instantiated_fixtures = false
|
24
|
+
end
|
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
require 'devise'
|
5
|
+
require 'devise_date_restrictable'
|
6
|
+
|
7
|
+
if defined?(Bundler)
|
8
|
+
# If you precompile assets before deploying to production, use this line
|
9
|
+
Bundler.require(*Rails.groups(assets: %w[development test]))
|
10
|
+
# If you want your assets lazily compiled in production, use this line
|
11
|
+
# Bundler.require(:default, :assets, Rails.env)
|
12
|
+
end
|
13
|
+
|
14
|
+
module RailsApp
|
15
|
+
class Application < Rails::Application
|
16
|
+
config.encoding = 'utf-8'
|
17
|
+
|
18
|
+
config.filter_parameters += [:password]
|
19
|
+
|
20
|
+
config.assets.enabled = true
|
21
|
+
|
22
|
+
config.assets.version = '1.0'
|
23
|
+
config.secret_key_base = 'ignorethisplox'
|
24
|
+
|
25
|
+
config.active_record.sqlite3.represent_boolean_as_integer = true
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
unless defined?( DEVISE_ORM )
|
4
|
+
DEVISE_ORM = ( ENV["DEVISE_ORM"] || :active_record ).to_sym
|
5
|
+
end
|
6
|
+
|
7
|
+
module Devise
|
8
|
+
module Test
|
9
|
+
# Detection for minor differences between Rails 4 and 5, 5.1, and 5.2 in tests.
|
10
|
+
|
11
|
+
def self.rails52?
|
12
|
+
Rails.version.start_with? '5.2'
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.rails51?
|
16
|
+
Rails.version.start_with? '5.1'
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.rails5?
|
20
|
+
Rails.version.start_with? '5'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set up gems listed in the Gemfile.
|
26
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path( '../../../../Gemfile', __FILE__ )
|
27
|
+
require 'bundler/setup' if File.exist?( ENV['BUNDLE_GEMFILE'] )
|
@@ -0,0 +1,27 @@
|
|
1
|
+
RailsApp::Application.configure do
|
2
|
+
config.cache_classes = true
|
3
|
+
config.eager_load = false
|
4
|
+
|
5
|
+
if Rails.version > "5"
|
6
|
+
config.public_file_server.enabled = true
|
7
|
+
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
|
8
|
+
else
|
9
|
+
config.serve_static_files = true
|
10
|
+
config.static_cache_control = 'public, max-age=3600'
|
11
|
+
end
|
12
|
+
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_controller.perform_caching = false
|
15
|
+
|
16
|
+
config.action_dispatch.show_exceptions = false
|
17
|
+
|
18
|
+
config.action_controller.allow_forgery_protection = false
|
19
|
+
|
20
|
+
config.action_mailer.delivery_method = :test
|
21
|
+
config.action_mailer.default_url_options = { host: 'test.host' }
|
22
|
+
|
23
|
+
config.active_support.deprecation = :stderr
|
24
|
+
I18n.enforce_available_locales = false
|
25
|
+
|
26
|
+
config.active_support.test_order = :sorted
|
27
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Devise.setup do |config|
|
2
|
+
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
|
3
|
+
|
4
|
+
require 'devise/orm/active_record'
|
5
|
+
config.secret_key = '24213d5a635a381c4d4758564214314d5e514e34405507614c'
|
6
|
+
config.case_insensitive_keys = [:email]
|
7
|
+
|
8
|
+
config.strip_whitespace_keys = [:email]
|
9
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
|
4
|
+
def change
|
5
|
+
create_table :users do |t|
|
6
|
+
|
7
|
+
## Database authenticatable
|
8
|
+
t.string :email, null: false, default: ""
|
9
|
+
t.string :encrypted_password, null: false, default: ""
|
10
|
+
|
11
|
+
## Restrictable
|
12
|
+
t.date :valid_from
|
13
|
+
t.date :valid_until
|
14
|
+
|
15
|
+
t.timestamps null: false
|
16
|
+
end
|
17
|
+
|
18
|
+
add_index :users, :email, unique: true
|
19
|
+
end
|
20
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RestrictionTests < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
# Check valid from
|
6
|
+
test 'ensure user cannot log in before valid_from' do
|
7
|
+
|
8
|
+
assert_not( User.new( valid_from: 1.day.from_now ).active_for_authentication? )
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'ensure user can log in after valid_from' do
|
13
|
+
|
14
|
+
assert( User.new( valid_from: 1.day.ago ).active_for_authentication? )
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
# Check valid until
|
19
|
+
test 'ensure user cannot log in after valid_until' do
|
20
|
+
|
21
|
+
assert_not( User.new( valid_until: 1.day.ago ).active_for_authentication? )
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'ensure user can log in before valid_until' do
|
26
|
+
|
27
|
+
assert( User.new( valid_until: 1.day.from_now ).active_for_authentication? )
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
# check ranges
|
32
|
+
test 'ensure user cannot log in before date range' do
|
33
|
+
|
34
|
+
assert_not( User.new( valid_from: 1.day.from_now, valid_until: 2.days.from_now ).active_for_authentication? )
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'ensure user cannot log in after date range' do
|
39
|
+
|
40
|
+
assert_not( User.new( valid_from: 2.days.ago, valid_until: 1.days.ago ).active_for_authentication? )
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'ensure user can log in during date range' do
|
45
|
+
|
46
|
+
assert( User.new( valid_from: 1.day.ago, valid_until: 1.days.from_now ).active_for_authentication? )
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# Edge-cases
|
51
|
+
test 'ensure user can still log in if date_until is set to the current date' do
|
52
|
+
|
53
|
+
assert( User.new( valid_until: Date.today ).active_for_authentication? )
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
test 'ensure user can log in if date_from is set to the current date' do
|
58
|
+
|
59
|
+
assert( User.new( valid_from: Date.today ).active_for_authentication? )
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
test 'ensure user can log in if both date_from and date_until are set to the current date' do
|
64
|
+
|
65
|
+
assert( User.new( valid_from: Date.today, valid_until: Date.today ).active_for_authentication? )
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ValidationTests < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
# Make sure we can set things independently
|
6
|
+
test 'ensure we can set valid_from without valid_until' do
|
7
|
+
|
8
|
+
assert( User.new( valid_from: Date.today ).valid? )
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'ensure we can set valid_until without valid_from' do
|
13
|
+
|
14
|
+
assert( User.new( valid_until: Date.today ).valid? )
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
# Now check the both
|
19
|
+
test 'ensure we can set valid_until and valid_from, as long as they’re chronologically sensible' do
|
20
|
+
|
21
|
+
assert( User.new( valid_from: ( Date.today - 1.day ), valid_until: Date.today ).valid? )
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'ensure we can set valid_until and valid_from for the same day' do
|
26
|
+
|
27
|
+
assert( User.new( valid_from: Date.today, valid_until: Date.today ).valid? )
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
test 'ensure we cannot set valid_until to a point before valid_until' do
|
32
|
+
|
33
|
+
assert_not( User.new( valid_from: Date.today, valid_until: ( Date.today - 1.day )).valid? )
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
# Check messaging while we’re here
|
38
|
+
test 'ensure the validation message looks sane' do
|
39
|
+
|
40
|
+
user = User.new( valid_from: Date.today, valid_until: ( Date.today - 1.day ))
|
41
|
+
user.validate
|
42
|
+
|
43
|
+
assert_equal( user.errors.messages[:valid_until].first, 'Cannot be before Valid from' )
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,43 +1,77 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise_date_restrictable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Pearse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: devise
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.0
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.0
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 5.
|
33
|
+
version: '5.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.3.13
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '1.3'
|
38
72
|
- - ">="
|
39
73
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
74
|
+
version: 1.3.13
|
41
75
|
description: This allows a Devise user’s validity to be restricted by a date range.
|
42
76
|
This allows administrators to set up accounts that are valid to-, or from-, or between
|
43
77
|
specific dates.
|
@@ -51,6 +85,7 @@ files:
|
|
51
85
|
- Gemfile
|
52
86
|
- LICENSE
|
53
87
|
- README.textile
|
88
|
+
- Rakefile
|
54
89
|
- config/locales/en.yml
|
55
90
|
- devise_date_restrictable.gemspec
|
56
91
|
- lib/devise_date_restrictable.rb
|
@@ -59,6 +94,21 @@ files:
|
|
59
94
|
- lib/devise_date_restrictable/version.rb
|
60
95
|
- lib/generators/devise_date_restrictable/devise_date_restrictable_generator.rb
|
61
96
|
- lib/generators/devise_date_restrictable/templates/migration.rb
|
97
|
+
- test/orm/active_record.rb
|
98
|
+
- test/rails_app/Rakefile
|
99
|
+
- test/rails_app/app/models/user.rb
|
100
|
+
- test/rails_app/config.ru
|
101
|
+
- test/rails_app/config/application.rb
|
102
|
+
- test/rails_app/config/boot.rb
|
103
|
+
- test/rails_app/config/database.yml
|
104
|
+
- test/rails_app/config/environment.rb
|
105
|
+
- test/rails_app/config/environments/test.rb
|
106
|
+
- test/rails_app/config/initializers/devise.rb
|
107
|
+
- test/rails_app/config/initializers/migration_class.rb
|
108
|
+
- test/rails_app/db/migrate/01_devise_create_users.rb
|
109
|
+
- test/test_helper.rb
|
110
|
+
- test/tests/restriction_tests.rb
|
111
|
+
- test/tests/validation_tests.rb
|
62
112
|
homepage: https://github.com/jonpearse/devise_date_restrictable
|
63
113
|
licenses:
|
64
114
|
- MIT
|
@@ -79,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
129
|
version: '0'
|
80
130
|
requirements: []
|
81
131
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.6.
|
132
|
+
rubygems_version: 2.6.14
|
83
133
|
signing_key:
|
84
134
|
specification_version: 4
|
85
135
|
summary: Devise plugin to restrict access based on a date range.
|