cleanup lib/api.rb for folio

refs CNVS-5305

with folio pages, we can just trust and use page.first_page and
page.last_page

test-plan:
 * /api/v1/courses
   - should have "first" link regardless of page (first, last, in
     between)
   - should have "last" link regardless of page (first, last, in
     between)
 * /api/v1/search/recipients?context=course_123
   - should have "first" link regardless of page (first, last, in
     between)
   - should not have "last" link when there's a next page
   - should have "last" link on last page (no next page)
 * exercise the conversation receipient search when the search results
   have just one hit
   - should not throw an error
 * load gradebook2 when there's only one page of enrollments
   - should not throw an error

Change-Id: If6dfa972db22a91350ee820ccbfe25008f6b0e90
Reviewed-on: https://gerrit.instructure.com/26538
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Jacob Fugal 2013-11-20 23:23:37 -07:00
parent 11c509fda6
commit 03f150a078
5 changed files with 15 additions and 17 deletions

View File

@ -107,8 +107,8 @@ define [
perPage = parseInt(url.match(@perPageRegex)[1], 10)
(@options.params ?= {}).per_page = perPage
if @urls.last
@totalPages = parseInt(@urls.last.match(@pageRegex)[1], 10)
if @urls.last and match = @urls.last.match(@pageRegex)
@totalPages = parseInt(match[1], 10)
@atLeastOnePageFetched = true

View File

@ -90,6 +90,9 @@ define [
return
lastPage = lastLink[0].match(/page=(\d+)/)[1]
lastPage = parseInt lastPage, 10
if lastPage == 1
@gotAllStudents()
return
fetchEnrollments = (page) =>
$.ajaxJSON @options[enrollmentsUrl], "GET", {page}

View File

@ -201,28 +201,23 @@ module Api
# The collection needs to be a will_paginate collection (or act like one)
# a new, paginated collection will be returned
def self.paginate(collection, controller, base_url, pagination_args = {})
pagination_args[:total_entries] = nil if pagination_args[:without_count]
pagination_args.reverse_merge!(
page: controller.params[:page],
per_page: per_page_for(controller,
default: pagination_args.delete(:default_per_page),
max: pagination_args.delete(:max_per_page)))
collection = collection.paginate(pagination_args)
return unless collection.respond_to?(:next_page)
first_page = collection.respond_to?(:first_page) && collection.first_page
first_page ||= 1
last_page = (pagination_args[:without_count] ? nil : collection.total_pages)
last_page = nil if last_page.to_i <= 1
links = build_links(base_url, {
:query_parameters => controller.request.query_parameters,
:per_page => collection.per_page,
:current => collection.current_page || first_page,
:current => collection.current_page,
:next => collection.next_page,
:prev => collection.previous_page,
:first => first_page,
:last => last_page,
:first => collection.first_page,
:last => collection.last_page,
})
controller.response.headers["Link"] = links.join(',') if links.length > 0
collection

View File

@ -306,7 +306,7 @@ describe SearchController, :type => :integration do
l['search'].should == 'cletus'
l['type'].should == 'user'
end
links.map{ |l| l[:rel] }.should == ['current', 'first']
links.map{ |l| l[:rel] }.should == ['current', 'first', 'last']
end
it "should paginate contexts and return proper pagination headers" do
@ -335,7 +335,7 @@ describe SearchController, :type => :integration do
l['search'].should == 'ofcourse'
l['type'].should == 'context'
end
links.map{ |l| l[:rel] }.should == ['current', 'first']
links.map{ |l| l[:rel] }.should == ['current', 'first', 'last']
end
it "should ignore invalid per_page" do
@ -361,7 +361,7 @@ describe SearchController, :type => :integration do
l['search'].should == 'cletus'
l['type'].should == 'user'
end
links.map{ |l| l[:rel] }.should == ['current', 'first']
links.map{ |l| l[:rel] }.should == ['current', 'first', 'last']
end
it "should paginate combined context/user results" do
@ -407,7 +407,7 @@ describe SearchController, :type => :integration do
l[:uri].to_s.should match(%r{api/v1/search/recipients})
l['search'].should == 'term'
end
links.map{ |l| l[:rel] }.should == ['current', 'first']
links.map{ |l| l[:rel] }.should == ['current', 'first', 'last']
end
end

View File

@ -251,7 +251,7 @@ describe "Users API", :type => :integration do
json.size.should == 1
json.each { |j| j['url'].should == "http://www.example.com/courses/1" }
response.headers['Link'].should_not match /next/
response.headers['Link'].should_not match /last/
response.headers['Link'].should match /last/
end
it "should recognize start_time parameter" do