Welcome to mirror list, hosted at ThFree Co, Russian Federation.

legacy_counter.rb « counters « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 823f99551682145effe6cfaca62dcbd862b6b354 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

module Gitlab
  module Counters
    # This class is a wrapper over ActiveRecord counter
    # for attributes that have not adopted Redis-backed BufferedCounter.
    class LegacyCounter
      def initialize(counter_record, attribute)
        @counter_record = counter_record
        @attribute = attribute
        @current_value = counter_record.method(attribute).call
      end

      def increment(increment)
        updated = update_counter_record_attribute(increment.amount)

        if updated == 1
          counter_record.execute_after_commit_callbacks
          @current_value += increment.amount
        end

        @current_value
      end

      def bulk_increment(increments)
        total_increment = increments.sum(&:amount)

        updated = update_counter_record_attribute(total_increment)

        if updated == 1
          counter_record.execute_after_commit_callbacks
          @current_value += total_increment
        end

        @current_value
      end

      private

      def update_counter_record_attribute(amount)
        counter_record.class.update_counters(counter_record.id, { attribute => amount })
      end

      attr_reader :counter_record, :attribute
    end
  end
end