wanelo/counter-cache

View on GitHub
lib/counter/cache/active_record_updater.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Counter
  module Cache
    class ActiveRecordUpdater < Struct.new(:options)
      def after_create(record)
        counter_for(record).update(:incr)
      end

      def after_destroy(record)
        counter_for(record).update(:decr)
      end

      private

      def counter_for(object)
        counter_class.new(object, options)
      end

      def counter_class
        options[:counter_class] || Counter::Cache::Counters::BufferCounter
      end
    end
  end
end