Navigation Menu

Skip to content

Commit

Permalink
Fixes ordering of the results
Browse files Browse the repository at this point in the history
  • Loading branch information
n8 committed Dec 11, 2012
1 parent b1bed81 commit b394a6f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 25 additions & 8 deletions lib/multi_fetch_fragments.rb
Expand Up @@ -14,9 +14,10 @@ def render_collection_with_multi_fetch_cache
spacer = find_template(@options[:spacer_template]).render(@view, @locals)
end

result = []
results = []

if ActionController::Base.perform_caching && @options[:cache].present?

keys_to_collection_map = {}

@collection.each do |item|
Expand All @@ -35,31 +36,47 @@ def render_collection_with_multi_fetch_cache
if value.present?
collections_object = keys_to_collection_map[key]
@collection.delete(collections_object)

result << value
end
end

non_cached_results = []

# sequentially render any non-cached objects remaining, and cache them
if @collection.any?
collections_objects = @collection.clone
# debugger

collection_objects_clone = @collection.clone

non_cached_results = @template ? collection_with_template : collection_without_template

non_cached_results.each_with_index do |item, index|
collection_object = collections_objects[index]

collection_object = collection_objects_clone[index]
key = collection_to_keys_map[collection_object]

debugger if key.blank?

Rails.cache.write(key, item)
end
end

result += non_cached_results
# re-sort the result according to the keys that were fed in
keys_to_collection_map.each do |key, value|

# was it in the cache?
cached_value = result_hash[key]
if cached_value
results << result_hash[key]
else
results << non_cached_results.shift
end
end

else
result = @template ? collection_with_template : collection_without_template
results = @template ? collection_with_template : collection_without_template
end

result.join(spacer).html_safe
results.join(spacer).html_safe
end

class Railtie < Rails::Railtie
Expand Down
2 changes: 1 addition & 1 deletion mutli_fetch_fragments.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'multi_fetch_fragments'
s.version = '0.0.5'
s.version = '0.0.6'
s.author = 'Nathan Kontny'
s.email = 'nate.kontny@gmail.com'
s.summary = 'multi_fetch_fragments allows you to render a collection of partials through Rails multi read caching mechanism.'
Expand Down

0 comments on commit b394a6f

Please sign in to comment.