diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 8f5c940e77..462910aa0d 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -253,6 +253,10 @@ class Enterprise < ActiveRecord::Base self.sells != "none" end + def is_hub + self.sells == 'any' + end + # Simplify enterprise categories for frontend logic and icons, and maybe other things. def category # Make this crazy logic human readable so we can argue about it sanely. @@ -366,26 +370,33 @@ class Enterprise < ActiveRecord::Base end def relate_to_owners_enterprises - # When a new enterprise is created, we relate them to all enterprises owned by - # the same owner, in both directions. So all enterprises owned by the same owner - # will have permissions to every other one, in both directions. + # When a new producer is created, it grants permissions to all pre-existing hubs + # When a new hub is created, + # - it grants permissions to all pre-existing hubs + # - all producers grant permission to it enterprises = owner.owned_enterprises.where('enterprises.id != ?', self) - enterprises.each do |enterprise| + # We grant permissions to all pre-existing hubs + enterprises.is_hub.each do |enterprise| EnterpriseRelationship.create!(parent: self, child: enterprise, permissions_list: [:add_to_order_cycle, :manage_products, :edit_profile, :create_variant_overrides]) + end - EnterpriseRelationship.create!(parent: enterprise, - child: self, - permissions_list: [:add_to_order_cycle, - :manage_products, - :edit_profile, - :create_variant_overrides]) + # All pre-existing producers grant permission to new hubs + if is_hub + enterprises.is_primary_producer.each do |enterprise| + EnterpriseRelationship.create!(parent: enterprise, + child: self, + permissions_list: [:add_to_order_cycle, + :manage_products, + :edit_profile, + :create_variant_overrides]) + end end end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 494a543260..365bfd6ab0 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -594,46 +594,70 @@ describe Enterprise do describe "callbacks" do describe "after creation" do let(:owner) { create(:user, enterprise_limit: 10) } - let(:hub1) { create(:distributor_enterprise, owner: owner) } let(:hub2) { create(:distributor_enterprise, owner: owner) } - let(:producer) { create(:supplier_enterprise, owner: owner) } + let(:hub3) { create(:distributor_enterprise, owner: owner) } + let(:producer1) { create(:supplier_enterprise, owner: owner) } + let(:producer2) { create(:supplier_enterprise, owner: owner) } - 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 bi-directional relationships for new hubs with the owner's hubs and producers" do - hub1 - hub2 - producer - enterprise = nil - - expect do - enterprise = create(:enterprise, owner: owner) - 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 + describe "when a producer is created" do + before do + hub1 + hub2 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 + it "creates links from the new producer to all hubs owned by the same user, granting all permissions" do + producer1 + + should_have_enterprise_relationship from: producer1, to: hub1, with: :all_permissions + should_have_enterprise_relationship from: producer1, to: hub2, with: :all_permissions + end + + it "does not create any other links" do + expect do + producer1 + end.to change(EnterpriseRelationship, :count).by(2) end end - it "establishes bi-directional relationships when producers are created" do - hub1 - hub2 - expect do - producer - end.to change(EnterpriseRelationship, :count).by(4) + describe "when a new hub is created" do + it "it creates links to the hub, from all producers owned by the same user, granting all permissions" do + producer1 + producer2 + hub1 + + should_have_enterprise_relationship from: producer1, to: hub1, with: :all_permissions + should_have_enterprise_relationship from: producer2, to: hub1, with: :all_permissions + end + + + it "creates links from the new hub to all hubs owned by the same user, granting all permissions" do + hub1 + hub2 + hub3 + + should_have_enterprise_relationship from: hub2, to: hub1, with: :all_permissions + should_have_enterprise_relationship from: hub3, to: hub1, with: :all_permissions + should_have_enterprise_relationship from: hub3, to: hub2, with: :all_permissions + end + + it "does not create any other links" do + producer1 + producer2 + expect { hub1 }.to change(EnterpriseRelationship, :count).by(2) # 2 producer links + expect { hub2 }.to change(EnterpriseRelationship, :count).by(3) # 2 producer links + 1 hub link + expect { hub3 }.to change(EnterpriseRelationship, :count).by(4) # 2 producer links + 2 hub links + end + end + + + def should_have_enterprise_relationship(opts={}) + er = EnterpriseRelationship.where(parent_id: opts[:from], child_id: opts[:to]).last + er.should_not be_nil + if opts[:with] == :all_permissions + er.permissions.map(&:name).sort.should == ['add_to_order_cycle', 'manage_products', 'edit_profile', 'create_variant_overrides'].sort + end end end end