mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Adding filtering to the products page
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user