Sha256: 171605420e4fceba9ae7288a609a1752fd72a7339ed4e9c3b94d842a40885577

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

require 'active_support/concern'
require 'active_support/lazy_load_hooks'

require 'counter_culture/version'
require 'counter_culture/extensions'
require 'counter_culture/counter'
require 'counter_culture/reconciler'
require 'counter_culture/skip_updates'

module CounterCulture
  mattr_accessor :batch_size
  self.batch_size = 1000

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

  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
end

# extend ActiveRecord with our own code here
ActiveSupport.on_load(:active_record) do
  include CounterCulture::Extensions
  include CounterCulture::SkipUpdates
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
counter_culture-3.8.2 lib/counter_culture.rb
counter_culture-3.8.1 lib/counter_culture.rb
counter_culture-3.8.0 lib/counter_culture.rb
counter_culture-3.7.0 lib/counter_culture.rb