From 033fe885135745a20b170033ef13d749f4e21402 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 12 Sep 2014 11:07:59 +1000 Subject: [PATCH] New product form shows permitted suppliers --- .../admin/products_controller_decorator.rb | 12 ++++++---- .../new/replace_form.html.haml.deface | 2 +- .../admin/bulk_product_update_spec.rb | 24 +++++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index a5b38126e5..75c946ad0f 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -1,5 +1,6 @@ Spree::Admin::ProductsController.class_eval do - before_filter :load_bpe_data, :only => :bulk_edit + before_filter :load_form_data, only: [:bulk_edit, :new] + before_filter :load_bpe_data, only: :bulk_edit alias_method :location_after_save_original, :location_after_save @@ -85,10 +86,13 @@ Spree::Admin::ProductsController.class_eval do private - def load_bpe_data - current_user.generate_spree_api_key! unless spree_current_user.spree_api_key - @spree_api_key = spree_current_user.spree_api_key + 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 + + def load_bpe_data + current_user.generate_spree_api_key! unless spree_current_user.spree_api_key + @spree_api_key = spree_current_user.spree_api_key + end end 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/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index 9f530f860c..1fd20266e0 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -745,8 +745,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 @@ -779,6 +777,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