Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10733 (closed defect: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] r8542 breaks caching

Reported by: Catfish Assigned to: core
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: major Keywords: cache rjs verified
Cc:

Description

Is anyone else having problems with the cache() method, changed in r8542?

It tries to get the template extension using first_render1.to_sym ... as far as I can tell, first_render never includes the extension : it's always 'welcome/index' rather than 'welcome/index.html.erb'. As such, using the cache() method tries to do nil.to_sym and breaks.

Attachments

functional_caching.diff (4.9 kB) - added by Catfish on 01/11/08 16:25:17.
cache_extension.diff (1.7 kB) - added by tcoppock on 01/23/08 16:55:13.
allows caching from RJS
cache_extension_r8787.diff (1.7 kB) - added by mpalmer on 02/02/08 23:09:41.
Update of tcoppock's patch against r8787

Change History

01/07/08 11:23:06 changed by Catfish

Grr, pesky formatting. That should have been

first_render[/\.(\w+)$/, 1].to_sym

01/08/08 13:14:20 changed by mindforge

I ran into the same issue.

Substituting the fragment Catfish mentions with

find_template_extension_for(first_render)[/\.(\w+)$/, 1].to_sym

returns what seems to be what the code wants.

However, I am using HAML, which does not provide a cache_haml_fragment method. Caching worked fine prior to the change via cache_erb_fragment. So I suggest changing the cache helper to call cache_erb_fragment by default. That's what it did before the change, so why break it?

01/08/08 20:41:53 changed by willcodeforfoo

I'm seeing this as well, rendering a partial inside a cache block. At line 35 of cache_helper.rb first_render is "dashboard/_sidebar" in my case, which the regexp can't extract an extension from.

01/10/08 00:47:11 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [8614]) Fix CacheHelper#cache (closes #10733) [mindforge]

01/10/08 14:38:28 changed by Catfish

  • status changed from closed to reopened.
  • resolution deleted.

This still breaks for single-extension templates (eg index.erb rather than index.html.erb), where find_template_extension_for(first_render) returns nil

01/11/08 16:24:35 changed by Catfish

I've attached a preliminary patch that adds a few functional caching tests. At present, caching is only tested in a very unit-testy way, never for testing that a controller will correctly cache something. There's only a couple of tests there at the moment, but they give test coverage for things that are completely untested otherwise - eg cache_helper.rb.

The patch also includes two cache_helper fixes - handler_for_extension should be handler_class_for_extension, and I've made the '.' in the template extension optional, so it will correctly deal with both 'html.erb' and just 'erb'

Finally, there's a failing test case in there that displays my cache problems. Trying to cache an erb partial that's rendered from an rjs template will fail, since find_template_extension_for(first_render) returns nil. I suspect this is an ActionController bug - shouldn't it be using the full path (complete with an extension) when calling @template.render_file in AC's base.rb:1100 ?

01/11/08 16:25:17 changed by Catfish

  • attachment functional_caching.diff added.

01/20/08 04:55:47 changed by tcoppock

@template.render_file does not have to use the full_path. It normally is able to correctly infer the extension/format of the file without it.

I think what's happening is that when the the RJS template goes for rendering, the variables that allows it to assume '.js.rjs' as part of the filename is set to what the ERB partial needed. So, find_template_extension_for(first_render) nils out since the parent file wasn't ERB.

render_template knows what the correct extension is for the file is. Rather than incorrectly infer from first_render, the cache helper can use what render_template already knew. That's what this patch does.

01/20/08 05:02:56 changed by tcoppock

  • summary changed from r8542 breaks caching to [PATCH] r8542 breaks caching.

01/20/08 16:50:32 changed by Catfish

Looks good to me. +1

01/22/08 22:28:14 changed by tcoppock

  • keywords set to cache rjs.

01/23/08 11:58:05 changed by chuyeow

+1. Patch fixes test_fragment_caching_in_rjs_partials in caching_test.rb.

01/23/08 16:55:13 changed by tcoppock

  • attachment cache_extension.diff added.

allows caching from RJS

01/23/08 16:56:13 changed by tcoppock

Fixed the diff to use relative paths to actionpack. No other change.

01/29/08 10:15:56 changed by zsombor

I think making cache_erb_fragment default defeated the purpose of cache_TEMPLATETYPE_fragment hook.

Right now the reason why HAML works is that they alias _hamlout.buffer to _erbout. Rails no longer hard codes _erbout, plus a low level variable trickery is not an API anyway.

Just to favor HAML into not making the right change, we are now making errors from templates not supporting cache less obvious. With a callback you get a message "template type does not support cache_something_fragment method", but with a variable you get a complaint about _erbout. One is actionable other is just obscure.

Seems reasonable to let HAML break and do the fix at their side.

02/02/08 23:09:41 changed by mpalmer

  • attachment cache_extension_r8787.diff added.

Update of tcoppock's patch against r8787

02/02/08 23:10:31 changed by mpalmer

  • keywords changed from cache rjs to cache rjs verified.

I've updated tcoppock's patch against current trunk and verified that it works correctly. +1 from me, FTW. The patch also fixes a CI failure.

02/03/08 01:11:46 changed by bitsweat

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [8791]) Fix missing extension for caching. Closes #10733 [Catfish, tcoppock, mpalmer]

02/03/08 01:55:40 changed by RSL

FWIW, recent changes in Haml's source work fine with this change.