Make Relation#create_with always merge rather than overwrite, not just when merging two relations. If you wish to overwrite, you can do relation.create_with(nil), or for a specific attribute, relation.create_with(:attr => nil).

This commit is contained in:
Jon Leighton 2011-01-03 10:04:40 +00:00 committed by Aaron Patterson
parent 31d101879f
commit 1313d386da
3 changed files with 12 additions and 4 deletions

@ -128,7 +128,7 @@ def readonly(value = true)
def create_with(value)
relation = clone
relation.create_with_value = value
relation.create_with_value = value && (@create_with_value || {}).merge(value)
relation
end

@ -53,9 +53,7 @@ def merge(r)
merged_relation.lock_value = r.lock_value unless merged_relation.lock_value
if r.create_with_value
merged_relation.create_with_value = (merged_relation.create_with_value || {}).merge(r.create_with_value)
end
merged_relation = merged_relation.create_with(r.create_with_value) if r.create_with_value
# Apply scope extension modules
merged_relation.send :apply_modules, r.extensions

@ -491,5 +491,15 @@ def test_create_with_merge
PoorDeveloperCalledJamis.create_with(:name => 'Aaron')).new
assert_equal 20, aaron.salary
assert_equal 'Aaron', aaron.name
aaron = PoorDeveloperCalledJamis.create_with(:name => 'foo', :salary => 20).
create_with(:name => 'Aaron').new
assert_equal 20, aaron.salary
assert_equal 'Aaron', aaron.name
end
def test_create_with_reset
jamis = PoorDeveloperCalledJamis.create_with(:name => 'Aaron').create_with(nil).new
assert_equal 'Jamis', jamis.name
end
end