Extract most everything to private methods - neatly groups concerns together

This commit is contained in:
Rohan Mitchell
2015-01-09 11:20:41 +11:00
parent bbc887a692
commit 7e55262ce9
2 changed files with 22 additions and 7 deletions

View File

@@ -4,14 +4,29 @@ module OpenFoodNetwork
@user = user
@distributor = distributor
end
def balance
orders = Spree::Order.where(distributor_id: @distributor, user_id: @user)
order_total = orders.sum &:total
payments = Spree::Payment.where(order_id: orders)
payment_total = payments.sum &:amount
payment_total - order_total
end
private
def order_total
orders.sum &:total
end
def payment_total
payments.sum &:amount
end
def orders
Spree::Order.where(distributor_id: @distributor, user_id: @user)
end
def payments
Spree::Payment.where(order_id: orders)
end
end
end

View File

@@ -39,6 +39,6 @@ module OpenFoodNetwork
UserBalanceCalculator.new(user2, hub1).balance.should == 10
end
end
end
end
end
end