Skip to content

Commit 3ae9818

Browse files
committed
remove deprecation warning when modifying a Relation with cached arel.
This adresses 1b7aa62#commitcomment-9147803
1 parent 65520c2 commit 3ae9818

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

activerecord/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Remove deprecation when modifying a relation with cached arel.
2+
This raises an `ImmutableRelation` error instead.
3+
4+
*Yves Senn*
5+
16
* Added `ActiveRecord::SecureToken` in order to encapsulate generation of
27
unique tokens for attributes in a model using `SecureRandom`.
38

activerecord/lib/active_record/relation/query_methods.rb

+14-21
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,14 @@ def not(opts, *rest)
6262

6363
Relation::MULTI_VALUE_METHODS.each do |name|
6464
class_eval <<-CODE, __FILE__, __LINE__ + 1
65-
def #{name}_values # def select_values
66-
@values[:#{name}] || [] # @values[:select] || []
67-
end # end
68-
#
69-
def #{name}_values=(values) # def select_values=(values)
70-
raise ImmutableRelation if @loaded # raise ImmutableRelation if @loaded
71-
check_cached_relation
72-
@values[:#{name}] = values # @values[:select] = values
73-
end # end
65+
def #{name}_values # def select_values
66+
@values[:#{name}] || [] # @values[:select] || []
67+
end # end
68+
#
69+
def #{name}_values=(values) # def select_values=(values)
70+
assert_mutability! # assert_mutability!
71+
@values[:#{name}] = values # @values[:select] = values
72+
end # end
7473
CODE
7574
end
7675

@@ -85,23 +84,12 @@ def #{name}_value # def readonly_value
8584
Relation::SINGLE_VALUE_METHODS.each do |name|
8685
class_eval <<-CODE, __FILE__, __LINE__ + 1
8786
def #{name}_value=(value) # def readonly_value=(value)
88-
raise ImmutableRelation if @loaded # raise ImmutableRelation if @loaded
89-
check_cached_relation
87+
assert_mutability! # assert_mutability!
9088
@values[:#{name}] = value # @values[:readonly] = value
9189
end # end
9290
CODE
9391
end
9492

95-
def check_cached_relation # :nodoc:
96-
if defined?(@arel) && @arel
97-
@arel = nil
98-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
99-
Modifying already cached Relation. The cache will be reset. Use a
100-
cloned Relation to prevent this warning.
101-
MSG
102-
end
103-
end
104-
10593
def create_with_value # :nodoc:
10694
@values[:create_with] || {}
10795
end
@@ -857,6 +845,11 @@ def arel # :nodoc:
857845

858846
private
859847

848+
def assert_mutability!
849+
raise ImmutableRelation if @loaded
850+
raise ImmutableRelation if defined?(@arel) && @arel
851+
end
852+
860853
def build_arel
861854
arel = Arel::SelectManager.new(table)
862855

activerecord/test/cases/relations_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,14 @@ def test_presence
16511651
end
16521652
end
16531653

1654+
test "relations with cached arel can't be mutated [internal API]" do
1655+
relation = Post.all
1656+
relation.count
1657+
1658+
assert_raises(ActiveRecord::ImmutableRelation) { relation.limit!(5) }
1659+
assert_raises(ActiveRecord::ImmutableRelation) { relation.where!("1 = 2") }
1660+
end
1661+
16541662
test "relations show the records in #inspect" do
16551663
relation = Post.limit(2)
16561664
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect

0 commit comments

Comments
 (0)