Use controller_class_path in NamedBase#route_url

The `route_url` method now returns the correct path when generating
a namespaced controller with a top-level model using `--model-name`.

Fixes #44662.
This commit is contained in:
Andrew White 2022-03-12 09:36:38 +00:00
parent 190291dc45
commit 7f5b3e04c8
No known key found for this signature in database
GPG Key ID: 7E83729F16B086CF
3 changed files with 37 additions and 1 deletions

@ -1,3 +1,36 @@
* Use `controller_class_path` in `Rails::Generators::NamedBase#route_url`
The `route_url` method now returns the correct path when generating
a namespaced controller with a top-level model using `--model-name`.
Previously, when running this command:
``` sh
bin/rails generate scaffold_controller Admin/Post --model-name Post
```
the comments above the controller action would look like:
``` ruby
# GET /posts
def index
@posts = Post.all
end
```
afterwards, they now look like this:
``` ruby
# GET /admin/posts
def index
@posts = Post.all
end
```
Fixes #44662.
*Andrew White*
* No longer add autoloaded paths to `$LOAD_PATH`.
This means it won't be possible to load them with a manual `require` call, the class or module can be referenced instead.

@ -127,7 +127,7 @@ def fixture_file_name # :doc:
end
def route_url # :doc:
@route_url ||= class_path.collect { |dname| "/" + dname }.join + "/" + plural_file_name
@route_url ||= controller_class_path.collect { |dname| "/" + dname }.join + "/" + plural_file_name
end
def url_helper_prefix # :doc:

@ -207,14 +207,17 @@ def self.all(klass)
def test_model_name_option
run_generator ["Admin::User", "--model-name=User"]
assert_file "app/controllers/admin/users_controller.rb" do |content|
assert_match "# GET /admin/users", content
assert_instance_method :index, content do |m|
assert_match("@users = User.all", m)
end
assert_match "# POST /admin/users", content
assert_instance_method :create, content do |m|
assert_match("redirect_to [:admin, @user]", m)
end
assert_match "# PATCH/PUT /admin/users/1", content
assert_instance_method :update, content do |m|
assert_match("redirect_to [:admin, @user]", m)
end