Module: CounterCulture

Defined in:
lib/counter_culture.rb,
lib/counter_culture/counter.rb,
lib/counter_culture/version.rb,
lib/counter_culture/extensions.rb,
lib/counter_culture/reconciler.rb,
lib/counter_culture/skip_updates.rb,
lib/counter_culture/with_connection.rb

Defined Under Namespace

Modules: Extensions, SkipUpdates Classes: Counter, Reconciler, WithConnection

Constant Summary collapse

VERSION =
'3.8.2'.freeze

Class Method Summary collapse

Class Method Details

.aggregate_counter_updatesObject



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
48
49
# File 'lib/counter_culture.rb', line 19

def self.aggregate_counter_updates
  return unless block_given?

  Thread.current[:aggregate_counter_updates] = true
  Thread.current[:aggregated_updates] = {}
  Thread.current[:primary_key_map] = {}

  result = yield

  # aggregate the updates for each target record and execute SQL queries
  Thread.current[:aggregated_updates].each do |klass, attrs|
    attrs.each do |rec_id, updates|
      update_snippets = updates.map do |operation, value|
        value = value.call if value.is_a?(Proc)
        %Q{#{operation} #{value.is_a?(String) ? "'#{value}'" : value}} unless value == 0
      end.compact

      if update_snippets.any?
        klass
          .where(Thread.current[:primary_key_map][klass] => rec_id)
          .update_all(update_snippets.join(', '))
      end
    end
  end

  result
ensure
  Thread.current[:aggregate_counter_updates] = false
  Thread.current[:aggregated_updates] = nil
  Thread.current[:primary_key_map] = nil
end

.config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
# File 'lib/counter_culture.rb', line 14

def self.config
  yield(self) if block_given?
  self
end