forked from Lateral-Link/haistack-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from andrecego/feat/add-candidates-list
Add PaginateService and update dependencies
- Loading branch information
Showing
14 changed files
with
270 additions
and
16 deletions.
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