class method
self.new
Ruby on Rails 6.1.7.10
Since v3.0.20Signature
self.new(klass, namespace = nil, name = nil)
Returns a new ActiveModel::Name instance. By default, the namespace and name option will take the namespace and name of the given class respectively.
module Foo class Bar end end ActiveModel::Name.new(Foo::Bar).to_s # => "Foo::Bar"
Parameters
-
klassreq -
namespaceopt = nil -
nameopt = nil
Source
# File activemodel/lib/active_model/naming.rb, line 164
def initialize(klass, namespace = nil, name = nil)
@name = name || klass.name
raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if @name.blank?
@unnamespaced = @name.delete_prefix("#{namespace.name}::") if namespace
@klass = klass
@singular = _singularize(@name)
@plural = ActiveSupport::Inflector.pluralize(@singular)
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(@name))
@human = ActiveSupport::Inflector.humanize(@element)
@collection = ActiveSupport::Inflector.tableize(@name)
@param_key = (namespace ? _singularize(@unnamespaced) : @singular)
@i18n_key = @name.underscore.to_sym
@route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup)
@singular_route_key = ActiveSupport::Inflector.singularize(@route_key)
@route_key << "_index" if @plural == @singular
end
Defined in activemodel/lib/active_model/naming.rb line 164
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveModel::Name