2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/inflector'
|
|
|
|
|
2009-06-17 11:37:39 -04:00
|
|
|
module ActiveModel
|
|
|
|
class Name < String
|
2009-07-28 22:04:34 -04:00
|
|
|
attr_reader :singular, :plural, :element, :collection, :partial_path, :human
|
2009-06-08 20:58:14 -04:00
|
|
|
alias_method :cache_key, :collection
|
2008-06-06 06:38:05 -04:00
|
|
|
|
|
|
|
def initialize(name)
|
|
|
|
super
|
2009-04-22 20:41:28 -04:00
|
|
|
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
|
|
|
|
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
|
2009-06-08 20:58:14 -04:00
|
|
|
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
|
2009-07-28 22:04:34 -04:00
|
|
|
@human = @element.gsub(/_/, " ")
|
2009-06-08 20:58:14 -04:00
|
|
|
@collection = ActiveSupport::Inflector.tableize(self).freeze
|
|
|
|
@partial_path = "#{@collection}/#{@element}".freeze
|
2008-06-06 06:38:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-17 11:37:39 -04:00
|
|
|
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
|
2008-06-06 06:38:05 -04:00
|
|
|
end
|
|
|
|
end
|