diff options
author | Douwe Maan <douwe@selenight.nl> | 2018-01-31 19:51:05 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2018-02-06 21:06:05 +0300 |
commit | 8d69436c901a3eee674e72c67d91de3994d30e0c (patch) | |
tree | c8071b0c0c78f2f711d51999793a2b66d9a9aea9 /app/models/user.rb | |
parent | 2d5f10b2a7f05b29ca5869b2d1acb53d5bcbf50b (diff) |
Validate user namespace before saving so that errors persist on model
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 4996cea718c..1df3772e63f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -77,7 +77,7 @@ class User < ActiveRecord::Base # # Namespace for personal projects - has_one :namespace, -> { where(type: nil) }, dependent: :destroy, foreign_key: :owner_id, autosave: true # rubocop:disable Cop/ActiveRecordDependent + has_one :namespace, -> { where(type: nil) }, dependent: :destroy, foreign_key: :owner_id, inverse_of: :owner, autosave: true # rubocop:disable Cop/ActiveRecordDependent # Profile has_many :keys, -> do @@ -171,7 +171,7 @@ class User < ActiveRecord::Base before_save :ensure_user_rights_and_limits, if: ->(user) { user.new_record? || user.external_changed? } before_save :skip_reconfirmation!, if: ->(user) { user.email_changed? && user.read_only_attribute?(:email) } before_save :check_for_verified_email, if: ->(user) { user.email_changed? && !user.new_record? } - after_save :ensure_namespace_correct + before_validation :ensure_namespace_correct after_update :username_changed_hook, if: :username_changed? after_destroy :post_destroy_hook after_destroy :remove_key_cache @@ -884,16 +884,10 @@ class User < ActiveRecord::Base end def ensure_namespace_correct - # Ensure user has namespace - create_namespace!(path: username, name: username) unless namespace - - if username_changed? - unless namespace.update_attributes(path: username, name: username) - namespace.errors.each do |attribute, message| - self.errors.add(:"namespace_#{attribute}", message) - end - raise ActiveRecord::RecordInvalid.new(namespace) - end + if namespace + namespace.path = namespace.name = username if username_changed? + else + build_namespace(path: username, name: username) end end |