Skip to content

Commit b1f0efe

Browse files
authored
Merge pull request rails#49069 from gmcgibbon/generate_correct_cpk_form_ids
Fix form_for id generation for new CPK models
2 parents 4f33e65 + 7ea1969 commit b1f0efe

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

actionview/lib/action_view/record_identifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def dom_id(record_or_class, prefix = nil)
112112
# make sure yourself that your dom ids are valid, in case you override this method.
113113
def record_key_for_dom_id(record) # :doc:
114114
key = convert_to_model(record).to_key
115-
key ? key.join(JOIN) : key
115+
key && key.all? ? key.join(JOIN) : nil
116116
end
117117
end
118118
end

actionview/test/lib/controller/fake_models.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,27 @@ def save
218218
end
219219
end
220220

221-
class CompositePrimaryKeyRecord
222-
extend ActiveModel::Naming
223-
include ActiveModel::Conversion
224-
attr_reader :id
221+
module Cpk
222+
class Book < Struct.new(:author_id, :id, :title)
223+
extend ActiveModel::Naming
224+
include ActiveModel::Conversion
225+
226+
def initialize(author_id: nil, id: nil, title: nil)
227+
self.author_id = author_id
228+
self.title = title
229+
self.id = id
230+
end
225231

226-
def initialize(id)
227-
@id = id
232+
def persisted?
233+
id.all?
234+
end
235+
236+
def id
237+
[@author_id, @id]
238+
end
239+
240+
def id=(id)
241+
@author_id, @id = Array(id)
242+
end
228243
end
229244
end

actionview/test/template/form_helper_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def @post.to_param; "123"; end
156156
end
157157
end
158158

159+
namespace(:cpk) do
160+
resources(:books)
161+
end
162+
159163
get "/foo", to: "controller#action"
160164
root to: "main#index"
161165
end
@@ -4113,6 +4117,20 @@ def test_form_for_only_instantiates_builder_once
41134117
assert_equal 1, initialization_count, "form builder instantiated more than once"
41144118
end
41154119

4120+
def test_form_for_with_new_cpk_model
4121+
form_for(Cpk::Book.new) { }
4122+
4123+
expected = whole_form("/cpk/books", "new_cpk_book", "new_cpk_book", method: "post")
4124+
assert_dom_equal expected, @rendered
4125+
end
4126+
4127+
def test_form_for_with_persisted_cpk_model
4128+
form_for(Cpk::Book.new(id: [1, 2], title: "Some book")) { }
4129+
4130+
expected = whole_form("/cpk/books/1-2", "edit_cpk_book_1_2", "edit_cpk_book", method: "patch")
4131+
assert_dom_equal expected, @rendered
4132+
end
4133+
41164134
private
41174135
def hidden_fields(options = {})
41184136
method = options[:method]

actionview/test/template/record_identifier_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def test_dom_id_with_saved_record
3131
end
3232

3333
def test_dom_id_with_composite_primary_key_record
34-
record = CompositePrimaryKeyRecord.new([1, 123])
35-
assert_equal("composite_primary_key_record_1_123", dom_id(record))
34+
record = Cpk::Book.new(id: [1, 123])
35+
assert_equal("cpk_book_1_123", dom_id(record))
3636
end
3737

3838
def test_dom_id_with_prefix

0 commit comments

Comments
 (0)