Module: ModelMixin::InstanceMethodsOnActivation

Defined in:
app/models/model_mixin.rb

Instance Method Summary collapse

Instance Method Details

#clear_tokenObject

Clears the user’s timestamped token and saves the record. This is part of the forgotten-password workflow.



101
102
103
# File 'app/models/model_mixin.rb', line 101

def clear_token # :nodoc:
  update_attributes :token => nil, :token_created_at => nil
end

#generate_tokenObject

Generates a unique, timestamped token.



85
86
87
88
89
90
# File 'app/models/model_mixin.rb', line 85

def generate_token # :nodoc:
  begin
    self.token = url_friendly_token
  end while self.class.exists?(:token => token)
  self.token_created_at = Time.now.utc
end

#generate_token!Object

Generates a unique, timestamped token which can be used in URLs, and saves the record. This is part of the forgotten-password workflow.



94
95
96
97
# File 'app/models/model_mixin.rb', line 94

def generate_token! # :nodoc:
  generate_token
  save
end

#has_matching_password?(plain_text_password) ⇒ Boolean

Returns true if the given plain_text_password is the user’s password, and false otherwise.

Returns:

  • (Boolean)


107
108
109
# File 'app/models/model_mixin.rb', line 107

def has_matching_password?(plain_text_password) # :nodoc:
  BCrypt::Password.new(password_digest) == plain_text_password
end

#has_matching_salt?(salt) ⇒ Boolean

Returns true if the given salt is the user’s salt, and false otherwise.

Returns:

  • (Boolean)


113
114
115
# File 'app/models/model_mixin.rb', line 113

def has_matching_salt?(salt) # :nodoc:
  password_salt == salt
end

#password=(plain_text_password) ⇒ Object

:nodoc:



77
78
79
80
81
82
# File 'app/models/model_mixin.rb', line 77

def password=(plain_text_password) # :nodoc:
  @password = plain_text_password
  unless @password.blank?
    self.password_digest = BCrypt::Password.create plain_text_password
  end
end

#password_saltObject

:nodoc:



117
118
119
# File 'app/models/model_mixin.rb', line 117

def password_salt # :nodoc:
  BCrypt::Password.new(password_digest).salt
end

#randomise_credentialsObject

Sets the ‘username` and `password` to random values. Use this for a user who should authenticate where they need to activate themselves first.



70
71
72
73
74
75
# File 'app/models/model_mixin.rb', line 70

def randomise_credentials
  begin
    self.username = SecureRandom.base64(10)
  end while self.class.exists?(:username => username)
  self.password = SecureRandom.base64(10)
end

#should_authenticate?Boolean

Override this in your model if you need to bypass the Quo Vadis validations.

Returns:

  • (Boolean)


63
64
65
# File 'app/models/model_mixin.rb', line 63

def should_authenticate?
  true
end