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

Ticket #2754 (closed enhancement: fixed)

Opened 4 years ago

Last modified 3 years ago

[PATCH] Give plugins and engines the ability to specify alternate base paths in ActionView

Reported by: duane.johnson@gmail.com Assigned to: David
Priority: normal Milestone:
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc: james.adam@gmail.com, skaes@web.de, jlong, chad@underbluewaters.net

Description

This patch adds a LOAD_PATH-like behavior for template files by providing a facility for searching the file system for alternate base_paths in ActionView::Base.

It's becoming more common for certain applications or plugins to specify an alternate base path in ActionPack by overriding the full_template_path method in ActionView::Base. The scenario is this: if the normal template file exists, render it, but if it doesn't exist, first check some other directories in case it exists before reporting that the view file couldn't be rendered. An alternate scenario exists also: If a file exists elsewhere, render it first, otherwise render the normal template file. Unfortunately, if full_template_path is overridden by several packages or plugins to get this effect, then these plugins can't coexist.

James Adam uses this technique in his Engines plugin, as do I (canadaduane) in my Productized Rails App generator. There are also some other applications that use this technique for finding default partials in the shared folder.

In order to fix this inability to coexist, this patch adds two ActionView::Base class variables: @@alternate_base_paths_before and @@alternate_base_paths_after (both are arrays). These arrays, if non-empty, are used in the full_template_path method to resolve the problem cited above. Alternate base paths can be added to these arrays, and these alternate paths will be checked in sequence to see if an alternate file can be found. There is no file system performance hit if these arrays are empty.

Attachments

alternate_base_paths2.diff (4.2 kB) - added by duane.johnson@gmail.com on 11/06/05 05:14:23.
Gives ActionView::Base an alternate base_path list
view_paths.diff (25.5 kB) - added by jlong on 01/03/07 13:28:34.
This patch deprecates ActionController::Base.template_root for the ActionController::Base.view_paths. Patched against r5831.

Change History

11/06/05 05:14:23 changed by duane.johnson@gmail.com

  • attachment alternate_base_paths2.diff added.

Gives ActionView::Base an alternate base_path list

11/06/05 07:22:04 changed by james.adam@gmail.com

  • cc set to james.adam@gmail.com.

12/13/05 01:25:22 changed by skaes

Although the patch successfully avoids additional file system accesses for compiled templates, it changes full_template_path from an O(1) operation into a O(n) operation, where n is the number of base paths. On pages whith a few partials, this could have a significant performance impact. And if you use partial collections, it gets really bad, as full_template_path will be called for each partial in the collection.

Another thing that bugs me is that plugins adding new base paths make it harder for an app developer to find the template which gets rendered for a given template path, especially if before_paths is not empty.

12/13/05 01:33:11 changed by skaes

  • cc changed from james.adam@gmail.com to james.adam@gmail.com,skaes@web.de.

12/13/05 14:28:35 changed by duane.johnson@gmail.com

True, but it's still an O(1) operation for 95% of the users who won't be using alternate path templates. To me, that's what's important--only the people who use the feature will get a performance hit. Is there an alternative?

Regarding the difficulty of finding a template, I think it would be sufficient to show the developer "Hey, I looked in the following locations and didn't find anything:" It would also be nice if we could have a new section on every error page to show the "template path stack".

01/03/07 13:28:34 changed by jlong

  • attachment view_paths.diff added.

This patch deprecates ActionController::Base.template_root for the ActionController::Base.view_paths. Patched against r5831.

01/03/07 13:29:25 changed by jlong

  • cc changed from james.adam@gmail.com,skaes@web.de to james.adam@gmail.com, skaes@web.de, jlong.

01/03/07 13:44:03 changed by jlong

  • version changed from 0.14.2 to edge.

02/04/07 20:48:15 changed by rick

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

(In [6120]) Allow Controllers to have multiple view_paths instead of a single template_root. Closes #2754 [John Long]

06/15/07 20:03:16 changed by underbluewaters

  • cc changed from james.adam@gmail.com, skaes@web.de, jlong to james.adam@gmail.com, skaes@web.de, jlong, chad@underbluewaters.net.