mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Extract data preparation into private methods
This commit is contained in:
@@ -5,22 +5,10 @@ class ProducerMailer < Spree::BaseMailer
|
||||
@producer = producer
|
||||
@coordinator = order_cycle.coordinator
|
||||
@order_cycle = order_cycle
|
||||
@line_items = aggregated_line_items_from(@order_cycle, @producer)
|
||||
|
||||
subject = "[#{Spree::Config.site_name}] Order cycle report"
|
||||
|
||||
@line_items = Spree::LineItem.
|
||||
joins(:order => :order_cycle, :variant => :product).
|
||||
where('order_cycles.id = ?', order_cycle).
|
||||
where('spree_products.supplier_id = ?', producer).
|
||||
merge(Spree::Order.complete)
|
||||
|
||||
# Arrange the items in a hash to group quantities
|
||||
@line_items = @line_items.inject({}) do |lis, li|
|
||||
lis[li.variant] ||= {line_item: li, quantity: 0}
|
||||
lis[li.variant][:quantity] += li.quantity
|
||||
lis
|
||||
end
|
||||
|
||||
mail(to: @producer.email,
|
||||
from: from_address,
|
||||
subject: subject,
|
||||
@@ -28,4 +16,27 @@ class ProducerMailer < Spree::BaseMailer
|
||||
cc: @coordinator.email)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def aggregated_line_items_from(order_cycle, producer)
|
||||
aggregate_line_items line_items_from(order_cycle, producer)
|
||||
end
|
||||
|
||||
def line_items_from(order_cycle, producer)
|
||||
Spree::LineItem.
|
||||
joins(:order => :order_cycle, :variant => :product).
|
||||
where('order_cycles.id = ?', order_cycle).
|
||||
merge(Spree::Product.in_supplier(producer)).
|
||||
merge(Spree::Order.complete)
|
||||
end
|
||||
|
||||
def aggregate_line_items(line_items)
|
||||
# Arrange the items in a hash to group quantities
|
||||
line_items.inject({}) do |lis, li|
|
||||
lis[li.variant] ||= {line_item: li, quantity: 0}
|
||||
lis[li.variant][:quantity] += li.quantity
|
||||
lis
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user