aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/abstract_controller/details_cache.rb
blob: be1a1c0f349ee5862b4a058b163fe675200a1c14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module AbstractController
  class HashKey
    @hash_keys = Hash.new {|h,k| h[k] = {} }

    def self.get(klass, details)
      @hash_keys[klass][details] ||= new(klass, details)
    end

    attr_reader :hash
    alias_method :eql?, :equal?

    def initialize(klass, details)
      @details, @hash = details, details.hash
    end

    def inspect
      "#<HashKey -- details: #{@details.inspect}>"
    end
  end

  module DetailsCache
    extend ActiveSupport::Concern

    module ClassMethods
      def clear_template_caches!
        ActionView::Partials::PartialRenderer::TEMPLATES.clear
        template_cache.clear
        super
      end

      def template_cache
        @template_cache ||= Hash.new {|h,k| h[k] = {} }
      end
    end

    def render_to_body(*args)
      Thread.current[:format_locale_key] = HashKey.get(self.class, details_for_render)
      super
    end

    private

      def with_template_cache(name, details)
        self.class.template_cache[HashKey.get(self.class, details)][name] ||= super
      end

  end
end