When enterprise created, grant permission to all co-owned enterprises, not just hubs

This commit is contained in:
Rohan Mitchell
2015-02-11 15:41:06 +11:00
parent 12dc0b93aa
commit 1d61e91afd
2 changed files with 19 additions and 12 deletions

View File

@@ -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,

View File

@@ -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