From 710bbbb3d9e621e02d4f0b705a6d53d96d4cf8e2 Mon Sep 17 00:00:00 2001 From: Carlos Paramio Date: Fri, 16 Oct 2009 09:11:13 +0200 Subject: [PATCH] virtual attributes on ActiveModel::Name are now lazy generated, basic documentation for ActiveModel::Name attributes included, better test coverage for ActiveModel::Name attributes included --- activemodel/lib/active_model/naming.rb | 62 ++++++++++++++++++++++++++----- activemodel/test/cases/naming_test.rb | 8 ++++ 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index b8c2a36..6834eca 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -2,17 +2,59 @@ require 'active_support/inflector' module ActiveModel class Name < String - attr_reader :singular, :plural, :element, :collection, :partial_path, :human + # Makes an underscored, lowercase, singular form from the name. + # + # Example: + # + # ActiveModel::Name.new('Post::TrackBack').singular # => "post_track_back" + def singular + @singular ||= ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze + end + + # Makes an underscored, lowercase, plural form from the name. + # + # Example: + # + # ActiveModel::Name.new('Post::TrackBack').plural # => "post_track_backs" + def plural + @plural ||= ActiveSupport::Inflector.pluralize(singular).freeze + end + + # Makes an underscored, lowercase, singular form from the last part of the name. + # + # Example: + # + # ActiveModel::Name.new('Post::TrackBack').element # => "track_back" + def element + @element ||= ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + end + + # Makes a lowercase, singular form from the last part of the name. + # + # Example: + # + # ActiveModel::Name.new('Post::TrackBack').human # => "track back" + def human + @human ||= element.gsub(/_/, " ") + end + + # Makes an underscored, lowercase, plural form from the last part of the name with a path preffix for the outer modules. + # + # Example: + # + # ActiveModel::Name.new('Post::TrackBack').collection # => "post/track_backs" + def collection + @collection ||= ActiveSupport::Inflector.tableize(self).freeze + end alias_method :cache_key, :collection - - def initialize(name) - super - @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze - @plural = ActiveSupport::Inflector.pluralize(@singular).freeze - @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze - @human = @element.gsub(/_/, " ") - @collection = ActiveSupport::Inflector.tableize(self).freeze - @partial_path = "#{@collection}/#{@element}".freeze + + # Makes a default path where to locate a partial for the model name. + # + # Example: + # + # ActiveModel::Name.new('Post::TrackBack').partial_path # => "post/track_backs/track_back" + def partial_path + @partial_path ||= "#{collection}/#{element}".freeze end end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index 4d97af3..eb37205 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -16,10 +16,18 @@ class NamingTest < ActiveModel::TestCase def test_element assert_equal 'track_back', @model_name.element end + + def test_human + assert_equal 'track back', @model_name.human + end def test_collection assert_equal 'post/track_backs', @model_name.collection end + + def test_cache_key + assert_equal 'post/track_backs', @model_name.cache_key + end def test_partial_path assert_equal 'post/track_backs/track_back', @model_name.partial_path -- 1.6.4