mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
Added new fields to fix VO deletion feature specs
This commit is contained in:
committed by
Rob Harrington
parent
75127f2a63
commit
25454d3e97
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user