Method: ActionController::Base#caching_key

Defined in:
lib/interlock/action_controller.rb

#caching_key(ignore = nil, tag = nil) ⇒ Object

Build the fragment key from a particular context. This must be deterministic and stateful except for the tag. We can’t scope the key to arbitrary params because the view doesn’t have access to which are relevant and which are not.

Note that the tag can be pretty much any object. Define to_interlock_tag if you need custom tagging for some class. ActiveRecord::Base already has it defined appropriately.

If you pass an Array of symbols as the tag, it will get value-mapped onto params and sorted. This makes granular scoping easier, although it doesn’t sidestep the normal blanket invalidations.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/interlock/action_controller.rb', line 14

def caching_key(ignore = nil, tag = nil)
  ignore = Array(ignore)
  ignore = Interlock::SCOPE_KEYS if ignore.include? :all    
  
  if (Interlock::SCOPE_KEYS - ignore).empty? and !tag
    raise Interlock::UsageError, "You must specify a :tag if you are ignoring the entire default scope."
  end
    
  if tag.is_a? Array and tag.all? {|x| x.is_a? Symbol}
    tag = tag.sort_by do |key|
      key.to_s
    end.map do |key| 
      params[key].to_interlock_tag
    end.join(";")
  end
  
  Interlock.caching_key(      
    ignore.include?(:controller) ? 'any' : controller_name,
    ignore.include?(:action) ? 'any' : action_name,
    ignore.include?(:id) ? 'all' : params[:id],
    tag
  )
end