summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/models/label.rb5
-rw-r--r--app/models/project_label.rb14
2 files changed, 17 insertions, 2 deletions
diff --git a/app/models/label.rb b/app/models/label.rb
index 0a68be7a30f..f844a3d3378 100644
--- a/app/models/label.rb
+++ b/app/models/label.rb
@@ -24,13 +24,14 @@ class Label < ActiveRecord::Base
# Don't allow ',' for label titles
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
- validates :title, uniqueness: true, unless: :template?
+ validates :title, uniqueness: { scope: [:group_id, :project_id] }
before_save :nullify_priority
default_scope { order(title: :asc) }
- scope :templates, -> { where(template: true) }
+ scope :templates, -> { where(template: true) }
+ scope :with_title, ->(title) { where(title: title) }
def self.prioritized
where.not(priority: nil).reorder(:priority, :title)
diff --git a/app/models/project_label.rb b/app/models/project_label.rb
index 3e41113e340..1171aa2dbb3 100644
--- a/app/models/project_label.rb
+++ b/app/models/project_label.rb
@@ -2,4 +2,18 @@ class ProjectLabel < Label
belongs_to :project
validates :project, presence: true
+
+ validate :title_must_not_exist_at_group_level
+
+ delegate :group, to: :project, allow_nil: true
+
+ private
+
+ def title_must_not_exist_at_group_level
+ return unless group.present?
+
+ if group.labels.with_title(self.title).exists?
+ errors.add(:title, :label_already_exists_at_group_level, group: group.name)
+ end
+ end
end