Skip to content

Commit

Permalink
Merge branch 'master' into wip-new-record-position
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Nov 7, 2023
2 parents 2c6d854 + 2c0b61b commit fd4a58c
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 68 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 6.0.6 (Next)

* Your change here.

### 6.0.5 (2023/11/08)

* [#75](https://github.com/mongoid/mongoid_orderable/pull/75): Fix: Preserve position when creating new records.
Expand Down
14 changes: 9 additions & 5 deletions lib/mongoid/orderable/handlers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ class Base

def initialize(doc)
@doc = doc

# Required for transactions, which perform some actions
# in the after_create callback.
@new_record = doc.new_record?
reset_new_record
end

protected
Expand Down Expand Up @@ -125,10 +122,17 @@ def move_all
doc.send(:move_all)
end

def clear_move_all!
def reset
reset_new_record
doc.send(:clear_move_all!)
end

# Required for transactions, which perform some actions
# in the after_create callback.
def reset_new_record
@new_record = doc.new_record?
end

def resolve_target_position(field, target_position, in_list)
target_position ||= 'bottom'

Expand Down
6 changes: 3 additions & 3 deletions lib/mongoid/orderable/handlers/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def before_create
end

def after_create
clear_move_all!
reset
end

def before_update
Expand All @@ -19,12 +19,12 @@ def before_update
end

def after_update
clear_move_all!
reset
end

def after_destroy
remove_all_positions
clear_move_all!
reset
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/orderable/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Mongoid
module Orderable
VERSION = '6.0.4'
VERSION = '6.0.5'
end
end
3 changes: 1 addition & 2 deletions lib/mongoid_orderable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

require 'mongoid'

require 'mongoid/orderable/version'

require 'mongoid/orderable'
require 'mongoid/orderable/version'
require 'mongoid/orderable/configs/global_config'
require 'mongoid/orderable/configs/field_config'
require 'mongoid/orderable/errors/invalid_target_position'
Expand Down
108 changes: 61 additions & 47 deletions spec/integration/multiple_fields_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,27 @@
describe 'inserting' do
let(:newbie) { MultipleFieldsOrderable.create! }

before do
@position = newbie.position
@record = MultipleFieldsOrderable.find(newbie.id)
end
before { @position = newbie.position }

it 'top' do
@record.move_to! :top
newbie.move_to! :top
expect(serial_nos).to eq([1, 2, 3, 4, 5, 6])
expect(@record.serial_no).to eq(1)
expect(@record.position).to eq(@position)
expect(newbie.serial_no).to eq(1)
expect(newbie.position).to eq(@position)
end

it 'bottom' do
@record.move_to! :bottom
newbie.move_to! :bottom
expect(serial_nos).to eq([1, 2, 3, 4, 5, 6])
expect(@record.serial_no).to eq(6)
expect(@record.position).to eq(@position)
expect(newbie.serial_no).to eq(6)
expect(newbie.position).to eq(@position)
end

it 'middle' do
@record.move_to! 4
newbie.move_to! 4
expect(serial_nos).to eq([1, 2, 3, 4, 5, 6])
expect(@record.serial_no).to eq(4)
expect(@record.position).to eq(@position)
expect(newbie.serial_no).to eq(4)
expect(newbie.position).to eq(@position)
end
end

Expand Down Expand Up @@ -166,30 +163,27 @@
describe 'inserting' do
let(:newbie) { MultipleFieldsOrderable.create! }

before do
@position = newbie.position
@record = MultipleFieldsOrderable.find(newbie.id)
end
before { @position = newbie.position }

it 'top' do
@record.move_serial_no_to! :top
newbie.move_serial_no_to! :top
expect(serial_nos).to eq([1, 2, 3, 4, 5, 6])
expect(@record.serial_no).to eq(1)
expect(@record.position).to eq(@position)
expect(newbie.serial_no).to eq(1)
expect(newbie.position).to eq(@position)
end

it 'bottom' do
@record.move_serial_no_to! :bottom
newbie.move_serial_no_to! :bottom
expect(serial_nos).to eq([1, 2, 3, 4, 5, 6])
expect(@record.serial_no).to eq(6)
expect(@record.position).to eq(@position)
expect(newbie.serial_no).to eq(6)
expect(newbie.position).to eq(@position)
end

it 'middle' do
@record.move_serial_no_to! 4
newbie.move_serial_no_to! 4
expect(serial_nos).to eq([1, 2, 3, 4, 5, 6])
expect(@record.serial_no).to eq(4)
expect(@record.position).to eq(@position)
expect(newbie.serial_no).to eq(4)
expect(newbie.position).to eq(@position)
end
end

Expand Down Expand Up @@ -322,30 +316,27 @@
describe 'inserting' do
let(:newbie) { MultipleFieldsOrderable.create! }

before do
@serial_no = newbie.serial_no
@record = MultipleFieldsOrderable.find(newbie.id)
end
before { @serial_no = newbie.serial_no }

it 'top' do
@record.move_position_to! :top
newbie.move_position_to! :top
expect(positions).to eq([0, 1, 2, 3, 4, 5])
expect(@record.position).to eq(0)
expect(@record.serial_no).to eq(@serial_no)
expect(newbie.position).to eq(0)
expect(newbie.serial_no).to eq(@serial_no)
end

it 'bottom' do
@record.move_position_to! :bottom
newbie.move_position_to! :bottom
expect(positions).to eq([0, 1, 2, 3, 4, 5])
expect(@record.position).to eq(5)
expect(@record.serial_no).to eq(@serial_no)
expect(newbie.position).to eq(5)
expect(newbie.serial_no).to eq(@serial_no)
end

it 'middle' do
@record.move_position_to! 4
newbie.move_position_to! 4
expect(positions).to eq([0, 1, 2, 3, 4, 5])
expect(@record.position).to eq(4)
expect(@record.serial_no).to eq(@serial_no)
expect(newbie.position).to eq(4)
expect(newbie.serial_no).to eq(@serial_no)
end
end

Expand Down Expand Up @@ -437,8 +428,8 @@
context 'group_count orderable' do
before :each do
MultipleFieldsOrderable.delete_all
2.times { MultipleFieldsOrderable.create! group_id: 1 }
3.times { MultipleFieldsOrderable.create! group_id: 2 }
2.times { MultipleFieldsOrderable.create!(group_id: 1) }
3.times { MultipleFieldsOrderable.create!(group_id: 2) }
end

let(:all_groups) { MultipleFieldsOrderable.order_by([:group_id, :asc], [:groups, :asc]).map(&:groups) }
Expand Down Expand Up @@ -466,24 +457,47 @@

describe 'inserting' do
it 'top' do
newbie = MultipleFieldsOrderable.create! group_id: 1
record = MultipleFieldsOrderable.find(newbie.id)
newbie = MultipleFieldsOrderable.create!(group_id: 1)
newbie.move_groups_to! :top
expect(all_groups).to eq([1, 2, 3, 1, 2, 3])
expect(newbie.groups).to eq(1)
end

it 'bottom' do
newbie = MultipleFieldsOrderable.create!(group_id: 2)
newbie.move_groups_to! :bottom
expect(all_groups).to eq([1, 2, 1, 2, 3, 4])
expect(newbie.groups).to eq(4)
end

it 'middle' do
newbie = MultipleFieldsOrderable.create!(group_id: 2)
newbie.move_groups_to! 2
expect(all_groups).to eq([1, 2, 1, 2, 3, 4])
expect(newbie.groups).to eq(2)
end
end

describe 'moving existing document' do
it 'top' do
newbie = MultipleFieldsOrderable.create!(group_id: 1)
record = MultipleFieldsOrderable.find(newbie._id)
record.move_groups_to! :top
expect(all_groups).to eq([1, 2, 3, 1, 2, 3])
expect(record.groups).to eq(1)
end

it 'bottom' do
newbie = MultipleFieldsOrderable.create! group_id: 2
record = MultipleFieldsOrderable.find(newbie.id)
newbie = MultipleFieldsOrderable.create!(group_id: 2)
record = MultipleFieldsOrderable.find(newbie._id)
record.move_groups_to! :bottom
expect(all_groups).to eq([1, 2, 1, 2, 3, 4])
expect(record.groups).to eq(4)
end

it 'middle' do
newbie = MultipleFieldsOrderable.create! group_id: 2
record = MultipleFieldsOrderable.find(newbie.id)
newbie = MultipleFieldsOrderable.create!(group_id: 2)
record = MultipleFieldsOrderable.find(newbie._id)
record.move_groups_to! 2
expect(all_groups).to eq([1, 2, 1, 2, 3, 4])
expect(record.groups).to eq(2)
Expand Down
18 changes: 8 additions & 10 deletions spec/integration/simple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def positions
expect(another.position).to eq(6)
newbie.save!
expect(positions).to eq([1, 2, 3, 4, 5, 6, 7])
# expect(newbie.position).to eq(6)
# expect(another.position).to eq(6) # not reloaded
expect(newbie.position).to eq(6)
expect(newbie.reload.position).to eq(6)
expect(another.position).to eq(6)
expect(another.reload.position).to eq(7)
end

Expand All @@ -97,42 +97,40 @@ def positions
another = SimpleOrderable.create!
newbie.save!
expect(positions).to eq([1, 2, 3, 4, 5, 6, 7])
# expect(newbie.position).to eq(6)
# expect(another.position).to eq(6) # not reloaded
expect(newbie.position).to eq(6)
expect(newbie.reload.position).to eq(6)
expect(another.position).to eq(6)
expect(another.reload.position).to eq(7)
end

it 'with correct specific position as a number' do
record = SimpleOrderable.create!(position: 3)
# expect(record.position).to eq(3)
expect(record.position).to eq(3)
expect(record.reload.position).to eq(3)
end

it 'with incorrect specific position as a number' do
record = SimpleOrderable.create!(position: -4)
# expect(record.position).to eq(1) # not reloaded
expect(record.position).to eq(1)
expect(record.reload.position).to eq(1)
end

it 'with correct specific position as a string' do
record = SimpleOrderable.create!(position: '4')
# expect(record.position).to eq(4)
expect(record.position).to eq(4)
expect(record.reload.position).to eq(4)
end

it 'with incorrect specific position as a string' do
record = SimpleOrderable.create!(position: '-4')
# expect(record.position).to eq(1)
expect(record.position).to eq(1)
expect(record.reload.position).to eq(1)
end

it 'should offset the positions of all the next elements' do
records = SimpleOrderable.all
expect(records.pluck(:position)).to eq([1, 2, 3, 4, 5])
SimpleOrderable.create!(position: 3)
# expect(records.pluck(:position)).to eq([1, 2, 3, 4, 5, 6]) # not reloaded
records.each(&:reload)
expect(records.pluck(:position)).to eq([1, 2, 4, 5, 6, 3])
end
end
Expand Down

0 comments on commit fd4a58c

Please sign in to comment.