Module: ActiveModel::Naming

Included in:
Translation, ActiveResource::Base
Defined in:
activemodel/lib/active_model/naming.rb

Overview

Active Model Naming

Creates a model_name method on your object.

To implement, just extend ActiveModel::Naming in your object:

class BookCover
  extend ActiveModel::Naming
end

BookCover.model_name        # => "BookCover"
BookCover.model_name.human  # => "Book cover"

Providing the functionality that ActiveModel::Naming provides in your object is required to pass the Active Model Lint test. So either extending the provided method below, or rolling your own is required..

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) plural(record_or_class)

Returns the plural class name of a record or class. Examples:

ActiveModel::Naming.plural(post)             # => "posts"
ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"


68
69
70
# File 'activemodel/lib/active_model/naming.rb', line 68

def self.plural(record_or_class)
  model_name_from_record_or_class(record_or_class).plural
end

+ (Object) singular(record_or_class)

Returns the singular class name of a record or class. Examples:

ActiveModel::Naming.singular(post)             # => "post"
ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"


76
77
78
# File 'activemodel/lib/active_model/naming.rb', line 76

def self.singular(record_or_class)
  model_name_from_record_or_class(record_or_class).singular
end

+ (Boolean) uncountable?(record_or_class)

Identifies whether the class name of a record or class is uncountable. Examples:

ActiveModel::Naming.uncountable?(Sheep) # => true
ActiveModel::Naming.uncountable?(Post) => false

Returns:

  • (Boolean)


84
85
86
# File 'activemodel/lib/active_model/naming.rb', line 84

def self.uncountable?(record_or_class)
  plural(record_or_class) == singular(record_or_class)
end

Instance Method Details

- (Object) model_name

Returns an ActiveModel::Name object for module. It can be used to retrieve all kinds of naming-related information.



60
61
62
# File 'activemodel/lib/active_model/naming.rb', line 60

def model_name
  @_model_name ||= ActiveModel::Name.new(self)
end