Added new fields to fix VO deletion feature specs

This commit is contained in:
Steve Pettitt
2015-11-28 18:26:41 +00:00
committed by Rob Harrington
parent 75127f2a63
commit 25454d3e97
3 changed files with 23 additions and 21 deletions

View File

@@ -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

View File

@@ -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'}

View File

@@ -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