diff --git a/app/controllers/spree/admin/variants_controller.rb b/app/controllers/spree/admin/variants_controller.rb index 6ccdd7f0f0..f1fc43483e 100644 --- a/app/controllers/spree/admin/variants_controller.rb +++ b/app/controllers/spree/admin/variants_controller.rb @@ -7,6 +7,8 @@ module Spree class VariantsController < ::Admin::ResourceController belongs_to 'spree/product' + before_action :load_data, only: [:new, :edit] + def index @url_filters = ::ProductFilters.new.extract(request.query_parameters) end @@ -107,6 +109,12 @@ module Spree :include_out_of_stock ).to_h.with_indifferent_access end + + private + + def load_data + @tax_categories = TaxCategory.order(:name) + end end end end diff --git a/app/views/spree/admin/products/_form.html.haml b/app/views/spree/admin/products/_form.html.haml index 745b542fe2..6fb51e7c10 100644 --- a/app/views/spree/admin/products/_form.html.haml +++ b/app/views/spree/admin/products/_form.html.haml @@ -43,11 +43,6 @@ = f.collection_select(:shipping_category_id, @shipping_categories, :id, :name, {}, { :class => 'select2' }) = f.error_message_on :shipping_category - = f.field_container :tax_category do - = f.label :tax_category_id, t(:tax_category) - = f.collection_select(:tax_category_id, @tax_categories, :id, :name, { :include_blank => t(:none) }, { :class => 'select2' }) - = f.error_message_on :tax_category - .clear %div diff --git a/app/views/spree/admin/variants/_form.html.haml b/app/views/spree/admin/variants/_form.html.haml index 4fcf2d2449..3f8f8d3021 100644 --- a/app/views/spree/admin/variants/_form.html.haml +++ b/app/views/spree/admin/variants/_form.html.haml @@ -64,4 +64,8 @@ - value = number_with_precision(@variant.send(field), precision: 2) = f.number_field field, value: value, class: 'fullwidth', step: 0.01 + .field + = f.label :tax_category_id, t(:tax_category) + = f.collection_select(:tax_category_id, @tax_categories, :id, :name, { include_blank: t(:none) }, { class: 'select2 fullwidth' }) + .clear diff --git a/spec/system/admin/products_spec.rb b/spec/system/admin/products_spec.rb index 1f22b1a71d..bf5b7ec794 100644 --- a/spec/system/admin/products_spec.rb +++ b/spec/system/admin/products_spec.rb @@ -296,7 +296,6 @@ describe ' visit spree.edit_admin_product_path product select 'Permitted Supplier', from: 'product_supplier_id' - select tax_category.name, from: 'product_tax_category_id' click_button 'Update' expect(flash_message).to eq('Product "a product" has been successfully updated!') product.reload diff --git a/spec/system/admin/variants_spec.rb b/spec/system/admin/variants_spec.rb index f95bc91d95..0a7e462d03 100644 --- a/spec/system/admin/variants_spec.rb +++ b/spec/system/admin/variants_spec.rb @@ -290,53 +290,56 @@ describe ' expect(variant.reload.deleted_at).not_to be_nil end - it "editing display name for a variant" do - product = create(:simple_product) - variant = product.variants.first + describe "editing variant attributes" do + let!(:variant) { create(:variant) } + let(:product) { variant.product } + let!(:tax_category) { create(:tax_category) } - # When I view the variant - login_as_admin - visit spree.admin_product_variants_path product - page.find('table.index .icon-edit').click + before do + login_as_admin + visit spree.edit_admin_product_variant_path product, variant + end - # It should allow the display name to be changed - expect(page).to have_field "variant_display_name" - expect(page).to have_field "variant_display_as" + it "editing display name for a variant" do + # It should allow the display name to be changed + expect(page).to have_field "variant_display_name" + expect(page).to have_field "variant_display_as" - # When I update the fields and save the variant - fill_in "variant_display_name", with: "Display Name" - fill_in "variant_display_as", with: "Display As This" - click_button 'Update' - expect(page).to have_content %(Variant "#{product.name}" has been successfully updated!) + # When I update the fields and save the variant + fill_in "variant_display_name", with: "Display Name" + fill_in "variant_display_as", with: "Display As This" + click_button 'Update' + expect(page).to have_content %(Variant "#{product.name}" has been successfully updated!) - # Then the displayed values should have been saved - expect(variant.reload.display_name).to eq("Display Name") - expect(variant.display_as).to eq("Display As This") - end + # Then the displayed values should have been saved + expect(variant.reload.display_name).to eq("Display Name") + expect(variant.display_as).to eq("Display As This") + end - it "editing weight for a variant" do - product = create(:simple_product) - variant = product.variants.first + it "editing weight for a variant" do + # It should allow the weight to be changed + expect(page).to have_field "unit_value_human" - # When I view the variant - login_as_admin - visit spree.admin_product_variants_path product + # When I update the fields and save the variant with invalid value + fill_in "unit_value_human", with: "1.234" + click_button 'Update' + expect(page).not_to have_content %(Variant "#{product.name}" has been successfully updated!) - page.find('table.index .icon-edit').click + fill_in "unit_value_human", with: "1.23" + click_button 'Update' + expect(page).not_to have_content %(Variant "#{product.name}" has been successfully updated!) - # It should allow the weight to be changed - expect(page).to have_field "unit_value_human" + # Then the displayed values should have been saved + expect(variant.reload.unit_value).to eq(1.23) + end - # When I update the fields and save the variant with invalid value - fill_in "unit_value_human", with: "1.234" - click_button 'Update' - expect(page).not_to have_content %(Variant "#{product.name}" has been successfully updated!) + context "editing variant tax category" do + it "editing variant tax category" do + select2_select tax_category.name, from: 'variant_tax_category_id' + click_button 'Update' - fill_in "unit_value_human", with: "1.23" - click_button 'Update' - expect(page).not_to have_content %(Variant "#{product.name}" has been successfully updated!) - - # Then the displayed values should have been saved - expect(variant.reload.unit_value).to eq(1.23) + expect(variant.reload.tax_category).to eq tax_category + end + end end end