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:

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

required_fields( klass ) click to toggle source

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

access_locked?() click to toggle source

Hook into lockable: verifies whether the user is locked for any reason.

Calls superclass method
# File lib/devise_date_restrictable/active_record.rb, line 41
def access_locked?

  super && date_restricted?

end
active_for_authentication?() click to toggle source

 Hook into authenticatable: verifies whether the user is available for authentication.

Calls superclass method
# File lib/devise_date_restrictable/active_record.rb, line 48
def active_for_authentication?

  super && !date_restricted?

end
date_restricted?() click to toggle source

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
inactive_message() click to toggle source

Returns an appropriate message should the user be locked out by this module.

Calls superclass method
# File lib/devise_date_restrictable/active_record.rb, line 55
def inactive_message

  date_restricted? ? :account_date_restricted : super

end

Private Instance Methods

date_restrictable_must_be_chronological() click to toggle source

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