Sha256: 8c1ade6a40e3c8721adfa88a7386300688a9de57cd703d2217d3a456a6052ad5

Contents?: true

Size: 1.89 KB

Versions: 21

Compression:

Stored size: 1.89 KB

Contents

require 'active_tools/active_record/custom_counter_cache/instance_methods'

module ActiveTools
  module ActiveRecord
    module CustomCounterCache
      extend ::ActiveSupport::Concern
      
      included do
      end
      
      module ClassMethods
        def custom_counter_cache_for(*args)
          mapping = args.extract_options!
          mapping.each do |assoc_name, value|
            assoc_name = assoc_name.to_s
            if assoc_name.last == "*"
              if value.is_a?(Hash)
                assoc_mapping = value.merge(assoc_name => value)
              end
              assoc_name = assoc_name[0..-2]
            else
              assoc_mapping = value
            end
            reflection = reflections[assoc_name.to_s]
          
            unless method_defined? :custom_counter_cache_after_create
              include ActiveRecord::CustomCounterCache::InstanceMethods
            end
          
            after_create lambda { |record|
              record.custom_counter_cache_after_create(assoc_name, reflection, assoc_mapping)
            }

            before_destroy lambda { |record|
              record.custom_counter_cache_before_destroy(assoc_name, reflection, assoc_mapping)
            }

            after_update lambda { |record|
              record.custom_counter_cache_after_update(assoc_name, reflection, assoc_mapping)
            }
          end
        end
      end

      def self.digger(owner, object, mapping)
        object.method_digger(mapping) do |object, key, response, value|
          if response && !response.is_a?(::ActiveRecord::Base)
            count = case value
            when String, Symbol then owner.send(value)
            when Fixnum then value
            end
            yield object, key, count          
          end
        end
      end
      
    end
  end
  
  module OnLoadActiveRecord
    include ActiveRecord::CustomCounterCache
  end
  
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
active_tools-0.0.50 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.42 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.41 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.40 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.39 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.38 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.37 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.36 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.34 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.33 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.32 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.31 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.30 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.29 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.28 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.27 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.26 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.25 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.24 lib/active_tools/active_record/custom_counter_cache.rb
active_tools-0.0.22 lib/active_tools/active_record/custom_counter_cache.rb