module Spree
  module Admin
    class OptionTypesController < ResourceController
      before_action :setup_new_option_value, only: :edit

      def update_values_positions
        params[:positions].each do |id, index|
          OptionValue.where(id: id).update_all(position: index)
        end

        respond_to do |format|
          format.html { redirect_to admin_product_variants_url(params[:product_id]) }
          format.js { render text: 'Ok' }
        end
      end

      private

      def location_after_save
        if @option_type.created_at == @option_type.updated_at
          edit_admin_option_type_url(@option_type)
        else
          admin_option_types_url
        end
      end

      def load_product
        @product = Product.find_by_param!(params[:product_id])
      end

      def setup_new_option_value
        @option_type.option_values.build if @option_type.option_values.empty?
      end

      def set_available_option_types
        @available_option_types = if @product.option_type_ids.any?
          OptionType.where('id NOT IN (?)', @product.option_type_ids)
        else
          OptionType.all
        end
      end
    end
  end
end

module Spree
  module Admin
    class OptionTypesController < ResourceController
      before_action :setup_new_option_value, only: :edit

      def update_values_positions
        params[:positions].each do |id, index|
          OptionValue.where(id: id).update_all(position: index)
        end

        respond_to do |format|
          format.html { redirect_to admin_product_variants_url(params[:product_id]) }
          format.js { render text: 'Ok' }
        end
      end

      private

      def location_after_save
        edit_admin_option_type_url(@option_type)
      end

      def load_product
        @product = Product.find_by_param!(params[:product_id])
      end

      def setup_new_option_value
        @option_type.option_values.build if @option_type.option_values.empty?
      end

      def set_available_option_types
        @available_option_types = if @product.option_type_ids.any?
          OptionType.where('id NOT IN (?)', @product.option_type_ids)
        else
          OptionType.all
        end
      end
    end
  end
end

Change routing for option type edit
Change routing for option type edit

Currently there is a different behaviour when adding option values to
something that has ever had its name or presentation altered. If either
has ever been altered it will route to the option types index.  If
neither has been altered it will route to the option type itself. This
will simply just route to the option type itself regardless and
eliminate the confusing changing behaviour.
