Module: Devision::Models::Rememberable

Extended by:
ActiveSupport::Concern
Defined in:
lib/devision/models/rememberable.rb

Overview

Rememberable manages generating and clearing token for remember the user from a saved cookie. Rememberable also has utility methods for dealing with serializing the user into the cookie and back from the cookie, trying to lookup the record based on the saved information. You probably wouldn’t use rememberable methods directly, they are used mostly internally for handling the remember token.

Options

Rememberable adds the following options in to a model:

* +remember_for+: the time you want the user will be remembered without
  asking for credentials. After this time the user will be blocked and
  will have to enter their credentials again. This configuration is also
  used to calculate the expires time for the cookie created to remember
  the user. By default remember_for is 2.weeks.

* +extend_remember_period+: if true, extends the user's remember period
  when remembered via cookie. False by default.

* +rememberable_options+: configuration options passed to the created cookie.

Examples

User.find(1).remember_me!  # regenerating the token
User.find(1).forget_me!    # clearing the token

# generating info to put into cookies
User.serialize_into_cookie(user)

# lookup the user based on the incoming cookie information
User.serialize_from_cookie(cookie_string)

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extend_remember_periodObject

Returns the value of attribute extend_remember_period.



38
39
40
# File 'lib/devision/models/rememberable.rb', line 38

def extend_remember_period
  @extend_remember_period
end

#remember_meObject

Returns the value of attribute remember_me.



38
39
40
# File 'lib/devision/models/rememberable.rb', line 38

def remember_me
  @remember_me
end

Class Method Details

.required_fields(klass) ⇒ Object



40
41
42
# File 'lib/devision/models/rememberable.rb', line 40

def self.required_fields(klass)
  [:remember_created_at]
end

Instance Method Details

#forget_me!Object

If the record is persisted, remove the remember token (but only if it exists), and save the record without validations.



54
55
56
57
58
59
# File 'lib/devision/models/rememberable.rb', line 54

def forget_me!
  return unless persisted?
  self.remember_token = nil if respond_to?(:remember_token=)
  self.remember_created_at = nil if self.class.expire_all_remember_me_on_sign_out
  save(validate: false)
end

#remember_expired?Boolean

Remember token should be expired if expiration time not overpass now.

Returns:

  • (Boolean)


62
63
64
# File 'lib/devision/models/rememberable.rb', line 62

def remember_expired?
  remember_created_at.nil? || (remember_expires_at <= Time.now.utc)
end

#remember_expires_atObject

Remember token expires at created time + remember_for configuration



67
68
69
# File 'lib/devision/models/rememberable.rb', line 67

def remember_expires_at
  remember_created_at + self.class.remember_for
end

#remember_me!(extend_period = false) ⇒ Object

Generate a new remember token and save the record without validations unless remember_across_browsers is true and the user already has a valid token.



46
47
48
49
50
# File 'lib/devision/models/rememberable.rb', line 46

def remember_me!(extend_period=false)
  self.remember_token = self.class.remember_token if generate_remember_token?
  self.remember_created_at = Time.now.utc if generate_remember_timestamp?(extend_period)
  save(validate: false) if self.changed?
end

#rememberable_optionsObject



84
85
86
# File 'lib/devision/models/rememberable.rb', line 84

def rememberable_options
  self.class.rememberable_options
end

#rememberable_valueObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/devision/models/rememberable.rb', line 71

def rememberable_value
  if respond_to?(:remember_token)
    remember_token
  elsif respond_to?(:authenticatable_salt) && (salt = authenticatable_salt)
    salt
  else
    raise "authenticable_salt returned nil for the #{self.class.name} model. " \
      "In order to use rememberable, you must ensure a password is always set " \
      "or have a remember_token column in your model or implement your own " \
      "rememberable_value in the model with custom logic."
  end
end