Class: VisitCounter::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/visit-counter/visit_counter.rb

Class Method Summary collapse

Class Method Details

.default_threshold(method) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/visit-counter/visit_counter.rb', line 84

def default_threshold(method)
  if method.to_sym == :static
    10
  elsif method.to_sym == :percent
    0.3
  end
end

.passed_limit?(object, staged_count, diff, name) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/visit-counter/visit_counter.rb', line 62

def passed_limit?(object, staged_count, diff, name)
  method = object.class.visit_counter_threshold_method || :percent
  threshold = object.class.visit_counter_threshold || default_threshold(method)

  if method.to_sym == :static
    diff >= threshold
  elsif method.to_sym == :percent
    return true if staged_count.to_i == 0
    diff.to_f / staged_count.to_f >= threshold
  end
end

.persist(object, staged_count, diff, name) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/visit-counter/visit_counter.rb', line 74

def persist(object, staged_count, diff, name)
  VisitCounter::Store.engine.with_lock(object) do
    object.update_attribute(name, staged_count + diff)
    if object.class.update_callback_method
      object.send(object.class.update_callback_method)
    end
    object.nullify_counter_cache(name, diff)
  end
end