Skip to content

Commit

Permalink
Extract query object
Browse files Browse the repository at this point in the history
  • Loading branch information
sauloperez committed Nov 20, 2020
1 parent 8525c7b commit afb6e90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 1 addition & 7 deletions app/controllers/admin/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions app/services/customers_with_balance.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit afb6e90

Please sign in to comment.