module RecordCache::ActiveRecordExtension
Public Class Methods
extended(mod)
click to toggle source
# File lib/record_cache.rb, line 209 def self.extended(mod) mod.send(:class_inheritable_accessor, :cached_indexes) end
Public Instance Methods
record_cache(*args)
click to toggle source
# File lib/record_cache.rb, line 213 def record_cache(*args) extend RecordCache::ClassMethods include RecordCache::InstanceMethods opts = args.pop opts[:fields] = args opts[:class] = self field_lookup = opts.delete(:field_lookup) || [] index = RecordCache::Index.new(opts) add_cached_index(index) first_index = (cached_indexes.size == 1) (class << self; self; end).module_eval do if index.includes_id? [:first, :all, :set, :raw, :ids].each do |type| next if type == :ids and index.name == 'by_id' define_method( index.find_method_name(type) ) do |keys| if self.send(:scope,:find) and self.send(:scope,:find).any? self.method_missing(index.find_method_name(type), keys) else index.find_by_field(keys, self, type) end end end end if not index.auto_name? and not index.full_record? field = index.fields.first if index.fields.size == 1 define_method( "all_#{index.name.pluralize}_by_#{index.index_field}" ) do |keys| index.field_lookup(keys, self, field, :all) end define_method( "#{index.name.pluralize}_by_#{index.index_field}" ) do |keys| index.field_lookup(keys, self, field) end define_method( "#{index.name.singularize}_by_#{index.index_field}" ) do |keys| index.field_lookup(keys, self, field, :first) end end if index.auto_name? (field_lookup + index.fields).each do |field| next if field == index.index_field plural_field = field.pluralize prefix = index.prefix prefix = "#{prefix}_" if prefix define_method( "all_#{prefix}#{plural_field}_by_#{index.index_field}" ) do |keys| index.field_lookup(keys, self, field, :all) end define_method( "#{prefix}#{plural_field}_by_#{index.index_field}" ) do |keys| index.field_lookup(keys, self, field) end define_method( "#{prefix}#{field}_by_#{index.index_field}" ) do |keys| index.field_lookup(keys, self, field, :first) end end end if first_index alias_method_chain :find, :caching alias_method_chain :update_all, :invalidate alias_method_chain :delete_all, :invalidate end end if first_index after_save :invalidate_record_cache_deferred after_destroy :invalidate_record_cache_deferred after_commit :complete_deferred_record_cache_invalidations after_rollback :complete_deferred_record_cache_invalidations end end