summaryrefslogtreecommitdiff
path: root/app/services/labels/update_service.rb
blob: c7d58266ea91c486766571f1494465e36d490f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Labels
  class UpdateService < Labels::BaseService
    def execute(label)
      Label.transaction do
        previous_title = label.title.dup
        label.update(params)

        return label unless label.valid? && label.group_label?

        if subject.is_a?(Group)
          update_labels(subject.projects, previous_title)
        end

        if subject.is_a?(Project)
          update_labels(subject.group, previous_title)
          update_labels(subject.group.projects - [subject], previous_title)
        end

        label
      end
    end

    private

    def update_labels(subject, title)
      Label.with_type(:group_label)
           .where(subject: subject, title: title)
           .update_all(params)
    end
  end
end