diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index b8068681ca..64f2550466 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -197,12 +197,13 @@ angular.module('order_cycle', ['ngResource']) this.order_cycle.outgoing_exchanges.push({enterprise_id: new_distributor_id, incoming: false, active: true, variants: {}, enterprise_fees: []}) removeExchange: (exchange) -> - incoming_index = this.order_cycle.incoming_exchanges.indexOf exchange - this.order_cycle.incoming_exchanges.splice(incoming_index, 1) if incoming_index > -1 - outgoing_index = this.order_cycle.outgoing_exchanges.indexOf exchange - this.order_cycle.outgoing_exchanges.splice(outgoing_index, 1) if outgoing_index > -1 - - this.removeDistributionOfVariant(variant_id) for variant_id, active of exchange.variants when active + if exchange.incoming + incoming_index = this.order_cycle.incoming_exchanges.indexOf exchange + this.order_cycle.incoming_exchanges.splice(incoming_index, 1) + this.removeDistributionOfVariant(variant_id) for variant_id, active of exchange.variants when active + else + outgoing_index = this.order_cycle.outgoing_exchanges.indexOf exchange + this.order_cycle.outgoing_exchanges.splice(outgoing_index, 1) if outgoing_index > -1 addCoordinatorFee: -> this.order_cycle.coordinator_fees.push({}) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index dfe5a3d30f..4c9c32dbae 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -2,7 +2,7 @@ require 'open_food_network/spree_api_key_loader' Spree::Admin::ProductsController.class_eval do include OpenFoodNetwork::SpreeApiKeyLoader - before_filter :load_bpe_data, :only => :bulk_edit + before_filter :load_form_data, :only => [:bulk_edit, :new, :edit] before_filter :load_spree_api_key, :only => :bulk_edit alias_method :location_after_save_original, :location_after_save @@ -89,7 +89,7 @@ Spree::Admin::ProductsController.class_eval do private - def load_bpe_data + def load_form_data @producers = OpenFoodNetwork::Permissions.new(spree_current_user).managed_product_enterprises.is_primary_producer.by_name @taxons = Spree::Taxon.order(:name) end diff --git a/app/overrides/add_supplier_to_admin_product.rb b/app/overrides/add_supplier_to_admin_product.rb deleted file mode 100644 index a608f4e282..0000000000 --- a/app/overrides/add_supplier_to_admin_product.rb +++ /dev/null @@ -1,5 +0,0 @@ -Deface::Override.new(:virtual_path => "spree/admin/products/_form", - :insert_top => "[data-hook='admin_product_form_right']", - :partial => "spree/admin/products/supplier_form", - :name => "add_supplier_to_admin_product", - :original => '18bd94de3eb8bdf8b669932bf04fc59e2e85288b') \ No newline at end of file diff --git a/app/overrides/spree/admin/products/_form/add_supplier.html.haml.deface b/app/overrides/spree/admin/products/_form/add_supplier.html.haml.deface new file mode 100644 index 0000000000..1cc609bef4 --- /dev/null +++ b/app/overrides/spree/admin/products/_form/add_supplier.html.haml.deface @@ -0,0 +1,7 @@ +/ insert_top "[data-hook='admin_product_form_right']" + += f.field_container :supplier do + = f.label :supplier + %br + = f.collection_select(:supplier_id, @producers, :id, :name, {:include_blank => true}, {:class => "select2"}) + = f.error_message_on :supplier diff --git a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface index 334739a4c2..7c627eec4e 100644 --- a/app/overrides/spree/admin/products/new/replace_form.html.haml.deface +++ b/app/overrides/spree/admin/products/new/replace_form.html.haml.deface @@ -7,7 +7,7 @@ = f.field_container :supplier do = f.label :supplier_id, t(:supplier) %span.required * - = f.collection_select(:supplier_id, Enterprise.is_primary_producer.managed_by(spree_current_user).by_name, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"}) + = f.collection_select(:supplier_id, @producers, :id, :name, {:include_blank => true}, {:class => "select2 fullwidth"}) = f.error_message_on :supplier .six.columns.omega = f.field_container :name do diff --git a/app/views/spree/admin/products/_supplier_form.html.haml b/app/views/spree/admin/products/_supplier_form.html.haml deleted file mode 100644 index 3e5c01c2a3..0000000000 --- a/app/views/spree/admin/products/_supplier_form.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= f.field_container :supplier do - = f.label :supplier - %br - = f.collection_select(:supplier_id, Enterprise.is_primary_producer.managed_by(spree_current_user).by_name, :id, :name, {:include_blank => true}, {:class => "select2"}) - = f.error_message_on :supplier diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index b2d5e41718..d47c699c8f 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -744,8 +744,6 @@ feature %q{ permissions_list: [:manage_products]) end - use_short_wait - before do @enterprise_user = create_enterprise_user @enterprise_user.enterprise_roles.build(enterprise: supplier_managed1).save @@ -778,6 +776,28 @@ feature %q{ expect(page).to have_field 'product_name', with: product_supplied_inactive.name end + it "allows me to create a product" do + taxon = create(:taxon, name: 'Fruit') + + visit '/admin/products/bulk_edit' + + find("a", text: "NEW PRODUCT").click + expect(page).to have_content 'NEW PRODUCT' + expect(page).to have_select 'product_supplier_id', with_options: [supplier_managed1.name, supplier_managed2.name, supplier_permitted.name] + + within 'fieldset#new_product' do + fill_in 'product_name', with: 'Big Bag Of Apples' + select supplier_permitted.name, from: 'product_supplier_id' + fill_in 'product_price', with: '10.00' + select taxon.name, from: 'product_primary_taxon_id' + end + click_button 'Create' + + expect(URI.parse(current_url).path).to eq '/admin/products/bulk_edit' + expect(flash_message).to eq 'Product "Big Bag Of Apples" has been successfully created!' + expect(page).to have_field "product_name", with: 'Big Bag Of Apples' + end + it "allows me to update a product" do p = product_supplied_permitted diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 201381f852..1f61b07d2a 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -86,11 +86,14 @@ feature %q{ context "as an enterprise user" do - before(:each) do + before do @new_user = create_enterprise_user @supplier2 = create(:supplier_enterprise, name: 'Another Supplier') + @supplier_permitted = create(:supplier_enterprise, name: 'Permitted Supplier') @new_user.enterprise_roles.build(enterprise: @supplier2).save @new_user.enterprise_roles.build(enterprise: @distributors[0]).save + create(:enterprise_relationship, parent: @supplier_permitted, child: @supplier2, + permissions_list: [:manage_products]) login_to_admin_as @new_user end @@ -116,9 +119,8 @@ feature %q{ select taxon.name, from: "product_primary_taxon_id" # Should only have suppliers listed which the user can manage - within "#product_supplier_id" do - page.should_not have_content @supplier.name - end + page.should have_select 'product_supplier_id', with_options: [@supplier2.name, @supplier_permitted.name] + page.should_not have_select 'product_supplier_id', with_options: [@supplier.name] click_button 'Create' @@ -127,6 +129,18 @@ feature %q{ product.supplier.should == @supplier2 end + scenario "editing a product" do + product = create(:simple_product, name: 'a product', supplier: @supplier2) + + visit spree.edit_admin_product_path product + + select 'Permitted Supplier', from: 'product_supplier_id' + click_button 'Update' + flash_message.should == 'Product "a product" has been successfully updated!' + product.reload + product.supplier.should == @supplier_permitted + end + scenario "editing product distributions" do product = create(:simple_product, supplier: @supplier2) diff --git a/spec/javascripts/unit/admin/services/enterprise_relationships_spec.js.coffee b/spec/javascripts/unit/admin/services/enterprise_relationships_spec.js.coffee index 9701377a73..d179f6857c 100644 --- a/spec/javascripts/unit/admin/services/enterprise_relationships_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/enterprise_relationships_spec.js.coffee @@ -12,5 +12,5 @@ describe "enterprise relationships", -> EnterpriseRelationships = _EnterpriseRelationships_ it "presents permission names", -> - 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" + expect(EnterpriseRelationships.permission_presentation("add_to_order_cycle")).toEqual "to add to order cycle" + expect(EnterpriseRelationships.permission_presentation("manage_products")).toEqual "to manage products" diff --git a/spec/javascripts/unit/darkswarm/services/hubs_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/hubs_spec.js.coffee index 6b1238d1d1..f8ae8230bc 100644 --- a/spec/javascripts/unit/darkswarm/services/hubs_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/hubs_spec.js.coffee @@ -8,18 +8,21 @@ describe "Hubs service", -> active: false orders_close_at: new Date() is_distributor: true + has_shopfront: true } { id: 3 active: false orders_close_at: new Date() is_distributor: true + has_shopfront: true } { id: 1 active: true orders_close_at: new Date() is_distributor: true + has_shopfront: true } ] diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 0d5127314a..2a8d001804 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -516,30 +516,44 @@ describe 'OrderCycle services', -> ] describe 'removing exchanges', -> - it 'removes incoming exchanges', -> - exchange = {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} - OrderCycle.order_cycle.incoming_exchanges = [exchange] - OrderCycle.removeExchange(exchange) - expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [] + exchange = null - it 'removes outgoing exchanges', -> - exchange = {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} - OrderCycle.order_cycle.outgoing_exchanges = [exchange] - OrderCycle.removeExchange(exchange) - expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [] - - it 'removes distribution of all exchange variants', -> + beforeEach -> spyOn(OrderCycle, 'removeDistributionOfVariant') exchange = enterprise_id: '123' active: true + incoming: false variants: {1: true, 2: false, 3: true} enterprise_fees: [] - OrderCycle.order_cycle.incoming_exchanges = [exchange] - OrderCycle.removeExchange(exchange) - expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('1') - expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalledWith('2') - expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('3') + + describe "removing incoming exchanges", -> + beforeEach -> + exchange.incoming = true + OrderCycle.order_cycle.incoming_exchanges = [exchange] + + it 'removes the exchange', -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [] + + it 'removes distribution of all exchange variants', -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('1') + expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalledWith('2') + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('3') + + describe "removing outgoing exchanges", -> + beforeEach -> + exchange.incoming = false + OrderCycle.order_cycle.outgoing_exchanges = [exchange] + + it 'removes the exchange', -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [] + + it "does not remove distribution of any variants", -> + OrderCycle.removeExchange(exchange) + expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalled() it 'adds coordinator fees', -> OrderCycle.addCoordinatorFee()