Skip to content

Commit 2587966

Browse files
authored
Merge pull request rails#51684 from fatkodima/raise-relation-with
Raise when a block is passed to `ActiveRecord::Relation#with`
2 parents 36b7c1d + 74275ac commit 2587966

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ def _select!(*fields) # :nodoc:
473473
# .with(posts_with_comments: Post.where("comments_count > ?", 0))
474474
# .with(posts_with_tags: Post.where("tags_count > ?", 0))
475475
def with(*args)
476+
raise ArgumentError, "ActiveRecord::Relation#with does not accept a block" if block_given?
476477
check_if_method_has_arguments!(__callee__, args)
477478
spawn.with!(*args)
478479
end

activerecord/test/cases/core_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_inspect_instance_includes_just_id_by_default
2323
end
2424

2525
def test_inspect_includes_attributes_from_attributes_for_inspect
26-
Topic.with(attributes_for_inspect: [:id, :title, :author_name]) do
26+
Topic.stub(:attributes_for_inspect, [:id, :title, :author_name]) do
2727
topic = topics(:first)
2828

2929
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David">), topic.inspect
@@ -33,7 +33,7 @@ def test_inspect_includes_attributes_from_attributes_for_inspect
3333
def test_inspect_instance_with_lambda_date_formatter
3434
before = Time::DATE_FORMATS[:inspect]
3535

36-
Topic.with(attributes_for_inspect: [:id, :last_read]) do
36+
Topic.stub(:attributes_for_inspect, [:id, :last_read]) do
3737
Time::DATE_FORMATS[:inspect] = ->(date) { "my_format" }
3838
topic = topics(:first)
3939

@@ -48,7 +48,7 @@ def test_inspect_new_instance
4848
end
4949

5050
def test_inspect_limited_select_instance
51-
Topic.with(attributes_for_inspect: [:id, :title]) do
51+
Topic.stub(:attributes_for_inspect, [:id, :title]) do
5252
assert_equal %(#<Topic id: 1>), Topic.all.merge!(select: "id", where: "id = 1").first.inspect
5353
assert_equal %(#<Topic id: 1, title: "The First Topic">), Topic.all.merge!(select: "id, title", where: "id = 1").first.inspect
5454
end
@@ -64,7 +64,7 @@ def test_inspect_class_without_table
6464
end
6565

6666
def test_inspect_with_attributes_for_inspect_all_lists_all_attributes
67-
Topic.with(attributes_for_inspect: :all) do
67+
Topic.stub(:attributes_for_inspect, :all) do
6868
topic = topics(:first)
6969

7070
assert_equal <<~STRING.squish, topic.inspect
@@ -108,7 +108,7 @@ def test_pretty_print_persisted
108108
end
109109

110110
def test_pretty_print_full
111-
Topic.with(attributes_for_inspect: :all) do
111+
Topic.stub(:attributes_for_inspect, :all) do
112112
topic = topics(:first)
113113
actual = +""
114114
PP.pp(topic, StringIO.new(actual))

activerecord/test/cases/integration_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ def test_to_param_for_a_composite_primary_key_model
103103
end
104104

105105
def test_param_delimiter_changes_delimiter_used_in_to_param
106-
Cpk::Order.with(param_delimiter: ",") do
106+
Cpk::Order.stub(:param_delimiter, ",") do
107107
assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param)
108108
end
109109
end
110110

111111
def test_param_delimiter_is_defined_per_class
112-
Cpk::Order.with(param_delimiter: ",") do
113-
Cpk::Book.with(param_delimiter: ";") do
112+
Cpk::Order.stub(:param_delimiter, ",") do
113+
Cpk::Book.stub(:param_delimiter, ";") do
114114
assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param)
115115
assert_equal("1;123", Cpk::Book.new(id: [1, 123]).to_param)
116116
end

activerecord/test/cases/relation/with_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ def test_with_left_joins
8282
assert_equal Post.count, records.size
8383
assert_equal POSTS_WITH_COMMENTS, records.filter_map { _1.id if _1.has_comments }
8484
end
85+
86+
def test_raises_when_using_block
87+
assert_raises(ArgumentError, match: "does not accept a block") do
88+
Post.with(attributes_for_inspect: :id) { }
89+
end
90+
end
8591
else
8692
def test_common_table_expressions_are_unsupported
8793
assert_raises ActiveRecord::StatementInvalid do

0 commit comments

Comments
 (0)