Skip to content

Commit 67aa8ad

Browse files
Merge pull request #93 from iamprich/sort_model_by_id
Sort models by id to ensure correct order of children
2 parents 567798e + 12d502c commit 67aa8ad

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/isomorphic_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def self.gather_records(records_to_process, force, record_being_saved)
242242
end if record.new? || record.changed? || (record == record_being_saved && force)
243243
record_index += 1
244244
end
245-
[models, associations, backing_records]
245+
[models.sort_by { |model| model[:id] }, associations, backing_records]
246246
end
247247

248248
def save_or_validate(save, validate, force, &block)

ruby/hyper-model/spec/batch2/relationships_spec.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ class TestComponent2 < React::Component::Base
9696
end
9797

9898
it "adding child to a new model on client after render" do
99-
# Hyperloop.configuration do |config|
100-
# #config.transport = :none
101-
# end
10299
m = FactoryBot.create(:test_model)
103100
m.child_models << FactoryBot.create(:child_model)
104101
mount "TestComponent2" do
@@ -122,6 +119,30 @@ def self.add_child
122119
page.should have_content("parent has 3 children")
123120
end
124121

122+
it "preserves the order of children" do
123+
isomorphic do
124+
ChildModel.class_eval do
125+
server_method :do_some_calc do
126+
child_attribute
127+
end
128+
end
129+
TestModel.class_eval do
130+
server_method :do_some_calc do
131+
child_models.collect(&:child_attribute).join(', ')
132+
end
133+
end
134+
end
135+
expect_promise do
136+
parent = TestModel.new
137+
4.times do |i|
138+
parent.child_models << ChildModel.new(child_attribute: i.to_s)
139+
end
140+
ReactiveRecord.load do
141+
parent.do_some_calc.tap { parent.child_models[3].do_some_calc }
142+
end
143+
end.to eq('0, 1, 2, 3')
144+
end
145+
125146
it "will re-render the count after an item is added or removed from a model" do
126147
m1 = FactoryBot.create(:test_model)
127148
mount "TestComponent2" do

ruby/hyper-model/spec/spec_helper.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ class ActiveRecord::Base
271271
# DatabaseCleaner.start
272272
# end
273273

274-
config.after(:each) do |example|
275-
unless example.exception
276-
# Clear session data
277-
Capybara.reset_sessions!
278-
# Rollback transaction
279-
DatabaseCleaner.clean
280-
end
274+
config.after(:each) do # |example|
275+
# unless example.exception
276+
# Clear session data
277+
Capybara.reset_sessions!
278+
# Rollback transaction
279+
DatabaseCleaner.clean
280+
# end
281281
end
282282

283283
config.after(:all, :js => true) do

0 commit comments

Comments
 (0)