Permalink
Browse files

Add #element and #collection to ModelName

  • Loading branch information...
1 parent cc5d313 commit f1e75e4378029d61b7b91dfc8a595dc3716bc000 @jeremy jeremy committed Jun 9, 2009
@@ -1,13 +1,15 @@
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 = underscore.tr('/', '_').freeze
- @plural = @singular.pluralize.freeze
- @cache_key = tableize.freeze
- @partial_path = "#{@cache_key}/#{demodulize.underscore}".freeze
+ @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
+ @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
+ @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
+ @collection = ActiveSupport::Inflector.tableize(self).freeze
+ @partial_path = "#{@collection}/#{@element}".freeze
end
end
@@ -16,7 +18,7 @@ module Module
# Returns an ActiveSupport::ModelName object for module. It can be
# used to retrieve all kinds of naming-related information.
def model_name
- @model_name ||= ModelName.new(name)
+ @model_name ||= ::ActiveSupport::ModelName.new(name)
end
end
end
@@ -13,6 +13,14 @@ def test_plural
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

0 comments on commit f1e75e4

Please sign in to comment.