Creating and then updating the new override updates the same override instead of creating a duplicate

This commit is contained in:
Rohan Mitchell
2014-12-12 12:06:55 +11:00
parent 31823f2dbd
commit 84b607433c
6 changed files with 56 additions and 3 deletions

View File

@@ -44,8 +44,9 @@ angular.module("ofn.admin").controller "AdminVariantOverridesCtrl", ($scope, $ti
else
StatusMessage.display 'progress', 'Saving...'
DirtyVariantOverrides.save()
.success (data) ->
.success (updatedVos) ->
DirtyVariantOverrides.clear()
VariantOverrides.updateIds updatedVos
$timeout -> StatusMessage.display 'success', 'Changes saved.'
.error (data, status) ->
$timeout -> StatusMessage.display 'failure', $scope.updateError(data, status)

View File

@@ -17,3 +17,7 @@ angular.module("ofn.admin").factory "VariantOverrides", (variantOverrides, Index
hub_id: hub.id
price: ''
count_on_hand: ''
updateIds: (updatedVos) ->
for vo in updatedVos
@variantOverrides[vo.hub_id][vo.variant_id].id = vo.id

View File

@@ -23,7 +23,8 @@ module Admin
vo_set.collection.each { |vo| authorize! :update, vo }
if vo_set.save
render json: {}
# Return saved VOs with IDs
render json: vo_set.collection, each_serializer: Api::Admin::VariantOverrideSerializer
else
if vo_set.errors.present?
render json: { errors: vo_set.errors }, status: 400

View File

@@ -1,3 +1,3 @@
class Api::Admin::VariantOverrideSerializer < ActiveModel::Serializer
attributes :id, :variant_id, :hub_id, :price, :count_on_hand
attributes :id, :hub_id, :variant_id, :price, :count_on_hand
end

View File

@@ -83,6 +83,38 @@ feature %q{
vo.count_on_hand.should == 123
end
describe "creating and then updating the new override" do
it "updates the same override instead of creating a duplicate" do
# When I create a new override
fill_in "variant-overrides-#{variant.id}-price", with: '777.77'
fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '123'
page.should have_content "Changes to one override remain unsaved."
expect do
click_button 'Save Changes'
page.should have_content "Changes saved."
end.to change(VariantOverride, :count).by(1)
# And I update its settings without reloading the page
fill_in "variant-overrides-#{variant.id}-price", with: '111.11'
fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '111'
page.should have_content "Changes to one override remain unsaved."
# Then I shouldn't see a new override
expect do
click_button 'Save Changes'
page.should have_content "Changes saved."
end.to change(VariantOverride, :count).by(0)
# And the override should be updated
vo = VariantOverride.last
vo.variant_id.should == variant.id
vo.hub_id.should == hub.id
vo.price.should == 111.11
vo.count_on_hand.should == 111
end
end
it "displays an error when unauthorised to access the page" do
fill_in "variant-overrides-#{variant.id}-price", with: '777.77'
fill_in "variant-overrides-#{variant.id}-count-on-hand", with: '123'

View File

@@ -51,3 +51,18 @@ describe "VariantOverrides service", ->
300: { hub_id: 30, variant_id: 300, price: '', count_on_hand: ''}
400: { hub_id: 30, variant_id: 400, price: '', count_on_hand: ''}
500: { hub_id: 30, variant_id: 500, price: '', count_on_hand: ''}
it "updates the IDs of variant overrides", ->
VariantOverrides.variantOverrides[2] = {}
VariantOverrides.variantOverrides[2][3] = {hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5}
VariantOverrides.variantOverrides[2][8] = {hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10}
updatedVos = [
{id: 1, hub_id: 2, variant_id: 3, price: "4.0", count_on_hand: 5}
{id: 6, hub_id: 2, variant_id: 8, price: "9.0", count_on_hand: 10}
]
VariantOverrides.updateIds updatedVos
expect(VariantOverrides.variantOverrides[2][3].id).toEqual 1
expect(VariantOverrides.variantOverrides[2][8].id).toEqual 6