From c207b4bb330463c39c202d1c66e2447b55332093 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 8 Jun 2017 15:22:01 +0200 Subject: Fix db:seed - only run some validations when the field was changed (#3592) * Fix db:seed - only run some validations when the field was changed * Add tests --- app/models/account.rb | 12 ++++++++---- app/models/user.rb | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 918d5b0f1..214d3b9fd 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -52,13 +52,17 @@ class Account < ApplicationRecord has_one :user, inverse_of: :account validates :username, presence: true - validates :username, uniqueness: { scope: :domain, case_sensitive: true }, unless: :local? + + # Remote user validations + with_options unless: :local? do + validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: :username_changed? + end # Local user validations with_options if: :local? do - validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, unreserved: true - validates :display_name, length: { maximum: 30 } - validates :note, length: { maximum: 160 } + validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, unreserved: true, if: :username_changed? + validates :display_name, length: { maximum: 30 }, if: :display_name_changed? + validates :note, length: { maximum: 160 }, if: :note_changed? end # Timelines diff --git a/app/models/user.rb b/app/models/user.rb index c0600fe75..0fd3983b4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -47,7 +47,7 @@ class User < ApplicationRecord accepts_nested_attributes_for :account validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale? - validates :email, email: true + validates :email, email: true, if: :email_changed? scope :recent, -> { order(id: :desc) } scope :admins, -> { where(admin: true) } -- cgit