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

Generators: decorators should respect --model-name #931

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Alexander-Senko
Copy link
Collaborator

Description

Decorator will respect --model-name passed to scaffold_controller or controller generator.

That could be of a particular use to solve namespace issues (e.g. when controllers are namespaced and models are not).

The specs have been refactored to reduce and DRY the code.

Testing

  1. Use controller or scaffold_controller generator with --model-name option.
  2. Check if the generated decorator matches the model name.

To-Dos

  • tests

References

@Alexander-Senko Alexander-Senko self-assigned this Aug 31, 2024
@Alexander-Senko Alexander-Senko added this to the 4.0.3 milestone Aug 31, 2024
@Alexander-Senko Alexander-Senko changed the title Generators: allow decorators not to match controller names Generators: decorators should respect --model-name Aug 31, 2024
Decorator will respect `--model-name` passed to `scaffold_controller` or
`controller` generator.

That could be of a particular use to solve namespace issues
(e.g. when controllers are namespaced and models are not).

The specs have been refactored to reduce and DRY the code.

Resolves drapergem#919.

module Rails
module Generators
class ControllerGenerator
include Rails::Generators::ModelHelpers

class_option :model_name, type: :string, desc: "ModelName to be used"
Copy link
Collaborator

@y-yagi y-yagi Sep 3, 2024

Choose a reason for hiding this comment

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

Hmm. Basically, controller doesn't need to know about model. And, the current implementation depends on controller name for naming. So overring controller name by model_name looks strange to me.
How about using decorator_name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

scaffold_controller already has that interface and I'd prefer controller to be uniform with it. Rails already has --model-name option and I'd rather use it.

I agree that model dependency for controller is dubious, but we already have a decorator hook for controller that needs to know about a model. That's why controller may also need to.

Another option is to remove decorator hook from controller and add it to model. In my opinion, decorators are bound to models rather than controllers or views. However, I'd keep the existing API for now rather than introducing another braking change.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that it might be good to hook my models, not controllers. But, it seems that the current behavior is intended #553.
So generating decorators by controller names is the intended behavior(At least, so far). Setting a decorator name by --model-name is inconsistent with that behavior I think.

Copy link
Collaborator Author

@Alexander-Senko Alexander-Senko Sep 4, 2024

Choose a reason for hiding this comment

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

So generating decorators by controller names is the intended behavior.

Yes, it is. And it's not being changed.

Models are also generated based on controller names, but we have --model-name to override that when needed.

scaffold_controller already has that interface…

So, why shouldn't a decorator respect it as well when it's specified explicitly?

I'm afraid, I'm missing your point… What is your solution for #919?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, it is. And it's not being changed.

I think this PR changes the behavior. For example, if you run ./bin/rails g scaffold admin/book name:string --model-name book, the result will be change between master branch and this PR.

To be honest, I don't think #919 is a bug. That is the intended behavior of the current generator structure(I can't understand why draper only hooks for controller though).
But of course, I understand the user's expectations. So adding decorator_name(or something like that) to override decorators name is a good solution I think.

@Alexander-Senko Alexander-Senko modified the milestones: 4.0.3, 4.1 Oct 3, 2024
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.

Scaffold generator: Invalid path when using --model-name=Record
2 participants