Skip to content

Commit

Permalink
fix(versions): add missing next and previous relations to paginated r…
Browse files Browse the repository at this point in the history
…esponse
  • Loading branch information
bethesque committed Dec 8, 2022
1 parent 144b817 commit 3b46847
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lib/pact_broker/pacticipants/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/pact_broker/repositories/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "pact_broker/repositories/page"

Sequel.extension :escaped_like

module PactBroker
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lib/pact_broker/repositories/page.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions spec/features/get_versions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 3b46847

Please sign in to comment.