2010-02-21 07:47:37 +00:00
|
|
|
require 'cases/helper'
|
|
|
|
require 'models/contact'
|
2011-07-08 21:54:15 +00:00
|
|
|
require 'models/helicopter'
|
2010-02-21 07:47:37 +00:00
|
|
|
|
|
|
|
class ConversionTest < ActiveModel::TestCase
|
|
|
|
test "to_model default implementation returns self" do
|
|
|
|
contact = Contact.new
|
|
|
|
assert_equal contact, contact.to_model
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_key default implementation returns nil for new records" do
|
|
|
|
assert_nil Contact.new.to_key
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_key default implementation returns the id in an array for persisted records" do
|
2013-05-02 00:10:06 +00:00
|
|
|
assert_equal [1], Contact.new(id: 1).to_key
|
2010-02-21 07:47:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "to_param default implementation returns nil for new records" do
|
|
|
|
assert_nil Contact.new.to_param
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_param default implementation returns a string of ids for persisted records" do
|
2013-05-02 00:10:06 +00:00
|
|
|
assert_equal "1", Contact.new(id: 1).to_param
|
2010-02-21 07:47:37 +00:00
|
|
|
end
|
2011-07-08 21:54:15 +00:00
|
|
|
|
2014-06-11 17:48:47 +00:00
|
|
|
test "to_param returns the string joined by '-'" do
|
|
|
|
assert_equal "abc-xyz", Contact.new(id: ["abc", "xyz"]).to_param
|
|
|
|
end
|
|
|
|
|
2014-02-04 09:27:46 +00:00
|
|
|
test "to_param returns nil if to_key is nil" do
|
|
|
|
klass = Class.new(Contact) do
|
|
|
|
def persisted?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nil klass.new.to_param
|
|
|
|
end
|
|
|
|
|
2012-03-10 22:40:27 +00:00
|
|
|
test "to_partial_path default implementation returns a string giving a relative path" do
|
2011-08-01 09:42:00 +00:00
|
|
|
assert_equal "contacts/contact", Contact.new.to_partial_path
|
|
|
|
assert_equal "helicopters/helicopter", Helicopter.new.to_partial_path,
|
|
|
|
"ActiveModel::Conversion#to_partial_path caching should be class-specific"
|
2011-07-08 21:54:15 +00:00
|
|
|
end
|
2013-01-14 19:05:43 +00:00
|
|
|
|
|
|
|
test "to_partial_path handles namespaced models" do
|
|
|
|
assert_equal "helicopter/comanches/comanche", Helicopter::Comanche.new.to_partial_path
|
|
|
|
end
|
2011-07-08 21:54:15 +00:00
|
|
|
end
|