Skip to content

Commit

Permalink
[back_assignments]
Browse files Browse the repository at this point in the history
- do not call save more than once
- propagate save for collection_of associations
  • Loading branch information
yann ARMAND committed Jan 26, 2015
1 parent 4cd377d commit 330c1b8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 21 deletions.
9 changes: 8 additions & 1 deletion lib/couchrest/model/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ def register_dirty_association(obj)
end

def save_dirty_association
dirty_associations.each do |obj|
while !dirty_associations.empty? do
obj = dirty_associations.pop
obj.save
end
end
Expand All @@ -238,41 +239,47 @@ def << obj
check_obj(obj)
casted_by[casted_by_property.to_s] << obj.id
obj.set_back_association(casted_by, casted_by.class.name)
casted_by.register_dirty_association(obj)
super(obj)
end

def push(obj)
check_obj(obj)
casted_by[casted_by_property.to_s].push obj.id
obj.set_back_association(casted_by, casted_by.class.name)
casted_by.register_dirty_association(obj)
super(obj)
end

def unshift(obj)
check_obj(obj)
casted_by[casted_by_property.to_s].unshift obj.id
obj.set_back_association(casted_by, casted_by.class.name)
casted_by.register_dirty_association(obj)
super(obj)
end

def []= index, obj
check_obj(obj)
casted_by[casted_by_property.to_s][index] = obj.id
obj.set_back_association(casted_by, casted_by.class.name)
casted_by.register_dirty_association(obj)
super(index, obj)
end

def pop
obj = casted_by.send(casted_by_property.options[:proxy_name]).last
casted_by[casted_by_property.to_s].pop
obj.set_back_association(nil, casted_by.class.name)
casted_by.register_dirty_association(obj)
super
end

def shift
obj = casted_by.send(casted_by_property.options[:proxy_name]).first
casted_by[casted_by_property.to_s].shift
obj.set_back_association(nil, casted_by.class.name)
casted_by.register_dirty_association(obj)
super
end

Expand Down
92 changes: 72 additions & 20 deletions spec/unit/assocations_dual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@
mummy.should_receive(:save)
father.save
end
end

it 'should not call save twice in a row' do
father.wife = mummy
mummy.should_receive(:save).exactly(1)
father.save
father.name = 'rogers'
father.save
end
end
end

describe 'of type collection_of' do
Expand All @@ -54,27 +60,55 @@
father.children << kid
kid.dad.should eq(father)
end
describe 'when object is saved' do
it 'should also save other side' do
father.children << kid
kid.should_receive(:save)
father.save
end
end
end

context 'Adding to the collection using push' do
it 'should populate the belongs_to property' do
father.children.push kid
kid.dad.should eq(father)
end
describe 'when object is saved' do
it 'should also save other side' do
father.children.push kid
kid.should_receive(:save)
father.save
end
end
end

context 'Adding to the collection using unshift' do
it 'should populate the belongs_to property' do
father.children.unshift kid
kid.dad.should eq(father)
end
describe 'when object is saved' do
it 'should also save other side' do
father.children.unshift kid
kid.should_receive(:save)
father.save
end
end
end

context 'Adding to the collection using [n]=' do
it 'should populate the belongs_to property' do
father.children[3]= kid
kid.dad.should eq(father)
end
describe 'when object is saved' do
it 'should also save other side' do
father.children[4] = kid
kid.should_receive(:save)
father.save
end
end
end

context 'removing from the collection using pop' do
Expand All @@ -83,6 +117,15 @@
father.children.pop
kid.dad.should be_nil
end
describe 'when object is saved' do
it 'should also save other side' do
father.children.push kid
father.save
father.children.pop
kid.should_receive(:save)
father.save
end
end
end

context 'removing from the collection using shift' do
Expand All @@ -91,6 +134,15 @@
father.children.shift
kid.dad.should be_nil
end
describe 'when object is saved' do
it 'should also save other side' do
father.children.push kid
father.save
father.children.shift
kid.should_receive(:save)
father.save
end
end
end

end
Expand All @@ -110,31 +162,31 @@
lambda { invoice.entries.push entry}.should_not raise_error
end

context 'Adding to the collection using unshift' do
it 'should set property without error' do
lambda { invoice.entries.unshift entry}.should_not raise_error
context 'Adding to the collection using unshift' do
it 'should set property without error' do
lambda { invoice.entries.unshift entry}.should_not raise_error
end
end
end

context 'Adding to the collection using []=' do
it 'should set property without error' do
lambda { invoice.entries[3]= entry}.should_not raise_error
context 'Adding to the collection using []=' do
it 'should set property without error' do
lambda { invoice.entries[3]= entry}.should_not raise_error
end
end
end

context 'removing from the collection using pop' do
it 'should set nil the belongs_to property' do
invoice.entries.push entry
lambda { invoice.entries.pop }.should_not raise_error
context 'removing from the collection using pop' do
it 'should set nil the belongs_to property' do
invoice.entries.push entry
lambda { invoice.entries.pop }.should_not raise_error
end
end
end

context 'removing from the collection using shift' do
it 'should set nil the belongs_to property' do
invoice.entries.push entry
lambda { invoice.entries.shift }.should_not raise_error
context 'removing from the collection using shift' do
it 'should set nil the belongs_to property' do
invoice.entries.push entry
lambda { invoice.entries.shift }.should_not raise_error
end
end
end

end
end
Expand Down

0 comments on commit 330c1b8

Please sign in to comment.