mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #39832 from p8/scaffold-generator-inflection-check
Raise if camelcase inflection impossible in generator
This commit is contained in:
commit
5f40eca4a5
2 changed files with 36 additions and 2 deletions
|
@ -11,6 +11,11 @@ module Rails
|
||||||
[WARNING] Rails cannot recover singular form from its plural form '%s'.
|
[WARNING] Rails cannot recover singular form from its plural form '%s'.
|
||||||
Please setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb.
|
Please setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb.
|
||||||
WARNING
|
WARNING
|
||||||
|
INFLECTION_IMPOSSIBLE_ERROR_MESSAGE = <<~ERROR
|
||||||
|
Rails cannot recover the underscored form from its camelcase form '%s'.
|
||||||
|
Please use an underscored name instead, either '%s' or '%s'.
|
||||||
|
Or setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb.
|
||||||
|
ERROR
|
||||||
mattr_accessor :skip_warn
|
mattr_accessor :skip_warn
|
||||||
|
|
||||||
def self.included(base) #:nodoc:
|
def self.included(base) #:nodoc:
|
||||||
|
@ -19,7 +24,7 @@ module Rails
|
||||||
|
|
||||||
def initialize(args, *_options)
|
def initialize(args, *_options)
|
||||||
super
|
super
|
||||||
if name == name.pluralize && name.singularize != name.pluralize && !options[:force_plural]
|
if plural_model_name?(name) && !options[:force_plural]
|
||||||
singular = name.singularize
|
singular = name.singularize
|
||||||
unless ModelHelpers.skip_warn
|
unless ModelHelpers.skip_warn
|
||||||
say PLURAL_MODEL_NAME_WARN_MESSAGE % [name, singular]
|
say PLURAL_MODEL_NAME_WARN_MESSAGE % [name, singular]
|
||||||
|
@ -27,11 +32,30 @@ module Rails
|
||||||
name.replace singular
|
name.replace singular
|
||||||
assign_names!(name)
|
assign_names!(name)
|
||||||
end
|
end
|
||||||
if name.singularize != name.pluralize.singularize && ! ModelHelpers.skip_warn
|
if inflection_impossible?(name)
|
||||||
|
option1 = name.singularize.underscore
|
||||||
|
option2 = name.pluralize.underscore.singularize
|
||||||
|
raise Error, INFLECTION_IMPOSSIBLE_ERROR_MESSAGE % [name, option1, option2]
|
||||||
|
end
|
||||||
|
if irregular_model_name?(name) && ! ModelHelpers.skip_warn
|
||||||
say IRREGULAR_MODEL_NAME_WARN_MESSAGE % [name.pluralize]
|
say IRREGULAR_MODEL_NAME_WARN_MESSAGE % [name.pluralize]
|
||||||
end
|
end
|
||||||
ModelHelpers.skip_warn = true
|
ModelHelpers.skip_warn = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def plural_model_name?(name)
|
||||||
|
name == name.pluralize && name.singularize != name.pluralize
|
||||||
|
end
|
||||||
|
|
||||||
|
def irregular_model_name?(name)
|
||||||
|
name.singularize != name.pluralize.singularize
|
||||||
|
end
|
||||||
|
|
||||||
|
def inflection_impossible?(name)
|
||||||
|
name != name.underscore &&
|
||||||
|
name.singularize.underscore != name.pluralize.underscore.singularize
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -115,6 +115,16 @@ class ModelGeneratorTest < Rails::Generators::TestCase
|
||||||
assert_no_match("[WARNING] Rails cannot recover singular form from its plural form", regular_content)
|
assert_no_match("[WARNING] Rails cannot recover singular form from its plural form", regular_content)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_impossible_inflection_rules_raises_an_error
|
||||||
|
content = capture(:stderr) { run_generator ["BFF"] }
|
||||||
|
message = <<~MESSAGE
|
||||||
|
Rails cannot recover the underscored form from its camelcase form 'BFF'.
|
||||||
|
Please use an underscored name instead, either 'bff' or 'bf_f'.
|
||||||
|
Or setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb.
|
||||||
|
MESSAGE
|
||||||
|
assert_match message, content
|
||||||
|
end
|
||||||
|
|
||||||
def test_model_with_underscored_parent_option
|
def test_model_with_underscored_parent_option
|
||||||
run_generator ["account", "--parent", "admin/account"]
|
run_generator ["account", "--parent", "admin/account"]
|
||||||
assert_file "app/models/account.rb", /class Account < Admin::Account/
|
assert_file "app/models/account.rb", /class Account < Admin::Account/
|
||||||
|
|
Loading…
Reference in a new issue