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
25 26 27 |
# File 'app/models/concerns/counter/recalculatable.rb', line 25 def count_by_sql recalc_scope.count end |
#recalc! ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 self.class.upsert( attributes.without("id", "created_at", "updated_at").symbolize_keys.merge(value: new_value), unique_by: [:parent_type, :parent_id, :name], on_duplicate: Arel.sql("value = counter_values.value + EXCLUDED.value"), record_timestamps: true ) reload end end end |
#recalc_scope ⇒ Object
use this scope when recalculating the value
34 35 36 |
# File 'app/models/concerns/counter/recalculatable.rb', line 34 def recalc_scope parent.association(definition.association_name).scope end |
#sum_by_sql ⇒ Object
29 30 31 |
# File 'app/models/concerns/counter/recalculatable.rb', line 29 def sum_by_sql recalc_scope.sum(definition.column_to_count) end |