Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject route with namespace into existing blocks #41584

Conversation

jonathanhefner
Copy link
Member

This allows successive invocations of the route generator action to re-use existing namespace blocks.

@rails-bot rails-bot bot added the railties label Mar 1, 2021
This allows successive invocations of the `route` generator action to
re-use existing `namespace` blocks.
@jonathanhefner jonathanhefner force-pushed the route-action-reuse-existing-namespace branch from 1902f84 to 106e20d Compare March 1, 2021 19:09
@jonathanhefner
Copy link
Member Author

@excid3 Would this address the issue that you described in #40388?

@excid3
Copy link
Contributor

excid3 commented Mar 1, 2021

@jonathanhefner I believe so. The only use case I really had was inserting into an existing namespace. 👍

routing_code = optimize_indentation(routing_code, base_indent + 2).lines.grep_v(/^[ ]{,#{prev_indent}}\S/).join
end

inject_into_file "config/routes.rb", routing_code, after: after_pattern, verbose: false, force: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making the routes file configurable since draw was added in #37892 ?

For example, with the admin gem I'm working on, it'd be great to be able to add draw :admin to the main routes.rb and have our generators add routes to config/routes/admin.rb

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea. Though I would prefer to do it in a follow-up PR.

@rafaelfranca rafaelfranca merged commit 3cf6378 into rails:main May 6, 2021
@ghiculescu
Copy link
Member

fyi #43950

jonathanhefner added a commit to jonathanhefner/rails that referenced this pull request Dec 24, 2021
Follow-up to rails#41584.

Thor uses a regexp with its own capture groups when revoking an
`inject_into_file` call.  Thus an `:after` regexp containing capture
groups will interfere with Thor's regexp.

This commit changes the `:after` regexp we pass to `inject_into_file`
so that it no longer contains capture groups.  This commit also cleans
up any newly-empty namespace blocks when revoking a route, since
`inject_into_file` does not handle that.

The majority of this commit is refactoring and clean-up.  The actual
regexp fix is just the following line:

```ruby
namespace_pattern = /#{Regexp.escape namespace_match.to_s}/
```

Fixes rails#43950.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants