-
Notifications
You must be signed in to change notification settings - Fork 526
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
Alexander-Senko
wants to merge
1
commit into
drapergem:master
Choose a base branch
from
Alexander-Senko:generators/model_name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
require "rails/generators" | ||
require "rails/generators/rails/controller/controller_generator" | ||
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator" | ||
require "rails/generators/model_helpers" | ||
|
||
module Rails | ||
module Generators | ||
class ControllerGenerator | ||
include Rails::Generators::ModelHelpers | ||
|
||
class_option :model_name, type: :string, desc: "ModelName to be used" | ||
|
||
hook_for :decorator, type: :boolean, default: true do |generator| | ||
invoke generator, [name.singularize] | ||
invoke generator, [options[:model_name] || name] | ||
end | ||
end | ||
|
||
class ScaffoldControllerGenerator | ||
hook_for :decorator, type: :boolean, default: true | ||
hook_for :decorator, type: :boolean, default: true do |generator| | ||
invoke generator, [options[:model_name] || name] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 aboutmodel
. And, the current implementation depends oncontroller
name for naming. So overringcontroller
name bymodel_name
looks strange to me.How about using
decorator_name
?There was a problem hiding this comment.
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 prefercontroller
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 adecorator
hook forcontroller
that needs to know about a model. That's whycontroller
may also need to.Another option is to remove
decorator
hook fromcontroller
and add it tomodel
. 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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
Models are also generated based on controller names, but we have
--model-name
to override that when needed.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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 betweenmaster
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 forcontroller
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.