Permalink
Browse files

Don't use identity map if loading readonly records, this will prevent…

… changing readonly status on already loaded records.
  • Loading branch information...
1 parent 448420f commit c0ad5e48f643e9721059e94ba23dab3159eebb93 @miloops miloops committed Sep 17, 2010
Showing with 35 additions and 8 deletions.
  1. +9 −2 activerecord/lib/active_record/relation.rb
  2. +26 −6 activerecord/test/cases/identity_map_test.rb
@@ -61,15 +61,22 @@ def respond_to?(method, include_private = false)
def to_a
return @records if loaded?
- @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel.to_sql, @bind_values)
+ readonly = @readonly_value.nil? ? @implicit_readonly : @readonly_value
+
+ @records = if readonly
+ IdentityMap.without do
+ eager_loading? ? find_with_associations : @klass.find_by_sql(arel.to_sql, @bind_values)
+ end
+ else
+ eager_loading? ? find_with_associations : @klass.find_by_sql(arel.to_sql, @bind_values)
+ end
preload = @preload_values
preload += @includes_values unless eager_loading?
preload.each {|associations| @klass.send(:preload_associations, @records, associations) }
# @readonly_value is true only if set explicitly. @implicit_readonly is true if there
# are JOINS and no explicit SELECT.
- readonly = @readonly_value.nil? ? @implicit_readonly : @readonly_value
@records.each { |record| record.readonly! } if readonly
@loaded = true
@@ -242,12 +242,6 @@ def test_eager_loading_with_conditions_on_string_joined_table_preloads
assert_equal authors(:david), assert_no_queries { posts[0].author}
end
- # Second search should not change read only status for collection
- def test_find_with_joins_option_implies_readonly
- Developer.joins(', projects').each { |d| assert d.readonly? }
- Developer.joins(', projects').readonly(false).each { |d| assert d.readonly? }
- end
-
def test_reload_object_if_save_failed
developer = Developer.first
developer.salary = 0
@@ -305,4 +299,30 @@ def test_associated_object_are_assigned_from_identity_map
assert_equal post.object_id, comment.post.target.object_id
end
end
+
+ def test_find_using_identity_map_respects_readonly_when_loading_associated_object_first
+ author = Author.first
+ readonly_comment = author.readonly_comments.first
+
+ comment = Comment.first
+ assert !comment.readonly?
+
+ assert readonly_comment.readonly?
+
+ assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save}
+ assert comment.save
+ end
+
+ def test_find_using_identity_map_respects_readonly
+ comment = Comment.first
+ assert !comment.readonly?
+
+ author = Author.first
+ readonly_comment = author.readonly_comments.first
+
+ assert readonly_comment.readonly?
+
+ assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save}
+ assert comment.save
+ end
end

0 comments on commit c0ad5e4

Please sign in to comment.