Permalink
Browse files

Merge pull request #13515 from kuldeepaggarwal/f-model-generation

Added warning when user tried to create model with pluralize name.
  • Loading branch information...
2 parents 624f855 + de8bef9 commit f8dcc485175d7e5ac3c5d2819b3a04cd2e5d4982 @chancancode chancancode committed Feb 23, 2014
View
26 railties/lib/rails/generators/model_helpers.rb
@@ -0,0 +1,26 @@
+require 'rails/generators/active_model'
+
+module Rails
+ module Generators
+ module ModelHelpers # :nodoc:
+ PLURAL_MODEL_NAME_WARN_MESSAGE = 'Plural version of the model detected, using singularized version. Override with --force-plural or setup custom inflection rules for this noun before running the generator.'
+ mattr_accessor :skip_warn
+
+ def self.included(base) #:nodoc:
+ base.class_option :force_plural, type: :boolean, default: false, desc: 'Forces the use of a plural model name'
+ end
+
+ def initialize(args, *_options)
+ super
+ if name == name.pluralize && name.singularize != name.pluralize && !options[:force_plural]
+ unless ModelHelpers.skip_warn
+ say PLURAL_MODEL_NAME_WARN_MESSAGE
+ ModelHelpers.skip_warn = true
+ end
+ name.replace name.singularize
+ assign_names!(name)
+ end
+ end
+ end
+ end
+end
View
4 railties/lib/rails/generators/rails/model/model_generator.rb
@@ -1,6 +1,10 @@
+require 'rails/generators/model_helpers'
+
module Rails
module Generators
class ModelGenerator < NamedBase # :nodoc:
+ include Rails::Generators::ModelHelpers
+
argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
hook_for :orm, required: true
end
View
13 railties/lib/rails/generators/resource_helpers.rb
@@ -1,14 +1,14 @@
require 'rails/generators/active_model'
+require 'rails/generators/model_helpers'
module Rails
module Generators
# Deal with controller names on scaffold and add some helpers to deal with
# ActiveModel.
module ResourceHelpers # :nodoc:
- mattr_accessor :skip_warn
def self.included(base) #:nodoc:
- base.class_option :force_plural, type: :boolean, desc: "Forces the use of a plural ModelName"
+ base.send :include, Rails::Generators::ModelHelpers
base.class_option :model_name, type: :string, desc: "ModelName to be used"
end
@@ -21,15 +21,6 @@ def initialize(*args) #:nodoc:
assign_names!(self.name)
end
- if name == name.pluralize && name.singularize != name.pluralize && !options[:force_plural]
- unless ResourceHelpers.skip_warn
- say "Plural version of the model detected, using singularized version. Override with --force-plural."
- ResourceHelpers.skip_warn = true
- end
- name.replace name.singularize
- assign_names!(name)
- end
-
assign_controller_names!(controller_name.pluralize)
end
View
7 railties/test/generators/model_generator_test.rb
@@ -34,6 +34,13 @@ def test_model_with_parent_option
assert_no_migration "db/migrate/create_accounts.rb"
end
+ def test_plural_names_are_singularized
+ content = run_generator ["accounts".freeze]
+ assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/
+ assert_file "test/models/account_test.rb", /class AccountTest/
+ assert_match(/Plural version of the model detected, using singularized version. Override with --force-plural or setup custom inflection rules for this noun before running the generator./, content)
+ end
+
def test_model_with_underscored_parent_option
run_generator ["account", "--parent", "admin/account"]
assert_file "app/models/account.rb", /class Account < Admin::Account/
View
2 railties/test/generators/resource_generator_test.rb
@@ -63,7 +63,7 @@ def test_plural_names_are_singularized
content = run_generator ["accounts".freeze]
assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/
assert_file "test/models/account_test.rb", /class AccountTest/
- assert_match(/Plural version of the model detected, using singularized version. Override with --force-plural./, content)
+ assert_match(/Plural version of the model detected, using singularized version. Override with --force-plural or setup custom inflection rules for this noun before running the generator./, content)
end
def test_plural_names_can_be_forced

2 comments on commit f8dcc48

@chancancode
Ruby on Rails member

cc @senny, feel free to revise the message

@senny
Ruby on Rails member

@chancancode message update in 174c9f0

Please sign in to comment.