Skip to content

Commit

Permalink
Merge pull request #51684 from fatkodima/raise-relation-with
Browse files Browse the repository at this point in the history
Raise when a block is passed to `ActiveRecord::Relation#with`
  • Loading branch information
byroot authored Apr 29, 2024
2 parents 36b7c1d + 74275ac commit 2587966
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions activerecord/test/cases/core_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/relation/with_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2587966

Please sign in to comment.