Compile Order And Distributors only on search

And move most logic into the report class like the others.
This commit is contained in:
Maikel Linke
2018-05-03 16:46:58 +10:00
parent 53436024e2
commit 1e80487afc
4 changed files with 39 additions and 27 deletions

View File

@@ -116,24 +116,8 @@ Spree::Admin::ReportsController.class_eval do
def orders_and_distributors
prepare_date_params params
permissions = OpenFoodNetwork::Permissions.new(spree_current_user)
@search = permissions.visible_orders.complete.not_state(:canceled).search(params[:q])
orders = @search.result
# If empty array is passed in, the where clause will return all line_items, which is bad
orders_with_hidden_details =
permissions.editable_orders.empty? ? orders : orders.where('id NOT IN (?)', permissions.editable_orders)
orders.select{ |order| orders_with_hidden_details.include? order }.each do |order|
# TODO We should really be hiding customer code here too, but until we
# have an actual association between order and customer, it's a bit tricky
order.bill_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
order.ship_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
order.assign_attributes(email: I18n.t('admin.reports.hidden'))
end
@report = OpenFoodNetwork::OrderAndDistributorReport.new orders
@report = OpenFoodNetwork::OrderAndDistributorReport.new spree_current_user, params, render_content?
@search = @report.search
csv_file_name = "orders_and_distributors_#{timestamp}.csv"
render_report(@report.header, @report.table, params[:csv], csv_file_name)
end

View File

@@ -1,8 +1,12 @@
module OpenFoodNetwork
class OrderAndDistributorReport
def initialize orders
@orders = orders
def initialize(user, params = {}, render_table = false)
@params = params
@user = user
@render_table = render_table
@permissions = OpenFoodNetwork::Permissions.new(user)
end
def header
@@ -27,10 +31,36 @@ module OpenFoodNetwork
I18n.t(:report_header_shipping_instructions)]
end
def search
@permissions.visible_orders.complete.not_state(:canceled).search(@params[:q])
end
def table
return [] unless @render_table
orders = search.result
# If empty array is passed in, the where clause will return all line_items, which is bad
orders_with_hidden_details =
@permissions.editable_orders.empty? ? orders : orders.where('id NOT IN (?)', @permissions.editable_orders)
orders.select{ |order| orders_with_hidden_details.include? order }.each do |order|
# TODO We should really be hiding customer code here too, but until we
# have an actual association between order and customer, it's a bit tricky
order.bill_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
order.ship_address.andand.assign_attributes(firstname: I18n.t('admin.reports.hidden'), lastname: "", phone: "", address1: "", address2: "", city: "", zipcode: "", state: nil)
order.assign_attributes(email: I18n.t('admin.reports.hidden'))
end
line_item_details orders
end
private
def line_item_details(orders)
order_and_distributor_details = []
@orders.each do |order|
orders.each do |order|
order.line_items.each do |line_item|
order_and_distributor_details << row_for(line_item, order)
end
@@ -39,8 +69,6 @@ module OpenFoodNetwork
order_and_distributor_details
end
private
# Returns a row with the data to display for the specified line_item and
# its order
#

View File

@@ -86,7 +86,7 @@ describe Spree::Admin::ReportsController, type: :controller do
describe 'Orders and Distributors' do
it "only shows orders that I have access to" do
[orderA1, orderA2, orderB1, orderB2]
spree_get :orders_and_distributors
spree_post :orders_and_distributors
expect(assigns(:search).result).to include(orderA1, orderB1)
expect(assigns(:search).result).not_to include(orderA2)

View File

@@ -23,7 +23,7 @@ module OpenFoodNetwork
end
it "should return a header row describing the report" do
subject = OrderAndDistributorReport.new [@order]
subject = OrderAndDistributorReport.new nil
header = subject.header
header.should == ["Order date", "Order Id",
@@ -34,9 +34,9 @@ module OpenFoodNetwork
end
it "should denormalise order and distributor details for display as csv" do
subject = OrderAndDistributorReport.new [@order]
subject = OrderAndDistributorReport.new create(:admin_user), {}, true
table = subject.table
table = subject.send(:line_item_details, [@order])
table[0].should == [@order.created_at, @order.id,
@bill_address.full_name, @order.email, @bill_address.phone, @bill_address.city,