From 4b919332a0417328bfe8e6014896fcc4a558f115 Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Thu, 5 Aug 2021 12:32:20 -0400 Subject: [PATCH] fixed pagination for circ history to have default direction be DESC --- lib/pagination/pagination_decorator.rb | 12 +++++++++++- models/items/circ_history/circ_history_item.rb | 4 ++-- spec/models/items/circ_history/past_loans_spec.rb | 6 +++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/pagination/pagination_decorator.rb b/lib/pagination/pagination_decorator.rb index 09641483..f02e9113 100644 --- a/lib/pagination/pagination_decorator.rb +++ b/lib/pagination/pagination_decorator.rb @@ -44,6 +44,9 @@ def page_number private_constant :Page private + def default_direction + 'ASC' + end def page(page) url = path(page.offset) Page.new(page: page, url: url) @@ -52,7 +55,7 @@ def path(offset) query = [] query.push("offset=#{offset}") if offset > 0 query.push("limit=#{@limit}") if @limit != 15 - query.push("direction=#{@direction}") if @direction != 'ASC' + query.push("direction=#{@direction}") if @direction != default_direction query.push("order_by=#{@order_by}") if @order_by != 'due_date' query_string = query.join('&') query_string = nil if query_string == '' @@ -62,3 +65,10 @@ def path(offset) end + +class CirculationHistoryPaginationDecorator < PaginationDecorator + private + def default_direction + 'DESC' + end +end diff --git a/models/items/circ_history/circ_history_item.rb b/models/items/circ_history/circ_history_item.rb index d709f1cc..411431a9 100644 --- a/models/items/circ_history/circ_history_item.rb +++ b/models/items/circ_history/circ_history_item.rb @@ -28,8 +28,8 @@ def self.for(uniqname:, offset: nil, limit: nil, pagination_params[:limit] = limit unless limit.nil? pagination_params[:current_offset] = offset unless offset.nil? pagination_params[:order_by] = order_by unless order_by.nil? - pagination_params[:direction] = direction unless direction.nil? - CirculationHistoryItems.new(parsed_response: pr, pagination: PaginationDecorator.new(**pagination_params) ) + direction.nil? ? pagination_params[:direction] = 'DESC' : pagination_params[:direction] = direction + CirculationHistoryItems.new(parsed_response: pr, pagination: CirculationHistoryPaginationDecorator.new(**pagination_params) ) else end end diff --git a/spec/models/items/circ_history/past_loans_spec.rb b/spec/models/items/circ_history/past_loans_spec.rb index d6f95f05..9488b44d 100644 --- a/spec/models/items/circ_history/past_loans_spec.rb +++ b/spec/models/items/circ_history/past_loans_spec.rb @@ -57,9 +57,9 @@ @loan = loans["loans"].delete_at(0).to_json end it "requests loans sorted by title" do - stub_circ_history_get_request( url: 'users/emcard/loans', output: @loan, query: { "offset" => 1, "limit" => 1, "direction" => "DESC", "order_by" => "title"} ) - loans = described_class.for(uniqname: 'emcard', offset: 1, limit: 1, direction: "DESC", order_by: "title") - expect(loans.pagination.next.url).to include("direction=DESC") + stub_circ_history_get_request( url: 'users/emcard/loans', output: @loan, query: { "offset" => 1, "limit" => 1, "direction" => "ASC", "order_by" => "title"} ) + loans = described_class.for(uniqname: 'emcard', offset: 1, limit: 1, direction: "ASC", order_by: "title") + expect(loans.pagination.next.url).to include("direction=ASC") expect(loans.pagination.next.url).to include("order_by=title") end end