aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorsnusnu <gamsnjaga@gmail.com>2010-02-20 08:24:10 +0100
committerYehuda Katz <yehudakatz@YK.local>2010-02-19 23:31:25 -0800
commit9acd686753c43612984aaa4002e80113fda2b255 (patch)
treef174d18c640e99fc079b667fc00a73ee09953541 /activemodel
parenta3c6ad7d5e567cf4d688147357c5de134763599d (diff)
downloadrails-9acd686753c43612984aaa4002e80113fda2b255.tar.gz
rails-9acd686753c43612984aaa4002e80113fda2b255.tar.bz2
rails-9acd686753c43612984aaa4002e80113fda2b255.zip
Adds #key and #to_param to the AMo interface
This commit introduces two new methods that every AMo compliant object must implement. Below are the default implementations along with the implied interface contract. # Returns an Enumerable of all (primary) key # attributes or nil if new_record? is true def key new_record? ? nil : [1] end # Returns a string representing the object's key # suitable for use in URLs, or nil if new_record? # is true def to_param key ? key.first.to_s : nil end 1) The #key method Previously rails' record_identifier code, which is used in the #dom_id helper, relied on calling #id on the record to provide a reasonable DOM id. Now with rails3 being all ORM agnostic, it's not safe anymore to assume that every record ever will have an #id as its primary key attribute. Having a #key method available on every AMo object means that #dom_id can be implemented using record.to_model.key # instead of record.id Using this we're able to take composite primary keys into account (e.g. available in datamapper) by implementing #dom_id using a newly added record_key_for_dom_id(record) method. The user can overwrite this method to provide customized versions of the object's key used in #dom_id. Also, dealing with more complex keys that can contain arbitrary strings, means that we need to make sure that we only provide DOM ids that are valid according to the spec. For this reason, this patch sends the key provided through a newly added sanitize_dom_id(candidate_id) method, that makes sure we only produce valid HTML The reason to not just add #dom_id to the AMo interface was that it feels like providing a DOM id should not be a model concern. Adding #dom_id to the AMo interface would force these concern on the model, while it's better left to be implemented in a helper. Now one could say the same is true for #to_param, and actually I think that it doesn't really fit into the model either, but it's used in AR and it's a main part of integrating into the rails router. This is different from #dom_id which is only used in view helpers and can be implemented on top of a semantically more meaningful method like #key. 2) The #to_param method Since the rails router relies on #to_param to be present, AR::Base implements it and returns the id by default, allowing the user to overwrite the method if desired. Now with different ORMs integrating into rails, every ORM railtie needs to implement it's own #to_param implementation while already providing code to be AMo compliant. Since the whole point of AMo compliance seems to be to integrate any ORM seamlessly into rails, it seems fair that all we really need to do as another ORM, is to be AMo compliant. By including #to_param into the official interface, we can make sure that this code can be centralized in the various AMo compliance layers, and not be added separately by every ORM railtie. 3) All specs pass
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/lint.rb23
-rw-r--r--activemodel/lib/active_model/naming.rb2
-rw-r--r--activemodel/test/cases/lint_test.rb8
3 files changed, 32 insertions, 1 deletions
diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb
index 7bf0ad712d..c97286e61c 100644
--- a/activemodel/lib/active_model/lint.rb
+++ b/activemodel/lib/active_model/lint.rb
@@ -13,6 +13,29 @@
module ActiveModel
module Lint
module Tests
+
+ # == Responds to <tt>key</tt>
+ #
+ # Returns an Enumerable of all (primary) key attributes
+ # or nil if model.new_record? is true
+ def test_key
+ assert model.respond_to?(:key), "The model should respond to key"
+ def model.new_record?() true end
+ assert model.key.nil?
+ def model.new_record?() false end
+ assert model.key.respond_to?(:each)
+ end
+
+ # == Responds to <tt>to_param</tt>
+ #
+ # Returns a string representing the object's key suitable for use in URLs
+ # or nil if model.new_record? is true
+ def test_to_param
+ assert model.respond_to?(:to_param), "The model should respond to to_param"
+ def model.new_record?() true end
+ assert model.to_param.nil?
+ end
+
# == Responds to <tt>valid?</tt>
#
# Returns a boolean that specifies whether the object is in a valid or invalid
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index 89e8f8b1ea..39512a427b 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -41,7 +41,7 @@ module ActiveModel
# To implement, just extend ActiveModel::Naming in your object:
#
# class BookCover
- # exten ActiveModel::Naming
+ # extend ActiveModel::Naming
# end
#
# BookCover.model_name #=> "BookCover"
diff --git a/activemodel/test/cases/lint_test.rb b/activemodel/test/cases/lint_test.rb
index 63804004ee..58716cbf85 100644
--- a/activemodel/test/cases/lint_test.rb
+++ b/activemodel/test/cases/lint_test.rb
@@ -10,6 +10,14 @@ class LintTest < ActiveModel::TestCase
self
end
+ def key
+ new_record? ? nil : [1]
+ end
+
+ def to_param
+ key ? key.first.to_s : nil
+ end
+
def valid?() true end
def new_record?() true end
def destroyed?() true end