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/person"
|
2009-10-20 20:20:01 -04:00
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class ActiveModelI18nTests < ActiveModel::TestCase
|
2009-10-20 20:20:01 -04:00
|
|
|
def setup
|
|
|
|
I18n.backend = I18n::Backend::Simple.new
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2014-03-07 12:59:57 -05:00
|
|
|
def teardown
|
|
|
|
I18n.backend.reload!
|
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
def test_translated_model_attributes
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { person: { name: "person name attribute" } } }
|
|
|
|
assert_equal "person name attribute", Person.human_attribute_name("name")
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
2010-01-06 20:45:50 -05:00
|
|
|
|
|
|
|
def test_translated_model_attributes_with_default
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", attributes: { name: "name default attribute" }
|
|
|
|
assert_equal "name default attribute", Person.human_attribute_name("name")
|
2010-01-06 20:45:50 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-12-05 09:31:06 -05:00
|
|
|
def test_translated_model_attributes_using_default_option
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "name default attribute", Person.human_attribute_name("name", default: "name default attribute")
|
2010-12-05 09:31:06 -05:00
|
|
|
end
|
|
|
|
|
2010-12-05 10:26:14 -05:00
|
|
|
def test_translated_model_attributes_using_default_option_as_symbol
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", default_name: "name default attribute"
|
|
|
|
assert_equal "name default attribute", Person.human_attribute_name("name", default: :default_name)
|
2010-12-05 10:26:14 -05:00
|
|
|
end
|
|
|
|
|
2010-12-05 09:32:29 -05:00
|
|
|
def test_translated_model_attributes_falling_back_to_default
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Name", Person.human_attribute_name("name")
|
2010-12-05 09:32:29 -05:00
|
|
|
end
|
|
|
|
|
2010-12-05 09:57:45 -05:00
|
|
|
def test_translated_model_attributes_using_default_option_as_symbol_and_falling_back_to_default
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Name", Person.human_attribute_name("name", default: :default_name)
|
2010-12-05 09:57:45 -05:00
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
def test_translated_model_attributes_with_symbols
|
2016-08-16 03:30:11 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { person: { name: "person name attribute" } } }
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "person name attribute", Person.human_attribute_name(:name)
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_translated_model_attributes_with_ancestor
|
2016-08-16 03:30:11 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { child: { name: "child name attribute" } } }
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "child name attribute", Child.human_attribute_name("name")
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_translated_model_attributes_with_ancestors_fallback
|
2016-08-16 03:30:11 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { person: { name: "person name attribute" } } }
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "person name attribute", Child.human_attribute_name("name")
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
|
|
|
|
2011-02-27 18:14:11 -05:00
|
|
|
def test_translated_model_attributes_with_attribute_matching_namespaced_model_name
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: {
|
2016-08-16 03:30:11 -04:00
|
|
|
person: { gender: "person gender" },
|
2016-08-06 13:37:12 -04:00
|
|
|
"person/gender": { attribute: "person gender attribute" }
|
2013-05-01 20:10:06 -04:00
|
|
|
} }
|
2011-02-27 18:14:11 -05:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "person gender", Person.human_attribute_name("gender")
|
|
|
|
assert_equal "person gender attribute", Person::Gender.human_attribute_name("attribute")
|
2011-02-27 18:14:11 -05:00
|
|
|
end
|
|
|
|
|
2012-04-14 05:00:54 -04:00
|
|
|
def test_translated_deeply_nested_model_attributes
|
2016-08-06 13:37:12 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { "person/contacts/addresses": { street: "Deeply Nested Address Street" } } }
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Deeply Nested Address Street", Person.human_attribute_name("contacts.addresses.street")
|
2012-04-14 05:00:54 -04:00
|
|
|
end
|
|
|
|
|
2011-12-05 08:57:47 -05:00
|
|
|
def test_translated_nested_model_attributes
|
2016-08-06 13:37:12 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { "person/addresses": { street: "Person Address Street" } } }
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal "Person Address Street", Person.human_attribute_name("addresses.street")
|
2011-12-05 08:57:47 -05:00
|
|
|
end
|
|
|
|
|
2011-12-05 09:39:41 -05:00
|
|
|
def test_translated_nested_model_attributes_with_namespace_fallback
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { attributes: { addresses: { street: "Cool Address Street" } } }
|
|
|
|
assert_equal "Cool Address Street", Person.human_attribute_name("addresses.street")
|
2011-12-05 08:57:47 -05:00
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
def test_translated_model_names
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { models: { person: "person model" } }
|
|
|
|
assert_equal "person model", Person.model_name.human
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_translated_model_names_with_sti
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { models: { child: "child model" } }
|
|
|
|
assert_equal "child model", Child.model_name.human
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
|
|
|
|
2015-12-03 00:37:05 -05:00
|
|
|
def test_translated_model_with_namespace
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { models: { 'person/gender': "gender model" } }
|
|
|
|
assert_equal "gender model", Person::Gender.model_name.human
|
2015-12-03 00:37:05 -05:00
|
|
|
end
|
|
|
|
|
2009-10-20 20:20:01 -04:00
|
|
|
def test_translated_model_names_with_ancestors_fallback
|
2016-08-06 12:38:23 -04:00
|
|
|
I18n.backend.store_translations "en", activemodel: { models: { person: "person model" } }
|
|
|
|
assert_equal "person model", Child.model_name.human
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|
2010-08-12 14:43:10 -04:00
|
|
|
|
|
|
|
def test_human_does_not_modify_options
|
2016-08-06 12:38:23 -04:00
|
|
|
options = { default: "person model" }
|
2010-08-12 14:43:10 -04:00
|
|
|
Person.model_name.human(options)
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal({ default: "person model" }, options)
|
2012-04-28 10:23:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_human_attribute_name_does_not_modify_options
|
2016-08-06 12:38:23 -04:00
|
|
|
options = { default: "Cool gender" }
|
|
|
|
Person.human_attribute_name("gender", options)
|
|
|
|
assert_equal({ default: "Cool gender" }, options)
|
2010-08-12 14:43:10 -04:00
|
|
|
end
|
2009-10-20 20:20:01 -04:00
|
|
|
end
|