Permalink
Browse files

Merge pull request #29814 from kamipo/dont_cache_scope_for_create

Don't cache `scope_for_create`
  • Loading branch information...
kaspth committed Jul 16, 2017
2 parents c74820d + d476553 commit 919b55d9747f69c566616d740616d11ef2db8c58
@@ -171,7 +171,7 @@ def initialize_attributes(record, except_from_scope_attributes = nil) #:nodoc:
skip_assign = [reflection.foreign_key, reflection.type].compact
assigned_keys = record.changed_attribute_names_to_save
assigned_keys += except_from_scope_attributes.keys.map(&:to_s)
attributes = scope_for_create.except(*(assigned_keys - skip_assign))
attributes = scope_for_create.except!(*(assigned_keys - skip_assign))
record.send(:_assign_attributes, attributes) if attributes.any?
set_inverse_instance(record)
end
@@ -31,7 +31,7 @@ def force_reload_reader
private
def scope_for_create
super.except(klass.primary_key)
super.except!(klass.primary_key)
end
def find_target
@@ -556,7 +556,7 @@ def reload
end
def reset
@to_sql = @scope_for_create = @arel = @loaded = @should_eager_load = nil
@to_sql = @arel = @loaded = @should_eager_load = nil
@records = [].freeze
@offsets = {}
self
@@ -590,7 +590,7 @@ def where_values_hash(relation_table_name = table_name)
end
def scope_for_create
@scope_for_create ||= where_values_hash.merge!(create_with_value.stringify_keys)
where_values_hash.merge!(create_with_value.stringify_keys)
end
# Returns true if relation needs eager loading.
@@ -88,22 +88,14 @@ def test_create_with_value
end
def test_create_with_value_with_wheres
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
relation.where!(id: 10)
relation.create_with_value = { hello: "world" }
assert_equal({ "hello" => "world", "id" => 10 }, relation.scope_for_create)
end
# FIXME: is this really wanted or expected behavior?
def test_scope_for_create_is_cached
relation = Relation.new(Post, Post.arel_table, Post.predicate_builder)
assert_equal({}, relation.scope_for_create)
relation.where!(id: 10)
assert_equal({}, relation.scope_for_create)
assert_equal({ "id" => 10 }, relation.scope_for_create)
relation.create_with_value = { hello: "world" }
assert_equal({}, relation.scope_for_create)
assert_equal({ "hello" => "world", "id" => 10 }, relation.scope_for_create)
end
def test_bad_constants_raise_errors

0 comments on commit 919b55d

Please sign in to comment.