Class: Occurro::Counter

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/occurro/counter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.increment_counters(model, count = 1) ⇒ Object

Public: increments ‘model’ counters by a ‘count’ factor



35
36
37
38
39
40
41
42
43
44
# File 'app/models/occurro/counter.rb', line 35

def self.increment_counters(model, count = 1)
  counter = Occurro::Counter.find_or_create_by_countable_type_and_countable_id(model.class.base_class.name, model.id)
  counter.update_attributes({
    :today      => counter.today      + count,
    :this_week  => counter.this_week  + count,
    :this_month => counter.this_month + count,
    :total      => counter.total      + count
  }) 
  Occurro::DailyCounter.increment_counters(model, count) if model.class.use_daily_counters?
end

Instance Method Details

#update_counter(period_type) ⇒ Object

Public: Rotate the counter value for the period_type

  • period_type: :daily #=> daily rotation :weekly #=> weekly rotation :monthly #=> monthly rotation



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/occurro/counter.rb', line 15

def update_counter(period_type)
  column_from, column_to =
    case period_type.to_s
    when 'daily'
      ['today', 'yesterday']
    when 'weekly'
      ['this_week', 'last_week']
    when 'monthly'
      ['this_month', 'last_month']
    else
      return false
    end

  self.send("#{column_to}=", self.send(column_from))
  self.send("#{column_from}=", 0)
  self.save 
end