Sha256: df02d56c05b978c5705ab29a4b454cfbda8a25759a3868e57e7ed6feee20d2ed

Contents?: true

Size: 556 Bytes

Versions: 2

Compression:

Stored size: 556 Bytes

Contents

module Counter::Recalculatable
  extend ActiveSupport::Concern

  ####################################################### Support for regenerating the counters
  def recalc!
    with_lock do
      new_value = definition.sum? ? sum_by_sql : count_by_sql
      update! value: new_value
    end
  end

  def count_by_sql
    recalc_scope.count
  end

  def sum_by_sql
    recalc_scope.sum(definition.column_to_count)
  end

  # use this scope when recalculating the value
  def recalc_scope
    parent.association(definition.association_name).scope
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
counterwise-0.1.1 app/models/concerns/counter/recalculatable.rb
counterwise-0.1.0 app/models/concerns/counter/recalculatable.rb