From 99cf77be270e71408a14f0c00472517adb5981b2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 8 Jun 2009 17:58:14 -0700 Subject: Add #element and #collection to ModelName --- activesupport/lib/active_support/core_ext/module/model_naming.rb | 8 +++++--- activesupport/test/core_ext/module/model_naming_test.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activesupport/lib/active_support/core_ext/module/model_naming.rb index 36fde87b23..13420bab07 100644 --- a/activesupport/lib/active_support/core_ext/module/model_naming.rb +++ b/activesupport/lib/active_support/core_ext/module/model_naming.rb @@ -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 diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb index da3b6c4932..37119f378a 100644 --- a/activesupport/test/core_ext/module/model_naming_test.rb +++ b/activesupport/test/core_ext/module/model_naming_test.rb @@ -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 -- cgit v1.2.3