From 7bc754a5982024df2c95c5896849af971555eab2 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Thu, 28 Nov 2013 13:34:03 +1100 Subject: [PATCH] Adding filtering to the products page --- .../admin/reports_controller_decorator.rb | 3 +- .../spree/admin/reports/customers.html.haml | 28 +++++++++++- lib/open_food_network/customers_report.rb | 32 +++++++++++++- spec/features/admin/reports_spec.rb | 18 +++++++- .../customers_report_spec.rb | 43 +++++++++++++++++++ 5 files changed, 119 insertions(+), 5 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 8234c40216..bae1f36374 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -45,9 +45,10 @@ Spree::Admin::ReportsController.class_eval do ] } + # This action is short because we refactored it like bosses def customers - @search = Spree::Order.search @report_types = REPORT_TYPES[:customers] + @report_type = params[:report_type] @report = OpenFoodNetwork::CustomersReport.new spree_current_user, params end diff --git a/app/views/spree/admin/reports/customers.html.haml b/app/views/spree/admin/reports/customers.html.haml index 9a5f114754..726d068010 100644 --- a/app/views/spree/admin/reports/customers.html.haml +++ b/app/views/spree/admin/reports/customers.html.haml @@ -1,5 +1,31 @@ -= form_for @search, :url => spree.customers_admin_reports_path do |s| += form_tag spree.customers_admin_reports_url do |f| + %br + = label_tag nil, "Distributor: " + = select_tag(:distributor_id, + options_from_collection_for_select(@distributors, :id, :name, params[:distributor_id]), + :include_blank => true) + %br + = label_tag nil, "Supplier: " + = select_tag(:supplier_id, + options_from_collection_for_select(@suppliers, :id, :name, params[:supplier_id]), + :include_blank => true) + + %br + = label_tag nil, "Order Cycle: " + - order_cycles_select = @order_cycles.collect {|oc| [ "#{oc.name}   (#{oc.orders_open_at.to_s(:short)} - #{oc.orders_close_at.to_s(:short)})".html_safe, oc.id ] } + = select_tag(:order_cycle_id, + options_for_select(order_cycles_select, params[:order_cycle_id]), + include_blank: true) + + %br + = label_tag nil, "Report Type: " + = select_tag(:report_type, options_for_select(@report_types, @report_type)) + + %br + %br + = check_box_tag :csv + = label_tag :csv, "Download as csv" %br = button t(:search) diff --git a/lib/open_food_network/customers_report.rb b/lib/open_food_network/customers_report.rb index 315b7b5ad5..7b70abf9c5 100644 --- a/lib/open_food_network/customers_report.rb +++ b/lib/open_food_network/customers_report.rb @@ -38,7 +38,37 @@ module OpenFoodNetwork end def orders - Spree::Order.managed_by(@user).complete.not_state(:canceled) + filter Spree::Order.managed_by(@user).complete.not_state(:canceled) + end + + def filter(orders) + filter_to_supplier filter_to_distributor filter_to_order_cycle orders + end + + def filter_to_supplier(orders) + if params[:supplier_id].to_i > 0 + orders.select do |order| + order.line_items.includes(:product).where("spree_products.supplier_id = ?", params[:supplier_id].to_i).count > 0 + end + else + orders + end + end + + def filter_to_distributor(orders) + if params[:distributor_id].to_i > 0 + orders.where(distributor_id: params[:distributor_id]) + else + orders + end + end + + def filter_to_order_cycle(orders) + if params[:order_cycle_id].to_i > 0 + orders.where(order_cycle_id: params[:order_cycle_id]) + else + orders + end end private diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index ce0248e381..1b3a2055e9 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -36,10 +36,24 @@ feature %q{ end scenario "customers report" do click_link "Mailing List" + expect(page).to have_select('report_type', selected: 'Mailing List') + + rows = find("table#listing_customers").all("thead tr") + table = rows.map { |r| r.all("th").map { |c| c.text.strip } } + table.sort.should == [ + ["Email", "First Name", "Last Name", "Suburb"] + ].sort end scenario "customers report" do click_link "Addresses" + expect(page).to have_select('report_type', selected: 'Addresses') + + rows = find("table#listing_customers").all("thead tr") + table = rows.map { |r| r.all("th").map { |c| c.text.strip } } + table.sort.should == [ + ["First Name", "Last Name", "Billing Address", "Email", "Phone", "Hub", "Hub Address", "Shipping Method"] + ].sort end end @@ -128,12 +142,12 @@ feature %q{ rows = find("table#listing_products").all("tr") table = rows.map { |r| r.all("th,td").map { |c| c.text.strip } } - table.should == [ + table.sort.should == [ ["Supplier", "Product", "SKU", "Variant", "On Hand", "Price"], [product_1.supplier.name, "Product Name", variant_1.sku, "Size: Test", "10", "100.0"], [product_1.supplier.name, "Product Name", variant_2.sku, "Size: S", "20", "80.0"], [product_2.supplier.name, "Product 2", product_2.master.sku, "", "9", "99.0"] - ] + ].sort end end diff --git a/spec/lib/open_food_network/customers_report_spec.rb b/spec/lib/open_food_network/customers_report_spec.rb index 161835c806..6658c57334 100644 --- a/spec/lib/open_food_network/customers_report_spec.rb +++ b/spec/lib/open_food_network/customers_report_spec.rb @@ -88,9 +88,52 @@ module OpenFoodNetwork o1 = create(:order, distributor: d1, completed_at: 1.day.ago) o2 = create(:order, distributor: d2, completed_at: 1.day.ago) + subject.should_receive(:filter).with([o1]).and_return([o1]) subject.orders.should == [o1] end end + + describe "Filtering orders" do + let(:orders) { Spree::Order.scoped } + let(:supplier) { create(:supplier_enterprise) } + it "should return all orders sans-params" do + subject.filter(orders).should == orders + end + + it "should return orders with a specific supplier" do + supplier = create(:supplier_enterprise) + supplier2 = create(:supplier_enterprise) + product1 = create(:simple_product, supplier: supplier) + product2 = create(:simple_product, supplier: supplier2) + order1 = create(:order) + order2 = create(:order) + order1.line_items << create(:line_item, product: product1) + order2.line_items << create(:line_item, product: product2) + + subject.stub(:params).and_return(supplier_id: supplier.id) + subject.filter(orders).sort.should == [order1] + end + + it "filters to a specific distributor" do + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + order1 = create(:order, distributor: d1) + order2 = create(:order, distributor: d2) + + subject.stub(:params).and_return(distributor_id: d1.id) + subject.filter(orders).sort.should == [order1] + end + + it "filters to a specific cycle" do + oc1 = create(:simple_order_cycle) + oc2 = create(:simple_order_cycle) + order1 = create(:order, order_cycle: oc1) + order2 = create(:order, order_cycle: oc2) + + subject.stub(:params).and_return(order_cycle_id: oc1.id) + subject.filter(orders).sort.should == [order1] + end + end end end end