Permalink
Browse files

to_key should return all exists keys (if any exists), regardless if t…

…he object is persisted or not. If you need it to reflect persistance, you should use to_param.
  • Loading branch information...
josevalim committed Aug 15, 2010
1 parent fd9f3ad commit d0cf212cb5a02db1b3df85e1a337ea1fc99d9b3b
Showing with 7 additions and 3 deletions.
  1. +6 −3 activemodel/lib/active_model/conversion.rb
  2. +1 −0 activemodel/lib/active_model/lint.rb
@@ -33,16 +33,19 @@ def to_model
self
end
- # Returns an Enumerable of all (primary) key attributes or nil if
- # persisted? is false
+ # Returns an Enumerable of all key attributes if any is set, regardless
+ # if the object is persisted or not.
+ #
+ # Note the default implementation uses persisted? just because all objects
+ # in Ruby 1.8.x responds to :id.
def to_key
persisted? ? [id] : nil
end
# Returns a string representing the object's key suitable for use in URLs,
# or nil if persisted? is false
def to_param
- to_key ? to_key.join('-') : nil
+ persisted? ? to_key.join('-') : nil
end
end
end
@@ -38,6 +38,7 @@ def model.persisted?() false end
# not persisted?, then to_param should always return nil.
def test_to_param
assert model.respond_to?(:to_param), "The model should respond to to_param"
+ def model.to_key() [1] end
def model.persisted?() false end
assert model.to_param.nil?
end

0 comments on commit d0cf212

Please sign in to comment.