Skip to content

Commit

Permalink
Improve ScheduleExceptions pagination.
Browse files Browse the repository at this point in the history
  • Loading branch information
tszolar committed Jun 8, 2015
1 parent f9944f6 commit ae89be9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/api/schedule_exceptions_endpoints.rb
Expand Up @@ -10,8 +10,7 @@ class ScheduleExceptionsEndpoints < Grape::API
params { use :pagination }

get do
dataset = paginate(ScheduleException.dataset)
ScheduleExceptionsRepresenter.new(dataset)
represent_paginated(ScheduleException.dataset, ScheduleExceptionsRepresenter)
end

params do
Expand Down
11 changes: 11 additions & 0 deletions app/helpers/api_helper.rb
@@ -1,4 +1,7 @@
require 'corefines'
require 'sirius_api'

using Corefines::Hash[:only, :rekey]
##
# Helper methods used in Grape's API endpoints.
module ApiHelper
Expand Down Expand Up @@ -41,4 +44,12 @@ def paginate(dataset)
.limit(params[:limit] || DEFAULT_LIMIT)
.offset(params[:offset] || DEFAULT_OFFSET)
end

def represent_paginated(dataset, representer)
paginated_dataset = paginate(dataset)
meta = paginated_dataset.opts
.rekey(:total_count => :count)
.only(:count, :offset, :limit)
representer.for_collection.new(paginated_dataset).to_hash({'meta' => meta})
end
end

0 comments on commit ae89be9

Please sign in to comment.