aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-08-15 11:29:15 -0300
committerJosé Valim <jose.valim@gmail.com>2010-08-15 11:29:15 -0300
commitd0cf212cb5a02db1b3df85e1a337ea1fc99d9b3b (patch)
treec76b4ab32010d486e0ddb4a9c259619d8c23ccc3 /activemodel
parentfd9f3ad36b7ad116dfd902db5b23e3178411dc73 (diff)
downloadrails-d0cf212cb5a02db1b3df85e1a337ea1fc99d9b3b.tar.gz
rails-d0cf212cb5a02db1b3df85e1a337ea1fc99d9b3b.tar.bz2
rails-d0cf212cb5a02db1b3df85e1a337ea1fc99d9b3b.zip
to_key should return all exists keys (if any exists), regardless if the object is persisted or not. If you need it to reflect persistance, you should use to_param.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/conversion.rb9
-rw-r--r--activemodel/lib/active_model/lint.rb1
2 files changed, 7 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb
index b998a59e81..ae0ab93e97 100644
--- a/activemodel/lib/active_model/conversion.rb
+++ b/activemodel/lib/active_model/conversion.rb
@@ -33,8 +33,11 @@ module ActiveModel
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
@@ -42,7 +45,7 @@ module ActiveModel
# 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
diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb
index 26eb4a3c41..d7a6da48ca 100644
--- a/activemodel/lib/active_model/lint.rb
+++ b/activemodel/lib/active_model/lint.rb
@@ -38,6 +38,7 @@ module ActiveModel
# 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