diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 97f98b288e..8f5c940e77 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -367,7 +367,8 @@ class Enterprise < ActiveRecord::Base def relate_to_owners_enterprises # When a new enterprise is created, we relate them to all enterprises owned by - # the same owner. + # the same owner, in both directions. So all enterprises owned by the same owner + # will have permissions to every other one, in both directions. enterprises = owner.owned_enterprises.where('enterprises.id != ?', self) @@ -378,6 +379,13 @@ class Enterprise < ActiveRecord::Base :manage_products, :edit_profile, :create_variant_overrides]) + + EnterpriseRelationship.create!(parent: enterprise, + child: self, + permissions_list: [:add_to_order_cycle, + :manage_products, + :edit_profile, + :create_variant_overrides]) end end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 1b285eecca..494a543260 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -602,8 +602,11 @@ describe Enterprise do let(:er1) { EnterpriseRelationship.where(child_id: hub1).last } let(:er2) { EnterpriseRelationship.where(child_id: hub2).last } let(:er3) { EnterpriseRelationship.where(child_id: producer).last } + let(:er4) { EnterpriseRelationship.where(parent_id: hub1).last } + let(:er5) { EnterpriseRelationship.where(parent_id: hub2).last } + let(:er6) { EnterpriseRelationship.where(parent_id: producer).last } - it "establishes relationships for new hubs with the owner's hubs and producers" do + it "establishes bi-directional relationships for new hubs with the owner's hubs and producers" do hub1 hub2 producer @@ -611,21 +614,26 @@ describe Enterprise do expect do enterprise = create(:enterprise, owner: owner) - end.to change(EnterpriseRelationship, :count).by(3) + end.to change(EnterpriseRelationship, :count).by(6) [er1, er2, er3].each do |er| er.parent.should == enterprise er.permissions.map(&:name).sort.should == ['add_to_order_cycle', 'manage_products', 'edit_profile', 'create_variant_overrides'].sort end + + [er4, er5, er6].each do |er| + er.child.should == enterprise + er.permissions.map(&:name).sort.should == ['add_to_order_cycle', 'manage_products', 'edit_profile', 'create_variant_overrides'].sort + end end - it "establishes relationships when producers are created" do + it "establishes bi-directional relationships when producers are created" do hub1 hub2 expect do producer - end.to change(EnterpriseRelationship, :count).by(2) + end.to change(EnterpriseRelationship, :count).by(4) end end end