Skip to content

Commit

Permalink
Extract query object out of UsersController
Browse files Browse the repository at this point in the history
  • Loading branch information
sauloperez committed Jan 14, 2021
1 parent 26f9065 commit cc908f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 1 addition & 6 deletions app/controllers/spree/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ def update

def orders_collection
if OpenFoodNetwork::FeatureToggle.enabled?(:customer_balance, spree_current_user)
orders = @user.orders
.where.not(Spree::Order.in_incomplete_state.where_values_hash)
.select('spree_orders.*')
.order('completed_at desc')

OutstandingBalance.new(orders).query
CompleteOrdersWithBalance.new(@user).query
else
@user.orders.where(state: 'complete').order('completed_at desc')
end
Expand Down
16 changes: 16 additions & 0 deletions app/queries/complete_orders_with_balance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class CompleteOrdersWithBalance
def initialize(user)
@user = user
end

def query
orders = @user.orders
.where.not(Spree::Order.in_incomplete_state.where_values_hash)
.select('spree_orders.*')
.order(completed_at: :desc)

OutstandingBalance.new(orders).query
end
end

0 comments on commit cc908f7

Please sign in to comment.