diff options
-rw-r--r-- | app/services/labels/base_service.rb | 30 | ||||
-rw-r--r-- | app/services/labels/destroy_service.rb | 38 | ||||
-rw-r--r-- | app/services/labels/toggle_subscription_service.rb | 38 | ||||
-rw-r--r-- | app/services/labels/update_service.rb | 38 | ||||
-rw-r--r-- | spec/services/labels/destroy_service_spec.rb | 4 |
5 files changed, 45 insertions, 103 deletions
diff --git a/app/services/labels/base_service.rb b/app/services/labels/base_service.rb index ff5d3f7a23b..d5aa3d97c94 100644 --- a/app/services/labels/base_service.rb +++ b/app/services/labels/base_service.rb @@ -8,6 +8,36 @@ module Labels attr_reader :subject, :user, :params + def replicate_global_label(label_type, title, &block) + if subject.nil? + block.call(find_labels(Group.all, label_type, title)) + block.call(find_labels(Project.all, label_type, title)) + end + + if subject.is_a?(Group) + block.call(find_labels(nil, label_type, title)) + block.call(find_labels(Group.where.not(id: subject), label_type, title)) + block.call(find_labels(Project.all, label_type, title)) + end + + if subject.is_a?(Project) + block.call(find_labels(nil, label_type, title)) + block.call(find_labels(Group.all, label_type, title)) + block.call(find_labels(Project.where.not(id: subject), label_type, title)) + end + end + + def replicate_group_label(label_type, title, &block) + if subject.is_a?(Group) + block.call(find_labels(subject.projects, label_type, title)) + end + + if subject.is_a?(Project) + block.call(find_labels(subject.group, label_type, title)) + block.call(find_labels(subject.group.projects.where.not(id: subject), label_type, title)) + end + end + def find_labels(subject, label_type, title) Label.with_type(label_type).where(subject: subject, title: title) end diff --git a/app/services/labels/destroy_service.rb b/app/services/labels/destroy_service.rb index 16d07e9687b..4540c42a199 100644 --- a/app/services/labels/destroy_service.rb +++ b/app/services/labels/destroy_service.rb @@ -6,45 +6,15 @@ module Labels return if label.project_label? - destroy_global_label(label.label_type, label.title) if label.global_label? - destroy_group_label(label.label_type, label.title) if label.group_label? + replicate_global_label(label.label_type, label.title, &destroy) if label.global_label? + replicate_group_label(label.label_type, label.title, &destroy) if label.group_label? end end private - def destroy_global_label(label_type, title) - if subject.nil? - destroy_labels(Group.all, label_type, title) - destroy_labels(Project.all, label_type, title) - end - - if subject.is_a?(Group) - destroy_labels(nil, label_type, title) - destroy_labels(Group.where.not(id: subject), label_type, title) - destroy_labels(Project.all, label_type, title) - end - - if subject.is_a?(Project) - destroy_labels(nil, label_type, title) - destroy_labels(Group.all, label_type, title) - destroy_labels(Project.where.not(id: subject), label_type, title) - end - end - - def destroy_group_label(label_type, title) - if subject.is_a?(Group) - destroy_labels(subject.projects, label_type, title) - end - - if subject.is_a?(Project) - destroy_labels(subject.group, label_type, title) - destroy_labels(subject.group.projects.where.not(id: subject), label_type, title) - end - end - - def destroy_labels(subject, label_type, title) - find_labels(subject, label_type, title).each(&:destroy) + def destroy + Proc.new { |labels| labels.each(&:destroy) } end end end diff --git a/app/services/labels/toggle_subscription_service.rb b/app/services/labels/toggle_subscription_service.rb index c643c82c3f2..63897e875b4 100644 --- a/app/services/labels/toggle_subscription_service.rb +++ b/app/services/labels/toggle_subscription_service.rb @@ -6,45 +6,17 @@ module Labels return if label.project_label? - toggle_subscription_to_global_label(label.label_type, label.title) if label.global_label? - toggle_subscription_to_group_label(label.label_type, label.title) if label.group_label? + replicate_global_label(label.label_type, label.title, &toggle_subscription) if label.global_label? + replicate_group_label(label.label_type, label.title, &toggle_subscription) if label.group_label? end end private - def toggle_subscription_to_global_label(label_type, title) - if subject.nil? - toggle_subscription(Group.all, label_type, title) - toggle_subscription(Project.all, label_type, title) + def toggle_subscription + Proc.new do |labels| + labels.each { |label| label.toggle_subscription(user) } end - - if subject.is_a?(Group) - toggle_subscription(nil, label_type, title) - toggle_subscription(Group.where.not(id: subject), label_type, title) - toggle_subscription(Project.all, label_type, title) - end - - if subject.is_a?(Project) - toggle_subscription(nil, label_type, title) - toggle_subscription(Group.all, label_type, title) - toggle_subscription(Project.where.not(id: subject), label_type, title) - end - end - - def toggle_subscription_to_group_label(label_type, title) - if subject.is_a?(Group) - toggle_subscription(subject.projects, label_type, title) - end - - if subject.is_a?(Project) - toggle_subscription(subject.group, label_type, title) - toggle_subscription(subject.group.projects.where.not(id: subject), label_type, title) - end - end - - def toggle_subscription(subject, label_type, title) - find_labels(subject, label_type, title).each { |label| label.toggle_subscription(user) } end end end diff --git a/app/services/labels/update_service.rb b/app/services/labels/update_service.rb index 12f40120822..eb46e3be86d 100644 --- a/app/services/labels/update_service.rb +++ b/app/services/labels/update_service.rb @@ -8,8 +8,8 @@ module Labels return label unless label.valid? - replicate_global_label(label.label_type, previous_title) if label.global_label? - replicate_group_label(label.label_type, previous_title) if label.group_label? + replicate_global_label(label.label_type, previous_title, &update_all) if label.global_label? + replicate_group_label(label.label_type, previous_title, &update_all) if label.group_label? label end @@ -17,38 +17,8 @@ module Labels private - def replicate_global_label(label_type, title) - if subject.nil? - replicate_label(Group.all, label_type, title) - replicate_label(Project.all, label_type, title) - end - - if subject.is_a?(Group) - replicate_label(nil, label_type, title) - replicate_label(Group.where.not(id: subject), label_type, title) - replicate_label(Project.all, label_type, title) - end - - if subject.is_a?(Project) - replicate_label(nil, label_type, title) - replicate_label(Group.all, label_type, title) - replicate_label(Project.where.not(id: subject), label_type, title) - end - end - - def replicate_group_label(label_type, title) - if subject.is_a?(Group) - replicate_label(subject.projects, label_type, title) - end - - if subject.is_a?(Project) - replicate_label(subject.group, label_type, title) - replicate_label(subject.group.projects.where.not(id: subject), label_type, title) - end - end - - def replicate_label(subject, label_type, title) - find_labels(subject, label_type, title).update_all(params) + def update_all + Proc.new { |labels| labels.update_all(params) } end end end diff --git a/spec/services/labels/destroy_service_spec.rb b/spec/services/labels/destroy_service_spec.rb index 9452fdf6df2..4977153db55 100644 --- a/spec/services/labels/destroy_service_spec.rb +++ b/spec/services/labels/destroy_service_spec.rb @@ -52,8 +52,8 @@ describe Labels::DestroyService, services: true do service.execute(label) - expect(project1.labels.where(title: 'Bug')).to be_empty - expect(project2.labels.where(title: 'Bug')).to be_empty + expect(project1.reload.labels).to be_empty + expect(project2.reload.labels).to be_empty end context 'inherited from a global label' do |