1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activemodel/test/cases/conversion_test.rb
Yves Senn 7d196cf360 #to_param returns nil if to_key returns nil. Closes #11399.
The documentation of `#to_key` (http://api.rubyonrails.org/classes/ActiveModel/Conversion.html#method-i-to_key)
states that it returns `nil` if there are no key attributes. `to_param` needs
to be aware of that fact and return `nil` as well.

Previously it raised the following exception:

```
  1) Error:
ConversionTest#test_to_param_returns_nil_if_to_key_is_nil:
NoMethodError: undefined method `join' for nil:NilClass
    /Users/senny/Projects/rails/activemodel/lib/active_model/conversion.rb:65:in `to_param'
    /Users/senny/Projects/rails/activemodel/test/cases/conversion_test.rb:34:in `block in <class:ConversionTest>'
```
2014-02-04 10:27:46 +01:00

46 lines
1.4 KiB
Ruby

require 'cases/helper'
require 'models/contact'
require 'models/helicopter'
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
assert_equal [1], Contact.new(id: 1).to_key
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
assert_equal "1", Contact.new(id: 1).to_param
end
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
test "to_partial_path default implementation returns a string giving a relative path" do
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"
end
test "to_partial_path handles namespaced models" do
assert_equal "helicopter/comanches/comanche", Helicopter::Comanche.new.to_partial_path
end
end