Skip to content

Commit

Permalink
Adds #key and #to_param to the AMo interface
Browse files Browse the repository at this point in the history
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
snusnu authored and Yehuda Katz committed Feb 20, 2010
1 parent a3c6ad7 commit 9acd686
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 2 deletions.
21 changes: 20 additions & 1 deletion actionpack/lib/action_controller/record_identifier.rb
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions actionpack/test/lib/controller/fake_models.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions actionpack/test/template/prototype_helper_test.rb
Expand Up @@ -4,13 +4,15 @@
class Bunny < Struct.new(:Bunny, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion
def key() id ? [id] : nil end
end

class Author
extend ActiveModel::Naming
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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions activemodel/lib/active_model/lint.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/naming.rb
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions activemodel/test/cases/lint_test.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions activerecord/lib/active_record/attribute_methods/primary_key.rb
Expand Up @@ -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
9 changes: 9 additions & 0 deletions activerecord/test/cases/pk_test.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 9acd686

Please sign in to comment.