Skip to content

Commit

Permalink
Merge pull request #17511 from NickLaMuro/ancestry_memoization_fixes
Browse files Browse the repository at this point in the history
Ancestry Patch updates/fixes
  • Loading branch information
kbrock authored Jun 5, 2018
2 parents dfa55f3 + 3742661 commit c6a6bed
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def project?
end

def visible_domains
MiqAeDomain.where(:tenant_id => ancestor_ids.append(id)).joins(:tenant).order('tenants.ancestry DESC NULLS LAST, priority DESC')
MiqAeDomain.where(:tenant_id => path_ids).joins(:tenant).order('tenants.ancestry DESC NULLS LAST, priority DESC')
end

def enabled_domains
Expand Down
10 changes: 10 additions & 0 deletions lib/patches/ancestry_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ def update_descendants_with_new_ancestry
end
end
end

def parent=(parent)
super
clear_memoized_instance_variables
end

def parent_id=(parent)
super
clear_memoized_instance_variables
end
end

module Ancestry
Expand Down
24 changes: 24 additions & 0 deletions spec/models/tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@
:tenant_id => t2_2.id)
end

# This spec is here to confirm that we don't mutate the memoized
# ancestor_ids value when calling `Tenant#visible_domains`.
it "does not affect the memoized ancestor_ids variable" do
expected_ancestor_ids = t1_1.ancestor_ids.dup # dup required, don't remove
t1_1.visible_domains
expect(t1_1.ancestor_ids).to eq(expected_ancestor_ids)
end

it "#visibile_domains sub_tenant" do
t1_1
expect(t1_1.visible_domains.collect(&:name)).to eq(%w(DOM5 DOM3 DOM1 DOM15 DOM10))
Expand Down Expand Up @@ -806,4 +814,20 @@
expect(tenantA1.build_tenant_tree).to be_empty
end
end

describe "setting a parent for a new record" do
it "passes back the parent assigned" do
tenant.save!
sub_tenant = FactoryGirl.build(:tenant, :parent => Tenant.root_tenant)

expect(sub_tenant.parent = tenant).to eq(tenant)
end

it "passes back the parent_id assigned" do
tenant.save!
sub_tenant = FactoryGirl.build(:tenant, :parent => Tenant.root_tenant)

expect(sub_tenant.parent_id = tenant.id).to eq(tenant.id)
end
end
end

0 comments on commit c6a6bed

Please sign in to comment.