Module: Counter::Recalculatable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Value
- Defined in:
- app/models/concerns/counter/recalculatable.rb
Instance Method Summary collapse
- #count_by_sql ⇒ Object
- #recalc! ⇒ Object
-
#recalc_scope ⇒ Object
use this scope when recalculating the value.
- #sum_by_sql ⇒ Object
Instance Method Details
#count_by_sql ⇒ Object
19 20 21 |
# File 'app/models/concerns/counter/recalculatable.rb', line 19 def count_by_sql recalc_scope.count end |
#recalc! ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/models/concerns/counter/recalculatable.rb', line 4 def recalc! if definition.calculated_value? recalculate_with_value! elsif 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_scope ⇒ Object
use this scope when recalculating the value
28 29 30 |
# File 'app/models/concerns/counter/recalculatable.rb', line 28 def recalc_scope parent.association(definition.association_name).scope end |
#sum_by_sql ⇒ Object
23 24 25 |
# File 'app/models/concerns/counter/recalculatable.rb', line 23 def sum_by_sql recalc_scope.sum(definition.column_to_count) end |