<NME> order_cycles_controller.rb
<BEF> # frozen_string_literal: true

module Admin
  class OrderCyclesController < Admin::ResourceController
    include ::OrderCyclesHelper
    include PaperTrailLogging

    prepend_before_action :set_order_cycle_id, only: [:incoming, :outgoing, :checkout_options]
    before_action :load_data_for_index, only: :index
    before_action :require_coordinator, only: :new
    before_action :remove_protected_attrs, only: [:update]
    before_action :require_order_cycle_set_params, only: [:bulk_update]
    around_action :protect_invalid_destroy, only: :destroy

    def index
      respond_to do |format|
        format.html
        format.json do
          render_as_json @collection,
                         ams_prefix: params[:ams_prefix],
                         current_user: spree_current_user,
                         subscriptions_count: OrderManagement::Subscriptions::Count.new(@collection)
        end
      end
    end

    def show
      respond_to do |format|
        format.html do
          redirect_to edit_admin_order_cycle_path(@order_cycle)
        end
        format.json do
          render_as_json @order_cycle, current_user: spree_current_user
        end
      end
    end

    def new
      respond_to do |format|
        format.html
        format.json do
          render_as_json @order_cycle, current_user: spree_current_user
        end
      end
    def create
      @order_cycle = OrderCycle.new(params[:order_cycle])

      respond_to do |format|
        if @order_cycle.save
          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!
          invoke_callbacks(:create, :after)
          flash[:notice] = I18n.t(:order_cycles_create_notice)
          format.html { redirect_to admin_order_cycles_path }
          format.json { render :json => { success: true } }
        else
          format.html
          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }
        end
      end
    end

    def update
      @order_cycle = OrderCycle.find params[:id]

      respond_to do |format|
        if @order_cycle.update_attributes(params[:order_cycle])
          unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?
            # Only update apply exchange information if it is actually submmitted
            OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!
          end
          invoke_callbacks(:update, :after)
          flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'
          format.html { redirect_to main_app.edit_admin_order_cycle_path(@order_cycle) }
          format.json { render :json => { :success => true } }
        else
          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }
        end
      end
    end

    def bulk_update
      if order_cycle_set.andand.save
        respond_to do |format|
          format.json { render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection) }
        end
      else
        respond_to do |format|
          order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }
          format.json { render :json => { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity }
        end
      end
    end

        render_as_json @order_cycles,
                       ams_prefix: 'index',
                       current_user: spree_current_user,
                       subscriptions_count: OrderManagement::Subscriptions::Count.new(@collection)
      else
        order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }
        render json: { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity
      end
    end

    def bulk_update_nil_subscription_line_items_price_estimate
      @collection.upcoming.each do |order_cycle|
        update_nil_subscription_line_items_price_estimate(order_cycle)
      end
    end

    def update_nil_subscription_line_items_price_estimate(order_cycle)
      order_cycle.schedules.each do |schedule|
        Subscription.where(schedule_id: schedule.id).each do |subscription|
          shop = Enterprise.managed_by(spree_current_user).find_by(id: subscription.shop_id)
          subscription.subscription_line_items.nil_price_estimate.each do |line_item|
            variant = OrderManagement::Subscriptions::
                VariantsList.eligible_variants(shop).find_by(id: line_item.variant_id)
            fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, order_cycle)
            price = variant.price + fee_calculator.indexed_fees_for(variant)
            line_item.update_column(:price_estimate, price)
          end
        end
      end
    end

    def clone
      @order_cycle = OrderCycle.find params[:id]
      @order_cycle.clone!
      redirect_to main_app.admin_order_cycles_path,
                  notice: I18n.t(:order_cycles_clone_notice, name: @order_cycle.name)
    end

    # Send notifications to all producers who are part of the order cycle
    def notify_producers
      OrderCycleNotificationJob.perform_later params[:id].to_i

      redirect_to main_app.admin_order_cycles_path,
                  notice: I18n.t(:order_cycles_email_to_producers_notice)
    end

    protected

    def collection
      return Enterprise.where("1=0") unless json_request?
      return order_cycles_from_set if params[:order_cycle_set].present?

      ocs = order_cycles
      ocs.undated | ocs.soonest_closing | ocs.soonest_opening | ocs.closed
    end

    def collection_actions
      [:index, :bulk_update]
    end

    private

    def order_cycles
      if params[:as] == "distributor"
        order_cycles_as_distributor
      elsif params[:as] == "producer"
        order_cycles_as_producer
      else
        order_cycles_as_both
      end
    end

    def order_cycles_as_distributor
      OrderCycle.
        preload(:schedules).
        ransack(raw_params[:q]).
        result.
        involving_managed_distributors_of(spree_current_user).
        order('updated_at DESC')
    end

    def order_cycles_as_producer
      OrderCycle.
        preload(:schedules).
        ransack(raw_params[:q]).
        result.
        involving_managed_producers_of(spree_current_user).
        order('updated_at DESC')
    end

    def order_cycles_as_both
      OrderCycle.
        preload(:schedules).
        ransack(raw_params[:q]).
        result.
        visible_by(spree_current_user)
    end

    def load_data_for_index
      if json_request?
        # Split ransack params into all those that currently exist and new ones
        #   to limit returned ocs to recent or undated
        orders_close_at_gt = raw_params[:q]&.delete(:orders_close_at_gt) || 31.days.ago
        raw_params[:q] = {
          g: [raw_params.delete(:q) || {}, { m: 'or',
                                             orders_close_at_gt: orders_close_at_gt,
                                             orders_close_at_null: true }]
        }
        @collection = collection
      end
    end

    def redirect_to_after_update_path
      if params[:context] == "checkout_options" && params[:save]
        redirect_to main_app.admin_order_cycle_checkout_options_path(@order_cycle)
      elsif params[:context] == "checkout_options" && params[:save_and_back_to_list]
        redirect_to main_app.admin_order_cycles_path
      else
        redirect_back(fallback_location: root_path)
      end
    end

    def require_coordinator
      @order_cycle.coordinator =
        permitted_coordinating_enterprises_for(@order_cycle).find_by(id: params[:coordinator_id])
      return if params[:coordinator_id] && @order_cycle.coordinator

      available_coordinators = permitted_coordinating_enterprises_for(@order_cycle)
      case available_coordinators.count
      when 0
        flash[:error] = I18n.t(:order_cycles_no_permission_to_coordinate_error)
        redirect_to main_app.admin_order_cycles_path
      when 1
        @order_cycle.coordinator = available_coordinators.first
      else
        if params[:coordinator_id]
          flash[:error] = I18n.t(:order_cycles_no_permission_to_create_error)
        end
        render :set_coordinator
      end
    end

    def protect_invalid_destroy
      # Can't delete if OC is linked to any orders or schedules
      if @order_cycle.schedules.any?
        redirect_to main_app.admin_order_cycles_url
        flash[:error] = I18n.t('admin.order_cycles.destroy_errors.schedule_present')
      else
        begin
          yield
        rescue ActiveRecord::InvalidForeignKey
          redirect_to main_app.admin_order_cycles_url
          flash[:error] = I18n.t('admin.order_cycles.destroy_errors.orders_present')
        end
      end
    end

    def remove_protected_attrs
      return if order_cycle_params.blank?

      order_cycle_params.delete :coordinator_id

      unless Enterprise.managed_by(spree_current_user).include?(@order_cycle.coordinator)
        order_cycle_params.delete_if do |k, _v|
          [:name, :orders_open_at, :orders_close_at].include? k.to_sym
        end
      end
    end

    def authorized_order_cycles
      managed_ids = managed_enterprises.map(&:id)

      (order_cycle_bulk_params[:collection_attributes] || []).keep_if do |_index, hash|
        order_cycle = OrderCycle.find(hash[:id])
        managed_ids.include?(order_cycle&.coordinator_id)
      end
    end

    def order_cycles_from_set
      return if authorized_order_cycles.blank?

      OrderCycle.where(id: authorized_order_cycles.map{ |_k, v| v[:id] })
    end

    def order_cycle_set
      @order_cycle_set ||= Sets::OrderCycleSet.new(@order_cycles, order_cycle_bulk_params)
    end

    def require_order_cycle_set_params
      return if params[:order_cycle_set].present?

      render json: { errors: t('admin.order_cycles.bulk_update.no_data') },
             status: :unprocessable_entity
    end

    def ams_prefix_whitelist
      [:basic, :index]
    end

    def order_cycle_params
      @order_cycle_params ||= PermittedAttributes::OrderCycle.new(params).call.
        to_h.with_indifferent_access
    end

    def order_cycle_bulk_params
      params.require(:order_cycle_set).permit(
        collection_attributes: [:id] + PermittedAttributes::OrderCycle.basic_attributes
      ).to_h.with_indifferent_access
    end
  end
