Skip to content

Commit ac91900

Browse files
committed
Refactor ModuleResult#pre_order
This is an optimization made by @danielkza at 805c7c1 which reduces the complexity of finding the root finding it through the Processing. I'm not sure the memozation through the attribute @pre_order worked at and if we should keep it.
1 parent ae8a0dd commit ac91900

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

app/models/module_result.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ def to_json(options={})
3131
end
3232

3333
def pre_order
34-
root = self
35-
root = root.parent until root.parent.nil?
36-
@pre_order ||= pre_order_traverse(root).to_a
34+
return @pre_order unless @pre_order.nil?
35+
return processing.root_module_result.pre_order unless parent.nil?
36+
37+
@pre_order = pre_order_traverse(self).to_a
3738
end
3839

3940
def descendants

spec/models/module_result_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,15 @@
164164
context 'when it is not the root module result' do
165165
let!(:root) { FactoryGirl.build(:module_result, id: 1)}
166166
let!(:child) { FactoryGirl.build(:module_result, id: 2)}
167+
let!(:processing) { FactoryGirl.build(:processing, root_module_result: root)}
168+
167169
before :each do
168-
child.expects(:parent).twice.returns(root)
170+
child.expects(:parent).returns(root)
171+
child.expects(:processing).returns(processing)
169172
root.expects(:parent).returns(nil)
170173
root.expects(:children).returns([child])
171174
end
175+
172176
it 'is expected to return the complete pre order tree traversal (from root)' do
173177
expect(child.pre_order).to eq([root, child])
174178
end

0 commit comments

Comments
 (0)