From 3b46847e05f5daeadd60326e69559df303bf5da8 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 8 Dec 2022 13:49:48 +1100 Subject: [PATCH] fix(versions): add missing next and previous relations to paginated response --- lib/pact_broker/pacticipants/repository.rb | 3 +-- lib/pact_broker/repositories/helpers.rb | 11 ++++++++++ lib/pact_broker/repositories/page.rb | 24 ++++++++++++++++++++++ spec/features/get_versions_spec.rb | 7 +++++-- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 lib/pact_broker/repositories/page.rb diff --git a/lib/pact_broker/pacticipants/repository.rb b/lib/pact_broker/pacticipants/repository.rb index 3b61e4f1f..11aa4e9f6 100644 --- a/lib/pact_broker/pacticipants/repository.rb +++ b/lib/pact_broker/pacticipants/repository.rb @@ -45,8 +45,7 @@ def find_all_pacticipant_versions_in_reverse_order name, pagination_options = ni .eager(tags: :head_tag) .eager(:pact_publications) .reverse_order(:order) - query = query.paginate(pagination_options[:page_number], pagination_options[:page_size]) if pagination_options - query.all + query.all_with_pagination_options(pagination_options) end def find_by_name_or_create name diff --git a/lib/pact_broker/repositories/helpers.rb b/lib/pact_broker/repositories/helpers.rb index be2c5d89e..c34f079a7 100644 --- a/lib/pact_broker/repositories/helpers.rb +++ b/lib/pact_broker/repositories/helpers.rb @@ -1,3 +1,5 @@ +require "pact_broker/repositories/page" + Sequel.extension :escaped_like module PactBroker @@ -6,6 +8,15 @@ module Helpers extend self + def all_with_pagination_options(pagination_options) + if pagination_options + query = paginate(pagination_options[:page_number], pagination_options[:page_size]) + Page.new(query.all, query) + else + all + end + end + def all_forbidding_lazy_load all.each{ | row | row.forbid_lazy_load if row.respond_to?(:forbid_lazy_load) } end diff --git a/lib/pact_broker/repositories/page.rb b/lib/pact_broker/repositories/page.rb new file mode 100644 index 000000000..50d27e300 --- /dev/null +++ b/lib/pact_broker/repositories/page.rb @@ -0,0 +1,24 @@ +require "forwardable" + +# An array that provides the pagination details + +module PactBroker + module Repositories + module Helpers + class Page < Array + extend Forwardable + + attr_reader :query + + PAGE_PROPERTIES = [:page_size, :page_count, :page_range, :current_page, :next_page, :prev_page, :first_page?, :last_page?, :pagination_record_count, :current_page_record_count, :current_page_record_range] + + delegate PAGE_PROPERTIES => :query + + def initialize(array, query) + super(array) + @query = query + end + end + end + end +end diff --git a/spec/features/get_versions_spec.rb b/spec/features/get_versions_spec.rb index da07624a1..341d33f27 100644 --- a/spec/features/get_versions_spec.rb +++ b/spec/features/get_versions_spec.rb @@ -8,8 +8,7 @@ context "when the pacticipant exists" do before do - TestDataBuilder.new - .create_consumer("Consumer") + td.create_consumer("Consumer") .create_consumer_version("1.0.0") .create_consumer_version("1.0.1") end @@ -28,6 +27,10 @@ it "paginates the response" do expect(last_response_body[:_links][:"versions"].size).to eq 1 end + + it "includes the pagination relations" do + expect(last_response_body[:_links]).to have_key(:next) + end end end