Skip to content

Commit 5b0e7a3

Browse files
authored
Merge pull request rails#52320 from rails/fxn/constant-deprecation
Deprecate ActiveRecord::ImmutableRelation
2 parents 4867559 + 1e2c260 commit 5b0e7a3

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

activerecord/lib/active_record/errors.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# frozen_string_literal: true
22

3+
require "active_support/deprecation"
4+
35
module ActiveRecord
6+
include ActiveSupport::Deprecation::DeprecatedConstantAccessor
7+
48
# = Active Record Errors
59
#
610
# Generic Active Record exception class.
@@ -476,10 +480,15 @@ def initialize(model = nil, description = nil)
476480
# relation.loaded? # => true
477481
#
478482
# # Methods which try to mutate a loaded relation fail.
479-
# relation.where!(title: 'TODO') # => ActiveRecord::ImmutableRelation
480-
# relation.limit!(5) # => ActiveRecord::ImmutableRelation
481-
class ImmutableRelation < ActiveRecordError
482-
end
483+
# relation.where!(title: 'TODO') # => ActiveRecord::UnmodifiableRelation
484+
# relation.limit!(5) # => ActiveRecord::UnmodifiableRelation
485+
class UnmodifiableRelation < ActiveRecordError
486+
end
487+
deprecate_constant(
488+
:ImmutableRelation,
489+
"ActiveRecord::UnmodifiableRelation",
490+
deprecator: ActiveRecord.deprecator
491+
)
483492

484493
# TransactionIsolationError will be raised under the following conditions:
485494
#

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def #{method_name} # def includes_values
174174
end # end
175175
176176
def #{method_name}=(value) # def includes_values=(value)
177-
assert_mutability! # assert_mutability!
177+
assert_modifiable! # assert_modifiable!
178178
@values[:#{name}] = value # @values[:includes] = value
179179
end # end
180180
CODE
@@ -814,7 +814,7 @@ def unscope!(*args) # :nodoc:
814814
if !VALID_UNSCOPING_VALUES.include?(scope)
815815
raise ArgumentError, "Called unscope() with invalid unscoping argument ':#{scope}'. Valid arguments are :#{VALID_UNSCOPING_VALUES.to_a.join(", :")}."
816816
end
817-
assert_mutability!
817+
assert_modifiable!
818818
@values.delete(scope)
819819
when Hash
820820
scope.each do |key, target_value|
@@ -1723,8 +1723,8 @@ def build_join_dependencies
17231723
)
17241724
end
17251725

1726-
def assert_mutability!
1727-
raise ImmutableRelation if @loaded || @arel
1726+
def assert_modifiable!
1727+
raise UnmodifiableRelation if @loaded || @arel
17281728
end
17291729

17301730
def build_arel(connection, aliases = nil)

activerecord/test/cases/errors_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "cases/helper"
4+
require "active_record/errors"
45

56
class ErrorsTest < ActiveRecord::TestCase
67
def test_can_be_instantiated_with_no_args
@@ -15,4 +16,11 @@ def test_can_be_instantiated_with_no_args
1516
end
1617
end
1718
end
19+
20+
def test_active_record_immutable_relation_deprecation
21+
expected_message = "ActiveRecord::ImmutableRelation is deprecated! Use ActiveRecord::UnmodifiableRelation instead"
22+
assert_deprecated(expected_message, ActiveRecord.deprecator) do
23+
assert_same ActiveRecord::UnmodifiableRelation, ActiveRecord::ImmutableRelation
24+
end
25+
end
1826
end

activerecord/test/cases/relations_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ def test_destroy_by
20572057
relation = Post.all
20582058
relation.to_a
20592059

2060-
assert_raises(ActiveRecord::ImmutableRelation) do
2060+
assert_raises(ActiveRecord::UnmodifiableRelation) do
20612061
relation.where! "foo"
20622062
end
20632063
end
@@ -2066,7 +2066,7 @@ def test_destroy_by
20662066
relation = Post.all
20672067
relation.to_a
20682068

2069-
assert_raises(ActiveRecord::ImmutableRelation) do
2069+
assert_raises(ActiveRecord::UnmodifiableRelation) do
20702070
relation.limit! 5
20712071
end
20722072
end
@@ -2075,7 +2075,7 @@ def test_destroy_by
20752075
relation = Post.all
20762076
relation.to_a
20772077

2078-
assert_raises(ActiveRecord::ImmutableRelation) do
2078+
assert_raises(ActiveRecord::UnmodifiableRelation) do
20792079
relation.merge! where: "foo"
20802080
end
20812081
end
@@ -2084,7 +2084,7 @@ def test_destroy_by
20842084
relation = Post.all
20852085
relation.to_a
20862086

2087-
assert_raises(ActiveRecord::ImmutableRelation) do
2087+
assert_raises(ActiveRecord::UnmodifiableRelation) do
20882088
relation.extending! Module.new
20892089
end
20902090
end
@@ -2093,8 +2093,8 @@ def test_destroy_by
20932093
relation = Post.all
20942094
relation.arel
20952095

2096-
assert_raises(ActiveRecord::ImmutableRelation) { relation.limit!(5) }
2097-
assert_raises(ActiveRecord::ImmutableRelation) { relation.where!("1 = 2") }
2096+
assert_raises(ActiveRecord::UnmodifiableRelation) { relation.limit!(5) }
2097+
assert_raises(ActiveRecord::UnmodifiableRelation) { relation.where!("1 = 2") }
20982098
end
20992099

21002100
test "relations show the records in #inspect" do

0 commit comments

Comments
 (0)