Class: VisitCounter::Helper
- Inherits:
-
Object
- Object
- VisitCounter::Helper
- Defined in:
- lib/visit-counter/visit_counter.rb
Class Method Summary collapse
- .default_threshold(method) ⇒ Object
- .passed_limit?(object, staged_count, diff, name) ⇒ Boolean
- .persist(object, staged_count, diff, name) ⇒ Object
Class Method Details
.default_threshold(method) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/visit-counter/visit_counter.rb', line 86 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
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 83 84 |
# File 'lib/visit-counter/visit_counter.rb', line 74 def persist(object, staged_count, diff, name) VisitCounter::Store.engine.with_lock(object) do if object.class.persist_with_callbacks object.send("#{name}=", staged_count + diff ) object.save else object.update_attribute(name, staged_count + diff) end object.nullify_counter_cache(name, diff) end end |