Module: Counter::Xhierarchical
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/counter/Xhierarchical.rb
Class Method Summary collapse
-
.increment_all!(counters, by: 1) ⇒ Object
In a single SQL transaction, increment the counters.
Instance Method Summary collapse
-
#counters_to_update ⇒ Object
Support hierarchy of counters e.g.
-
#dependant_counters ⇒ Object
Override this to add other counters.
- #perform_update!(increment) ⇒ Object
Class Method Details
.increment_all!(counters, by: 1) ⇒ Object
In a single SQL transaction, increment the counters
20 21 22 |
# File 'app/models/concerns/counter/Xhierarchical.rb', line 20 def self.increment_all! counters, by: 1 Counter.lock.where(id: counters).update_all! "value = value + ?, updated_at: NOW()", by end |
Instance Method Details
#counters_to_update ⇒ Object
Support hierarchy of counters e.g. a open counter for an email > a newsletter > a drip_campaign > a site
6 7 8 |
# File 'app/models/concerns/counter/Xhierarchical.rb', line 6 def counters_to_update [self] + dependant_counters.flat_map { |c| c.counters_to_update } end |
#dependant_counters ⇒ Object
Override this to add other counters
11 12 13 |
# File 'app/models/concerns/counter/Xhierarchical.rb', line 11 def dependant_counters [] end |
#perform_update!(increment) ⇒ Object
15 16 17 |
# File 'app/models/concerns/counter/Xhierarchical.rb', line 15 def perform_update! increment Counter.increment_all! counters_to_update, by: increment end |