Module: Counter::Recalculatable

Extended by:
ActiveSupport::Concern
Included in:
Value
Defined in:
app/models/concerns/counter/recalculatable.rb

Instance Method Summary collapse

Instance Method Details

#count_by_sqlObject



12
13
14
# File 'app/models/concerns/counter/recalculatable.rb', line 12

def count_by_sql
  recalc_scope.count
end

#recalc!Object

Support for regenerating the counters



5
6
7
8
9
10
# File 'app/models/concerns/counter/recalculatable.rb', line 5

def recalc!
  with_lock do
    new_value = definition.sum? ? sum_by_sql : count_by_sql
    update! value: new_value
  end
end

#recalc_scopeObject

use this scope when recalculating the value



21
22
23
# File 'app/models/concerns/counter/recalculatable.rb', line 21

def recalc_scope
  parent.association(definition.association_name).scope
end

#sum_by_sqlObject



16
17
18
# File 'app/models/concerns/counter/recalculatable.rb', line 16

def sum_by_sql
  recalc_scope.sum(definition.column_to_count)
end