From 25454d3e977ff36eb5c6ebf78c2de63e5ce35c2d Mon Sep 17 00:00:00 2001 From: Steve Pettitt Date: Sat, 28 Nov 2015 18:26:41 +0000 Subject: [PATCH] Added new fields to fix VO deletion feature specs --- .../admin/variant_overrides_controller.rb | 31 ++++++++++--------- .../_products_variants.html.haml | 2 +- spec/features/admin/variant_overrides_spec.rb | 11 ++++--- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/controllers/admin/variant_overrides_controller.rb b/app/controllers/admin/variant_overrides_controller.rb index 8e6029c08b..e91d8f5dd7 100644 --- a/app/controllers/admin/variant_overrides_controller.rb +++ b/app/controllers/admin/variant_overrides_controller.rb @@ -6,23 +6,22 @@ module Admin before_filter :load_spree_api_key, only: :index before_filter :load_data + before_filter :load_collection, only: [:bulk_update, :bulk_reset] def index end def bulk_update - collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }] - vo_set = VariantOverrideSet.new @variant_overrides, collection_attributes: collection_hash # Ensure we're authorised to update all variant overrides - vo_set.collection.each { |vo| authorize! :update, vo } + @vo_set.collection.each { |vo| authorize! :update, vo } - if vo_set.save + if @vo_set.save # Return saved VOs with IDs - render json: vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer + render json: @vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer else - if vo_set.errors.present? - render json: { errors: vo_set.errors }, status: 400 + if @vo_set.errors.present? + render json: { errors: @vo_set.errors }, status: 400 else render nothing: true, status: 500 end @@ -30,18 +29,15 @@ module Admin end def bulk_reset - collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }] - vo_set = VariantOverrideSet.new @variant_overrides, collection_attributes: collection_hash - # Ensure we're authorised to update all variant overrides. - vo_set.collection.each { |vo| authorize! :bulk_reset, vo } + @vo_set.collection.each { |vo| authorize! :bulk_reset, vo } # Changed this to use class method instead, to ensure the value in the database is used to reset and not a dirty passed-in value #vo_set.collection.map! { |vo| vo = vo.reset_stock! } - vo_set.collection.map! { |vo| VariantOverride.reset_stock!(vo.hub,vo.variant) } - render json: vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer - if vo_set.errors.present? - render json: { errors: vo_set.errors }, status: 400 + @vo_set.collection.map! { |vo| VariantOverride.reset_stock!(vo.hub,vo.variant) } + render json: @vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer + if @vo_set.errors.present? + render json: { errors: @vo_set.errors }, status: 400 end end @@ -61,6 +57,11 @@ module Admin @variant_overrides = VariantOverride.for_hubs(@hubs) end + def load_collection + collection_hash = Hash[params[:variant_overrides].each_with_index.map { |vo, i| [i, vo] }] + @vo_set = VariantOverrideSet.new @variant_overrides, collection_attributes: collection_hash + end + def collection end diff --git a/app/views/admin/variant_overrides/_products_variants.html.haml b/app/views/admin/variant_overrides/_products_variants.html.haml index 9e5bf1f356..9115a079a2 100644 --- a/app/views/admin/variant_overrides/_products_variants.html.haml +++ b/app/views/admin/variant_overrides/_products_variants.html.haml @@ -15,6 +15,6 @@ %input{name: 'variant-overrides-{{ variant.id }}-count-on-hand', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].count_on_hand'}, placeholder: '{{ variant.on_hand }}', 'ofn-track-variant-override' => 'count_on_hand'} %td - %input{name: 'variant-overrides-{{ variant.id }}-enable_reset', type: 'checkbox', ng: {model: 'variantOverrides[hub.id][variant.id].enable_reset'}, placeholder: '{{ variant.enable_reset }}', 'ofn-track-variant-override' => 'enable_reset'} + %input{name: 'variant-overrides-{{ variant.id }}-enable-reset', type: 'checkbox', ng: {model: 'variantOverrides[hub.id][variant.id].enable_reset'}, placeholder: '{{ variant.enable_reset }}', 'ofn-track-variant-override' => 'enable_reset'} %td %input{name: 'variant-overrides-{{ variant.id }}-default-stock', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock ? variant.default_stock : "Default stock"}}', 'ofn-track-variant-override' => 'default_stock'} diff --git a/spec/features/admin/variant_overrides_spec.rb b/spec/features/admin/variant_overrides_spec.rb index 7f1500a810..955e02054c 100644 --- a/spec/features/admin/variant_overrides_spec.rb +++ b/spec/features/admin/variant_overrides_spec.rb @@ -183,7 +183,6 @@ feature %q{ context "with overrides" do let!(:vo) { create(:variant_override, variant: variant, hub: hub, price: 77.77, count_on_hand: 11111, default_stock: 1000, enable_reset: true) } let!(:vo_no_auth) { create(:variant_override, variant: variant, hub: hub3, price: 1, count_on_hand: 2) } - let!(:vo_no_) { create(:variant_override, variant: variant, hub: hub, price: 77.77, count_on_hand: 11111, default_stock: 1000, enable_reset: false) } before do visit '/admin/variant_overrides' @@ -205,7 +204,7 @@ feature %q{ page.should have_content "Changes saved." end.to change(VariantOverride, :count).by(0) - vo = VariantOverride.last + vo.reload vo.variant_id.should == variant.id vo.hub_id.should == hub.id vo.price.should == 22.22 @@ -215,7 +214,9 @@ feature %q{ # This fails for me and can't find where this automatic deletion is implemented? it "deletes overrides when values are cleared" do fill_in "variant-overrides-#{variant.id}-price", with: '' - fill_in "variant-overrides-#{variant.id}-count_on_hand", with: '' + fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '' + fill_in "variant-overrides-#{variant.id}-default-stock", with: '' + page.uncheck "variant-overrides-#{variant.id}-enable-reset" page.should have_content "Changes to one override remain unsaved." expect do @@ -229,9 +230,9 @@ feature %q{ it "resets stock to defaults" do click_button 'Reset Stock to Defaults' page.should have_content 'Stocks reset to defaults.' - vo = VariantOverride.last - vo.count_on_hand.should == 1000 + vo.reload page.should have_input "variant-overrides-#{variant.id}-count-on-hand", with: '1000', placeholder: '12' + vo.count_on_hand.should == 1000 end it "prompts to save changes before reset if any are pending" do