From ac2e988c90e11c2c0429c80e0d92c20f1cd5d59a Mon Sep 17 00:00:00 2001 From: shields Date: Sun, 28 Aug 2022 22:37:13 +0900 Subject: [PATCH] Fixes for mongoid orderable embedded docs --- spec/integration/embedded_spec.rb | 212 +++++++++++++++++++++++++++++- 1 file changed, 207 insertions(+), 5 deletions(-) diff --git a/spec/integration/embedded_spec.rb b/spec/integration/embedded_spec.rb index f55a466..e2b30d8 100644 --- a/spec/integration/embedded_spec.rb +++ b/spec/integration/embedded_spec.rb @@ -19,11 +19,213 @@ def positions expect(positions).to eq([[1, 2], [1, 2, 3]]) end - it 'moves an item returned by a query to position' do - embedded_orderable1 = EmbedsOrderable.first.embedded_orderables.where(position: 1).first - embedded_orderable2 = EmbedsOrderable.first.embedded_orderables.where(position: 2).first - embedded_orderable1.move_to! 2 - expect(embedded_orderable2.reload.position).to eq(1) + context '#move_to' do + + it 'moves an embedded item to top position' do + parent = EmbedsOrderable.first + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb2.move_to!(1) + + [emb1, emb2, parent].each(&:reload) + # TODO: needs to sort order + # expect(parent.embedded_orderables).to eq([emb2, emb1]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2]) + expect(emb1.position).to eq(2) + expect(emb2.position).to eq(1) + end + + it 'moves an embedded item to top position by symbol' do + parent = EmbedsOrderable.first + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb2.move_to!(:top) + + [emb1, emb2, parent].each(&:reload) + # TODO: needs to sort order + # expect(parent.embedded_orderables).to eq([emb2, emb1]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2]) + expect(emb1.position).to eq(2) + expect(emb2.position).to eq(1) + end + + it 'moves an embedded item above top position' do + parent = EmbedsOrderable.first + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb2.move_to!(0) + + [emb1, emb2, parent].each(&:reload) + # TODO: needs to sort order; remove sort from position + # expect(parent.embedded_orderables).to eq([emb2, emb1]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2]) + expect(emb1.position).to eq(2) + expect(emb2.position).to eq(1) + end + + it 'moves an embedded item to middle position' do + parent = EmbedsOrderable.last + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb3 = parent.embedded_orderables.where(position: 3).first + emb3.move_to!(2) + + [emb1, emb2, emb3, parent].each(&:reload) + # TODO: needs to sort order + # expect(parent.embedded_orderables).to eq([emb1, emb3, emb2]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2, 3]) + expect(emb1.position).to eq(1) + expect(emb2.position).to eq(3) + expect(emb3.position).to eq(2) + end + + it 'moves an embedded item to bottom position' do + parent = EmbedsOrderable.first + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb1.move_to!(2) + + [emb1, emb2, parent].each(&:reload) + # TODO: needs to sort order + # expect(parent.embedded_orderables).to eq([emb2, emb1]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2]) + expect(emb1.position).to eq(2) + expect(emb2.position).to eq(1) + end + + it 'moves an embedded item to bottom position by symbol' do + parent = EmbedsOrderable.first + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb1.move_to!(:bottom) + + [emb1, emb2, parent].each(&:reload) + # TODO: needs to sort order + # expect(parent.embedded_orderables).to eq([emb2, emb1]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2]) + expect(emb1.position).to eq(2) + expect(emb2.position).to eq(1) + end + + it 'moves an embedded item below bottom position' do + parent = EmbedsOrderable.first + emb1 = parent.embedded_orderables.where(position: 1).first + emb2 = parent.embedded_orderables.where(position: 2).first + emb1.move_to!(3) + + [emb1, emb2, parent].each(&:reload) + # TODO: needs to sort order; remove sort from position + # expect(parent.embedded_orderables).to eq([emb2, emb1]) + expect(parent.embedded_orderables.map(&:position).sort).to eq([1, 2]) + expect(emb1.position).to eq(2) + expect(emb2.position).to eq(1) + end + end + + context '#save! on parent' do + let!(:parent) { EmbedsOrderable.first } + let!(:emb1) { parent.embedded_orderables.where(position: 1).first } + let!(:emb2) { parent.embedded_orderables.where(position: 2).first } + + it 'saves new item with position set to top' do + emb3 = parent.embedded_orderables.build + emb3.position = 1 + parent.save! + # expect(parent.embedded_orderables).to eq([emb3, emb1, emb2]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set above top' do + emb3 = parent.embedded_orderables.build + emb3.position = 2 + parent.save! + # expect(parent.embedded_orderables).to eq([emb3, emb1, emb2]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set to middle' do + emb3 = parent.embedded_orderables.build + emb3.position = 2 + parent.save! + # expect(parent.embedded_orderables).to eq([emb1, emb3, emb2]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set to bottom' do + emb3 = parent.embedded_orderables.build + emb3.position = 3 + parent.save! + # expect(parent.embedded_orderables).to eq([emb1, emb2, emb3]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set below bottom' do + emb3 = parent.embedded_orderables.build + emb3.position = 4 + parent.save! + # expect(parent.embedded_orderables).to eq([emb1, emb2, emb3]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with nil position' do + parent.embedded_orderables.build + parent.save! + # expect(parent.embedded_orderables).to eq([emb1, emb2, emb3]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + end + + context '#save! on child' do + let!(:parent) { EmbedsOrderable.first } + let!(:emb1) { parent.embedded_orderables.where(position: 1).first } + let!(:emb2) { parent.embedded_orderables.where(position: 2).first } + + it 'saves new item with position set to top' do + emb3 = parent.embedded_orderables.build + emb3.position = 1 + emb3.save! + # expect(parent.embedded_orderables).to eq([emb3, emb1, emb2]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set above top' do + emb3 = parent.embedded_orderables.build + emb3.position = 2 + emb3.save! + # expect(parent.embedded_orderables).to eq([emb3, emb1, emb2]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set to middle' do + emb3 = parent.embedded_orderables.build + emb3.position = 2 + emb3.save! + # expect(parent.embedded_orderables).to eq([emb1, emb3, emb2]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set to bottom' do + emb3 = parent.embedded_orderables.build + emb3.position = 3 + emb3.save! + # expect(parent.embedded_orderables).to eq([emb1, emb2, emb3]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with position set below bottom' do + emb3 = parent.embedded_orderables.build + emb3.position = 4 + emb3.save! + # expect(parent.embedded_orderables).to eq([emb1, emb2, emb3]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end + + it 'saves new item with nil position' do + emb3 = parent.embedded_orderables.build + emb3.save! + # expect(parent.embedded_orderables).to eq([emb1, emb2, emb3]) + expect(parent.reload.embedded_orderables.map(&:position)).to eq [1, 2, 3] + end end end