<commit_before>module Spree
  module Admin
    class ShippingMethodsController < ResourceController
      before_filter :load_data, except: [:index]
      before_filter :set_shipping_category, only: [:create, :update]
      before_filter :set_zones, only: [:create, :update]
      before_filter :do_not_destroy_referenced_shipping_methods, only: :destroy
      before_filter :load_hubs, only: [:new, :edit, :create, :update]

      # Sort shipping methods by distributor name
      def collection
        collection = super
        collection = collection.managed_by(spree_current_user).by_name

        if params.key? :enterprise_id
          distributor = Enterprise.find params[:enterprise_id]
          collection = collection.for_distributor(distributor)
        end

        collection
      end

      # Spree allows soft deletes of shipping_methods but our reports are not adapted to that
      #   Here we prevent the deletion (even soft) of shipping_methods that are referenced in orders
      def do_not_destroy_referenced_shipping_methods
        order = Order.joins(shipments: :shipping_rates)
          .where( spree_shipping_rates: { shipping_method_id: @object } )
          .first
        return unless order
        flash[:error] = I18n.t(:shipping_method_destroy_error, number: order.number)
        redirect_to(collection_url) && return
      end

      def destroy
        @object.touch :deleted_at

        flash[:success] = flash_message_for(@object, :successfully_removed)

        respond_with(@object) do |format|
          format.html { redirect_to collection_url }
          format.js { render_js_for_destroy }
        end
      end

      private<commit_msg>Refactor ship methods controller destroy action: remove single action before_filter<commit_after>module Spree
  module Admin
    class ShippingMethodsController < ResourceController
      before_filter :load_data, except: [:index]
      before_filter :set_shipping_category, only: [:create, :update]
      before_filter :set_zones, only: [:create, :update]
      before_filter :load_hubs, only: [:new, :edit, :create, :update]

      # Sort shipping methods by distributor name
      def collection
        collection = super
        collection = collection.managed_by(spree_current_user).by_name

        if params.key? :enterprise_id
          distributor = Enterprise.find params[:enterprise_id]
          collection = collection.for_distributor(distributor)
        end

        collection
      end

      def destroy
        # Our reports are not adapted to soft deleted shipping_methods so here we prevent
        #   the deletion (even soft) of shipping_methods that are referenced in orders
        if order = order_referenced_by_shipping_method
          flash[:error] = I18n.t(:shipping_method_destroy_error, number: order.number)
          redirect_to(collection_url) && return
        end

        @object.touch :deleted_at
        flash[:success] = flash_message_for(@object, :successfully_removed)

        respond_with(@object) do |format|
          format.html { redirect_to collection_url }
        end
      end

      private

      def order_referenced_by_shipping_method
        Order.joins(shipments: :shipping_rates)
          .where( spree_shipping_rates: { shipping_method_id: @object } )
          .first
      end
{'fraction_non_alphanumeric': 0.06911057692307693, 'fraction_numerical': 0.001201923076923077, 'mean_word_length': 2.9583828775267538, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552599', 'n_tokens_mistral': 926, 'n_tokens_neox': 895, 'n_words': 240}