File tree Expand file tree Collapse file tree 4 files changed +31
-14
lines changed Expand file tree Collapse file tree 4 files changed +31
-14
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ require "active_support/deprecation"
4
+
3
5
module ActiveRecord
6
+ include ActiveSupport ::Deprecation ::DeprecatedConstantAccessor
7
+
4
8
# = Active Record Errors
5
9
#
6
10
# Generic Active Record exception class.
@@ -476,10 +480,15 @@ def initialize(model = nil, description = nil)
476
480
# relation.loaded? # => true
477
481
#
478
482
# # 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
+ )
483
492
484
493
# TransactionIsolationError will be raised under the following conditions:
485
494
#
Original file line number Diff line number Diff line change @@ -174,7 +174,7 @@ def #{method_name} # def includes_values
174
174
end # end
175
175
176
176
def #{ method_name } =(value) # def includes_values=(value)
177
- assert_mutability ! # assert_mutability !
177
+ assert_modifiable ! # assert_modifiable !
178
178
@values[:#{ name } ] = value # @values[:includes] = value
179
179
end # end
180
180
CODE
@@ -814,7 +814,7 @@ def unscope!(*args) # :nodoc:
814
814
if !VALID_UNSCOPING_VALUES . include? ( scope )
815
815
raise ArgumentError , "Called unscope() with invalid unscoping argument ':#{ scope } '. Valid arguments are :#{ VALID_UNSCOPING_VALUES . to_a . join ( ", :" ) } ."
816
816
end
817
- assert_mutability !
817
+ assert_modifiable !
818
818
@values . delete ( scope )
819
819
when Hash
820
820
scope . each do |key , target_value |
@@ -1723,8 +1723,8 @@ def build_join_dependencies
1723
1723
)
1724
1724
end
1725
1725
1726
- def assert_mutability !
1727
- raise ImmutableRelation if @loaded || @arel
1726
+ def assert_modifiable !
1727
+ raise UnmodifiableRelation if @loaded || @arel
1728
1728
end
1729
1729
1730
1730
def build_arel ( connection , aliases = nil )
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "cases/helper"
4
+ require "active_record/errors"
4
5
5
6
class ErrorsTest < ActiveRecord ::TestCase
6
7
def test_can_be_instantiated_with_no_args
@@ -15,4 +16,11 @@ def test_can_be_instantiated_with_no_args
15
16
end
16
17
end
17
18
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
18
26
end
Original file line number Diff line number Diff line change @@ -2057,7 +2057,7 @@ def test_destroy_by
2057
2057
relation = Post . all
2058
2058
relation . to_a
2059
2059
2060
- assert_raises ( ActiveRecord ::ImmutableRelation ) do
2060
+ assert_raises ( ActiveRecord ::UnmodifiableRelation ) do
2061
2061
relation . where! "foo"
2062
2062
end
2063
2063
end
@@ -2066,7 +2066,7 @@ def test_destroy_by
2066
2066
relation = Post . all
2067
2067
relation . to_a
2068
2068
2069
- assert_raises ( ActiveRecord ::ImmutableRelation ) do
2069
+ assert_raises ( ActiveRecord ::UnmodifiableRelation ) do
2070
2070
relation . limit! 5
2071
2071
end
2072
2072
end
@@ -2075,7 +2075,7 @@ def test_destroy_by
2075
2075
relation = Post . all
2076
2076
relation . to_a
2077
2077
2078
- assert_raises ( ActiveRecord ::ImmutableRelation ) do
2078
+ assert_raises ( ActiveRecord ::UnmodifiableRelation ) do
2079
2079
relation . merge! where : "foo"
2080
2080
end
2081
2081
end
@@ -2084,7 +2084,7 @@ def test_destroy_by
2084
2084
relation = Post . all
2085
2085
relation . to_a
2086
2086
2087
- assert_raises ( ActiveRecord ::ImmutableRelation ) do
2087
+ assert_raises ( ActiveRecord ::UnmodifiableRelation ) do
2088
2088
relation . extending! Module . new
2089
2089
end
2090
2090
end
@@ -2093,8 +2093,8 @@ def test_destroy_by
2093
2093
relation = Post . all
2094
2094
relation . arel
2095
2095
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" ) }
2098
2098
end
2099
2099
2100
2100
test "relations show the records in #inspect" do
You can’t perform that action at this time.
0 commit comments