From 66567fd9e6c07e02586ac65bca271968290a9b6d Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 6 Jun 2018 15:23:20 +1000 Subject: [PATCH] Unify report table rendering There is a lot of code duplication in the report views and we would like to change that code. So we move it into one file first. --- .../spree/admin/reports_controller_decorator.rb | 2 ++ app/views/spree/admin/reports/_table.html.haml | 15 +++++++++++++++ .../spree/admin/reports/bulk_coop.html.haml | 17 ++--------------- .../spree/admin/reports/customers.html.haml | 16 +--------------- .../reports/order_cycle_management.html.haml | 16 +--------------- .../reports/orders_and_distributors.html.haml | 16 +--------------- .../reports/orders_and_fulfillment.html.haml | 16 +--------------- app/views/spree/admin/reports/packing.html.haml | 16 +--------------- .../spree/admin/reports/payments.html.haml | 17 ++--------------- .../reports/products_and_inventory.html.haml | 16 +--------------- .../reports/users_and_enterprises.html.haml | 17 ++--------------- .../spree/admin/reports/xero_invoices.html.haml | 14 +------------- spec/features/admin/reports_spec.rb | 2 +- 13 files changed, 31 insertions(+), 149 deletions(-) create mode 100644 app/views/spree/admin/reports/_table.html.haml diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 52abed6fde..7dea34335d 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -240,6 +240,8 @@ Spree::Admin::ReportsController.class_eval do def render_report(header, table, create_csv, csv_file_name) send_data csv_report(header, table), filename: csv_file_name if create_csv + @header = header + @table = table # Rendering HTML is the default. end diff --git a/app/views/spree/admin/reports/_table.html.haml b/app/views/spree/admin/reports/_table.html.haml new file mode 100644 index 0000000000..11fed58584 --- /dev/null +++ b/app/views/spree/admin/reports/_table.html.haml @@ -0,0 +1,15 @@ +%br +%br +%table{id: id} + %thead + %tr + - @header.each do |heading| + %th=heading + %tbody + - @table.each do |row| + %tr + - row.each do |column| + %td= column + - if @table.empty? + %tr + %td{:colspan => "2"}= t(:none) diff --git a/app/views/spree/admin/reports/bulk_coop.html.haml b/app/views/spree/admin/reports/bulk_coop.html.haml index 71bd570658..a3a3b8e367 100644 --- a/app/views/spree/admin/reports/bulk_coop.html.haml +++ b/app/views/spree/admin/reports/bulk_coop.html.haml @@ -15,18 +15,5 @@ %br %br = button t(:search) -%br -%br -%table#listing_orders.index - %thead - %tr{'data-hook' => "orders_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @table.each do |row| - %tr - - row.each do |column| - %td= column - - if @table.empty? - %tr - %td{:colspan => "2"}= t(:none) + += render "table", id: "listing_orders" diff --git a/app/views/spree/admin/reports/customers.html.haml b/app/views/spree/admin/reports/customers.html.haml index 3ab25e5c6c..63d084f016 100644 --- a/app/views/spree/admin/reports/customers.html.haml +++ b/app/views/spree/admin/reports/customers.html.haml @@ -29,18 +29,4 @@ %br = button t(:search) -%br -%br -%table#listing_customers.index - %thead - %tr{'data-hook' => "orders_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @report.table.each do |row| - %tr - - row.each do |column| - %td= column - - if @report.table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_customers" diff --git a/app/views/spree/admin/reports/order_cycle_management.html.haml b/app/views/spree/admin/reports/order_cycle_management.html.haml index bf4a1ed2e8..54d9eca68a 100644 --- a/app/views/spree/admin/reports/order_cycle_management.html.haml +++ b/app/views/spree/admin/reports/order_cycle_management.html.haml @@ -29,18 +29,4 @@ .row = button t(:search) -%br -%br -%table#listing_ocm_orders.index - %thead - %tr{'data-hook' => "orders_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @table.each do |row| - %tr - - row.each do |column| - %td= column - - if @table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_ocm_orders" diff --git a/app/views/spree/admin/reports/orders_and_distributors.html.haml b/app/views/spree/admin/reports/orders_and_distributors.html.haml index aa710b1db6..2279438c25 100644 --- a/app/views/spree/admin/reports/orders_and_distributors.html.haml +++ b/app/views/spree/admin/reports/orders_and_distributors.html.haml @@ -6,18 +6,4 @@ %br = button t(:search) -%br -%br -%table#listing_orders.index - %thead - %tr{'data-hook' => 'orders_header'} - - @report.header.each do |heading| - %th=heading - %tbody - - @report.table.each do |row| - %tr - - row.each do |column| - %td= column - - if @report.table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_orders" diff --git a/app/views/spree/admin/reports/orders_and_fulfillment.html.haml b/app/views/spree/admin/reports/orders_and_fulfillment.html.haml index 5bda882108..9bf5b6bd24 100644 --- a/app/views/spree/admin/reports/orders_and_fulfillment.html.haml +++ b/app/views/spree/admin/reports/orders_and_fulfillment.html.haml @@ -25,18 +25,4 @@ .row = button t(:search) -%br -%br -%table#listing_orders.index - %thead - %tr{'data-hook' => "orders_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @table.each do |row| - %tr - - row.each do |column| - %td= column - - if @table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_orders" diff --git a/app/views/spree/admin/reports/packing.html.haml b/app/views/spree/admin/reports/packing.html.haml index fa9d22eb70..742520d715 100644 --- a/app/views/spree/admin/reports/packing.html.haml +++ b/app/views/spree/admin/reports/packing.html.haml @@ -25,18 +25,4 @@ .row = button t(:search) -%br -%br -%table#listing_orders.index - %thead - %tr{'data-hook' => "orders_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @table.each do |row| - %tr - - row.each do |column| - %td= column - - if @table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_orders" diff --git a/app/views/spree/admin/reports/payments.html.haml b/app/views/spree/admin/reports/payments.html.haml index b69708fbd1..23b046e7c0 100644 --- a/app/views/spree/admin/reports/payments.html.haml +++ b/app/views/spree/admin/reports/payments.html.haml @@ -15,18 +15,5 @@ %br %br = button t(:search) -%br -%br -%table#listing_orders.index - %thead - %tr{'data-hook' => "orders_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @table.each do |row| - %tr - - row.each do |column| - %td= column - - if @table.empty? - %tr - %td{:colspan => "2"}= t(:none) + += render "table", id: "listing_orders" diff --git a/app/views/spree/admin/reports/products_and_inventory.html.haml b/app/views/spree/admin/reports/products_and_inventory.html.haml index 40c4fbfccf..3d552b81be 100644 --- a/app/views/spree/admin/reports/products_and_inventory.html.haml +++ b/app/views/spree/admin/reports/products_and_inventory.html.haml @@ -31,18 +31,4 @@ = label_tag :csv, t(:report_customers_csv) %br = button t(:search) -%br -%br -%table#listing_products.index - %thead - %tr{'data-hook' => "products_header"} - - @report.header.each do |heading| - %th=heading - %tbody - - @report.table.each do |row| - %tr - - row.each do |column| - %td= column - - if @report.table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_products" diff --git a/app/views/spree/admin/reports/users_and_enterprises.html.haml b/app/views/spree/admin/reports/users_and_enterprises.html.haml index 1b57373c38..d7a521911f 100644 --- a/app/views/spree/admin/reports/users_and_enterprises.html.haml +++ b/app/views/spree/admin/reports/users_and_enterprises.html.haml @@ -17,18 +17,5 @@ = label_tag :csv, t(:report_customers_csv) .row = button t(:search) -%br -%br -%table#users_and_enterprises - %thead - %tr - - @report.header.each do |heading| - %th=heading - %tbody - - @report.table.each do |row| - %tr - - row.each do |column| - %td= column - - if @report.table.empty? - %tr - %td{:colspan => "2"}= t(:none) \ No newline at end of file + += render "table", id: "users_and_enterprises" diff --git a/app/views/spree/admin/reports/xero_invoices.html.haml b/app/views/spree/admin/reports/xero_invoices.html.haml index 8669089f81..0139b42a68 100644 --- a/app/views/spree/admin/reports/xero_invoices.html.haml +++ b/app/views/spree/admin/reports/xero_invoices.html.haml @@ -32,16 +32,4 @@ .four.columns.alpha= button t(:search) -%table#listing_invoices.index - %thead - %tr - - @report.header.each do |header| - %th= header - %tbody - - @report.table.each do |row| - %tr - - row.each do |column| - %td= column - - if @report.table.empty? - %tr - %td{:colspan => "2"}= t(:none) += render "table", id: "listing_invoices" diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb index 74260047f6..31be0bd499 100644 --- a/spec/features/admin/reports_spec.rb +++ b/spec/features/admin/reports_spec.rb @@ -119,7 +119,7 @@ feature %q{ #select 'Pack By Customer', from: 'report_type' click_button 'Search' - rows = find("table#listing_orders.index").all("thead tr") + rows = find("table#listing_orders").all("thead tr") table = rows.map { |r| r.all("th").map { |c| c.text.strip } } expect(table.sort).to eq([ ["Hub", "Code", "First Name", "Last Name", "Supplier", "Product", "Variant", "Quantity", "TempControlled?"]