Class: Accountly::ChangeEmailForm

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/forms/accountly/change_email_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ ChangeEmailForm

Returns a new instance of ChangeEmailForm.



10
11
12
# File 'app/forms/accountly/change_email_form.rb', line 10

def initialize(user)
  @user = user
end

Instance Attribute Details

#new_emailObject

Returns the value of attribute new_email.



5
6
7
# File 'app/forms/accountly/change_email_form.rb', line 5

def new_email
  @new_email
end

#new_email_confirmationObject

Returns the value of attribute new_email_confirmation.



5
6
7
# File 'app/forms/accountly/change_email_form.rb', line 5

def new_email_confirmation
  @new_email_confirmation
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'app/forms/accountly/change_email_form.rb', line 5

def password
  @password
end

Instance Method Details

#error_add(attribute, error) ⇒ Object



67
68
69
# File 'app/forms/accountly/change_email_form.rb', line 67

def error_add(attribute, error)
  errors.add attribute, I18n.t("activemodel.errors.models.accountly/change_email_form.attributes.#{attribute}.#{error}")
end

#submit(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/forms/accountly/change_email_form.rb', line 14

def submit(params)
  self.password = params[:password]
  self.new_email = params[:new_email]
  self.new_email_confirmation = params[:new_email_confirmation]
  if valid?
    @user.new_email = new_email
    @user.save!
    @user.send_new_email_request
    true
  else
    false
  end
end

#validate_steps_emailObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/forms/accountly/change_email_form.rb', line 28

def validate_steps_email

  if new_email.present?
    if new_email =~ EMAIL_REGEX
      if @user.confirmed_duplicate_email(self.new_email)
        if new_email_confirmation.present?
          if new_email_confirmation == new_email
            true
          else
            error_add :new_email_confirmation, 'confirmation'
            false
          end
        else
          error_add :new_email_confirmation, 'blank'
          false
        end
      else
        error_add :new_email, 'duplicate'
      end
    else
      error_add :new_email, 'invalid'
      false
    end
  else
    error_add :new_email, 'blank'
    false
  end
end

#verify_passwordObject



57
58
59
60
61
62
63
64
65
# File 'app/forms/accountly/change_email_form.rb', line 57

def verify_password
  if password.blank?
    error_add :password, 'blank'
  else
    unless @user.authenticate(password)
      error_add :password, 'invalid'
    end
  end
end