module Devise::Models::DateRestrictable
DateRestrictable
provides the ability to restrict a user’s access by date. This can be used to limit logging in either before- or after a certain date, or outside of a given date range.
Where dates are given they are inclusive, that is:
-
if
valid_from
is specified, the user may log in after 00:00:00 on that date -
if
valid_until
is specified, the user may log in until 23:59:59 on that date
This module also provides basic validation to ensure that, where both valid_from
and valid_until
are specified, this is done in a sensible chronological order.
Public Class Methods
Returns an array of fields required by this module.
# File lib/devise_date_restrictable/active_record.rb, line 34 def self.required_fields( klass ) %i{ valid_from, valid_until } end
Public Instance Methods
Hook into lockable: verifies whether the user is locked for any reason.
# File lib/devise_date_restrictable/active_record.rb, line 41 def access_locked? super && date_restricted? end
Hook into authenticatable: verifies whether the user is available for authentication.
# File lib/devise_date_restrictable/active_record.rb, line 48 def active_for_authentication? super && !date_restricted? end
Returns whether or not the user is currently restricted by date.
# File lib/devise_date_restrictable/active_record.rb, line 25 def date_restricted? now = Date.today !((valid_from.nil? or now >= valid_from) and (valid_until.nil? or now <= valid_until)) end
Returns an appropriate message should the user be locked out by this module.
# File lib/devise_date_restrictable/active_record.rb, line 55 def inactive_message date_restricted? ? :account_date_restricted : super end
Private Instance Methods
Internal validation function to ensure that, when both -from and -to dates are specified, they make chronological sense.
# File lib/devise_date_restrictable/active_record.rb, line 65 def date_restrictable_must_be_chronological # bounce unless we have both dates return if valid_from.blank? or valid_until.blank? # otherwise… unless valid_until.to_date >= valid_from.to_date field_name = self.class.human_attribute_name( :valid_from ) errors.add( :valid_until, :must_be_on_or_after, { field: field_name }) end end