mirror of https://github.com/rails/rails
Fix form_for id generation for new CPK models
Adds tests for form_for use with composite primary key models. Fixes bug related to new CPK model dom id generation.
This commit is contained in:
parent
735cba5bed
commit
7ea19698b0
|
@ -112,7 +112,7 @@ module ActionView
|
|||
# make sure yourself that your dom ids are valid, in case you override this method.
|
||||
def record_key_for_dom_id(record) # :doc:
|
||||
key = convert_to_model(record).to_key
|
||||
key ? key.join(JOIN) : key
|
||||
key && key.all? ? key.join(JOIN) : nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -218,12 +218,27 @@ class Plane
|
|||
end
|
||||
end
|
||||
|
||||
class CompositePrimaryKeyRecord
|
||||
extend ActiveModel::Naming
|
||||
include ActiveModel::Conversion
|
||||
attr_reader :id
|
||||
module Cpk
|
||||
class Book < Struct.new(:author_id, :id, :title)
|
||||
extend ActiveModel::Naming
|
||||
include ActiveModel::Conversion
|
||||
|
||||
def initialize(id)
|
||||
@id = id
|
||||
def initialize(author_id: nil, id: nil, title: nil)
|
||||
self.author_id = author_id
|
||||
self.title = title
|
||||
self.id = id
|
||||
end
|
||||
|
||||
def persisted?
|
||||
id.all?
|
||||
end
|
||||
|
||||
def id
|
||||
[@author_id, @id]
|
||||
end
|
||||
|
||||
def id=(id)
|
||||
@author_id, @id = Array(id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -156,6 +156,10 @@ class FormHelperTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
namespace(:cpk) do
|
||||
resources(:books)
|
||||
end
|
||||
|
||||
get "/foo", to: "controller#action"
|
||||
root to: "main#index"
|
||||
end
|
||||
|
@ -4113,6 +4117,20 @@ class FormHelperTest < ActionView::TestCase
|
|||
assert_equal 1, initialization_count, "form builder instantiated more than once"
|
||||
end
|
||||
|
||||
def test_form_for_with_new_cpk_model
|
||||
form_for(Cpk::Book.new) { }
|
||||
|
||||
expected = whole_form("/cpk/books", "new_cpk_book", "new_cpk_book", method: "post")
|
||||
assert_dom_equal expected, @rendered
|
||||
end
|
||||
|
||||
def test_form_for_with_persisted_cpk_model
|
||||
form_for(Cpk::Book.new(id: [1, 2], title: "Some book")) { }
|
||||
|
||||
expected = whole_form("/cpk/books/1-2", "edit_cpk_book_1_2", "edit_cpk_book", method: "patch")
|
||||
assert_dom_equal expected, @rendered
|
||||
end
|
||||
|
||||
private
|
||||
def hidden_fields(options = {})
|
||||
method = options[:method]
|
||||
|
|
|
@ -31,8 +31,8 @@ class RecordIdentifierTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_dom_id_with_composite_primary_key_record
|
||||
record = CompositePrimaryKeyRecord.new([1, 123])
|
||||
assert_equal("composite_primary_key_record_1_123", dom_id(record))
|
||||
record = Cpk::Book.new(id: [1, 123])
|
||||
assert_equal("cpk_book_1_123", dom_id(record))
|
||||
end
|
||||
|
||||
def test_dom_id_with_prefix
|
||||
|
|
Loading…
Reference in New Issue