n8 / multi_fetch_fragments

Multi-fetch Fragments makes rendering and caching a collection of template partials easier and faster.
http://ninjasandrobots.com/rails-faster-partial-rendering-and-caching
MIT License
539 stars 63 forks source link

Compatability with Tire collections #12

Open xpac27 opened 11 years ago

xpac27 commented 11 years ago

Hi ! Thanks for this awesome gem !

I noticed it's not compatible with Tire's collections (the ElasticSearch DSL). For example, if I request a collection of items from tire, then render this collection's partials with cache activated it will work at first. But once I reload the page I get:

NoMethodError: undefined method `delete_if' for #<Tire::Results::Collection:0x007ff3b7845fa0>

This is because cached items are removed from the collection in render_collection_with_multi_fetch_cache using the delete method from ActiveRecords. It doesn't work with Tire's collections since they don't implement this method. However, Tire's collections support Enumerable so it's easy to add the delete method :

module Tire
  module Results
    class Collection
      def delete item
        @results = @results.reject {|i| i == item}
      end
    end
  end
end

I'm not sure if this is a Tire incompatibility or a multi_fetch_fragments one but has you mentioned, in an other issue, that using delete is pretty dangerous, I though it might be useful to use an other technique. But I'm new with your gem so maybe I'm totally wrong :)