Ivan Kutsarov
Ivan Kutsarov

Reputation: 1031

What exactly does this Rails module do, and how does it work?

I am fairly new to Ruby and I fail to see the connection between all those classes and methods. Could you please explain a little what exactly each method does:

module Naming
# Returns an ActiveModel::Name object for module. It can be
# used to retrieve all kinds of naming-related information.
def model_name
  @_model_name ||= begin
    namespace = self.parents.detect do |n|
      n.respond_to?(:use_relative_model_naming?) && n.use_relativve_model_naming?
    end
    ActiveModel::Name.new(self, namespace)
  end
end

# Returns the plural class name of a record or class. Examples:
#
#   ActiveModel::Naming.plural(post)             # => "posts"
#   ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
def self.plural(record_or_class)
  model_name_from_record_or_class(record_or_class).plural
end

# Returns the singular class name of a record or class. Examples:
#
#   ActiveModel::Naming.singular(post)             # => "post"
#   ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
def self.singular(record_or_class)
  model_name_from_record_or_class(record_or_class).singular
end

# Identifies whether the class name of a record or class is uncountable. Examples:
#
#   ActiveModel::Naming.uncountable?(Sheep) # => true
#   ActiveModel::Naming.uncountable?(Post) => false
def self.uncountable?(record_or_class)
  plural(record_or_class) == singular(record_or_class)
end

# Returns string to use while generating route names. It differs for
# namespaced models regarding whether it's inside isolated engine.
#
# For isolated engine:
# ActiveModel::Naming.route_key(Blog::Post) #=> post
#
# For shared engine:
# ActiveModel::Naming.route_key(Blog::Post) #=> blog_post
def self.singular_route_key(record_or_class)
  model_name_from_record_or_class(record_or_class).singular_route_key
end

# Returns string to use while generating route names. It differs for
# namespaced models regarding whether it's inside isolated engine.
#
# For isolated engine:
# ActiveModel::Naming.route_key(Blog::Post) #=> posts
#
# For shared engine:
# ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts
#
# The route key also considers if the noun is uncountable and, in
# such cases, automatically appends _index.
def self.route_key(record_or_class)
  model_name_from_record_or_class(record_or_class).route_key
end

# Returns string to use for params names. It differs for
# namespaced models regarding whether it's inside isolated engine.
#
# For isolated engine:
# ActiveModel::Naming.param_key(Blog::Post) #=> post
#
# For shared engine:
# ActiveModel::Naming.param_key(Blog::Post) #=> blog_post
def self.param_key(record_or_class)
  model_name_from_record_or_class(record_or_class).param_key
end

private
  def self.model_name_from_record_or_class(record_or_class)
    (record_or_class.is_a?(Class) ? record_or_class :     convert_to_model(record_or_class).class).model_name
  end

  def self.convert_to_model(object)
    object.respond_to?(:to_model) ? object.to_model : object
  end
end

end

I know there are comments for each method but I still fail to understand basic meta.

Upvotes: 1

Views: 344

Answers (1)

m_x
m_x

Reputation: 12554

this module is part of ActiveModel, and it helps ensuring naming conventions.

The goal of all this is to provide a standard interface that helps deducing from a single object :

  • where a file belongs in the app structure (this is the magic behind render in the controller, that deduces from your controller name where the view should be, for instance)
  • what standard REST routes should lead to this resource
  • what key in the params hash will generate a form_for helper for a specific object
  • and so on...

it is difficult to explain any more this module, as it is ubiquitously used by independant logic bits that rely on naming conventions.

Upvotes: 1

Related Questions