Module: StaticActiveRecordContext

Defined in:
lib/static_active_record_context.rb

Overview

StaticActiveRecordContext

Simple module to extends technoweenie’s rad ActiveRecordContext plugin svn.techno-weenie.net/projects/plugins/active_record_context/ to permanently cache active record data for the life of the class

As with active_record_context, only finds based on ids are cache hits, however id finders are the majority of calls from associations. If cache hits on fields and methods are needed, refer to acts_as_static_record

class TelephoneCarriers < ActiveRecord::Base
  extend StaticActiveRecordContext
  has_many :phone_numbers
end

The following would exercise a cache hit

phone_number.telephone_carrier

The static cache is available both inside and outside with_context block, where as the cache for typical records the context is only with the block.

PhoneNumber.with_context {
  PhoneNumber.find :all
  TelephoneCarriers.find :all
}

phone = PhoneNumber.find_by_id(1)             # not a cache hit
phone.telephone_carrier                       # cache hit
telephone_carrier = TelephoneCarrier.find(1)  # cache hit

Developers

Homepage

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc



39
40
41
# File 'lib/static_active_record_context.rb', line 39

def self.extended(base)#:nodoc
  base.class_inheritable_accessor :static_record_context
end

Instance Method Details

#context_cacheObject

:nodoc:



43
44
45
# File 'lib/static_active_record_context.rb', line 43

def context_cache#:nodoc:
  self.static_record_context ||= {}
end

#context_cache=(map) ⇒ Object

:nodoc:



47
48
49
# File 'lib/static_active_record_context.rb', line 47

def context_cache=(map)#:nodoc:
  self.static_record_context = map unless map.nil?
end

#reload_context_cacheObject

Reload the cache for this class only



52
53
54
# File 'lib/static_active_record_context.rb', line 52

def reload_context_cache
  self.static_record_context = {}
end

#with_context(&block) ⇒ Object

Call ActiveRecord::Base to cache the other objects



57
58
59
# File 'lib/static_active_record_context.rb', line 57

def with_context(&block)#:nodoc:
  ActiveRecord::Base.with_context(&block)
end