From c6d328554010c395e31581cc7a9386d87b31378f Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 27 Nov 2018 16:30:01 -0500 Subject: [PATCH] Fix Relationship#grandchildren grandchild_conditions did not properly building sql. The sql would never return any rows Now it returns the correct nodes --- app/models/relationship.rb | 2 +- spec/models/mixins/relationship_mixin_spec.rb | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/models/relationship.rb b/app/models/relationship.rb index 9babad135e68..ee93e267ddbf 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -197,7 +197,7 @@ def self.stringify_resource_pairs(resource_pairs, options = {}) def grandchild_conditions t = self.class.arel_table t.grouping(t[:ancestry].matches("#{child_ancestry}/%", nil, true).and( - t[:ancestry].does_not_match("#{child_ancestry}/%", nil, true))) + t[:ancestry].does_not_match("#{child_ancestry}/%/%", nil, true))) end def child_and_grandchild_conditions diff --git a/spec/models/mixins/relationship_mixin_spec.rb b/spec/models/mixins/relationship_mixin_spec.rb index 41dff5cab217..f12bc60400c8 100644 --- a/spec/models/mixins/relationship_mixin_spec.rb +++ b/spec/models/mixins/relationship_mixin_spec.rb @@ -813,7 +813,7 @@ end end - describe "#parent_rel_ids" do + describe "#parent_rels" do it "works with relationships" do pars = vms[8].with_relationship_type(test_rel_type, &:parent_rels) pars_vms = pars.map(&:resource) @@ -839,6 +839,24 @@ end end + describe "#grandchild_rels" do + it "works with relationships" do + vms[0].with_relationship_type(test_rel_type) do + rels = vms[0].grandchild_rels + expect(rels.map(&:resource)).to match_array([vms[3], vms[4], vms[5], vms[6], vms[7]]) + end + end + end + + describe "#child_and_grandchild_rels" do + it "works with relationships" do + vms[0].with_relationship_type(test_rel_type) do + rels = vms[0].child_and_grandchild_rels + expect(rels.map(&:resource)).to match_array([vms[1], vms[2], vms[3], vms[4], vms[5], vms[6], vms[7]]) + end + end + end + protected def build_relationship_tree(tree, rel_type = test_rel_type, base_factory = :vm_vmware)