end

<MSG> Remove unnecessary respond_to blocks from OrderCyclesController

<DFF> @@ -45,49 +45,38 @@ module Admin
     def create
       @order_cycle = OrderCycle.new(params[:order_cycle])
 
-      respond_to do |format|
-        if @order_cycle.save
-          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!
-          invoke_callbacks(:create, :after)
-          flash[:notice] = I18n.t(:order_cycles_create_notice)
-          format.html { redirect_to admin_order_cycles_path }
-          format.json { render :json => { success: true } }
-        else
-          format.html
-          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }
-        end
+      if @order_cycle.save
+        OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!
+        invoke_callbacks(:create, :after)
+        flash[:notice] = I18n.t(:order_cycles_create_notice)
+        render json: { success: true }
+      else
+        render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity
       end
     end
 
     def update
       @order_cycle = OrderCycle.find params[:id]
 
-      respond_to do |format|
-        if @order_cycle.update_attributes(params[:order_cycle])
-          unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?
-            # Only update apply exchange information if it is actually submmitted
-            OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!
-          end
-          invoke_callbacks(:update, :after)
-          flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'
-          format.html { redirect_to main_app.edit_admin_order_cycle_path(@order_cycle) }
-          format.json { render :json => { :success => true } }
-        else
-          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }
+      if @order_cycle.update_attributes(params[:order_cycle])
+        unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?
+          # Only update apply exchange information if it is actually submmitted
+          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!
         end
