require File.expand_path(File.dirname(__FILE__) + '/abstract_unit') class ModelExtrasTest < Test::Unit::TestCase def test_humanized_attribute assert_equal "Jonathan", people(:jonathan).humanized_attribute(:name) # String remains unchanged assert_equal "2003-01-01", people(:jonathan).humanized_attribute(:birth_date) # Date becomes a string assert_equal "Male", people(:jonathan).humanized_attribute(:gender_id) # belongs_to queries related object assert_nil people(:david).humanized_attribute(:user_id) end def test_humanized_attribute_when_foreign_key_invalid p = people(:jonathan) p.gender = genders(:female) assert genders(:male).destroy assert_nothing_raised do assert_nil p.humanized_attribute(:gender_id, :read_original_attribute) end end def test_humanized_modified_attributes p = people(:jonathan) p.update_attributes(:name => "Pop", :birth_date => Date.new(2000, 4, 5), :gender => genders(:female)) assert_equal({ "name" => "Jonathan", "birth_date" => "2003-01-01", "gender_id" => "Male" }, p.humanized_modified_attributes) end def test_humanized_belongs_to_polymorphic assert_equal "Jonathan", addresses(:jonathan).humanized_attribute(:addressable_id) end def test_default_to_summary assert_equal people(:jonathan).name, people(:jonathan).to_summary end end