-
-
Save kwando/9663d5781e34ee82a8f8 to your computer and use it in GitHub Desktop.
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
class MailboxQuery | |
include Virtus.model | |
include ActiveModel::Validations | |
attribute :page, Integer, default: 1 | |
attribute :per_page, Integer, default: 25 | |
validates :page, presence: true, numericality: {only_integer: true, greater_than: 0} | |
validates :per_page, presence: true, numericality: {only_integer: true, greater_than: 0, less_than_or_equal_to: 100} | |
def offset | |
per_page * (page - 1) | |
end | |
end | |
# In my controller / endpoint | |
query = MailboxQuery.new(params) | |
unless query.valid? | |
# render bad request with errors | |
else | |
# do the proper thing | |
mails = mailbox_store.load(limit: query.per_page, offset: query.offset) | |
# render json or whatever | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment