Skip to content

Commit 621bc68

Browse files
authored
Merge pull request rails#49080 from gmcgibbon/generate_correct_cpk_param
Fix to_param id generation for partial composite keys
2 parents 9d8ac62 + a7cc807 commit 621bc68

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

actionview/test/template/url_helper_test.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ def setup
13021302
@routes = WorkshopsController::ROUTES
13031303
end
13041304

1305-
def test_new_resource
1305+
def test_index_resource
13061306
@controller = WorkshopsController.new
13071307

13081308
get :index
@@ -1316,6 +1316,13 @@ def test_existing_resource
13161316
assert_equal %{/workshops/1\n<a href="/workshops/1">Workshop</a>}, @response.body
13171317
end
13181318

1319+
def test_existing_cpk_resource
1320+
@controller = WorkshopsController.new
1321+
1322+
get :show, params: { id: "1-27" }
1323+
assert_equal %{/workshops/1-27\n<a href="/workshops/1-27">Workshop</a>}, @response.body
1324+
end
1325+
13191326
def test_current_page_when_options_does_not_respond_to_to_hash
13201327
@controller = WorkshopsController.new
13211328

activemodel/lib/active_model/conversion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def to_key
8888
# person = Person.new(1)
8989
# person.to_param # => "1"
9090
def to_param
91-
(persisted? && key = to_key) ? key.join(self.class.param_delimiter) : nil
91+
(persisted? && (key = to_key) && key.all?) ? key.join(self.class.param_delimiter) : nil
9292
end
9393

9494
# Returns a +string+ identifying the path associated with the object.

activemodel/test/cases/conversion_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class ConversionTest < ActiveModel::TestCase
3434
assert_equal "abc-xyz", Contact.new(id: ["abc", "xyz"]).to_param
3535
end
3636

37+
test "to_param returns nil if composite id is incomplete" do
38+
assert_nil Contact.new(id: [1, nil]).to_param
39+
end
40+
3741
test "to_param returns nil if to_key is nil" do
3842
klass = Class.new(Contact) do
3943
def persisted?

0 commit comments

Comments
 (0)