2016-08-06 13:15:47 -04:00
|
|
|
require "rails/generators/active_model"
|
2014-02-23 02:40:41 -05:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
module ModelHelpers # :nodoc:
|
2014-02-27 11:34:01 -05:00
|
|
|
PLURAL_MODEL_NAME_WARN_MESSAGE = "[WARNING] The model name '%s' was recognized as a plural, using the singular '%s' instead. " \
|
2014-02-24 03:43:30 -05:00
|
|
|
"Override with --force-plural or setup custom inflection rules for this noun before running the generator."
|
2014-02-23 02:40:41 -05:00
|
|
|
mattr_accessor :skip_warn
|
|
|
|
|
|
|
|
def self.included(base) #:nodoc:
|
2016-08-06 13:15:47 -04:00
|
|
|
base.class_option :force_plural, type: :boolean, default: false, desc: "Forces the use of the given model name"
|
2014-02-23 02:40:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(args, *_options)
|
|
|
|
super
|
|
|
|
if name == name.pluralize && name.singularize != name.pluralize && !options[:force_plural]
|
2014-02-24 03:43:30 -05:00
|
|
|
singular = name.singularize
|
2014-02-23 02:40:41 -05:00
|
|
|
unless ModelHelpers.skip_warn
|
2014-02-24 03:43:30 -05:00
|
|
|
say PLURAL_MODEL_NAME_WARN_MESSAGE % [name, singular]
|
2014-02-23 02:40:41 -05:00
|
|
|
ModelHelpers.skip_warn = true
|
|
|
|
end
|
2014-02-24 03:43:30 -05:00
|
|
|
name.replace singular
|
2014-02-23 02:40:41 -05:00
|
|
|
assign_names!(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|