+        invoke_callbacks(:update, :after)
+        flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'
+        render json: { :success => true }
+      else
+        render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity
       end
     end
 
     def bulk_update
       if order_cycle_set.andand.save
-        respond_to do |format|
-          format.json { render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection) }
-        end
+        render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection)
       else
-        respond_to do |format|
-          order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }
-          format.json { render :json => { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity }
-        end
+        order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }
+        render json: { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity
       end
     end
 

{'commit': "diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb\nindex 55b08de40..2139404dc 100644\n--- a/app/controllers/admin/order_cycles_controller.rb\n+++ b/app/controllers/admin/order_cycles_controller.rb\n@@ -45,49 +45,38 @@ module Admin\n     def create\n       @order_cycle = OrderCycle.new(params[:order_cycle])\n \n-      respond_to do |format|\n-        if @order_cycle.save\n-          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n-          invoke_callbacks(:create, :after)\n-          flash[:notice] = I18n.t(:order_cycles_create_notice)\n-          format.html { redirect_to admin_order_cycles_path }\n-          format.json { render :json => { success: true } }\n-        else\n-          format.html\n-          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }\n-        end\n+      if @order_cycle.save\n+        OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n+        invoke_callbacks(:create, :after)\n+        flash[:notice] = I18n.t(:order_cycles_create_notice)\n+        render json: { success: true }\n+      else\n+        render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity\n       end\n     end\n \n     def update\n       @order_cycle = OrderCycle.find params[:id]\n \n-      respond_to do |format|\n-        if @order_cycle.update_attributes(params[:order_cycle])\n-          unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?\n-            # Only update apply exchange information if it is actually submmitted\n-            OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n-          end\n-          invoke_callbacks(:update, :after)\n-          flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'\n-          format.html { redirect_to main_app.edit_admin_order_cycle_path(@order_cycle) }\n-          format.json { render :json => { :success => true } }\n-        else\n-          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }\n+      if @order_cycle.update_attributes(params[:order_cycle])\n+        unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?\n+          # Only update apply exchange information if it is actually submmitted\n+          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n         end\n+        invoke_callbacks(:update, :after)\n+        flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'\n+        render json: { :success => true }\n+      else\n+        render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity\n       end\n     end\n \n     def bulk_update\n       if order_cycle_set.andand.save\n-        respond_to do |format|\n-          format.json { render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection) }\n-        end\n+        render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection)\n       else\n-        respond_to do |format|\n-          order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }\n-          format.json { render :json => { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity }\n-        end\n+        order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }\n+        render json: { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity\n       end\n     end\n \n", 'commit_subject': 'Remove unnecessary respond_to blocks from OrderCyclesController', 'repo_name': 'openfoodfoundation/openfoodnetwork', 'committer': {'name': 'Rob Harrington', 'email': '4c1b7a69c57001e8088d6662955012658921d8fa@gmail.com', 'time_sec': '1529624768', 'tz_offset': '600', 'date': {'seconds': '1529624768'}}, 'commit_tree': '3a6849112663a5c49d81078754a64654fdabfd8f', 'commit_message': 'Remove unnecessary respond_to blocks from OrderCyclesController\n', 'language_name': 'HTML', 'license': 'agpl-3.0', 'hunks': "@@ -45,49 +45,38 @@ module Admin\n     def create\n       @order_cycle = OrderCycle.new(params[:order_cycle])\n \n-      respond_to do |format|\n-        if @order_cycle.save\n-          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n-          invoke_callbacks(:create, :after)\n-          flash[:notice] = I18n.t(:order_cycles_create_notice)\n-          format.html { redirect_to admin_order_cycles_path }\n-          format.json { render :json => { success: true } }\n-        else\n-          format.html\n-          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }\n-        end\n+      if @order_cycle.save\n+        OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n+        invoke_callbacks(:create, :after)\n+        flash[:notice] = I18n.t(:order_cycles_create_notice)\n+        render json: { success: true }\n+      else\n+        render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity\n       end\n     end\n \n     def update\n       @order_cycle = OrderCycle.find params[:id]\n \n-      respond_to do |format|\n-        if @order_cycle.update_attributes(params[:order_cycle])\n-          unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?\n-            # Only update apply exchange information if it is actually submmitted\n-            OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n-          end\n-          invoke_callbacks(:update, :after)\n-          flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'\n-          format.html { redirect_to main_app.edit_admin_order_cycle_path(@order_cycle) }\n-          format.json { render :json => { :success => true } }\n-        else\n-          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }\n+      if @order_cycle.update_attributes(params[:order_cycle])\n+        unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?\n+          # Only update apply exchange information if it is actually submmitted\n+          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n         end\n+        invoke_callbacks(:update, :after)\n+        flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == '1'\n+        render json: { :success => true }\n+      else\n+        render json: { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity\n       end\n     end\n \n     def bulk_update\n       if order_cycle_set.andand.save\n-        respond_to do |format|\n-          format.json { render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection) }\n-        end\n+        render_as_json @order_cycles, ams_prefix: 'index', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection)\n       else\n-        respond_to do |format|\n-          order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }\n-          format.json { render :json => { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity }\n-        end\n+        order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }\n+        render json: { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity\n       end\n     end\n \n", 'addition_count': 19, 'deletion_count': 30, 'src_file': 'a/app/controllers/admin/order_cycles_controller.rb', 'tgt_file': 'b/app/controllers/admin/order_cycles_controller.rb', 'file_extension': '.rb', 'before_file': '# frozen_string_literal: true\n\nmodule Admin\n  class OrderCyclesController < Admin::ResourceController\n    include ::OrderCyclesHelper\n    include PaperTrailLogging\n\n    prepend_before_action :set_order_cycle_id, only: [:incoming, :outgoing, :checkout_options]\n    before_action :load_data_for_index, only: :index\n    before_action :require_coordinator, only: :new\n    before_action :remove_protected_attrs, only: [:update]\n    before_action :require_order_cycle_set_params, only: [:bulk_update]\n    around_action :protect_invalid_destroy, only: :destroy\n\n    def index\n      respond_to do |format|\n        format.html\n        format.json do\n          render_as_json @collection,\n                         ams_prefix: params[:ams_prefix],\n                         current_user: spree_current_user,\n                         subscriptions_count: OrderManagement::Subscriptions::Count.new(@collection)\n        end\n      end\n    end\n\n    def show\n      respond_to do |format|\n        format.html do\n          redirect_to edit_admin_order_cycle_path(@order_cycle)\n        end\n        format.json do\n          render_as_json @order_cycle, current_user: spree_current_user\n        end\n      end\n    end\n\n    def new\n      respond_to do |format|\n        format.html\n        format.json do\n          render_as_json @order_cycle, current_user: spree_current_user\n        end\n      end\n    def create\n      @order_cycle = OrderCycle.new(params[:order_cycle])\n\n      respond_to do |format|\n        if @order_cycle.save\n          OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n          invoke_callbacks(:create, :after)\n          flash[:notice] = I18n.t(:order_cycles_create_notice)\n          format.html { redirect_to admin_order_cycles_path }\n          format.json { render :json => { success: true } }\n        else\n          format.html\n          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }\n        end\n      end\n    end\n\n    def update\n      @order_cycle = OrderCycle.find params[:id]\n\n      respond_to do |format|\n        if @order_cycle.update_attributes(params[:order_cycle])\n          unless params[:order_cycle][:incoming_exchanges].nil? && params[:order_cycle][:outgoing_exchanges].nil?\n            # Only update apply exchange information if it is actually submmitted\n            OpenFoodNetwork::OrderCycleFormApplicator.new(@order_cycle, spree_current_user).go!\n          end\n          invoke_callbacks(:update, :after)\n          flash[:notice] = I18n.t(:order_cycles_update_notice) if params[:reloading] == \'1\'\n          format.html { redirect_to main_app.edit_admin_order_cycle_path(@order_cycle) }\n          format.json { render :json => { :success => true } }\n        else\n          format.json { render :json => { errors: @order_cycle.errors.full_messages }, status: :unprocessable_entity }\n        end\n      end\n    end\n\n    def bulk_update\n      if order_cycle_set.andand.save\n        respond_to do |format|\n          format.json { render_as_json @order_cycles, ams_prefix: \'index\', current_user: spree_current_user, subscriptions_count: SubscriptionsCount.new(@collection) }\n        end\n      else\n        respond_to do |format|\n          order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }\n          format.json { render :json => { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity }\n        end\n      end\n    end\n\n        render_as_json @order_cycles,\n                       ams_prefix: \'index\',\n                       current_user: spree_current_user,\n                       subscriptions_count: OrderManagement::Subscriptions::Count.new(@collection)\n      else\n        order_cycle = order_cycle_set.collection.find{ |oc| oc.errors.present? }\n        render json: { errors: order_cycle.errors.full_messages }, status: :unprocessable_entity\n      end\n    end\n\n    def bulk_update_nil_subscription_line_items_price_estimate\n      @collection.upcoming.each do |order_cycle|\n        update_nil_subscription_line_items_price_estimate(order_cycle)\n      end\n    end\n\n    def update_nil_subscription_line_items_price_estimate(order_cycle)\n      order_cycle.schedules.each do |schedule|\n        Subscription.where(schedule_id: schedule.id).each do |subscription|\n          shop = Enterprise.managed_by(spree_current_user).find_by(id: subscription.shop_id)\n          subscription.subscription_line_items.nil_price_estimate.each do |line_item|\n            variant = OrderManagement::Subscriptions::\n                VariantsList.eligible_variants(shop).find_by(id: line_item.variant_id)\n            fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(shop, order_cycle)\n            price = variant.price + fee_calculator.indexed_fees_for(variant)\n            line_item.update_column(:price_estimate, price)\n          end\n        end\n      end\n    end\n\n    def clone\n      @order_cycle = OrderCycle.find params[:id]\n      @order_cycle.clone!\n      redirect_to main_app.admin_order_cycles_path,\n                  notice: I18n.t(:order_cycles_clone_notice, name: @order_cycle.name)\n    end\n\n    # Send notifications to all producers who are part of the order cycle\n    def notify_producers\n      OrderCycleNotificationJob.perform_later params[:id].to_i\n\n      redirect_to main_app.admin_order_cycles_path,\n                  notice: I18n.t(:order_cycles_email_to_producers_notice)\n    end\n\n    protected\n\n    def collection\n      return Enterprise.where("1=0") unless json_request?\n      return order_cycles_from_set if params[:order_cycle_set].present?\n\n      ocs = order_cycles\n      ocs.undated | ocs.soonest_closing | ocs.soonest_opening | ocs.closed\n    end\n\n    def collection_actions\n      [:index, :bulk_update]\n    end\n\n    private\n\n    def order_cycles\n      if params[:as] == "distributor"\n        order_cycles_as_distributor\n      elsif params[:as] == "producer"\n        order_cycles_as_producer\n      else\n        order_cycles_as_both\n      end\n    end\n\n    def order_cycles_as_distributor\n      OrderCycle.\n        preload(:schedules).\n        ransack(raw_params[:q]).\n        result.\n        involving_managed_distributors_of(spree_current_user).\n        order(\'updated_at DESC\')\n    end\n\n    def order_cycles_as_producer\n      OrderCycle.\n        preload(:schedules).\n        ransack(raw_params[:q]).\n        result.\n        involving_managed_producers_of(spree_current_user).\n        order(\'updated_at DESC\')\n    end\n\n    def order_cycles_as_both\n      OrderCycle.\n        preload(:schedules).\n        ransack(raw_params[:q]).\n        result.\n        visible_by(spree_current_user)\n    end\n\n    def load_data_for_index\n      if json_request?\n        # Split ransack params into all those that currently exist and new ones\n        #   to limit returned ocs to recent or undated\n        orders_close_at_gt = raw_params[:q]&.delete(:orders_close_at_gt) || 31.days.ago\n        raw_params[:q] = {\n          g: [raw_params.delete(:q) || {}, { m: \'or\',\n                                             orders_close_at_gt: orders_close_at_gt,\n                                             orders_close_at_null: true }]\n        }\n        @collection = collection\n      end\n    end\n\n    def redirect_to_after_update_path\n      if params[:context] == "checkout_options" && params[:save]\n        redirect_to main_app.admin_order_cycle_checkout_options_path(@order_cycle)\n      elsif params[:context] == "checkout_options" && params[:save_and_back_to_list]\n        redirect_to main_app.admin_order_cycles_path\n      else\n        redirect_back(fallback_location: root_path)\n      end\n    end\n\n    def require_coordinator\n      @order_cycle.coordinator =\n        permitted_coordinating_enterprises_for(@order_cycle).find_by(id: params[:coordinator_id])\n      return if params[:coordinator_id] && @order_cycle.coordinator\n\n      available_coordinators = permitted_coordinating_enterprises_for(@order_cycle)\n      case available_coordinators.count\n      when 0\n        flash[:error] = I18n.t(:order_cycles_no_permission_to_coordinate_error)\n        redirect_to main_app.admin_order_cycles_path\n      when 1\n        @order_cycle.coordinator = available_coordinators.first\n      else\n        if params[:coordinator_id]\n          flash[:error] = I18n.t(:order_cycles_no_permission_to_create_error)\n        end\n        render :set_coordinator\n      end\n    end\n\n    def protect_invalid_destroy\n      # Can\'t delete if OC is linked to any orders or schedules\n      if @order_cycle.schedules.any?\n        redirect_to main_app.admin_order_cycles_url\n        flash[:error] = I18n.t(\'admin.order_cycles.destroy_errors.schedule_present\')\n      else\n        begin\n          yield\n        rescue ActiveRecord::InvalidForeignKey\n          redirect_to main_app.admin_order_cycles_url\n          flash[:error] = I18n.t(\'admin.order_cycles.destroy_errors.orders_present\')\n        end\n      end\n    end\n\n    def remove_protected_attrs\n      return if order_cycle_params.blank?\n\n      order_cycle_params.delete :coordinator_id\n\n      unless Enterprise.managed_by(spree_current_user).include?(@order_cycle.coordinator)\n        order_cycle_params.delete_if do |k, _v|\n          [:name, :orders_open_at, :orders_close_at].include? k.to_sym\n        end\n      end\n    end\n\n    def authorized_order_cycles\n      managed_ids = managed_enterprises.map(&:id)\n\n      (order_cycle_bulk_params[:collection_attributes] || []).keep_if do |_index, hash|\n        order_cycle = OrderCycle.find(hash[:id])\n        managed_ids.include?(order_cycle&.coordinator_id)\n      end\n    end\n\n    def order_cycles_from_set\n      return if authorized_order_cycles.blank?\n\n      OrderCycle.where(id: authorized_order_cycles.map{ |_k, v| v[:id] })\n    end\n\n    def order_cycle_set\n      @order_cycle_set ||= Sets::OrderCycleSet.new(@order_cycles, order_cycle_bulk_params)\n    end\n\n    def require_order_cycle_set_params\n      return if params[:order_cycle_set].present?\n\n      render json: { errors: t(\'admin.order_cycles.bulk_update.no_data\') },\n             status: :unprocessable_entity\n    end\n\n    def ams_prefix_whitelist\n      [:basic, :index]\n    end\n\n    def order_cycle_params\n      @order_cycle_params ||= PermittedAttributes::OrderCycle.new(params).call.\n        to_h.with_indifferent_access\n    end\n\n    def order_cycle_bulk_params\n      params.require(:order_cycle_set).permit(\n        collection_attributes: [:id] + PermittedAttributes::OrderCycle.basic_attributes\n      ).to_h.with_indifferent_access\n    end\n  end\nend\n', 'message': 'Remove unnecessary respond_to blocks from OrderCyclesController\n', 'author_name': 'Rob Harrington', 'lang': 'rb'}