2009-06-17 15:37:39 +00:00
|
|
|
require 'cases/helper'
|
2010-07-21 11:00:56 +00:00
|
|
|
require 'models/contact'
|
|
|
|
require 'models/sheep'
|
2009-12-23 11:28:02 +00:00
|
|
|
require 'models/track_back'
|
2010-08-31 20:17:42 +00:00
|
|
|
require 'models/blog_post'
|
2008-06-06 10:38:05 +00:00
|
|
|
|
2009-09-25 05:56:20 +00:00
|
|
|
class NamingTest < ActiveModel::TestCase
|
2008-06-06 10:38:05 +00:00
|
|
|
def setup
|
2009-12-23 11:28:02 +00:00
|
|
|
@model_name = ActiveModel::Name.new(Post::TrackBack)
|
2008-06-06 10:38:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
2008-11-07 17:45:48 +00:00
|
|
|
assert_equal 'post_track_back', @model_name.singular
|
2008-06-06 10:38:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
2008-11-07 17:45:48 +00:00
|
|
|
assert_equal 'post_track_backs', @model_name.plural
|
2008-06-06 10:38:05 +00:00
|
|
|
end
|
|
|
|
|
2009-06-09 00:58:14 +00:00
|
|
|
def test_element
|
|
|
|
assert_equal 'track_back', @model_name.element
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
|
|
|
assert_equal 'post/track_backs', @model_name.collection
|
|
|
|
end
|
|
|
|
|
2008-06-06 10:38:05 +00:00
|
|
|
def test_partial_path
|
2008-11-07 17:45:48 +00:00
|
|
|
assert_equal 'post/track_backs/track_back', @model_name.partial_path
|
2008-06-06 10:38:05 +00:00
|
|
|
end
|
2010-08-12 18:43:10 +00:00
|
|
|
|
|
|
|
def test_human
|
|
|
|
assert_equal 'Track back', @model_name.human
|
|
|
|
end
|
2008-06-06 10:38:05 +00:00
|
|
|
end
|
2010-07-21 11:00:56 +00:00
|
|
|
|
2010-08-31 20:17:42 +00:00
|
|
|
class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase
|
|
|
|
def setup
|
|
|
|
@model_name = ActiveModel::Name.new(Blog::Post, Blog)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
|
|
|
assert_equal 'blog_post', @model_name.singular
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
|
|
|
assert_equal 'blog_posts', @model_name.plural
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
|
|
|
assert_equal 'post', @model_name.element
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
|
|
|
assert_equal 'blog/posts', @model_name.collection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_partial_path
|
|
|
|
assert_equal 'blog/posts/post', @model_name.partial_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
|
|
|
assert_equal 'Post', @model_name.human
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
|
|
|
assert_equal 'posts', @model_name.route_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
|
|
|
assert_equal 'post', @model_name.param_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_recognizing_namespace
|
|
|
|
assert_equal 'Post', Blog::Post.model_name.instance_variable_get("@unnamespaced")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase
|
|
|
|
def setup
|
|
|
|
@model_name = ActiveModel::Name.new(Blog::Post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
|
|
|
assert_equal 'blog_post', @model_name.singular
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
|
|
|
assert_equal 'blog_posts', @model_name.plural
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
|
|
|
assert_equal 'post', @model_name.element
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
|
|
|
assert_equal 'blog/posts', @model_name.collection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_partial_path
|
|
|
|
assert_equal 'blog/posts/post', @model_name.partial_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
|
|
|
assert_equal 'Post', @model_name.human
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
|
|
|
assert_equal 'blog_posts', @model_name.route_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
|
|
|
assert_equal 'blog_post', @model_name.param_key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-20 14:48:51 +00:00
|
|
|
class NamingWithSuppliedModelNameTest < ActiveModel::TestCase
|
|
|
|
def setup
|
|
|
|
@model_name = ActiveModel::Name.new(Blog::Post, nil, 'Article')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
|
|
|
assert_equal 'article', @model_name.singular
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
|
|
|
assert_equal 'articles', @model_name.plural
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
|
|
|
assert_equal 'article', @model_name.element
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
|
|
|
assert_equal 'articles', @model_name.collection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_partial_path
|
|
|
|
assert_equal 'articles/article', @model_name.partial_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
|
|
|
'Article'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
|
|
|
assert_equal 'articles', @model_name.route_key
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
|
|
|
assert_equal 'article', @model_name.param_key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-07-21 11:00:56 +00:00
|
|
|
class NamingHelpersTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@klass = Contact
|
|
|
|
@record = @klass.new
|
|
|
|
@singular = 'contact'
|
|
|
|
@plural = 'contacts'
|
|
|
|
@uncountable = Sheep
|
2010-08-31 20:17:42 +00:00
|
|
|
@route_key = 'contacts'
|
|
|
|
@param_key = 'contact'
|
2010-07-21 11:00:56 +00:00
|
|
|
end
|
|
|
|
|
2010-10-04 19:04:39 +00:00
|
|
|
def test_to_model_called_on_record
|
|
|
|
assert_equal 'post_named_track_backs', plural(Post::TrackBack.new)
|
|
|
|
end
|
|
|
|
|
2010-07-21 11:00:56 +00:00
|
|
|
def test_singular
|
|
|
|
assert_equal @singular, singular(@record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular_for_class
|
|
|
|
assert_equal @singular, singular(@klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
|
|
|
assert_equal @plural, plural(@record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural_for_class
|
|
|
|
assert_equal @plural, plural(@klass)
|
|
|
|
end
|
|
|
|
|
2010-08-31 20:17:42 +00:00
|
|
|
def test_route_key
|
|
|
|
assert_equal @route_key, route_key(@record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key_for_class
|
|
|
|
assert_equal @route_key, route_key(@klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
|
|
|
assert_equal @param_key, param_key(@record)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key_for_class
|
|
|
|
assert_equal @param_key, param_key(@klass)
|
|
|
|
end
|
|
|
|
|
2010-07-21 11:00:56 +00:00
|
|
|
def test_uncountable
|
|
|
|
assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable"
|
|
|
|
assert !uncountable?(@klass), "Expected 'contact' to be countable"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def method_missing(method, *args)
|
|
|
|
ActiveModel::Naming.send(method, *args)
|
|
|
|
end
|
|
|
|
end
|