123456789_123456789_123456789_123456789_123456789_

Module: Rails::Generators::ModelHelpers

Do not use. This module is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: railties/lib/rails/generators/model_helpers.rb

Constant Summary

  • INFLECTION_IMPOSSIBLE_ERROR_MESSAGE =
    # File 'railties/lib/rails/generators/model_helpers.rb', line 14
    <<~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
  • IRREGULAR_MODEL_NAME_WARN_MESSAGE =
    # File 'railties/lib/rails/generators/model_helpers.rb', line 10
    <<~WARNING
    [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.
    WARNING
  • PLURAL_MODEL_NAME_WARN_MESSAGE =
    # File 'railties/lib/rails/generators/model_helpers.rb', line 8
    "[WARNING] The model name '%s' was recognized as a plural, using the singular '%s' instead. " \
    "Override with --force-plural or setup custom inflection rules for this noun before running the generator."

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Attribute Details

.skip_warn (rw) Also known as: #skip_warn

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 19

mattr_accessor :skip_warn

Class Method Details

.included(base)

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 21

def self.included(base) # :nodoc:
  base.class_option :force_plural, type: :boolean, default: false,
    desc: "Do not singularize the model name, even if it appears plural"
end

Instance Attribute Details

#skip_warn (rw)

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 19

mattr_accessor :skip_warn

Instance Method Details

#inflection_impossible?(name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 56

def inflection_impossible?(name)
  name != name.underscore &&
    name.singularize.underscore != name.pluralize.underscore.singularize
end

#initialize(args, *_options)

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 26

def initialize(args, *_options)
  super
  if plural_model_name?(name) && !options[:force_plural]
    singular = name.singularize
    unless ModelHelpers.skip_warn
      say PLURAL_MODEL_NAME_WARN_MESSAGE % [name, singular]
    end
    name.replace singular
    assign_names!(name)
  end
  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]
  end
  ModelHelpers.skip_warn = true
end

#irregular_model_name?(name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 52

def irregular_model_name?(name)
  name.singularize != name.pluralize.singularize
end

#plural_model_name?(name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/model_helpers.rb', line 48

def plural_model_name?(name)
  name == name.pluralize && name.singularize != name.pluralize
end