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

      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

        def new_before
          @object.attributes = @object.product.master.attributes.except('id', 'created_at', 'deleted_at',
                                                                        'sku', 'is_master')
          # Shallow Clone of the default price to populate the price field.
          @object.default_price = @object.product.master.default_price.clone
        end

        def collection
          @deleted = (params.key?(:deleted) && params[:deleted] == "on") ? "checked" : ""

          if @deleted.blank?
            @collection ||= super
          else
            @collection ||= Variant.where(:product_id => parent.id).deleted
          end
          @collection
        end
    end
  end
end<commit_msg>Fix rubocop issues in variant controller<commit_after>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

      def create
        on_demand = params[:variant].delete(:on_demand)
        on_hand = params[:variant].delete(:on_hand)

        super

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

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

      def new_before
        @object.attributes = @object.product.master.
          attributes.except('id', 'created_at', 'deleted_at', 'sku', 'is_master')
        # Shallow Clone of the default price to populate the price field.
        @object.default_price = @object.product.master.default_price.clone
      end

      def collection
        @deleted = params.key?(:deleted) && params[:deleted] == "on" ? "checked" : ""

        @collection ||= if @deleted.blank?
                          super
                        else
                          Variant.where(product_id: parent.id).deleted
                        end
        @collection
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09262472885032538, 'fraction_numerical': 0.0, 'mean_word_length': 2.0295663600525624, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2488851', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1255, 'n_words': 255}