class ActiveModel::Name

Attributes

cache_key[RW]
collection[RW]
element[RW]
i18n_key[RW]
name[RW]
param_key[RW]
plural[RW]
route_key[RW]
singular[RW]
singular_route_key[RW]

Public Class Methods

new(klass, namespace = nil, name = nil, locale = :en) click to toggle source

Returns a new ActiveModel::Name instance. By default, the namespace and name option will take the namespace and name of the given class respectively. Use locale argument for singularize and pluralize model name.

module Foo
  class Bar
  end
end

ActiveModel::Name.new(Foo::Bar).to_s
# => "Foo::Bar"
# File lib/active_model/naming.rb, line 166
def initialize(klass, namespace = nil, name = nil, locale = :en)
  @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, locale)
  @uncountable  = @plural == @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, locale) : @plural.dup)
  @singular_route_key = ActiveSupport::Inflector.singularize(@route_key, locale)
  @route_key << "_index" if @uncountable
end

Public Instance Methods

!~(regexp) click to toggle source

Equivalent to String#!~. Match the class name against the given regexp. Returns true if there is no match, otherwise false.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name !~ /Post/ # => false
BlogPost.model_name !~ /\d/   # => true
# File lib/active_model/naming.rb, line 83
    
<→(other) click to toggle source

Equivalent to String#<=>.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name <=> 'BlogPost'  # => 0
BlogPost.model_name <=> 'Blog'      # => 1
BlogPost.model_name <=> 'BlogPosts' # => -1
# File lib/active_model/naming.rb, line 50
    
==(other) click to toggle source

Equivalent to String#==. Returns true if the class name and other are equal, otherwise false.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name == 'BlogPost'  # => true
BlogPost.model_name == 'Blog Post' # => false
# File lib/active_model/naming.rb, line 19
    
===(other) click to toggle source

Equivalent to #==.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name === 'BlogPost'  # => true
BlogPost.model_name === 'Blog Post' # => false
# File lib/active_model/naming.rb, line 35
    
=~(regexp) click to toggle source

Equivalent to String#=~. Match the class name against the given regexp. Returns the position where the match starts or nil if there is no match.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name =~ /Post/ # => 4
BlogPost.model_name =~ /\d/   # => nil
# File lib/active_model/naming.rb, line 66
    
eql?(other) click to toggle source

Equivalent to String#eql?. Returns true if the class name and other have the same length and content, otherwise false.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.eql?('BlogPost')  # => true
BlogPost.model_name.eql?('Blog Post') # => false
# File lib/active_model/naming.rb, line 99
    
human(options = {}) click to toggle source

Transform the model name into a more human format, using I18n. By default, it will underscore then humanize the class name.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.human # => "Blog post"

Specify options with additional translating options.

# File lib/active_model/naming.rb, line 197
def human(options = {})
  return @human if i18n_keys.empty? || i18n_scope.empty?

  key, *defaults = i18n_keys
  defaults << options[:default] if options[:default]
  defaults << MISSING_TRANSLATION

  translation = I18n.translate(key, scope: i18n_scope, count: 1, **options, default: defaults)
  translation = @human if translation == MISSING_TRANSLATION
  translation
end
match?(regexp) click to toggle source

Equivalent to String#match?. Match the class name against the given regexp. Returns true if there is a match, otherwise false.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.match?(/Post/) # => true
BlogPost.model_name.match?(/\d/) # => false
# File lib/active_model/naming.rb, line 115
    
to_s() click to toggle source

Returns the class name.

class BlogPost
  extend ActiveModel::Naming
end

BlogPost.model_name.to_s # => "BlogPost"
# File lib/active_model/naming.rb, line 131
    
to_str() click to toggle source

Equivalent to to_s.

# File lib/active_model/naming.rb, line 151
delegate :==, :===, :<=>, :=~, :"!~", :eql?, :match?, :to_s,
         :to_str, :as_json, to: :name
uncountable?() click to toggle source
# File lib/active_model/naming.rb, line 209
def uncountable?
  @uncountable
end

Private Instance Methods

_singularize(string) click to toggle source
# File lib/active_model/naming.rb, line 216
def _singularize(string)
  ActiveSupport::Inflector.underscore(string).tr("/", "_")
end
i18n_keys() click to toggle source
# File lib/active_model/naming.rb, line 220
def i18n_keys
  @i18n_keys ||= if @klass.respond_to?(:lookup_ancestors)
    @klass.lookup_ancestors.map { |klass| klass.model_name.i18n_key }
  else
    []
  end
end
i18n_scope() click to toggle source
# File lib/active_model/naming.rb, line 228
def i18n_scope
  @i18n_scope ||= @klass.respond_to?(:i18n_scope) ? [@klass.i18n_scope, :models] : []
end