Move data processing to model, add payment method preload

This commit is contained in:
Steve Pettitt
2016-03-10 21:42:42 +00:00
parent b84f49a1c3
commit fe3c0b8d29
2 changed files with 13 additions and 9 deletions

View File

@@ -52,14 +52,7 @@ module InjectionHelper
end
def inject_orders_by_distributor
# Convert ActiveRecord::Relation to array for serialization
data_array = spree_current_user.orders_by_distributor.to_a
data_array.each do |enterprise|
enterprise.distributed_orders.each do |order|
order.payments.keep_if{ |payment| payment.state == "completed" }
end
end
data_array.sort!{ |a, b| b.distributed_orders.length <=> a.distributed_orders.length }
data_array = spree_current_user.orders_by_distributor
inject_json_ams "orders_by_distributor", data_array, Api::OrdersByDistributorSerializer
end

View File

@@ -56,7 +56,18 @@ Spree.user_class.class_eval do
# Returns orders and their associated payments for all distributors that have been ordered from
def orders_by_distributor
Enterprise.includes(distributed_orders: :payments).where(enterprises: { id: self.enterprises_ordered_from }, spree_orders: { state: 'complete', user_id: self.id }).order('spree_orders.completed_at DESC')
data_array = Enterprise.includes(distributed_orders: {payments: :payment_method})
.where(enterprises: { id: self.enterprises_ordered_from },
spree_orders: { state: 'complete', user_id: self.id}
).order('spree_orders.completed_at DESC')
.to_a
# Remove uncompleted payments as these will not be reflected in order balance
data_array.each do |enterprise|
enterprise.distributed_orders.each do |order|
order.payments.keep_if{ |payment| payment.state == "completed" }
end
end
data_array.sort!{ |a, b| b.distributed_orders.length <=> a.distributed_orders.length }
end
private