diff --git a/app/controllers/spree/admin/variants_controller.rb b/app/controllers/spree/admin/variants_controller.rb index ff3605a08c..a879ffa81f 100644 --- a/app/controllers/spree/admin/variants_controller.rb +++ b/app/controllers/spree/admin/variants_controller.rb @@ -1,26 +1,52 @@ +require 'open_food_network/scope_variants_for_search' + module Spree module Admin class VariantsController < ResourceController + helper 'spree/products' belongs_to 'spree/product', :find_by => :permalink new_action.before :new_before - # override the destory method to set deleted_at value - # instead of actually deleting the product. - def destroy - @variant = Variant.find(params[:id]) - if @variant.destroy - flash[:success] = Spree.t('notice_messages.variant_deleted') - else - flash[:success] = Spree.t('notice_messages.variant_not_deleted') + def create + on_demand = params[:variant].delete(:on_demand) + on_hand = params[:variant].delete(:on_hand) + + super + + if @object.present? && @object.valid? + @object.on_demand = on_demand if on_demand.present? + @object.on_hand = on_hand.to_i if on_hand.present? end + end + + def search + scoper = OpenFoodNetwork::ScopeVariantsForSearch.new(params) + @variants = scoper.search + render json: @variants, each_serializer: ::Api::Admin::VariantSerializer + end + + def destroy + @variant = Spree::Variant.find(params[:id]) + flash[:success] = if VariantDeleter.new.delete(@variant) # This line changed + Spree.t('notice_messages.variant_deleted') + else + Spree.t('notice_messages.variant_not_deleted') + end respond_with(@variant) do |format| format.html { redirect_to admin_product_variants_url(params[:product_id]) } - format.js { render_js_for_destroy } + format.js { render_js_for_destroy } end end protected + + def create_before + option_values = params[:new_variant] + option_values.andand.each_value { |id| @object.option_values << OptionValue.find(id) } + @object.save + end + def new_before @object.attributes = @object.product.master.attributes.except('id', 'created_at', 'deleted_at', 'sku', 'is_master') diff --git a/app/controllers/spree/admin/variants_controller_decorator.rb b/app/controllers/spree/admin/variants_controller_decorator.rb deleted file mode 100644 index 6577f6c6c6..0000000000 --- a/app/controllers/spree/admin/variants_controller_decorator.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'open_food_network/scope_variants_for_search' - -Spree::Admin::VariantsController.class_eval do - helper 'spree/products' - - def create - on_demand = params[:variant].delete(:on_demand) - on_hand = params[:variant].delete(:on_hand) - - super - - if @object.present? && @object.valid? - @object.on_demand = on_demand if on_demand.present? - @object.on_hand = on_hand.to_i if on_hand.present? - end - end - - def search - scoper = OpenFoodNetwork::ScopeVariantsForSearch.new(params) - @variants = scoper.search - render json: @variants, each_serializer: Api::Admin::VariantSerializer - end - - def destroy - @variant = Spree::Variant.find(params[:id]) - flash[:success] = if VariantDeleter.new.delete(@variant) # This line changed - Spree.t('notice_messages.variant_deleted') - else - Spree.t('notice_messages.variant_not_deleted') - end - - respond_with(@variant) do |format| - format.html { redirect_to admin_product_variants_url(params[:product_id]) } - format.js { render_js_for_destroy } - end - end - - protected - - def create_before - option_values = params[:new_variant] - option_values.andand.each_value { |id| @object.option_values << OptionValue.find(id) } - @object.save - end -end