Sha256: 5c857ee17af18dfaacd2f569f9f721cf1856ccf361ba6b1c1e6ec4f64abe608b
Contents?: true
Size: 927 Bytes
Versions: 1
Compression:
Stored size: 927 Bytes
Contents
module Counter::Recalculatable extend ActiveSupport::Concern 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 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
counterwise-0.1.4 | app/models/concerns/counter/recalculatable.rb |