summaryrefslogtreecommitdiff
path: root/app/models/project.rb
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-01-16 17:11:55 +0000
committerNick Thomas <nick@gitlab.com>2019-01-16 17:11:55 +0000
commitdedaec13a57abdbeaded37c1ae9526a4f3d82e8b (patch)
tree4d66f74afef4c3b4e7501bb4072e94b7444513c0 /app/models/project.rb
parentc8e3261729189cc26310a32926e74f97c1401b42 (diff)
parent2d9a6f2bd3b4ac55d5e59c8e9eb2013fa46798d9 (diff)
downloadgitlab-ce-dedaec13a57abdbeaded37c1ae9526a4f3d82e8b.tar.gz
Merge branch 'refactor-checking-personal-project-limits' into 'master'
Refactor checking personal project limits See merge request gitlab-org/gitlab-ce!24396
Diffstat (limited to 'app/models/project.rb')
-rw-r--r--app/models/project.rb24
1 files changed, 14 insertions, 10 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index 376f8efacb1..15465d9b356 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -331,7 +331,7 @@ class Project < ActiveRecord::Base
ports: ->(project) { project.persisted? ? VALID_MIRROR_PORTS : VALID_IMPORT_PORTS },
enforce_user: true }, if: [:external_import?, :import_url_changed?]
validates :star_count, numericality: { greater_than_or_equal_to: 0 }
- validate :check_limit, on: :create
+ validate :check_personal_projects_limit, on: :create
validate :check_repository_path_availability, on: :update, if: ->(project) { project.renamed? }
validate :visibility_level_allowed_by_group, if: -> { changes.has_key?(:visibility_level) }
validate :visibility_level_allowed_as_fork, if: -> { changes.has_key?(:visibility_level) }
@@ -809,18 +809,22 @@ class Project < ActiveRecord::Base
::Gitlab::CurrentSettings.mirror_available
end
- def check_limit
- unless creator.can_create_project? || namespace.kind == 'group'
- projects_limit = creator.projects_limit
+ def check_personal_projects_limit
+ # Since this method is called as validation hook, `creator` might not be
+ # present. Since the validation for that will fail, we can just return
+ # early.
+ return if !creator || creator.can_create_project? ||
+ namespace.kind == 'group'
- if projects_limit == 0
- self.errors.add(:limit_reached, "Personal project creation is not allowed. Please contact your administrator with questions")
+ limit = creator.projects_limit
+ error =
+ if limit.zero?
+ _('Personal project creation is not allowed. Please contact your administrator with questions')
else
- self.errors.add(:limit_reached, "Your project limit is #{projects_limit} projects! Please contact your administrator to increase it")
+ _('Your project limit is %{limit} projects! Please contact your administrator to increase it')
end
- end
- rescue
- self.errors.add(:base, "Can't check your ability to create project")
+
+ self.errors.add(:limit_reached, error % { limit: limit })
end
def visibility_level_allowed_by_group