From 0f00107de1c7ad401617ddee71fc9cd3d27e338c Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Mon, 4 Jan 2021 20:21:24 -0300 Subject: [PATCH 01/17] add pagination data concern --- app/controllers/concerns/pagination_data.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/controllers/concerns/pagination_data.rb diff --git a/app/controllers/concerns/pagination_data.rb b/app/controllers/concerns/pagination_data.rb new file mode 100644 index 0000000000..d03c54304a --- /dev/null +++ b/app/controllers/concerns/pagination_data.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +module PaginationData + extend ActiveSupport::Concern + + def pagination_data(objects, default_page: nil, default_per_page: nil) + { + results: objects.total_count, + pages: objects.num_pages, + page: (params[:page] || default_page).to_i, + per_page: (params[:per_page] || default_per_page).to_i + } + end + + def pagination_required? + params[:page].present? || params[:per_page].present? + end +end From 9bb7096be100192b488aea743ebac781086f1c4e Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Mon, 4 Jan 2021 20:21:39 -0300 Subject: [PATCH 02/17] apply pagination data concern to products_controller --- app/controllers/api/products_controller.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index e59b64aba1..3d61e2669b 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -7,6 +7,8 @@ module Api DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 15 + include PaginationData + skip_authorization_check only: [:show, :bulk_products, :overridable] def show @@ -140,7 +142,9 @@ module Api # This line is used by the PagedFetcher JS service (inventory). pages: products.num_pages, # This hash is used by the BulkProducts JS service. - pagination: pagination_data(products) + pagination: pagination_data(products, + default_page: DEFAULT_PAGE, + default_per_page: DEFAULT_PER_PAGE) }.to_json end @@ -148,15 +152,6 @@ module Api (params[:q] || {}).reverse_merge(s: 'created_at desc') end - def pagination_data(results) - { - results: results.total_count, - pages: results.num_pages, - page: (params[:page] || DEFAULT_PAGE).to_i, - per_page: (params[:per_page] || DEFAULT_PER_PAGE).to_i - } - end - def product_params params.require(:product).permit PermittedAttributes::Product.attributes end From 1c5f6e7222686fb885de29047660ff29674721a7 Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Mon, 4 Jan 2021 20:22:14 -0300 Subject: [PATCH 03/17] apply pagination data concern to orders controller --- app/controllers/api/orders_controller.rb | 8 ++++++-- app/services/search_orders.rb | 13 +------------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index 7339af3c61..e0dc176820 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -1,5 +1,7 @@ module Api class OrdersController < Api::BaseController + include PaginationData + def show authorize! :read, order render json: order, serializer: Api::OrderDetailedSerializer, current_order: order @@ -10,9 +12,11 @@ module Api search_results = SearchOrders.new(params, current_api_user) + orders = search_results.orders + render json: { - orders: serialized_orders(search_results.orders), - pagination: search_results.pagination_data + orders: orders, + pagination: pagination_required? ? pagination_data(orders) : nil } end diff --git a/app/services/search_orders.rb b/app/services/search_orders.rb index 30afb964a7..351dccddb2 100644 --- a/app/services/search_orders.rb +++ b/app/services/search_orders.rb @@ -8,17 +8,6 @@ class SearchOrders @orders = fetch_orders end - def pagination_data - return unless using_pagination? - - { - results: @orders.total_count, - pages: @orders.num_pages, - page: params[:page].to_i, - per_page: params[:per_page].to_i - } - end - private attr_reader :params, :current_user @@ -50,6 +39,6 @@ class SearchOrders end def using_pagination? - params[:per_page] + params[:page] end end From 3f324537a58e5ee12f30c3ceabdffb79865ad096 Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Mon, 4 Jan 2021 20:22:43 -0300 Subject: [PATCH 04/17] apply pagination data concern to exchange products controller --- .../api/exchange_products_controller.rb | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index ff9cc1ea46..49347e8e6e 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -9,6 +9,7 @@ module Api skip_authorization_check only: [:index] + include PaginationData # If exchange_id is present in the URL: # Lists Products that can be added to that Exchange # @@ -84,22 +85,14 @@ module Api ) result = { products: serializer } - result = result.merge(pagination: pagination_data(paginated_products)) if pagination_required? + if pagination_required? + result = result.merge( + pagination: pagination_data(paginated_products, + default_per_page: DEFAULT_PER_PAGE) + ) + end render text: result.to_json end - - def pagination_data(paginated_products) - { - results: paginated_products.total_count, - pages: paginated_products.num_pages, - page: params[:page].to_i, - per_page: (params[:per_page] || DEFAULT_PER_PAGE).to_i - } - end - - def pagination_required? - params[:page].present? - end end end From ab586f58b114255d4c2e9e0e85862c7ce36bc80d Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Mon, 4 Jan 2021 20:22:58 -0300 Subject: [PATCH 05/17] apply pagination data concern to bulk line items controller --- .../admin/bulk_line_items_controller.rb | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/app/controllers/admin/bulk_line_items_controller.rb b/app/controllers/admin/bulk_line_items_controller.rb index 99e3b68998..a6007bac25 100644 --- a/app/controllers/admin/bulk_line_items_controller.rb +++ b/app/controllers/admin/bulk_line_items_controller.rb @@ -1,5 +1,8 @@ module Admin class BulkLineItemsController < Spree::Admin::BaseController + include PaginationData + + DEFAULT_PAGE = 1 # GET /admin/bulk_line_items.json # def index @@ -12,9 +15,9 @@ module Admin ransack(params[:q]).result. reorder('spree_line_items.order_id ASC, spree_line_items.id ASC') - @line_items = @line_items.page(page).per(params[:per_page]) if using_pagination? + @line_items = @line_items.page(page).per(params[:per_page]) if pagination_required? - render json: { line_items: serialized_line_items, pagination: pagination_data } + render json: { line_items: serialized_line_items, pagination: (pagination_required? ? pagination_data(@line_items, default_page: DEFAULT_PAGE) : nil) } end # PUT /admin/bulk_line_items/:id.json @@ -87,23 +90,8 @@ module Admin ::Permissions::Order.new(spree_current_user) end - def using_pagination? - params[:per_page] - end - - def pagination_data - return unless using_pagination? - - { - results: @line_items.total_count, - pages: @line_items.num_pages, - page: page.to_i, - per_page: params[:per_page].to_i - } - end - def page - params[:page] || 1 + params[:page] || DEFAULT_PAGE end end end From ccac16c5b2e3b9f308a10413b6fa75ff913549de Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 00:07:38 -0300 Subject: [PATCH 06/17] fix orders controllers specs errors --- app/controllers/api/orders_controller.rb | 2 +- spec/services/search_orders_spec.rb | 16 ---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index e0dc176820..8f74933724 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -15,7 +15,7 @@ module Api orders = search_results.orders render json: { - orders: orders, + orders: serialized_orders(orders), pagination: pagination_required? ? pagination_data(orders) : nil } end diff --git a/spec/services/search_orders_spec.rb b/spec/services/search_orders_spec.rb index 207c76a054..145e09e33f 100644 --- a/spec/services/search_orders_spec.rb +++ b/spec/services/search_orders_spec.rb @@ -18,20 +18,4 @@ describe SearchOrders do expect(service.orders.count).to eq 3 end end - - describe '#pagination_data' do - let(:params) { { per_page: 15, page: 1 } } - let(:service) { SearchOrders.new(params, enterprise_user) } - - it 'returns pagination data' do - pagination_data = { - results: 3, - pages: 1, - page: 1, - per_page: 15 - } - - expect(service.pagination_data).to eq pagination_data - end - end end From efd4b97f5c33112e6c14e9ffde00c85c1254ca3d Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 00:33:29 -0300 Subject: [PATCH 07/17] fix depracation warnings from kaminary --- app/controllers/concerns/pagination_data.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/concerns/pagination_data.rb b/app/controllers/concerns/pagination_data.rb index d03c54304a..014fc4cf7a 100644 --- a/app/controllers/concerns/pagination_data.rb +++ b/app/controllers/concerns/pagination_data.rb @@ -6,7 +6,7 @@ module PaginationData def pagination_data(objects, default_page: nil, default_per_page: nil) { results: objects.total_count, - pages: objects.num_pages, + pages: objects.total_pages, page: (params[:page] || default_page).to_i, per_page: (params[:per_page] || default_per_page).to_i } From 330d42ea987ef8297756164731093b67cd820557 Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 11:48:02 -0300 Subject: [PATCH 08/17] change inclue position to start of class --- app/controllers/api/exchange_products_controller.rb | 3 ++- app/controllers/api/products_controller.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index 49347e8e6e..a4385f8330 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -5,11 +5,12 @@ # Pagination is optional and can be required by using param[:page] module Api class ExchangeProductsController < Api::BaseController + include PaginationData + DEFAULT_PER_PAGE = 100 skip_authorization_check only: [:index] - include PaginationData # If exchange_id is present in the URL: # Lists Products that can be added to that Exchange # diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 3d61e2669b..92846fc1a0 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -3,11 +3,12 @@ require 'spree/core/product_duplicator' module Api class ProductsController < Api::BaseController + include PaginationData + respond_to :json DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 15 - include PaginationData skip_authorization_check only: [:show, :bulk_products, :overridable] From 81c3c1cf327b010173872181337ea885c7d15fe7 Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 11:57:54 -0300 Subject: [PATCH 09/17] remove default page --- app/controllers/admin/bulk_line_items_controller.rb | 4 +--- app/controllers/api/products_controller.rb | 2 -- app/controllers/concerns/pagination_data.rb | 4 ++-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/bulk_line_items_controller.rb b/app/controllers/admin/bulk_line_items_controller.rb index a6007bac25..b89b08740b 100644 --- a/app/controllers/admin/bulk_line_items_controller.rb +++ b/app/controllers/admin/bulk_line_items_controller.rb @@ -1,8 +1,6 @@ module Admin class BulkLineItemsController < Spree::Admin::BaseController include PaginationData - - DEFAULT_PAGE = 1 # GET /admin/bulk_line_items.json # def index @@ -17,7 +15,7 @@ module Admin @line_items = @line_items.page(page).per(params[:per_page]) if pagination_required? - render json: { line_items: serialized_line_items, pagination: (pagination_required? ? pagination_data(@line_items, default_page: DEFAULT_PAGE) : nil) } + render json: { line_items: serialized_line_items, pagination: (pagination_required? ? pagination_data(@line_items) : nil) } end # PUT /admin/bulk_line_items/:id.json diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 92846fc1a0..617e4d90ac 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -6,7 +6,6 @@ module Api include PaginationData respond_to :json - DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 15 @@ -144,7 +143,6 @@ module Api pages: products.num_pages, # This hash is used by the BulkProducts JS service. pagination: pagination_data(products, - default_page: DEFAULT_PAGE, default_per_page: DEFAULT_PER_PAGE) }.to_json end diff --git a/app/controllers/concerns/pagination_data.rb b/app/controllers/concerns/pagination_data.rb index 014fc4cf7a..b8645c57a2 100644 --- a/app/controllers/concerns/pagination_data.rb +++ b/app/controllers/concerns/pagination_data.rb @@ -3,11 +3,11 @@ module PaginationData extend ActiveSupport::Concern - def pagination_data(objects, default_page: nil, default_per_page: nil) + def pagination_data(objects, default_per_page: nil) { results: objects.total_count, pages: objects.total_pages, - page: (params[:page] || default_page).to_i, + page: (params[:page] || 1).to_i, per_page: (params[:per_page] || default_per_page).to_i } end From 9a62ba8ed561d44690b5702d778b2cef04b9617e Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 11:59:49 -0300 Subject: [PATCH 10/17] remove hound errors --- app/controllers/api/exchange_products_controller.rb | 1 - app/controllers/api/products_controller.rb | 2 -- 2 files changed, 3 deletions(-) diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index a4385f8330..9a19a30e48 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -6,7 +6,6 @@ module Api class ExchangeProductsController < Api::BaseController include PaginationData - DEFAULT_PER_PAGE = 100 skip_authorization_check only: [:index] diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 617e4d90ac..3f466129d4 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -4,11 +4,9 @@ require 'spree/core/product_duplicator' module Api class ProductsController < Api::BaseController include PaginationData - respond_to :json DEFAULT_PER_PAGE = 15 - skip_authorization_check only: [:show, :bulk_products, :overridable] def show From 4d217c9e9d07ba46f862498afbae506181f828ee Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 12:04:59 -0300 Subject: [PATCH 11/17] refactor render paginated products --- .../api/exchange_products_controller.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index 9a19a30e48..004cf58b18 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -78,21 +78,16 @@ module Api end def render_paginated_products(paginated_products) - serializer = ActiveModel::ArraySerializer.new( + serialized_products = ActiveModel::ArraySerializer.new( paginated_products, each_serializer: Api::Admin::ForOrderCycle::SuppliedProductSerializer, order_cycle: @order_cycle ) - result = { products: serializer } - if pagination_required? - result = result.merge( - pagination: pagination_data(paginated_products, - default_per_page: DEFAULT_PER_PAGE) - ) - end - - render text: result.to_json + render json: { + products: serialized_products, + pagination: pagination_required? ? pagination_data(paginated_products, default_per_page: DEFAULT_PER_PAGE) : nil + } end end end From c99b250a5fa492314c72d782b1ac6d527dfb736e Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 12:18:47 -0300 Subject: [PATCH 12/17] refactor pagination data removing default_per_page parameter --- app/controllers/api/exchange_products_controller.rb | 2 +- app/controllers/api/products_controller.rb | 3 +-- app/controllers/concerns/pagination_data.rb | 10 +++++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index 004cf58b18..6b9150b4ac 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -86,7 +86,7 @@ module Api render json: { products: serialized_products, - pagination: pagination_required? ? pagination_data(paginated_products, default_per_page: DEFAULT_PER_PAGE) : nil + pagination: pagination_required? ? pagination_data(paginated_products) : nil } end end diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 3f466129d4..987a8da1af 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -140,8 +140,7 @@ module Api # This line is used by the PagedFetcher JS service (inventory). pages: products.num_pages, # This hash is used by the BulkProducts JS service. - pagination: pagination_data(products, - default_per_page: DEFAULT_PER_PAGE) + pagination: pagination_data(products) }.to_json end diff --git a/app/controllers/concerns/pagination_data.rb b/app/controllers/concerns/pagination_data.rb index b8645c57a2..ec133465cd 100644 --- a/app/controllers/concerns/pagination_data.rb +++ b/app/controllers/concerns/pagination_data.rb @@ -3,7 +3,7 @@ module PaginationData extend ActiveSupport::Concern - def pagination_data(objects, default_per_page: nil) + def pagination_data(objects) { results: objects.total_count, pages: objects.total_pages, @@ -15,4 +15,12 @@ module PaginationData def pagination_required? params[:page].present? || params[:per_page].present? end + + private + + def default_per_page + return unless defined? DEFAULT_PER_PAGE + + DEFAULT_PER_PAGE + end end From f53089c16c3f64703d346b16b76c35f815a982fb Mon Sep 17 00:00:00 2001 From: Paulo Vilarinho Date: Tue, 5 Jan 2021 15:11:26 -0300 Subject: [PATCH 13/17] fix products controller spec erros --- app/controllers/api/products_controller.rb | 2 +- app/controllers/concerns/pagination_data.rb | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 987a8da1af..3509d33bfd 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -63,7 +63,7 @@ module Api @products = product_query. ransack(query_params_with_defaults). result. - page(params[:page] || DEFAULT_PAGE). + page(params[:page] || 1). per(params[:per_page] || DEFAULT_PER_PAGE) render_paged_products @products diff --git a/app/controllers/concerns/pagination_data.rb b/app/controllers/concerns/pagination_data.rb index ec133465cd..9bbcf5eb2c 100644 --- a/app/controllers/concerns/pagination_data.rb +++ b/app/controllers/concerns/pagination_data.rb @@ -16,11 +16,9 @@ module PaginationData params[:page].present? || params[:per_page].present? end - private - def default_per_page - return unless defined? DEFAULT_PER_PAGE + return unless defined? self.class::DEFAULT_PER_PAGE - DEFAULT_PER_PAGE + self.class::DEFAULT_PER_PAGE end end From 47f9a3f08a7acd149d1df498cd0e8512bcef497b Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 5 Jan 2021 18:51:24 +0000 Subject: [PATCH 14/17] Add guard clause and remove conditionals Sometimes the objects are not paginated. In this case we need to avoid trying to render pagination data, as it will throw an error. This guard clause also means we can remove messy conditionals from several controllers. --- app/controllers/admin/bulk_line_items_controller.rb | 5 ++++- app/controllers/api/exchange_products_controller.rb | 2 +- app/controllers/api/orders_controller.rb | 2 +- app/controllers/concerns/pagination_data.rb | 2 ++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/bulk_line_items_controller.rb b/app/controllers/admin/bulk_line_items_controller.rb index b89b08740b..179c400f76 100644 --- a/app/controllers/admin/bulk_line_items_controller.rb +++ b/app/controllers/admin/bulk_line_items_controller.rb @@ -15,7 +15,10 @@ module Admin @line_items = @line_items.page(page).per(params[:per_page]) if pagination_required? - render json: { line_items: serialized_line_items, pagination: (pagination_required? ? pagination_data(@line_items) : nil) } + render json: { + line_items: serialized_line_items, + pagination: pagination_data(@line_items) + } end # PUT /admin/bulk_line_items/:id.json diff --git a/app/controllers/api/exchange_products_controller.rb b/app/controllers/api/exchange_products_controller.rb index 6b9150b4ac..5b2e0f154d 100644 --- a/app/controllers/api/exchange_products_controller.rb +++ b/app/controllers/api/exchange_products_controller.rb @@ -86,7 +86,7 @@ module Api render json: { products: serialized_products, - pagination: pagination_required? ? pagination_data(paginated_products) : nil + pagination: pagination_data(paginated_products) } end end diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index 8f74933724..b1cbf222f7 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -16,7 +16,7 @@ module Api render json: { orders: serialized_orders(orders), - pagination: pagination_required? ? pagination_data(orders) : nil + pagination: pagination_data(orders) } end diff --git a/app/controllers/concerns/pagination_data.rb b/app/controllers/concerns/pagination_data.rb index 9bbcf5eb2c..c0f588681c 100644 --- a/app/controllers/concerns/pagination_data.rb +++ b/app/controllers/concerns/pagination_data.rb @@ -4,6 +4,8 @@ module PaginationData extend ActiveSupport::Concern def pagination_data(objects) + return unless objects.respond_to? :total_count + { results: objects.total_count, pages: objects.total_pages, From 4bb26533065f8beab4a03411cb500db0fae128d8 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 5 Jan 2021 18:55:22 +0000 Subject: [PATCH 15/17] Tidy up pagination in Api::ProductsController#overridable and Angular PagedFetcher service Refactors away some unnecessary mess and unblocks the last remaining issue in upgrading the Kaminari gem (deprecated calls to #num_pages method) --- .../index_utils/services/paged_fetcher.js.coffee | 2 +- app/controllers/api/products_controller.rb | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/admin/index_utils/services/paged_fetcher.js.coffee b/app/assets/javascripts/admin/index_utils/services/paged_fetcher.js.coffee index 96836ef9cc..732f4a811f 100644 --- a/app/assets/javascripts/admin/index_utils/services/paged_fetcher.js.coffee +++ b/app/assets/javascripts/admin/index_utils/services/paged_fetcher.js.coffee @@ -16,7 +16,7 @@ angular.module("admin.indexUtils").factory "PagedFetcher", (dataFetcher) -> fetchPages: (url, page, pageCallback) -> dataFetcher(@urlForPage(url, page)).then (data) => @page++ - @last_page = data.pages + @last_page = data.pagination.pages pageCallback(data) if pageCallback diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index 3509d33bfd..0dd2d26d60 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -130,18 +130,15 @@ module Api end def render_paged_products(products, product_serializer = ::Api::Admin::ProductSerializer) - serializer = ActiveModel::ArraySerializer.new( + serialized_products = ActiveModel::ArraySerializer.new( products, each_serializer: product_serializer ) - render text: { - products: serializer, - # This line is used by the PagedFetcher JS service (inventory). - pages: products.num_pages, - # This hash is used by the BulkProducts JS service. + render json: { + products: serialized_products, pagination: pagination_data(products) - }.to_json + } end def query_params_with_defaults From fe0a0395d878ae9260bb7620f761775f616fa42e Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:07:46 +0000 Subject: [PATCH 16/17] Remove DEFAULT_PAGE constants --- app/controllers/admin/bulk_line_items_controller.rb | 2 +- app/services/products_renderer.rb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/bulk_line_items_controller.rb b/app/controllers/admin/bulk_line_items_controller.rb index 179c400f76..fff22ea718 100644 --- a/app/controllers/admin/bulk_line_items_controller.rb +++ b/app/controllers/admin/bulk_line_items_controller.rb @@ -92,7 +92,7 @@ module Admin end def page - params[:page] || DEFAULT_PAGE + params[:page] || 1 end end end diff --git a/app/services/products_renderer.rb b/app/services/products_renderer.rb index 72d61b50b1..83bbae5790 100644 --- a/app/services/products_renderer.rb +++ b/app/services/products_renderer.rb @@ -2,7 +2,6 @@ require 'open_food_network/scope_product_to_hub' class ProductsRenderer class NoProducts < RuntimeError; end - DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 10 def initialize(distributor, order_cycle, customer, args = {}) @@ -51,7 +50,7 @@ class ProductsRenderer query. ransack(args[:q]). result. - page(args[:page] || DEFAULT_PAGE). + page(args[:page] || 1). per(args[:per_page] || DEFAULT_PER_PAGE) end From 540aa913a3552c454ceb3974be7863fd64dc15bd Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 5 Jan 2021 21:08:02 +0000 Subject: [PATCH 17/17] Tidy up Api::OrderController#index --- app/controllers/api/orders_controller.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index b1cbf222f7..65176a0008 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -10,9 +10,7 @@ module Api def index authorize! :admin, Spree::Order - search_results = SearchOrders.new(params, current_api_user) - - orders = search_results.orders + orders = SearchOrders.new(params, current_api_user).orders render json: { orders: serialized_orders(orders),