summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-09-12 23:29:08 -0300
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-09-16 10:56:48 -0300
commitc324b3012d45bd66137a1d902c5ab1401a6ae649 (patch)
tree7e0b386e56077908adc4492e8df01552cedca96c
parent5067d7e4a9c31dd531bcd5eb9cca6f25253f1ed1 (diff)
downloadgitlab-ce-c324b3012d45bd66137a1d902c5ab1401a6ae649.tar.gz
Add service to destroy labels for a group/project
-rw-r--r--app/controllers/groups/labels_controller.rb2
-rw-r--r--app/controllers/projects/labels_controller.rb2
-rw-r--r--app/services/labels/destroy_service.rb19
-rw-r--r--spec/services/labels/destroy_service_spec.rb39
4 files changed, 60 insertions, 2 deletions
diff --git a/app/controllers/groups/labels_controller.rb b/app/controllers/groups/labels_controller.rb
index b8fc43eba25..0a0b593a3b5 100644
--- a/app/controllers/groups/labels_controller.rb
+++ b/app/controllers/groups/labels_controller.rb
@@ -44,7 +44,7 @@ class Groups::LabelsController < Groups::ApplicationController
end
def destroy
- @label.destroy
+ Labels::DestroyService.new(@group, current_user).execute(@label)
respond_to do |format|
format.html do
diff --git a/app/controllers/projects/labels_controller.rb b/app/controllers/projects/labels_controller.rb
index ccfb4bcae93..0d8c13abc41 100644
--- a/app/controllers/projects/labels_controller.rb
+++ b/app/controllers/projects/labels_controller.rb
@@ -63,7 +63,7 @@ class Projects::LabelsController < Projects::ApplicationController
end
def destroy
- @label.destroy
+ Labels::DestroyService.new(@project, current_user).execute(@label)
respond_to do |format|
format.html do
diff --git a/app/services/labels/destroy_service.rb b/app/services/labels/destroy_service.rb
new file mode 100644
index 00000000000..2bd065e13cf
--- /dev/null
+++ b/app/services/labels/destroy_service.rb
@@ -0,0 +1,19 @@
+module Labels
+ class DestroyService < Labels::BaseService
+ def execute(label)
+ Label.transaction do
+ destroy_project_labels(label.title) if subject.is_a?(Group)
+ label.destroy
+ end
+ end
+
+ private
+
+ def destroy_project_labels(title)
+ subject.projects.each do |project|
+ label = project.labels.find_by(title: title)
+ label.destroy if label.present?
+ end
+ end
+ end
+end
diff --git a/spec/services/labels/destroy_service_spec.rb b/spec/services/labels/destroy_service_spec.rb
new file mode 100644
index 00000000000..cb6b048862e
--- /dev/null
+++ b/spec/services/labels/destroy_service_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+describe Labels::DestroyService, services: true do
+ describe '#execute' do
+ let(:group) { create(:group) }
+ let!(:project1) { create(:empty_project, group: group) }
+
+ context 'with a group as subject' do
+ let!(:label) { create(:label, subject: group, title: 'Bug') }
+
+ subject(:service) { described_class.new(group, double) }
+
+ it 'removes the label' do
+ expect { service.execute(label) }.to change(group.labels, :count).by(-1)
+ end
+
+ it 'removes the label from projects of the group' do
+ project2 = create(:empty_project, group: group)
+ create(:label, subject: project1, title: 'Bug')
+ create(:label, subject: project2, title: 'Bug')
+
+ service.execute(label)
+
+ expect(project1.labels.where(title: 'Bug')).to be_empty
+ expect(project2.labels.where(title: 'Bug')).to be_empty
+ end
+ end
+
+ context 'with a project as subject' do
+ subject(:service) { described_class.new(project1, double) }
+
+ it 'removes the label' do
+ label = create(:label, subject: project1)
+
+ expect { service.execute(label) }.to change(project1.labels, :count).by(-1)
+ end
+ end
+ end
+end