Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/multi_fetch_fragments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module MultiFetchFragments
end

private

def render_collection_with_multi_fetch_cache

return nil if @collection.blank?
Expand All @@ -22,7 +23,7 @@ def render_collection_with_multi_fetch_cache
keys_to_collection_map = {}

@collection.each do |item|
key = @options[:cache].respond_to?(:call) ? @options[:cache].call(item) : item
key = cache_option.respond_to?(:call) ? cache_option.call(item) : item

key_with_optional_digest = nil
if defined?(@view.fragment_name_with_digest)
Expand All @@ -33,7 +34,6 @@ def render_collection_with_multi_fetch_cache
key_with_optional_digest = key
end


expanded_key = fragment_cache_key(key_with_optional_digest)

keys_to_collection_map[expanded_key] = item
Expand Down Expand Up @@ -78,12 +78,15 @@ def render_collection_with_multi_fetch_cache
results.join(spacer).html_safe
end

def cache_option
@options[:cache] || @locals[:cache]
end

def cache_collection?
cache_option = @options[:cache].presence || @locals[:cache].presence
ActionController::Base.perform_caching && cache_option
end

# from Rails fragment_cache_key in ActionController::Caching::Fragments. Adding it here since it's tucked inside an instance method on the controller, and
# from Rails fragment_cache_key in ActionController::Caching::Fragments. Adding it here since it's tucked inside an instance method on the controller, and
# it's utility could be used in a view without a controller
def fragment_cache_key(key)
ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views)
Expand Down