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

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

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

J
Joshua Peek 已提交
18 19 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
      @_model_name ||= ActiveModel::Name.new(name)
    end
24 25
  end
end