diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-09-21 17:47:58 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-19 14:58:24 -0200 |
commit | e28058c4107ce454a84b3e3b5750f936dace7db1 (patch) | |
tree | 8eb12341289e76b94907a12067ad8dc85b07f71e | |
parent | cfedd42badc6b84457d1de35cb31988777462d5a (diff) | |
download | gitlab-ce-e28058c4107ce454a84b3e3b5750f936dace7db1.tar.gz |
Validate if project label title does not exist at group level
-rw-r--r-- | app/models/label.rb | 5 | ||||
-rw-r--r-- | app/models/project_label.rb | 14 | ||||
-rw-r--r-- | config/locales/en.yml | 1 | ||||
-rw-r--r-- | spec/factories/labels.rb | 6 | ||||
-rw-r--r-- | spec/models/label_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/project_label_spec.rb | 34 |
6 files changed, 59 insertions, 3 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 diff --git a/config/locales/en.yml b/config/locales/en.yml index cedb5e207bd..12a59be79f0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -5,6 +5,7 @@ en: hello: "Hello world" errors: messages: + label_already_exists_at_group_level: "already exists at group level for %{group}. Please choose another one." wrong_size: "is the wrong size (should be %{file_size})" size_too_small: "is too small (should be at least %{file_size})" size_too_big: "is too big (should be at most %{file_size})" diff --git a/spec/factories/labels.rb b/spec/factories/labels.rb index ec4c56457ea..5c789d72bac 100644 --- a/spec/factories/labels.rb +++ b/spec/factories/labels.rb @@ -4,4 +4,10 @@ FactoryGirl.define do color "#990000" project end + + factory :group_label, class: GroupLabel do + sequence(:title) { |n| "label#{n}" } + color "#990000" + group + end end diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb index 1f1fe45d5a7..ab640e216cf 100644 --- a/spec/models/label_spec.rb +++ b/spec/models/label_spec.rb @@ -13,7 +13,7 @@ describe Label, models: true do end describe 'validation' do - it { is_expected.to validate_uniqueness_of(:title) } + it { is_expected.to validate_uniqueness_of(:title).scoped_to([:group_id, :project_id]) } it 'validates color code' do is_expected.not_to allow_value('G-ITLAB').for(:color) diff --git a/spec/models/project_label_spec.rb b/spec/models/project_label_spec.rb index 93062b9d402..355bb2a938c 100644 --- a/spec/models/project_label_spec.rb +++ b/spec/models/project_label_spec.rb @@ -7,5 +7,39 @@ describe ProjectLabel, models: true do describe 'validations' do it { is_expected.to validate_presence_of(:project) } + + context 'validates if title must not exist at group level' do + let(:group) { create(:group, name: 'gitlab-org') } + let(:project) { create(:empty_project, group: group) } + + before do + create(:group_label, group: group, title: 'Bug') + end + + it 'returns error if title already exists at group level' do + label = described_class.new(project: project, title: 'Bug') + + label.valid? + + expect(label.errors[:title]).to include 'already exists at group level for gitlab-org. Please choose another one.' + end + + it 'does not returns error if title does not exist at group level' do + label = described_class.new(project: project, title: 'Security') + + label.valid? + + expect(label.errors[:title]).to be_empty + end + + it 'does not returns error if project does not belong to group' do + another_project = create(:empty_project) + label = described_class.new(project: another_project, title: 'Bug') + + label.valid? + + expect(label.errors[:title]).to be_empty + end + end end end |