From afb6e90a2f6c5c8268031856aef6c70d3d276f66 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Thu, 19 Nov 2020 16:35:35 +0100 Subject: [PATCH] Extract query object --- app/controllers/admin/customers_controller.rb | 8 +------- app/services/customers_with_balance.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 app/services/customers_with_balance.rb diff --git a/app/controllers/admin/customers_controller.rb b/app/controllers/admin/customers_controller.rb index 1565eedc0c0b..0b47918d1ce5 100644 --- a/app/controllers/admin/customers_controller.rb +++ b/app/controllers/admin/customers_controller.rb @@ -65,13 +65,7 @@ def destroy def collection return Customer.where("1=0") unless json_request? && params[:enterprise_id].present? - Customer.of(managed_enterprise_id). - includes(:bill_address, :ship_address, user: :credit_cards). - joins(:orders). - merge(Spree::Order.complete.not_state(:canceled)). - group("customers.id"). - select("customers.*"). - select("SUM(total - payment_total) AS balance_value") + CustomersWithBalance.new(managed_enterprise_id).query end def managed_enterprise_id diff --git a/app/services/customers_with_balance.rb b/app/services/customers_with_balance.rb new file mode 100644 index 000000000000..0abd804ff273 --- /dev/null +++ b/app/services/customers_with_balance.rb @@ -0,0 +1,19 @@ +class CustomersWithBalance + def initialize(enterprise_id) + @enterprise_id = enterprise_id + end + + def query + Customer.of(enterprise_id). + includes(:bill_address, :ship_address, user: :credit_cards). + joins(:orders). + merge(Spree::Order.complete.not_state(:canceled)). + group("customers.id"). + select("customers.*"). + select("SUM(total - payment_total) AS balance_value") + end + + private + + attr_reader :enterprise_id +end