Module: RailheadMultifetch

Defined in:
lib/railhead_multifetch.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
# File 'lib/railhead_multifetch.rb', line 6

def self.included(base)
  base.class_eval do
    alias_method_chain :render_collection, :multifetch
  end
end

Instance Method Details

#render_collection_with_multifetchObject



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
# File 'lib/railhead_multifetch.rb', line 12

def render_collection_with_multifetch
  if @view.controller.perform_caching and (@options[:cache].present? or @locals[:cache].present?)
    return nil if @collection.blank?
    results = []
    keymap = {}
    digest = ActionView::PartialDigestor.new(name: @template.virtual_path, finder: @view.lookup_context).digest

    @collection.each do |object|
      key_base = @options[:cache].respond_to?(:call) ? @options[:cache].call(object) : object
      key = @view.controller.fragment_cache_key([key_base, digest])
      keymap[key] = object
    end
    mutable_keys = keymap.keys.map { |key| key.dup }
    cached_results = Rails.cache.read_multi(*mutable_keys)
    @collection = (keymap.keys - cached_results.keys).map { |key| keymap[key] }
    non_cached_results = @collection.present? ? collection_with_template : []
    mutable_keys.each { |key| results << (cached_results[key] || non_cached_results.shift) }

    if @options.key?(:spacer_template)
      spacer = find_template(@options[:spacer_template]).render(@view, @locals)
    end
    results.join(spacer).html_safe
  else
    render_collection_without_multifetch
  end
end