Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Modified polymorphic_url to check for model's namespace

This change allows using namespaced models with polymorphic_url,
in the way that you would use them without namespace.

Let's say that you have Blog::Post model in namespaced Engine. When you use
polymorphic_path with Blog::Post instances, like in form_for(@post),
it will look for blog_posts_path named url helper. As we are inside Blog::Engine,
it's annoying to always use the prefix. With this commit, blog_ prefix will be
removed and posts_path will be called.
  • Loading branch information...
commit 8fb9df535e9fcf4c117ffd3254027e0fe2425cb7 1 parent 8284fd3
@drogus drogus authored
View
4 actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
@@ -111,6 +111,10 @@ def polymorphic_url(record_or_hash_or_array, options = {})
args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
end
+ if namespace = record.class.parents.detect { |n| n.respond_to?(:_railtie) }
+ named_route.sub!(/#{namespace._railtie.railtie_name}_/, '')
+ end
+
url_for _routes.url_helpers.__send__("hash_for_#{named_route}", *args)
end
View
34 railties/test/railties/engine_test.rb
@@ -352,6 +352,18 @@ class Engine < ::Rails::Engine
end
RUBY
+ @plugin.write "app/models/bukkits/post.rb", <<-RUBY
+ module Bukkits
+ class Post
+ extend ActiveModel::Naming
+
+ def to_param
+ "1"
+ end
+ end
+ end
+ RUBY
+
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match "/bar" => "bar#index", :as => "bar"
@@ -361,10 +373,14 @@ class Engine < ::Rails::Engine
@plugin.write "config/routes.rb", <<-RUBY
Bukkits::Engine.routes.draw do
- match "/foo" => "bukkits/foo#index", :as => "foo"
- match "/foo/show" => "bukkits/foo#show"
- match "/from_app" => "bukkits/foo#from_app"
- match "/routes_helpers_in_view" => "bukkits/foo#routes_helpers_in_view"
+ namespace(:bukkits, :path => nil, :shallow_path => nil, :as => nil) do
+ match "/foo" => "foo#index", :as => "foo"
+ match "/foo/show" => "foo#show"
+ match "/from_app" => "foo#from_app"
+ match "/routes_helpers_in_view" => "foo#routes_helpers_in_view"
+ match "/polymorphic_path_without_namespace" => "foo#polymorphic_path_without_namespace"
+ resources :posts
+ end
end
RUBY
@@ -401,6 +417,10 @@ def from_app
def routes_helpers_in_view
render :inline => "<%= foo_path %>, <%= app.bar_path %>"
end
+
+ def polymorphic_path_without_namespace
+ render :text => polymorphic_path(Post.new)
+ end
end
RUBY
@@ -411,6 +431,8 @@ class MyMailer < ActionMailer::Base
end
RUBY
+ add_to_config("config.action_dispatch.show_exceptions = false")
+
boot_rails
assert_equal "bukkits_", Bukkits.table_name_prefix
@@ -434,6 +456,10 @@ class MyMailer < ActionMailer::Base
env = Rack::MockRequest.env_for("/bukkits/routes_helpers_in_view")
response = AppTemplate::Application.call(env)
assert_equal "/bukkits/foo, /bar", response[2].body
+
+ env = Rack::MockRequest.env_for("/bukkits/polymorphic_path_without_namespace")
+ response = AppTemplate::Application.call(env)
+ assert_equal "/bukkits/posts/1", response[2].body
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.