Generate namespaced routes correctly for generators. Fix for #11532

This commit is contained in:
Prathamesh Sonpatki 2013-07-21 20:36:35 +05:30
parent 4934f19a78
commit 53c5794a4b
3 changed files with 58 additions and 1 deletions

@ -1,3 +1,21 @@
* Generate nested route for namespaced controller generated using
`rails g controller`.
Fixes #11532.
Example:
rails g controller admin/dashboard index
# Before:
get "dashboard/index"
# After:
namespace :admin do
get "dashboard/index"
end
*Prathamesh Sonpatki*
* Fix the event name of action_dispatch requests.
*Rafael Mendonça França*

@ -10,11 +10,45 @@ def create_controller_files
def add_routes
actions.reverse.each do |action|
route %{get "#{file_name}/#{action}"}
route generate_routing_code(action)
end
end
hook_for :template_engine, :test_framework, :helper, :assets
private
# This method creates nested route entry for namespaced resources.
# For eg. rails g controller foo/bar/baz index
# Will generate -
# namespace :foo do
# namespace :bar do
# get "baz/index"
# end
# end
def generate_routing_code(action)
depth = class_path.length
# Create 'namespace' ladder
# namespace :foo do
# namespace :bar do
namespace_ladder = class_path.each_with_index.map do |ns, i|
%{#{" " * i * 2}namespace :#{ns} do\n }
end.join
# Create route
# get "baz/index"
route = %{#{" " * depth * 2}get "#{file_name}/#{action}"\n}
# Create `end` ladder
# end
# end
end_ladder = (1..depth).reverse_each.map do |i|
"#{" " * i * 2}end\n"
end.join
# Combine the 3 parts to generate complete route entry
namespace_ladder + route + end_ladder
end
end
end
end

@ -82,4 +82,9 @@ def test_actions_are_turned_into_methods
assert_instance_method :bar, controller
end
end
def test_namespaced_routes_are_created_in_routes
run_generator ["admin/dashboard", "index"]
assert_file "config/routes.rb", /namespace :admin do\n\s+get "dashboard\/index"\n/
end
end