2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/contact"
|
|
|
|
require "models/sheep"
|
|
|
|
require "models/track_back"
|
|
|
|
require "models/blog_post"
|
2008-06-06 06:38:05 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingTest < ActiveModel::TestCase
|
2008-06-06 06:38:05 -04:00
|
|
|
def setup
|
2009-12-23 06:28:02 -05:00
|
|
|
@model_name = ActiveModel::Name.new(Post::TrackBack)
|
2008-06-06 06:38:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post_track_back", @model_name.singular
|
2008-06-06 06:38:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post_track_backs", @model_name.plural
|
2008-06-06 06:38:05 -04:00
|
|
|
end
|
|
|
|
|
2009-06-08 20:58:14 -04:00
|
|
|
def test_element
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "track_back", @model_name.element
|
2009-06-08 20:58:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post/track_backs", @model_name.collection
|
2009-06-08 20:58:14 -04:00
|
|
|
end
|
|
|
|
|
2010-08-12 14:43:10 -04:00
|
|
|
def test_human
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Track back", @model_name.human
|
2010-08-12 14:43:10 -04:00
|
|
|
end
|
2011-12-05 21:52:45 -05:00
|
|
|
|
2013-01-22 22:13:43 -05:00
|
|
|
def test_route_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post_track_backs", @model_name.route_key
|
2013-01-22 22:13:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post_track_back", @model_name.param_key
|
2013-01-22 22:13:43 -05:00
|
|
|
end
|
|
|
|
|
2011-12-05 21:52:45 -05:00
|
|
|
def test_i18n_key
|
|
|
|
assert_equal :"post/track_back", @model_name.i18n_key
|
|
|
|
end
|
2008-06-06 06:38:05 -04:00
|
|
|
end
|
2010-07-21 07:00:56 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingWithNamespacedModelInIsolatedNamespaceTest < ActiveModel::TestCase
|
2010-08-31 16:17:42 -04:00
|
|
|
def setup
|
|
|
|
@model_name = ActiveModel::Name.new(Blog::Post, Blog)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_post", @model_name.singular
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_posts", @model_name.plural
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post", @model_name.element
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog/posts", @model_name.collection
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Post", @model_name.human
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "posts", @model_name.route_key
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post", @model_name.param_key
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
2011-12-05 21:52:45 -05:00
|
|
|
|
|
|
|
def test_i18n_key
|
|
|
|
assert_equal :"blog/post", @model_name.i18n_key
|
|
|
|
end
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingWithNamespacedModelInSharedNamespaceTest < ActiveModel::TestCase
|
2010-08-31 16:17:42 -04:00
|
|
|
def setup
|
|
|
|
@model_name = ActiveModel::Name.new(Blog::Post)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_post", @model_name.singular
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_posts", @model_name.plural
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post", @model_name.element
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog/posts", @model_name.collection
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Post", @model_name.human
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_posts", @model_name.route_key
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_post", @model_name.param_key
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
2011-12-05 21:52:45 -05:00
|
|
|
|
|
|
|
def test_i18n_key
|
|
|
|
assert_equal :"blog/post", @model_name.i18n_key
|
|
|
|
end
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingWithSuppliedModelNameTest < ActiveModel::TestCase
|
2011-05-20 10:48:51 -04:00
|
|
|
def setup
|
2016-08-06 12:38:23 -04:00
|
|
|
@model_name = ActiveModel::Name.new(Blog::Post, nil, "Article")
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "article", @model_name.singular
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "articles", @model_name.plural
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "article", @model_name.element
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "articles", @model_name.collection
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Article", @model_name.human
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "articles", @model_name.route_key
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "article", @model_name.param_key
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
2011-12-05 21:52:45 -05:00
|
|
|
|
|
|
|
def test_i18n_key
|
|
|
|
assert_equal :"article", @model_name.i18n_key
|
|
|
|
end
|
2011-05-20 10:48:51 -04:00
|
|
|
end
|
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingUsingRelativeModelNameTest < ActiveModel::TestCase
|
2011-11-23 14:06:45 -05:00
|
|
|
def setup
|
|
|
|
@model_name = Blog::Post.model_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_singular
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_post", @model_name.singular
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_plural
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog_posts", @model_name.plural
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_element
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post", @model_name.element
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "blog/posts", @model_name.collection
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_human
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Post", @model_name.human
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "posts", @model_name.route_key
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_param_key
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post", @model_name.param_key
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
2011-12-05 21:52:45 -05:00
|
|
|
|
|
|
|
def test_i18n_key
|
|
|
|
assert_equal :"blog/post", @model_name.i18n_key
|
|
|
|
end
|
2011-11-23 14:06:45 -05:00
|
|
|
end
|
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingHelpersTest < ActiveModel::TestCase
|
2010-07-21 07:00:56 -04:00
|
|
|
def setup
|
|
|
|
@klass = Contact
|
|
|
|
@record = @klass.new
|
2016-08-06 12:38:23 -04:00
|
|
|
@singular = "contact"
|
|
|
|
@plural = "contacts"
|
2010-07-21 07:00:56 -04:00
|
|
|
@uncountable = Sheep
|
2016-08-06 12:38:23 -04:00
|
|
|
@singular_route_key = "contact"
|
|
|
|
@route_key = "contacts"
|
|
|
|
@param_key = "contact"
|
2010-07-21 07:00:56 -04:00
|
|
|
end
|
|
|
|
|
2010-10-04 15:04:39 -04:00
|
|
|
def test_to_model_called_on_record
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "post_named_track_backs", plural(Post::TrackBack.new)
|
2010-10-04 15:04:39 -04:00
|
|
|
end
|
|
|
|
|
2010-07-21 07:00:56 -04: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 16:17:42 -04:00
|
|
|
def test_route_key
|
|
|
|
assert_equal @route_key, route_key(@record)
|
2011-12-08 13:52:26 -05:00
|
|
|
assert_equal @singular_route_key, singular_route_key(@record)
|
2010-08-31 16:17:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_route_key_for_class
|
|
|
|
assert_equal @route_key, route_key(@klass)
|
2011-12-08 13:52:26 -05:00
|
|
|
assert_equal @singular_route_key, singular_route_key(@klass)
|
2010-08-31 16:17:42 -04:00
|
|
|
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 07:00:56 -04:00
|
|
|
def test_uncountable
|
2013-03-15 02:25:03 -04:00
|
|
|
assert uncountable?(@uncountable), "Expected 'sheep' to be uncountable"
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not uncountable?(@klass), "Expected 'contact' to be countable"
|
2010-07-21 07:00:56 -04:00
|
|
|
end
|
|
|
|
|
2011-12-08 13:52:26 -05:00
|
|
|
def test_uncountable_route_key
|
|
|
|
assert_equal "sheep", singular_route_key(@uncountable)
|
|
|
|
assert_equal "sheep_index", route_key(@uncountable)
|
|
|
|
end
|
|
|
|
|
2010-07-21 07:00:56 -04:00
|
|
|
private
|
|
|
|
def method_missing(method, *args)
|
2020-10-02 00:50:14 -04:00
|
|
|
ActiveModel::Naming.public_send(method, *args)
|
2010-07-21 07:00:56 -04:00
|
|
|
end
|
|
|
|
end
|
2011-11-24 09:50:21 -05:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NameWithAnonymousClassTest < ActiveModel::TestCase
|
2011-11-24 09:50:21 -05:00
|
|
|
def test_anonymous_class_without_name_argument
|
|
|
|
assert_raises(ArgumentError) do
|
2011-11-26 08:33:58 -05:00
|
|
|
ActiveModel::Name.new(Class.new)
|
2011-11-24 09:50:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_anonymous_class_with_name_argument
|
|
|
|
model_name = ActiveModel::Name.new(Class.new, nil, "Anonymous")
|
|
|
|
assert_equal "Anonymous", model_name
|
|
|
|
end
|
|
|
|
end
|
2014-06-22 22:06:44 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class NamingMethodDelegationTest < ActiveModel::TestCase
|
2014-06-22 22:06:44 -04:00
|
|
|
def test_model_name
|
|
|
|
assert_equal Blog::Post.model_name, Blog::Post.new.model_name
|
|
|
|
end
|
|
|
|
end
|
2020-06-24 22:38:12 -04:00
|
|
|
|
|
|
|
class OverridingAccessorsTest < ActiveModel::TestCase
|
|
|
|
def test_overriding_accessors_keys
|
|
|
|
model_name = ActiveModel::Name.new(Post::TrackBack).tap do |name|
|
|
|
|
name.singular = :singular
|
|
|
|
name.plural = :plural
|
|
|
|
name.element = :element
|
|
|
|
name.collection = :collection
|
|
|
|
name.singular_route_key = :singular_route_key
|
|
|
|
name.route_key = :route_key
|
|
|
|
name.param_key = :param_key
|
|
|
|
name.i18n_key = :i18n_key
|
|
|
|
name.name = :name
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :singular, model_name.singular
|
|
|
|
assert_equal :plural, model_name.plural
|
|
|
|
assert_equal :element, model_name.element
|
|
|
|
assert_equal :collection, model_name.collection
|
|
|
|
assert_equal :singular_route_key, model_name.singular_route_key
|
|
|
|
assert_equal :route_key, model_name.route_key
|
|
|
|
assert_equal :param_key, model_name.param_key
|
|
|
|
assert_equal :i18n_key, model_name.i18n_key
|
|
|
|
assert_equal :name, model_name.name
|
|
|
|
end
|
|
|
|
end
|