Add config option to turn off prefixing partial path with controller namespace #5625

Merged
merged 2 commits into from Mar 29, 2012

Projects

None yet

5 participants

@nertzy
Contributor
nertzy commented Mar 28, 2012

It's annoying that if my controller happens to be namespaced, I am not allowed to render objects by using <%= render @objects %> unless I have a separate set of partials under the namespace.

See http://blog.obiefernandez.com/content/2012/01/rendering-collections-of-heterogeneous-objects-in-rails-32.html for more justification.

cc @josevalim

@nertzy
Contributor
nertzy commented Mar 28, 2012

By the way, I'd like feedback on how I implemented this, and the right way to document the config setting.

@josevalim
Member

@nertzy thanks for the pull request. Your implementation will accidentally break any kind of namespaced rendering, not only the partial one. I think we should instead:

  1. Add an option to AV::Base

  2. Check this option only here: https://github.com/nertzy/rails/blob/66d4134b9df0a5d8d7fba55f44f4307a29b48f2d/actionpack/lib/action_view/renderer/partial_renderer.rb#L430

About the docs, you can update this guide:

https://github.com/rails/rails/blob/master/guides/source/configuring.textile

@nertzy
Contributor
nertzy commented Mar 28, 2012

Thanks!

One difference with the ActionView::Base solution is that you won't be able to activate and de-activate the prefixing on a per-controller basis. I don't actually have a use case for this but it seemed like a nice side effect.

That said, it's definitely more of a ActionView concern and it's probably better not to mix things up too much.

@nertzy
Contributor
nertzy commented Mar 28, 2012

cc @Peeja

@josevalim
Member

@nertzy if we want to eventually set it in the controller, it could be part of the config object the controller sends to the view. but i'd worry about this when a use case actually appears :)

@nertzy
Contributor
nertzy commented Mar 29, 2012

OK, I've moved it to ActionView. I also made the prefix caching optional to reduce object creation when it's turned off.

Next I'll update with docs and push again.

@nertzy nertzy Make controller namespace partial prefix optional
config.action_view.prefix_partial_path_with_controller_namespace

This allows you to choose to render @post using
/posts/_post.erb instead of /admin/posts/_post.erb
inside Admin::PostsController.
18d275a
@nertzy
Contributor
nertzy commented Mar 29, 2012

OK, it should be ready to go.

@josevalim josevalim merged commit e51322a into rails:master Mar 29, 2012
@toupeira

Will this make it into 3.2.7?

@toupeira

Thanks @nertzy, I actually looked up the Google Cache of that article yesterday too, and ended up using a view resolver as described in this article linked from Obie's post, which doesn't require monkey-patching Rails.

@oriolgual
Contributor

Any reason why this was never merged into 3.2?

@steveklabnik
Member

3-2 isn't getting new features, just bug fixes..

@oriolgual
Contributor

Thanks for the heads up @steveklabnik!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment