Determine searching state by params, not request

We may want to use GET for searching or POST to display a certain report
type.
This commit is contained in:
Maikel Linke
2018-05-04 12:44:51 +10:00
parent 8a1a540f3d
commit 40b0a0bd5a
2 changed files with 32 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ Spree::Admin::ReportsController.class_eval do
include Spree::ReportsHelper
before_filter :cache_search_state
# Fetches user's distributors, suppliers and order_cycles
before_filter :load_data, only: [:customers, :products_and_inventory, :order_cycle_management, :packing]
@@ -220,10 +221,34 @@ Spree::Admin::ReportsController.class_eval do
private
# Some actions are changing the `params` object. That is unfortunate Spree
# behavior and we are building on it. So we have to look at `params` early
# to check if we are searching or just displaying a report search form.
def cache_search_state
search_keys = [
# search parameter for ransack
:q,
# common in all reports, only set for CSV rendering
:csv,
# `button` is included in all forms. It's not important for searching,
# but the Users & Enterprises report doesn't have any other parameter
# for an empty search. So we use this one to display data.
:button,
# Some reports use filtering by enterprise or order cycle
:distributor_id,
:supplier_id,
:order_cycle_id,
# Xero Invoices can be filtered by date
:invoice_date,
:due_date
]
@searching = search_keys.any? { |key| params.key? key }
end
# We don't want to render data unless search params are supplied.
# Compiling data can take a long time.
def render_content?
request.post?
@searching
end
def render_report(header, table, create_csv, csv_file_name)

View File

@@ -70,7 +70,7 @@ describe Spree::Admin::ReportsController, type: :controller do
describe 'Orders & Fulfillment' do
it "shows all orders in order cycles I coordinate" do
spree_post :orders_and_fulfillment
spree_post :orders_and_fulfillment, {q: {}}
expect(resulting_orders).to include orderA1, orderA2
expect(resulting_orders).not_to include orderB1, orderB2
@@ -98,7 +98,7 @@ describe Spree::Admin::ReportsController, type: :controller do
let!(:present_objects) { [orderA1, orderA2, orderB1, orderB2] }
it "only shows orders that I have access to" do
spree_post :bulk_coop
spree_post :bulk_coop, {q: {}}
expect(resulting_orders).to include(orderA1, orderB1)
expect(resulting_orders).not_to include(orderA2)
@@ -123,7 +123,7 @@ describe Spree::Admin::ReportsController, type: :controller do
let!(:present_objects) { [orderA1, orderA2, orderB1, orderB2] }
it "only shows orders that I distribute" do
spree_post :orders_and_fulfillment
spree_post :orders_and_fulfillment, {q: {}}
expect(resulting_orders).to include orderA1, orderB1
expect(resulting_orders).not_to include orderA2, orderB2
@@ -166,7 +166,7 @@ describe Spree::Admin::ReportsController, type: :controller do
end
it "only shows product line items that I am supplying" do
spree_post :bulk_coop
spree_post :bulk_coop, {q: {}}
expect(resulting_products).to include product1
expect(resulting_products).not_to include product2, product3
@@ -191,7 +191,7 @@ describe Spree::Admin::ReportsController, type: :controller do
end
it "only shows product line items that I am supplying" do
spree_post :orders_and_fulfillment
spree_post :orders_and_fulfillment, {q: {}}
expect(resulting_products).to include product1
expect(resulting_products).not_to include product2, product3
@@ -322,7 +322,7 @@ describe Spree::Admin::ReportsController, type: :controller do
end
it "shows report data" do
spree_post :users_and_enterprises
spree_post :users_and_enterprises, {q: {}}
expect(assigns(:report).table.empty?).to be false
end
end