Skip to content

Commit

Permalink
Merge pull request #746 from mpugach/use_standart_AR_uniqueness_message
Browse files Browse the repository at this point in the history
Use standart ActiveRecord error message for email uniqueness validation
  • Loading branch information
booleanbetrayal authored Oct 6, 2016
2 parents ad8216c + b7d7359 commit 123cfb7
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
# only validate unique email among users that registered by email
def unique_email_user
if provider == 'email' and self.class.where(provider: 'email', email: email).count > 0
errors.add(:email, I18n.t("errors.messages.already_in_use"))
errors.add(:email, :taken)
end
end

Expand Down
3 changes: 1 addition & 2 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ de:
validate_sign_up_params: "Bitte übermitteln sie vollständige Anmeldeinformationen im Body des Requests."
validate_account_update_params: "Bitte übermitteln sie vollständige Informationen zur Aktualisierung im Body des Requests."
not_email: "ist keine E-Mail Adresse"
already_in_use: "bereits in Verwendung"
devise:
mailer:
confirmation_instructions:
Expand All @@ -47,4 +46,4 @@ de:
unlock_link_msg: "Klicken Sie auf den Link unten , um Ihr Konto zu entsperren :"
unlock_link: "Entsperren Sie Ihr Konto "
hello: "hallo"
welcome: "willkommen"
welcome: "willkommen"
3 changes: 1 addition & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ en:
successfully_updated: "Your password has been successfully updated."
errors:
messages:
already_in_use: "already in use"
validate_sign_up_params: "Please submit proper sign up data in request body."
validate_account_update_params: "Please submit proper account update data in request body."
not_email: "is not an email"
Expand All @@ -44,4 +43,4 @@ en:
unlock_link_msg: "Click the link below to unlock your account:"
unlock_link: "Unlock my account"
hello: "hello"
welcome: "welcome"
welcome: "welcome"
3 changes: 1 addition & 2 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ es:
validate_sign_up_params: "Los datos introducidos en la solicitud de acceso no son válidos."
validate_account_update_params: "Los datos introducidos en la solicitud de actualización no son válidos."
not_email: "no es un correo electrónico"
already_in_use: "ya ha sido ocupado"
devise:
mailer:
confirmation_instructions:
Expand All @@ -47,4 +46,4 @@ es:
unlock_link_msg: "Para desbloquear su cuenta ingrese en el siguiente link:"
unlock_link: "Desbloquear cuenta"
hello: "hola"
welcome: "bienvenido"
welcome: "bienvenido"
3 changes: 1 addition & 2 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ fr:
successfully_updated: "Votre mot de passe a été correctement mis à jour."
errors:
messages:
already_in_use: "déjà utilisé(e)"
validate_sign_up_params: "Les données d'inscription dans le corps de la requête ne sont pas valides."
validate_account_update_params: "Les données de mise à jour dans le corps de la requête ne sont pas valides."
not_email: "n'est pas une adresse e-mail"
Expand All @@ -47,4 +46,4 @@ fr:
unlock_link_msg: "Cliquez sur le lien ci-dessous pour déverrouiller votre compte:"
unlock_link: "Déverrouiller mon compte"
hello: "bonjour"
welcome: "bienvenue"
welcome: "bienvenue"
1 change: 0 additions & 1 deletion config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ ja:
successfully_updated: "パスワードの更新に成功しました。"
errors:
messages:
already_in_use: "すでに利用されています。"
validate_sign_up_params: "リクエストボディに適切なアカウント新規登録データを送信してください。"
validate_account_update_params: "リクエストボディに適切なアカウント更新のデータを送信してください。"
not_email: "はメールアドレスではありません"
Expand Down
1 change: 0 additions & 1 deletion config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ nl:
successfully_updated: "Uw wachtwoord is aangepast."
errors:
messages:
already_in_use: "al in gebruik"
validate_sign_up_params: "Gegevens voor aanmaken van het account zijn niet geldig."
validate_account_update_params: "Gegevens voor updaten van het account zijn niet geldig."
not_email: "is geen geldig e-emailadres"
4 changes: 1 addition & 3 deletions config/locales/pl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pl:
validate_sign_up_params: "Proszę dostarczyć odpowiednie dane logowania w ciele zapytania."
validate_account_update_params: "Proszę dostarczyć odpowiednie dane aktualizacji konta w ciele zapytania."
not_email: "nie jest prawidłowym adresem e-mail"
messages:
already_in_use: "już w użyciu"
devise:
mailer:
confirmation_instructions:
Expand All @@ -47,4 +45,4 @@ pl:
unlock_link_msg: "Kliknij poniższy link, aby odblokować konto :"
unlock_link: "Odblokować konto"
hello: "halo"
welcome: "witam"
welcome: "witam"
3 changes: 1 addition & 2 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pt-BR:
successfully_updated: "Senha atualizada com sucesso."
errors:
messages:
already_in_use: "em uso"
validate_sign_up_params: "Os dados submetidos na requisição de cadastro são inválidos."
validate_account_update_params: "Os dados submetidos para atualização de conta são inválidos."
not_email: "não é um e-mail"
Expand All @@ -44,4 +43,4 @@ pt-BR:
unlock_link_msg: "Clique no link abaixo para desbloquear sua conta:"
unlock_link: "Desbloquear minha conta"
hello: "olá"
welcome: "bem-vindo"
welcome: "bem-vindo"
4 changes: 1 addition & 3 deletions config/locales/pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pt:
validate_sign_up_params: "Os dados submetidos na requisição de registo são inválidos."
validate_account_update_params: "Os dados submetidos para atualização de conta são inválidos."
not_email: "não é um e-mail"
messages:
already_in_use: "em uso"
devise:
mailer:
confirmation_instructions:
Expand All @@ -47,4 +45,4 @@ pt:
unlock_link_msg: "Clique no link abaixo para desbloquear sua conta:"
unlock_link: "Desbloquear minha conta"
hello: "olá"
welcome: "bem-vindo"
welcome: "bem-vindo"
1 change: 0 additions & 1 deletion config/locales/ro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ ro:
successfully_updated: "Parola contului a fost schimbată cu succes."
errors:
messages:
already_in_use: "este deja folosit"
validate_sign_up_params: "Trimite credențiale valide în body-ul request-ului."
validate_account_update_params: "Trimite credențiale valide în body-ul request-ului."
not_email: "nu este un email"
Expand Down
7 changes: 3 additions & 4 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ ru:
missing_passwords: "Вы должны заполнить поля 'пароль' и 'повторите пароль'."
successfully_updated: "Ваш пароль успешно обновлён."
errors:
validate_sign_up_params: "Пожалуйста, укажите надлежащие данные для регистрации в теле запроса."
validate_account_update_params: "Пожалуйста, укажите надлежащие данные для обновления учетной записи в теле запроса."
not_email: "не является электронной почтой"
messages:
already_in_use: "уже используется"
validate_sign_up_params: "Пожалуйста, укажите надлежащие данные для регистрации в теле запроса."
validate_account_update_params: "Пожалуйста, укажите надлежащие данные для обновления учетной записи в теле запроса."
not_email: "не является электронной почтой"
devise:
mailer:
confirmation_instructions:
Expand Down
10 changes: 1 addition & 9 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ zh-CN:
successfully_updated: "您的密码已被修改。"
errors:
messages:
already_in_use: "已被使用。"
validate_sign_up_params: "请在request body中填入有效的注册内容"
validate_account_update_params: "请在request body中填入有效的更新帐号资料"
not_email: "这不是一个合适的邮箱。"
Expand All @@ -43,12 +42,5 @@ zh-CN:
account_lock_msg: "由于多次登入失败,我们已锁定你的帐号"
unlock_link_msg: "可以使用下面的链接解锁你的帐号"
unlock_link: "解锁帐号"
activerecord:
errors:
models:
user:
attributes:
email:
already_in_use: "邮箱已被使用"
hello: "你好"
welcome: "欢迎"
welcome: "欢迎"
1 change: 0 additions & 1 deletion config/locales/zh-HK.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ zh-TW:
successfully_updated: "您的密碼已被修改。"
errors:
messages:
already_in_use: "已被使用。"
validate_sign_up_params: "請在request body中填入有效的註冊內容"
validate_account_update_params: "請在request body中填入有效的更新帳號資料"
not_email: "這不是一個合適的電郵。"
Expand Down
1 change: 0 additions & 1 deletion config/locales/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ zh-TW:
successfully_updated: "您的密碼已被修改。"
errors:
messages:
already_in_use: "已被使用。"
validate_sign_up_params: "請在request body中填入有效的註冊內容"
validate_account_update_params: "請在request body中填入有效的更新帳號資料"
not_email: "這不是一個合適的電郵。"
Expand Down
22 changes: 22 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ class UserTest < ActiveSupport::TestCase
end
end

describe 'email uniqueness' do
test 'model should not save if email is taken' do
provider = 'email'

User.create(
email: @email,
provider: provider,
password: @password,
password_confirmation: @password
)

@resource.email = @email
@resource.provider = provider
@resource.password = @password
@resource.password_confirmation = @password

refute @resource.save
assert @resource.errors.messages[:email] == [I18n.t('errors.messages.taken')]
assert @resource.errors.messages[:email].none? { |e| e =~ /translation missing/ }
end
end

describe 'oauth2 authentication' do
test 'model should save even if email is blank' do
@resource.provider = 'facebook'
Expand Down

0 comments on commit 123cfb7

Please sign in to comment.