public
Description: simple identity map for active record. eager loading associations FTL
Clone URL: git://github.com/technoweenie/active_record_context.git
Click here to lend your support to: active_record_context and make a donation at www.pledgie.com !
add #cached method as a shortcut for getting cached records for a model

git-svn-id: 
http://svn.techno-weenie.net/projects/plugins/active_record_context@2836 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Sat Apr 07 16:25:15 -0700 2007
commit  a50087b83300a43f9ce15d91cf02dd85d5bf9a7e
tree    ade68c521822068eddb232719b23828fd693b105
parent  dd59f8aa8ca5573285624e85641d938f7dfdc287
...
17
18
19
20
21
22
 
 
 
23
24
25
26
 
 
27
28
29
30
31
32
 
33
34
35
...
17
18
19
 
 
 
20
21
22
23
24
 
 
25
26
27
28
29
30
31
 
32
33
34
35
0
@@ -17,19 +17,19 @@ module Technoweenie
0
     end
0
     
0
     def find_one_with_context(id, options)
0
- cached = options[:conditions].nil? && find_in_context(id)
0
- logger.debug "[Context] Found #{name} ##{id}}" if log_context_activity && cached
0
- cached ? cached : find_one_without_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
     end
0
 
0
- def find_in_context(id)
0
- context_cache && context_cache[self] && context_cache[self][id.to_i]
0
+ def cached
0
+ context_cache ? (context_cache[self] ||= {}) : {}
0
     end
0
     
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(context_cache[self] ||= {}) do |memo, record|
0
+ records.inject(cached) do |memo, record|
0
         memo.update record.id => record
0
       end
0
     end

Comments

    No one has commented yet.