Module: AuthHelpers::Model::Updatable

Defined in:
lib/auth_helpers/model/updatable.rb

Overview

Hacks into update attributes to dael with email, email confirmation, password and password confirmation. If the e-mail changes, it resends the confirmation instructions if the confirmable module is also included.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
# File 'lib/auth_helpers/model/updatable.rb', line 9

def self.included(base)
  base.send :attr_accessor, :email_confirmation, :password_confirmation
  base.send :attr_accessible, :email, :email_confirmation, :password, :password_confirmation
  base.send :validates_confirmation_of, :email, :password
end

Instance Method Details

#update_attributes(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/auth_helpers/model/updatable.rb', line 15

def update_attributes(options)
  options.delete(:email)              if options[:email] == self.email
  options.delete(:email_confirmation) if options[:email_confirmation].blank?

  options.delete(:password)              if options[:password].blank? || self.valid_password?(options[:password])
  options.delete(:password_confirmation) if options[:password_confirmation].blank?

  # Force confirmations (if confirmation is nil, it won't validate, it has to be at least blank)
  options[:email_confirmation]    ||= '' if options[:email]
  options[:password_confirmation] ||= '' if options[:password]

  if super(options)
    if options[:email] && self.respond_to?(:send_confirmation_instructions)
      self.send_confirmation_instructions(:update)
    end

    return true
  end

  return false
end