Permalink
Browse files

Revert "Refactor of active_model/naming.rb and allow collection and e…

…lement to be writable"

This reverts commit f7862b2.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
spastorino authored and josevalim committed May 12, 2010
1 parent bea3c26 commit 6334006b815eadd83575fe6d00e9010838530ee7
Showing with 5 additions and 59 deletions.
  1. +5 −29 activemodel/lib/active_model/naming.rb
  2. +0 −30 activemodel/test/cases/naming_test.rb
@@ -3,36 +3,18 @@
module ActiveModel
class Name < String
- attr_reader :singular, :plural, :element
+ attr_reader :singular, :plural, :element, :collection, :partial_path
+ alias_method :cache_key, :collection
def initialize(klass)
super(klass.name)
@klass = klass
@singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
@plural = ActiveSupport::Inflector.pluralize(@singular).freeze
- @collection = nil
- self.element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
- end
-
- def element=(element)
- @element = element
+ @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
@human = ActiveSupport::Inflector.humanize(@element).freeze
- @default_collection = nil
- @partial_path = nil
- end
-
- def collection
- @collection || default_collection
- end
- alias_method :cache_key, :collection
-
- def collection=(collection)
- @collection = collection
- @partial_path = nil
- end
-
- def partial_path
- @partial_path ||= "#{collection}/#{@element}"
+ @collection = ActiveSupport::Inflector.tableize(self).freeze
+ @partial_path = "#{@collection}/#{@element}".freeze
end
# Transform the model name into a more humane format, using I18n. By default,
@@ -52,12 +34,6 @@ def human(options={})
options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
I18n.translate(defaults.shift, options)
end
-
- private
-
- def default_collection
- @default_collection ||= ActiveSupport::Inflector.tableize(self.sub(/[^:]*$/, @element)).freeze
- end
end
# ActiveModel::Naming is a module that creates a +model_name+ method on your
@@ -18,41 +18,11 @@ def test_element
assert_equal 'track_back', @model_name.element
end
- def test_set_element
- @model_name.element = 'foo'
-
- assert_equal 'foo', @model_name.element
- assert_equal 'Foo', @model_name.human
- assert_equal 'post/foos', @model_name.collection
- assert_equal 'post/foos/foo', @model_name.partial_path
- end
-
- def test_human
- assert_equal 'Track back', @model_name.human
- end
-
- def test_set_collection
- @model_name.collection = 'foo'
-
- assert_equal 'foo', @model_name.collection
- assert_equal 'foo/track_back', @model_name.partial_path
- 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
-
- def test_should_preserve_custom_collection
- @model_name.collection = 'bar'
- @model_name.element = 'foo'
-
- assert_equal 'foo', @model_name.element
- assert_equal 'Foo', @model_name.human
- assert_equal 'bar', @model_name.collection
- assert_equal 'bar/foo', @model_name.partial_path
- end
end

0 comments on commit 6334006

Please sign in to comment.