Skip to content
Browse files

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
  • Loading branch information...
1 parent a3c6ad7 commit 9acd686753c43612984aaa4002e80113fda2b255 @snusnu snusnu committed with Yehuda Katz
View
21 actionpack/lib/action_controller/record_identifier.rb
@@ -60,13 +60,32 @@ def dom_class(record_or_class, prefix = nil)
#
# dom_id(Post.find(45), :edit) # => "edit_post_45"
def dom_id(record, prefix = nil)
- if record_id = record.id
+ if record_id = record_key_for_dom_id(record)
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
else
dom_class(record, prefix || NEW)
end
end
+ # Returns a string representation of the key attribute(s) that is suitable for use in an HTML DOM id.
+ # This can be overwritten to customize the default generated string representation if desired.
+ # If you need to read back a key from a dom_id in order to query for the underlying database record,
+ # you should write a helper like 'person_record_from_dom_id' that will extract the key either based
+ # on the default implementation (which just joins all key attributes with '-') or on your own
+ # overwritten version of the method. By default, this implementation passes the key string through a
+ # method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
+ # make sure yourself that your dom ids are valid, in case you overwrite this method.
+ def record_key_for_dom_id(record)
+ return record.id unless record.respond_to?(:to_model)
+ key = record.to_model.key
+ key ? sanitize_dom_id(key.join('-')) : key
+ end
+
+ # Replaces characters that are invalid in HTML DOM ids with valid ones.
+ def sanitize_dom_id(candidate_id)
+ candidate_id # TODO implement conversion to valid DOM id values
+ end
+
# Returns the plural class name of a record or class. Examples:
#
# plural_class_name(post) # => "posts"
View
16 actionpack/test/lib/controller/fake_models.rb
@@ -6,6 +6,10 @@ class Customer < Struct.new(:name, :id)
undef_method :to_json
+ def key
+ id ? [id] : nil
+ end
+
def to_param
id.to_s
end
@@ -43,6 +47,10 @@ class Question < Struct.new(:name, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion
+ def key
+ id ? [id] : nil
+ end
+
def to_param
id.to_s
end
@@ -59,6 +67,10 @@ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost
alias_method :secret?, :secret
+ def key
+ id ? [id] : nil
+ end
+
def new_record=(boolean)
@new_record = boolean
end
@@ -84,6 +96,7 @@ class Comment
attr_reader :id
attr_reader :post_id
def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
+ def key; id ? [id] : nil end
def save; @id = 1; @post_id = 1 end
def new_record?; @id.nil? end
def to_param; @id; end
@@ -103,6 +116,7 @@ class Tag
attr_reader :id
attr_reader :post_id
def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
+ def key; id ? [id] : nil end
def save; @id = 1; @post_id = 1 end
def new_record?; @id.nil? end
def to_param; @id; end
@@ -122,6 +136,7 @@ class CommentRelevance
attr_reader :id
attr_reader :comment_id
def initialize(id = nil, comment_id = nil); @id, @comment_id = id, comment_id end
+ def key; id ? [id] : nil end
def save; @id = 1; @comment_id = 1 end
def new_record?; @id.nil? end
def to_param; @id; end
@@ -137,6 +152,7 @@ class TagRelevance
attr_reader :id
attr_reader :tag_id
def initialize(id = nil, tag_id = nil); @id, @tag_id = id, tag_id end
+ def key; id ? [id] : nil end
def save; @id = 1; @tag_id = 1 end
def new_record?; @id.nil? end
def to_param; @id; end
View
3 actionpack/test/template/prototype_helper_test.rb
@@ -4,6 +4,7 @@
class Bunny < Struct.new(:Bunny, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion
+ def key() id ? [id] : nil end
end
class Author
@@ -11,6 +12,7 @@ class Author
include ActiveModel::Conversion
attr_reader :id
+ def key() id ? [id] : nil end
def save; @id = 1 end
def new_record?; @id.nil? end
def name
@@ -23,6 +25,7 @@ class Article
include ActiveModel::Conversion
attr_reader :id
attr_reader :author_id
+ def key() id ? [id] : nil end
def save; @id = 1; @author_id = 1 end
def new_record?; @id.nil? end
def name
View
23 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
View
2 activemodel/lib/active_model/naming.rb
@@ -41,7 +41,7 @@ def human(options={})
# To implement, just extend ActiveModel::Naming in your object:
#
# class BookCover
- # exten ActiveModel::Naming
+ # extend ActiveModel::Naming
# end
#
# BookCover.model_name #=> "BookCover"
View
8 activemodel/test/cases/lint_test.rb
@@ -10,6 +10,14 @@ def to_model
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
View
14 activerecord/lib/active_record/attribute_methods/primary_key.rb
@@ -39,6 +39,20 @@ def set_primary_key(value = nil, &block)
end
alias :primary_key= :set_primary_key
end
+
+ module InstanceMethods
+
+ # Returns this record's primary key value wrapped in an Array
+ # or nil if the record is a new_record?
+ # This is done to comply with the AMo interface that expects
+ # every AMo compliant object to respond_to?(:key) and return
+ # an Enumerable object from that call, or nil if new_record?
+ def key
+ new_record? ? nil : [ self.id ]
+ end
+
+ end
+
end
end
end
View
9 activerecord/test/cases/pk_test.rb
@@ -9,6 +9,15 @@
class PrimaryKeysTest < ActiveRecord::TestCase
fixtures :topics, :subscribers, :movies, :mixed_case_monkeys
+ def test_key
+ # test new record
+ topic = Topic.new
+ assert topic.key.nil?
+ # test existing record
+ topic = Topic.find(1)
+ assert_equal topic.key, [1]
+ end
+
def test_integer_key
topic = Topic.find(1)
assert_equal(topics(:first).author_name, topic.author_name)

0 comments on commit 9acd686

Please sign in to comment.
Something went wrong with that request. Please try again.