1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add #element and #collection to ModelName

This commit is contained in:
Jeremy Kemper 2009-06-08 17:58:14 -07:00
parent d9f16fafec
commit 99cf77be27
2 changed files with 13 additions and 3 deletions

View file

@ -2,14 +2,16 @@ require 'active_support/inflector'
module ActiveSupport
class ModelName < String
attr_reader :singular, :plural, :cache_key, :partial_path
attr_reader :singular, :plural, :element, :collection, :partial_path
alias_method :cache_key, :collection
def initialize(name)
super
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
@cache_key = tableize.freeze
@partial_path = "#{@cache_key}/#{ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self))}".freeze
@element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
@collection = ActiveSupport::Inflector.tableize(self).freeze
@partial_path = "#{@collection}/#{@element}".freeze
end
end
end

View file

@ -14,6 +14,14 @@ class ModelNamingTest < Test::Unit::TestCase
assert_equal 'post_track_backs', @model_name.plural
end
def test_element
assert_equal 'track_back', @model_name.element
end
def test_collection
assert_equal 'post/track_backs', @model_name.collection
end
def test_partial_path
assert_equal 'post/track_backs/track_back', @model_name.partial_path
end