naming.rb 967 字节
Newer Older
1 2
require 'active_support/inflector'

J
Joshua Peek 已提交
3 4
module ActiveModel
  class Name < String
5
    attr_reader :singular, :plural, :element, :collection, :partial_path, :human
6
    alias_method :cache_key, :collection
7

8 9 10
    def initialize(klass, name)
      super(name)
      @klass = klass
11 12
      @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
      @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
13
      @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
14
      @human = ActiveSupport::Inflector.humanize(@element).freeze
15 16
      @collection = ActiveSupport::Inflector.tableize(self).freeze
      @partial_path = "#{@collection}/#{@element}".freeze
17 18 19
    end
  end

J
Joshua Peek 已提交
20 21 22 23
  module Naming
    # Returns an ActiveModel::Name object for module. It can be
    # used to retrieve all kinds of naming-related information.
    def model_name
24
      @_model_name ||= ActiveModel::Name.new(self, name)
J
Joshua Peek 已提交
25
    end
26 27
  end
end