Merge remote-tracking branch 'origin/master' into group-pages

This commit is contained in:
Maikel Linke
2015-02-26 17:22:13 +11:00
4 changed files with 79 additions and 42 deletions

View File

@@ -8,6 +8,6 @@ Darkswarm.directive "ofnEmptiesCart", (CurrentHub, Cart, Navigation, storage) ->
if CurrentHub.hub?.id and CurrentHub.hub.id isnt scope.hub.id and !Cart.empty()
elm.bind 'click', (ev)->
ev.preventDefault()
if confirm "Are you sure? This will change your selected Hub and remove any items in you shopping cart."
if confirm "Are you sure? This will change your selected Hub and remove any items in your shopping cart."
storage.clearAll() # One day this will have to be moar GRANULAR
Navigation.go scope.hub.path

View File

@@ -49,6 +49,8 @@ module Admin
flash[:success] = 'Enterprises updated successfully'
redirect_to main_app.admin_enterprises_path
else
touched_ids = params[:enterprise_set][:collection_attributes].values.map { |v| v[:id].to_i }
@enterprise_set.collection.select! { |e| touched_ids.include? e.id }
flash[:error] = 'Update failed'
render :index
end

View File

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

View File

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