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



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

def count_by_sql
  recalc_scope.count
end

#recalc!Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/concerns/counter/recalculatable.rb', line 4

def recalc!
  if definition.calculated?
    calculate!
  elsif definition.manual?
    raise Counter::Error.new("Can't recalculate a manual counter")
  else
    with_lock do
      new_value = definition.sum? ? sum_by_sql : count_by_sql
      update! value: new_value
    end
  end
end

#recalc_scopeObject

use this scope when recalculating the value



26
27
28
# File 'app/models/concerns/counter/recalculatable.rb', line 26

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

#sum_by_sqlObject



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

def sum_by_sql
  recalc_scope.sum(definition.column_to_count)
end