diff options
Diffstat (limited to 'app/models/change_email_validator.rb')
-rw-r--r-- | app/models/change_email_validator.rb | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/app/models/change_email_validator.rb b/app/models/change_email_validator.rb index 2ddebb177..5cc13d4c2 100644 --- a/app/models/change_email_validator.rb +++ b/app/models/change_email_validator.rb @@ -1,36 +1,27 @@ -# == Schema Information -# Schema version: 114 -# -# Table name: change_email_validators -# -# old_email :string -# new_email :string -# password :string -# user_circumstance :string -# - # models/changeemail_validator.rb: # Validates email change form submissions. # # Copyright (c) 2010 UK Citizens Online Democracy. All rights reserved. -# Email: francis@mysociety.org; WWW: http://www.mysociety.org/ - -class ChangeEmailValidator < ActiveRecord::BaseWithoutTable - strip_attributes! +# Email: hello@mysociety.org; WWW: http://www.mysociety.org/ - column :old_email, :string - column :new_email, :string - column :password, :string - column :user_circumstance, :string +class ChangeEmailValidator + include ActiveModel::Validations - attr_accessor :logged_in_user + attr_accessor :old_email, :new_email, :password, :user_circumstance, :logged_in_user validates_presence_of :old_email, :message => N_("Please enter your old email address") validates_presence_of :new_email, :message => N_("Please enter your new email address") validates_presence_of :password, :message => N_("Please enter your password"), :unless => :changing_email validate :password_and_format_of_email - def changing_email() + def initialize(attributes = {}) + attributes.each do |name, value| + send("#{name}=", value) + end + end + + + def changing_email self.user_circumstance == 'change_email' end @@ -41,11 +32,11 @@ class ChangeEmailValidator < ActiveRecord::BaseWithoutTable errors.add(:old_email, _("Old email doesn't look like a valid address")) end - if !errors[:old_email] + if errors[:old_email].blank? if self.old_email.downcase != self.logged_in_user.email.downcase errors.add(:old_email, _("Old email address isn't the same as the address of the account you are logged in with")) elsif (!self.changing_email) && (!self.logged_in_user.has_this_password?(self.password)) - if !errors[:password] + if errors[:password].blank? errors.add(:password, _("Password is not correct")) end end @@ -55,5 +46,4 @@ class ChangeEmailValidator < ActiveRecord::BaseWithoutTable errors.add(:new_email, _("New email doesn't look like a valid address")) end end - end |