forked from Lateral-Link/haistack-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PaginateService and update dependencies #8
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d7750f3
feat: add PaginateService
andrecego 8bd21b3
deps: add gem 'oj' for json serialization performance
andrecego 7112fd0
lint: update rubocop configs and lint
andrecego 17ca29a
feat: add candidates listing endpoint
andrecego 1e65381
chore: update candidates Swagger documentation
andrecego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class Candidate < ApplicationRecord | ||
def as_json(options = {}) | ||
serializable_fields = %i[id name email birthdate] | ||
super(options.merge(only: serializable_fields)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
# PaginateService is responsible for paginating a relation and returning metadata about the pagination. | ||
class PaginateService | ||
DEFAULT_PAGE = 1 | ||
DEFAULT_PER_PAGE = 20 | ||
MAX_PER_PAGE = 100 | ||
|
||
# Initializes a new instance of PaginateService. | ||
# | ||
# @param relation [Object] The relation object to paginate. | ||
# @param page [Integer] The page number to retrieve (default: 1). | ||
# @param per_page [Integer] The number of records per page (default: 20). | ||
def initialize(relation:, page: nil, per_page: nil) | ||
@relation = relation | ||
@page = page || DEFAULT_PAGE | ||
@per_page = [per_page || DEFAULT_PER_PAGE, MAX_PER_PAGE].min | ||
end | ||
|
||
# Paginates the relation based on the specified page and per_page values. | ||
# | ||
# @return [Object] The paginated relation. | ||
def call | ||
@relation.offset(offset).limit(per_page) | ||
end | ||
|
||
# Returns metadata about the pagination. | ||
# | ||
# @return [Hash] The metadata hash containing page, per_page, total_count, and total_pages. | ||
def meta | ||
{ page: page, per_page: per_page, total_count: total_count, total_pages: total_pages } | ||
end | ||
|
||
private | ||
|
||
attr_reader :relation, :page, :per_page | ||
|
||
def offset | ||
(page - 1) * per_page | ||
end | ||
|
||
def total_pages | ||
(total_count.to_f / per_page).ceil | ||
end | ||
|
||
def total_count | ||
@total_count ||= relation.offset(nil).limit(nil).count | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
# Optimize JSON serialization https://github.com/ohler55/oj/blob/develop/pages/Rails.md | ||
Oj.optimize_rails |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe PaginateService do | ||
describe '#call' do | ||
subject(:paginate_service) { described_class.new(relation: relation, page: page, per_page: per_page) } | ||
|
||
let(:relation) { Candidate.all } | ||
let(:page) { 1 } | ||
let(:per_page) { 1 } | ||
|
||
context 'when there are records of the given relation' do | ||
before { create_list(:candidate, 2) } | ||
|
||
it 'returns first page of results' do | ||
expect(paginate_service.call.order(:id).map(&:attributes)).to eq([Candidate.first.attributes]) | ||
end | ||
|
||
it 'returns only the number of records specified by per_page' do | ||
expect(paginate_service.call.count).to eq(1) | ||
end | ||
|
||
context 'when on second page' do | ||
let(:page) { 2 } | ||
|
||
it 'returns second page of results' do | ||
expect(paginate_service.call.order(:id).map(&:attributes)).to eq([Candidate.last.attributes]) | ||
end | ||
end | ||
end | ||
|
||
context 'when the relation is empty' do | ||
let(:relation) { Candidate.all } | ||
|
||
it 'returns the relation' do | ||
expect(paginate_service.call).to eq([]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance optimizations for json serialization: https://github.com/ohler55/oj?tab=readme-ov-file#performance-comparisons