From 17a0b18e50e61e4f07e1ccb98fa5f5eb12e27574 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 1 Feb 2018 16:23:40 +1100 Subject: [PATCH] Reduce cognitive complexity of StandingLineItemsController#build --- .../admin/standing_line_items_controller.rb | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/standing_line_items_controller.rb b/app/controllers/admin/standing_line_items_controller.rb index 60e09f96e8..dcc392c3f3 100644 --- a/app/controllers/admin/standing_line_items_controller.rb +++ b/app/controllers/admin/standing_line_items_controller.rb @@ -4,19 +4,16 @@ require 'open_food_network/order_cycle_permissions' module Admin class StandingLineItemsController < ResourceController before_filter :load_build_context, only: [:build] + before_filter :ensure_shop, only: [:build] + before_filter :ensure_variant, only: [:build] respond_to :json def build - return render json: { errors: ['Unauthorised'] }, status: :unauthorized unless @shop - if @variant - @standing_line_item.assign_attributes(params[:standing_line_item]) - fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle) if @order_cycle - OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant) - render json: @standing_line_item, serializer: Api::Admin::StandingLineItemSerializer, fee_calculator: fee_calculator - else - render json: { errors: ["#{@shop.name} is not permitted to sell the selected product"] }, status: :unprocessable_entity - end + @standing_line_item.assign_attributes(params[:standing_line_item]) + fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle) if @order_cycle + OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant) + render json: @standing_line_item, serializer: Api::Admin::StandingLineItemSerializer, fee_calculator: fee_calculator end private @@ -35,5 +32,16 @@ module Admin def new_actions [:new, :create, :build] # Added build end + + def ensure_shop + return if @shop + render json: { errors: ['Unauthorised'] }, status: :unauthorized + end + + def ensure_variant + return if @variant + error = "#{@shop.name} is not permitted to sell the selected product" + render json: { errors: [error] }, status: :unprocessable_entity + end end end