Class: Gitlab::Counters::LegacyCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/counters/legacy_counter.rb

Overview

This class is a wrapper over ActiveRecord counter for attributes that have not adopted Redis-backed BufferedCounter.

Instance Method Summary collapse

Constructor Details

#initialize(counter_record, attribute) ⇒ LegacyCounter

Returns a new instance of LegacyCounter.



8
9
10
11
12
# File 'lib/gitlab/counters/legacy_counter.rb', line 8

def initialize(counter_record, attribute)
  @counter_record = counter_record
  @attribute = attribute
  @current_value = counter_record.method(attribute).call
end

Instance Method Details

#bulk_increment(increments) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/counters/legacy_counter.rb', line 25

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

#increment(increment) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/counters/legacy_counter.rb', line 14

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