diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 81615afd2d..97f98b288e 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -68,7 +68,7 @@ class Enterprise < ActiveRecord::Base before_validation :set_unused_address_fields after_validation :geocode_address - after_create :relate_to_owners_hubs + after_create :relate_to_owners_enterprises # TODO: Later versions of devise have a dedicated after_confirmation callback, so use that after_update :welcome_after_confirm, if: lambda { confirmation_token_changed? && confirmation_token.nil? } after_create :send_welcome_email, if: lambda { email_is_known? } @@ -365,12 +365,15 @@ class Enterprise < ActiveRecord::Base end end - def relate_to_owners_hubs - hubs = owner.owned_enterprises.is_hub.where('enterprises.id != ?', self) + def relate_to_owners_enterprises + # When a new enterprise is created, we relate them to all enterprises owned by + # the same owner. - hubs.each do |hub| + enterprises = owner.owned_enterprises.where('enterprises.id != ?', self) + + enterprises.each do |enterprise| EnterpriseRelationship.create!(parent: self, - child: hub, + child: enterprise, permissions_list: [:add_to_order_cycle, :manage_products, :edit_profile, diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 15bc7c2bbd..1b285eecca 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -601,27 +601,31 @@ 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 } - it "establishes relationships with the owner's hubs" do + it "establishes 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(2) + end.to change(EnterpriseRelationship, :count).by(3) - [er1, er2].each do |er| + [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 end - it "doesn't relate to enterprises that aren't sells=='any'" do - producer + it "establishes relationships when producers are created" do + hub1 + hub2 + expect do - enterprise = create(:enterprise, owner: owner) - end.to change(EnterpriseRelationship, :count).by(0) + producer + end.to change(EnterpriseRelationship, :count).by(2) end end end