Skip to content

Commit

Permalink
Merge pull request #31053 from y-yagi/fix_namespaced_generator
Browse files Browse the repository at this point in the history
Generate the correct path in nested scaffold generator
  • Loading branch information
eileencodes committed Nov 5, 2017
2 parents 68efb01 + 4dcb630 commit 03ac95a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
18 changes: 9 additions & 9 deletions railties/lib/rails/generators/named_base.rb
Expand Up @@ -158,26 +158,26 @@ def redirect_resource_name # :doc:

def model_resource_name(prefix: "") # :doc:
resource_name = "#{prefix}#{singular_table_name}"
if controller_class_path.empty?
resource_name
else
if options[:model_name]
"[#{controller_class_path.map { |name| ":" + name }.join(", ")}, #{resource_name}]"
else
resource_name
end
end

def singular_route_name # :doc:
if controller_class_path.empty?
singular_table_name
else
if options[:model_name]
"#{controller_class_path.join('_')}_#{singular_table_name}"
else
singular_table_name
end
end

def plural_route_name # :doc:
if controller_class_path.empty?
plural_table_name
else
if options[:model_name]
"#{controller_class_path.join('_')}_#{plural_table_name}"
else
plural_table_name
end
end

Expand Down
22 changes: 22 additions & 0 deletions railties/test/generators/named_base_test.rb
Expand Up @@ -33,6 +33,17 @@ def test_named_generator_attributes
assert_name g, "foos", :plural_name
assert_name g, "admin.foo", :i18n_scope
assert_name g, "admin_foos", :table_name
assert_name g, "admin/foos", :controller_name
assert_name g, %w(admin), :controller_class_path
assert_name g, "Admin::Foos", :controller_class_name
assert_name g, "admin/foos", :controller_file_path
assert_name g, "foos", :controller_file_name
assert_name g, "admin.foos", :controller_i18n_scope
assert_name g, "admin_foo", :singular_route_name
assert_name g, "admin_foos", :plural_route_name
assert_name g, "@admin_foo", :redirect_resource_name
assert_name g, "admin_foo", :model_resource_name
assert_name g, "admin_foos", :index_helper
end

def test_named_generator_attributes_as_ruby
Expand All @@ -47,6 +58,17 @@ def test_named_generator_attributes_as_ruby
assert_name g, "foos", :plural_name
assert_name g, "admin.foo", :i18n_scope
assert_name g, "admin_foos", :table_name
assert_name g, "Admin::Foos", :controller_name
assert_name g, %w(admin), :controller_class_path
assert_name g, "Admin::Foos", :controller_class_name
assert_name g, "admin/foos", :controller_file_path
assert_name g, "foos", :controller_file_name
assert_name g, "admin.foos", :controller_i18n_scope
assert_name g, "admin_foo", :singular_route_name
assert_name g, "admin_foos", :plural_route_name
assert_name g, "@admin_foo", :redirect_resource_name
assert_name g, "admin_foo", :model_resource_name
assert_name g, "admin_foos", :index_helper
end

def test_named_generator_attributes_without_pluralized
Expand Down
9 changes: 8 additions & 1 deletion railties/test/generators/scaffold_generator_test.rb
Expand Up @@ -282,7 +282,14 @@ def test_scaffold_with_namespace_on_invoke
/class Admin::RolesTest < ApplicationSystemTestCase/

# Views
%w(index edit new show _form).each do |view|
assert_file "app/views/admin/roles/index.html.erb" do |content|
assert_match("'Show', admin_role", content)
assert_match("'Edit', edit_admin_role_path(admin_role)", content)
assert_match("'Destroy', admin_role", content)
assert_match("'New Admin Role', new_admin_role_path", content)
end

%w(edit new show _form).each do |view|
assert_file "app/views/admin/roles/#{view}.html.erb"
end
assert_no_file "app/views/layouts/admin/roles.html.erb"
Expand Down

0 comments on commit 03ac95a

Please sign in to comment.