mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Change 'add products to OC' permission into the more general 'add enterprise to OC'
This commit is contained in:
@@ -2,7 +2,7 @@ angular.module("ofn.admin").factory 'EnterpriseRelationships', ($http, enterpris
|
||||
new class EnterpriseRelationships
|
||||
create_errors: ""
|
||||
all_permissions: [
|
||||
'add_products_to_order_cycle'
|
||||
'add_to_order_cycle'
|
||||
'manage_products'
|
||||
]
|
||||
|
||||
@@ -24,5 +24,5 @@ angular.module("ofn.admin").factory 'EnterpriseRelationships', ($http, enterpris
|
||||
|
||||
permission_presentation: (permission) ->
|
||||
switch permission
|
||||
when "add_products_to_order_cycle" then "can add products to order cycle from"
|
||||
when "manage_products" then "can manage the products of"
|
||||
when "add_to_order_cycle" then "can add to order cycle"
|
||||
when "manage_products" then "can manage the products of"
|
||||
|
||||
@@ -3,6 +3,10 @@ module OrderCyclesHelper
|
||||
@current_order_cycle ||= current_order(false).andand.order_cycle
|
||||
end
|
||||
|
||||
def order_cycle_producer_enterprises
|
||||
OpenFoodNetwork::Permissions.new(spree_current_user).order_cycle_enterprises.is_primary_producer.by_name
|
||||
end
|
||||
|
||||
def coordinating_enterprises
|
||||
Enterprise.is_distributor.managed_by(spree_current_user).by_name
|
||||
end
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
%tr.products{'ng-show' => 'exchange.showProducts'}
|
||||
= render 'exchange_supplied_products_form'
|
||||
|
||||
= select_tag :new_supplier_id, options_from_collection_for_select(OpenFoodNetwork::Permissions.new(spree_current_user).order_cycle_producers, :id, :name), {'ng-model' => 'new_supplier_id'}
|
||||
= select_tag :new_supplier_id, options_from_collection_for_select(order_cycle_producer_enterprises, :id, :name), {'ng-model' => 'new_supplier_id'}
|
||||
= f.submit 'Add supplier', 'ng-click' => 'addSupplier($event)'
|
||||
|
||||
|
||||
|
||||
@@ -4,15 +4,17 @@ module OpenFoodNetwork
|
||||
@user = user
|
||||
end
|
||||
|
||||
# Find producers for which an admin is allowed to add their products to an order cycle
|
||||
def order_cycle_producers
|
||||
(managed_producers + related_producers_with(:add_products_to_order_cycle)).
|
||||
sort_by(&:name)
|
||||
# Find enterprises that an admin is allowed to add to an order cycle
|
||||
def order_cycle_enterprises
|
||||
managed_enterprise_ids = managed_enterprises.pluck :id
|
||||
permitted_enterprise_ids = related_enterprises_with(:add_to_order_cycle).pluck :id
|
||||
|
||||
Enterprise.where('id IN (?)', managed_enterprise_ids + permitted_enterprise_ids)
|
||||
end
|
||||
|
||||
# Find the exchanges of an order cycle that an admin can manage
|
||||
def order_cycle_exchanges(order_cycle)
|
||||
enterprises = managed_enterprises + related_enterprises_with(:add_products_to_order_cycle)
|
||||
enterprises = managed_enterprises + related_enterprises_with(:add_to_order_cycle)
|
||||
order_cycle.exchanges.to_enterprises(enterprises).from_enterprises(enterprises)
|
||||
end
|
||||
|
||||
@@ -23,10 +25,6 @@ module OpenFoodNetwork
|
||||
Enterprise.managed_by(@user)
|
||||
end
|
||||
|
||||
def managed_producers
|
||||
managed_enterprises.is_primary_producer.by_name
|
||||
end
|
||||
|
||||
def related_enterprises_with(permission)
|
||||
parent_ids = EnterpriseRelationship.
|
||||
permitting(managed_enterprises).
|
||||
@@ -35,9 +33,5 @@ module OpenFoodNetwork
|
||||
|
||||
Enterprise.where('id IN (?)', parent_ids)
|
||||
end
|
||||
|
||||
def related_producers_with(permission)
|
||||
related_enterprises_with(permission).is_primary_producer
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,9 +14,9 @@ feature %q{
|
||||
scenario "listing relationships" do
|
||||
# Given some enterprises with relationships
|
||||
e1, e2, e3, e4 = create(:enterprise), create(:enterprise), create(:enterprise), create(:enterprise)
|
||||
create(:enterprise_relationship, parent: e1, child: e2, permissions_list: [:add_products_to_order_cycle])
|
||||
create(:enterprise_relationship, parent: e1, child: e2, permissions_list: [:add_to_order_cycle])
|
||||
create(:enterprise_relationship, parent: e2, child: e3, permissions_list: [:manage_products])
|
||||
create(:enterprise_relationship, parent: e3, child: e4, permissions_list: [:add_products_to_order_cycle, :manage_products])
|
||||
create(:enterprise_relationship, parent: e3, child: e4, permissions_list: [:add_to_order_cycle, :manage_products])
|
||||
|
||||
# When I go to the relationships page
|
||||
click_link 'Enterprises'
|
||||
@@ -24,10 +24,10 @@ feature %q{
|
||||
|
||||
# Then I should see the relationships
|
||||
within('table#enterprise-relationships') do
|
||||
page.should have_relationship e1, e2, ['can add products to order cycle from']
|
||||
page.should have_relationship e1, e2, ['can add to order cycle']
|
||||
page.should have_relationship e2, e3, ['can manage the products of']
|
||||
page.should have_relationship e3, e4,
|
||||
['can add products to order cycle from', 'can manage the products of']
|
||||
['can add to order cycle', 'can manage the products of']
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,16 +38,16 @@ feature %q{
|
||||
|
||||
visit admin_enterprise_relationships_path
|
||||
select 'One', from: 'enterprise_relationship_parent_id'
|
||||
check 'can add products to order cycle from'
|
||||
check 'can add to order cycle'
|
||||
check 'can manage the products of'
|
||||
uncheck 'can manage the products of'
|
||||
select 'Two', from: 'enterprise_relationship_child_id'
|
||||
click_button 'Create'
|
||||
|
||||
page.should have_relationship e1, e2, ['can add products to order cycle from']
|
||||
page.should have_relationship e1, e2, ['can add to order cycle']
|
||||
er = EnterpriseRelationship.where(parent_id: e1, child_id: e2).first
|
||||
er.should be_present
|
||||
er.permissions.map(&:name).should == ['add_products_to_order_cycle']
|
||||
er.permissions.map(&:name).should == ['add_to_order_cycle']
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -444,7 +444,7 @@ feature %q{
|
||||
let!(:distributor_managed_fee) { create(:enterprise_fee, enterprise: distributor_managed, name: 'Managed distributor fee') }
|
||||
let!(:supplier_permitted_relationship) do
|
||||
create(:enterprise_relationship, parent: supplier_permitted, child: supplier_managed,
|
||||
permissions_list: [:add_products_to_order_cycle])
|
||||
permissions_list: [:add_to_order_cycle])
|
||||
end
|
||||
|
||||
before do
|
||||
|
||||
@@ -12,5 +12,5 @@ describe "enterprise relationships", ->
|
||||
EnterpriseRelationships = _EnterpriseRelationships_
|
||||
|
||||
it "presents permission names", ->
|
||||
expect(EnterpriseRelationships.permission_presentation("add_products_to_order_cycle")).toEqual "can add products to order cycle from"
|
||||
expect(EnterpriseRelationships.permission_presentation("add_to_order_cycle")).toEqual "can add to order cycle"
|
||||
expect(EnterpriseRelationships.permission_presentation("manage_products")).toEqual "can manage the products of"
|
||||
|
||||
@@ -8,16 +8,20 @@ module OpenFoodNetwork
|
||||
let(:e1) { create(:enterprise) }
|
||||
let(:e2) { create(:enterprise) }
|
||||
|
||||
describe "finding producers that can be added to an order cycle" do
|
||||
let(:producer1) { double(:enterprise1, name: 'A') }
|
||||
let(:producer2) { double(:enterprise2, name: 'B') }
|
||||
let(:producer3) { double(:enterprise3, name: 'C') }
|
||||
describe "finding enterprises that can be added to an order cycle" do
|
||||
before do
|
||||
permissions.stub(:managed_enterprises) { Enterprise.where('1=0') }
|
||||
permissions.stub(:related_enterprises_with) { Enterprise.where('1=0') }
|
||||
end
|
||||
|
||||
it "returns managed producers and related+permitted enterprises, sorted by name" do
|
||||
permissions.stub(:managed_producers) { [producer1, producer3] }
|
||||
permissions.stub(:related_producers_with) { [producer2] }
|
||||
it "returns managed enterprises" do
|
||||
permissions.stub(:managed_enterprises) { Enterprise.where(id: e1) }
|
||||
permissions.order_cycle_enterprises.should == [e1]
|
||||
end
|
||||
|
||||
permissions.order_cycle_producers.should == [producer1, producer2, producer3]
|
||||
it "returns permitted enterprises" do
|
||||
permissions.stub(:related_enterprises_with) { Enterprise.where(id: e2) }
|
||||
permissions.order_cycle_enterprises.should == [e2]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -66,12 +70,5 @@ module OpenFoodNetwork
|
||||
permissions.send(:related_enterprises_with, permission).should == []
|
||||
end
|
||||
end
|
||||
|
||||
describe "finding related producers with a particular permission" do
|
||||
it "returns permitted related enterprises that are also producers" do
|
||||
permissions.stub_chain(:related_enterprises_with, :is_primary_producer) { [e1] }
|
||||
permissions.send(:related_producers_with, permission).should == [e1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user