0
module ActiveRecordContext
0
- def self.extended(base)
0
- alias_method_chain :find_every, :context
0
- alias_method_chain :find_one, :context
0
+ def self.included(base)
0
+ base.extend ClassMethods
0
+ base.alias_method_chain :reload, :context
0
+ base.cattr_accessor :context_cache
0
- mattr_accessor :log_context_activity
0
- mattr_reader :context_cache
0
- # Preloads the record from the given array of IDs. The ids should all be the same type.
0
- # You can pass an array of active record models if that model belongs to the current one.
0
- # users = User.find :all
0
- # Avatar.prefetch users # performs this automatically: users.collect { |user| user.avatar_id }
0
- return [] if ids.blank?
0
- ids = ids.collect { |record| record.send(prefetch_default) } if initial.respond_to?(prefetch_default)
0
- find :all, :conditions => { :id => ids }
0
- # defaults to the foreign key of the current model
0
- @prefetch_default ||= name.foreign_key
0
- def find_every_with_context(options)
0
- returning find_every_without_context(options) do |records|
0
- store_in_context records
0
+ def self.extended(base)
0
+ alias_method_chain :find_every, :context
0
+ alias_method_chain :find_one, :context
0
- def find_one_with_context(id, options)
0
- record = options[:conditions].nil? && cached[id.to_i]
0
- logger.debug("[Context] #{record ? :Found : :Missed} #{name} ##{id}") if log_context_activity
0
- record ? record : find_one_without_context(id, options)
0
- context_cache ? (context_cache[self.base_class] ||= {}) : {}
0
- def store_in_context(records)
0
- return if context_cache.nil?
0
- logger.debug "[Context] Storing #{name} records: #{records.collect(&:id).to_sentence}" if log_context_activity
0
- records.inject(cached) do |memo, record|
0
- memo.update record.id => record
0
+ # Preloads the record from the given array of IDs. The ids should all be the same type.
0
+ # You can pass an array of active record models if that model belongs to the current one.
0
+ # users = User.find :all
0
+ # Avatar.prefetch users # performs this automatically: users.collect { |user| user.avatar_id }
0
+ return [] if ids.blank?
0
+ ids = ids.collect { |record| record.send(prefetch_default) } if initial.respond_to?(prefetch_default)
0
+ find :all, :conditions => { :id => ids }
0
+ # defaults to the foreign key of the current model
0
+ @prefetch_default ||= name.foreign_key
0
+ def find_every_with_context(options)
0
+ returning find_every_without_context(options) do |records|
0
+ store_in_context records
0
+ def find_one_with_context(id, options)
0
+ record = options[:conditions].nil? && cached[id.to_i]
0
+ logger.debug("[Context] #{record ? :Found : :Missed} #{name} ##{id}")
0
+ record ? record : find_one_without_context(id, options)
0
+ context_cache ? (context_cache[self.base_class] ||= {}) : {}
0
+ def store_in_context(records)
0
+ return if context_cache.nil?
0
+ logger.debug "[Context] Storing #{name} records: #{records.collect(&:id).to_sentence}"
0
+ records.inject(cached) do |memo, record|
0
+ memo.update record.id => record
0
+ # Enables the context cache inside this block.
0
+ self.context_cache = {}
0
+ self.context_cache = nil
0
- # Enables the context cache inside this block.
0
+ def reload_with_context(options = nil)
0
+ self.class.cached[id.to_i] = nil
0
+ reload_without_context(options)
0
\ No newline at end of file
Comments
No one has commented yet.