From d81037d6581f4ff88ad42b0d6571865388fec4f1 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 27 Sep 2022 11:41:28 +1000 Subject: [PATCH] Simplify branching logic in SearchesController --- .../spree/admin/search_controller.rb | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/app/controllers/spree/admin/search_controller.rb b/app/controllers/spree/admin/search_controller.rb index 3d4e95f0b5..7032a17e22 100644 --- a/app/controllers/spree/admin/search_controller.rb +++ b/app/controllers/spree/admin/search_controller.rb @@ -18,19 +18,28 @@ module Spree end def customers - @customers = [] - if enterprises.where(id: search_params[:distributor_id].to_i).present? - @customers = Customer. - ransack(m: 'or', email_start: search_params[:q], first_name_start: search_params[:q], - last_name_start: search_params[:q]). - result. - where(enterprise_id: search_params[:distributor_id].to_i) - end - render json: @customers, each_serializer: ::Api::Admin::CustomerSerializer + render json: load_customers, each_serializer: ::Api::Admin::CustomerSerializer end private + def load_customers + return [] unless valid_enterprise_given? + + Customer.ransack( + m: 'or', email_start: search_params[:q], + first_name_start: search_params[:q], last_name_start: search_params[:q] + ).result.where(enterprise_id: search_params[:distributor_id].to_i) + end + + def valid_enterprise_given? + return true if spree_current_user.admin? + + spree_current_user.enterprises.where( + id: search_params[:distributor_id].to_i + ).present? + end + def ransack_hash { m: 'or', @@ -45,14 +54,6 @@ module Spree def search_params params.permit(:q, :distributor_id).to_h.with_indifferent_access end - - def enterprises - if spree_current_user.admin? - Enterprise.all - else - spree_current_user.enterprises - end - end end end end