Merge pull request #51684 from fatkodima/raise-relation-with

Raise when a block is passed to `ActiveRecord::Relation#with`
This commit is contained in:
Jean Boussier 2024-04-29 13:50:53 +02:00 committed by GitHub
commit 258796693b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 8 deletions

@ -473,6 +473,7 @@ def _select!(*fields) # :nodoc:
# .with(posts_with_comments: Post.where("comments_count > ?", 0))
# .with(posts_with_tags: Post.where("tags_count > ?", 0))
def with(*args)
raise ArgumentError, "ActiveRecord::Relation#with does not accept a block" if block_given?
check_if_method_has_arguments!(__callee__, args)
spawn.with!(*args)
end

@ -23,7 +23,7 @@ def test_inspect_instance_includes_just_id_by_default
end
def test_inspect_includes_attributes_from_attributes_for_inspect
Topic.with(attributes_for_inspect: [:id, :title, :author_name]) do
Topic.stub(:attributes_for_inspect, [:id, :title, :author_name]) do
topic = topics(:first)
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
def test_inspect_instance_with_lambda_date_formatter
before = Time::DATE_FORMATS[:inspect]
Topic.with(attributes_for_inspect: [:id, :last_read]) do
Topic.stub(:attributes_for_inspect, [:id, :last_read]) do
Time::DATE_FORMATS[:inspect] = ->(date) { "my_format" }
topic = topics(:first)
@ -48,7 +48,7 @@ def test_inspect_new_instance
end
def test_inspect_limited_select_instance
Topic.with(attributes_for_inspect: [:id, :title]) do
Topic.stub(:attributes_for_inspect, [:id, :title]) do
assert_equal %(#<Topic id: 1>), Topic.all.merge!(select: "id", where: "id = 1").first.inspect
assert_equal %(#<Topic id: 1, title: "The First Topic">), Topic.all.merge!(select: "id, title", where: "id = 1").first.inspect
end
@ -64,7 +64,7 @@ def test_inspect_class_without_table
end
def test_inspect_with_attributes_for_inspect_all_lists_all_attributes
Topic.with(attributes_for_inspect: :all) do
Topic.stub(:attributes_for_inspect, :all) do
topic = topics(:first)
assert_equal <<~STRING.squish, topic.inspect
@ -108,7 +108,7 @@ def test_pretty_print_persisted
end
def test_pretty_print_full
Topic.with(attributes_for_inspect: :all) do
Topic.stub(:attributes_for_inspect, :all) do
topic = topics(:first)
actual = +""
PP.pp(topic, StringIO.new(actual))

@ -103,14 +103,14 @@ def test_to_param_for_a_composite_primary_key_model
end
def test_param_delimiter_changes_delimiter_used_in_to_param
Cpk::Order.with(param_delimiter: ",") do
Cpk::Order.stub(:param_delimiter, ",") do
assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param)
end
end
def test_param_delimiter_is_defined_per_class
Cpk::Order.with(param_delimiter: ",") do
Cpk::Book.with(param_delimiter: ";") do
Cpk::Order.stub(:param_delimiter, ",") do
Cpk::Book.stub(:param_delimiter, ";") do
assert_equal("1,123", Cpk::Order.new(id: [1, 123]).to_param)
assert_equal("1;123", Cpk::Book.new(id: [1, 123]).to_param)
end

@ -82,6 +82,12 @@ def test_with_left_joins
assert_equal Post.count, records.size
assert_equal POSTS_WITH_COMMENTS, records.filter_map { _1.id if _1.has_comments }
end
def test_raises_when_using_block
assert_raises(ArgumentError, match: "does not accept a block") do
Post.with(attributes_for_inspect: :id) { }
end
end
else
def test_common_table_expressions_are_unsupported
assert_raises ActiveRecord::StatementInvalid do