switch paginated/bookmarked collection for folio

refs CNVS-8789

test-plan: specs

Change-Id: I8aedcdd139b2d54845116f2a762ca2d0fbb85a6b
Reviewed-on: https://gerrit.instructure.com/26537
Reviewed-by: Cody Cutrer <cody@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
QA-Review: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Jacob Fugal 2013-11-20 23:15:44 -07:00
parent 5bbcbe8103
commit ae09f28353
4 changed files with 33 additions and 40 deletions

View File

@ -17,6 +17,8 @@
#
class BookmarkedCollection::Collection < Array
include Folio::Page
FIRST_TOKEN = "first"
def initialize(bookmarker)
@ -60,8 +62,6 @@ class BookmarkedCollection::Collection < Array
end
end
attr_accessor :per_page
# typically not set unless part of a merger of many collections
# (including merging by sharding).
#
@ -78,7 +78,8 @@ class BookmarkedCollection::Collection < Array
end
def page_to_bookmark(page)
if page.nil? || page == FIRST_TOKEN
page = first_page if page.nil?
if page == first_page
nil
else
if page =~ /^bookmark:/
@ -100,8 +101,12 @@ class BookmarkedCollection::Collection < Array
page
end
def first_page
FIRST_TOKEN
end
def current_page
bookmark_to_page(current_bookmark)
bookmark_to_page(current_bookmark) || first_page
end
def next_page
@ -111,11 +116,4 @@ class BookmarkedCollection::Collection < Array
def has_more!
self.next_bookmark = bookmark_for(last)
end
# always uses FIRST_TOKEN
def first_page; FIRST_TOKEN; end
# not supported, but defined for WillPaginate interface
attr_reader :previous_page, :last_page, :total_pages
attr_accessor :total_entries
end

View File

@ -55,18 +55,7 @@ module PaginatedCollection
end
class Collection < Array
# these are set before the collection is populated
attr_accessor :current_page, :per_page
# these can optionally be set while populating the collection
attr_accessor :next_page, :previous_page, :first_page, :last_page, :total_entries
def total_pages
if total_entries
(total_entries / per_page.to_f).ceil
else
nil
end
end
include Folio::Page
end
class Proxy
@ -86,7 +75,9 @@ module PaginatedCollection
def configure_pager(pager, options)
raise(ArgumentError, "per_page required") unless options[:per_page] && options[:per_page] > 0
pager.current_page = options[:page]
current_page = options.fetch(:page) { nil }
current_page = pager.first_page if current_page.nil?
pager.current_page = current_page
pager.per_page = options[:per_page]
pager.total_entries = options[:total_entries]
pager

View File

@ -49,9 +49,9 @@ describe "BookmarkedCollection::Collection" do
end
describe "#current_page" do
it "should be nil if current_bookmark is nil" do
it "should be first_page if current_bookmark is nil" do
@collection.current_bookmark = nil
@collection.current_page.should be_nil
@collection.current_page.should == @collection.first_page
end
it "should lead with a 'bookmark:' prefix otherwise" do
@ -70,7 +70,7 @@ describe "BookmarkedCollection::Collection" do
page2.should_not == page1
@collection.current_bookmark = nil
@collection.current_page.should be_nil
@collection.current_page.should == @collection.first_page
end
end
@ -156,10 +156,24 @@ describe "BookmarkedCollection::Collection" do
end
end
describe "last_page" do
it "should assume the current_page is the last_page if there's no next_page" do
@collection.current_bookmark = "bookmark1"
@collection.next_bookmark = nil
@collection.last_page.should == @collection.current_page
end
it "should assume the last_page is unknown if there's a next_page" do
@collection.current_bookmark = "bookmark1"
@collection.next_bookmark = "bookmark2"
@collection.last_page.should be_nil
end
end
describe "remaining will_paginate support" do
it "should support per_page" do
value = 5
@collection.per_page.should be_nil
@collection.per_page.should == Folio.per_page
@collection.per_page = value
@collection.per_page.should == value
end
@ -175,19 +189,8 @@ describe "BookmarkedCollection::Collection" do
@collection.previous_page.should be_nil
end
it "should support reading empty last_page" do
@collection.last_page.should be_nil
end
it "should support reading empty total_pages" do
@collection.total_pages.should be_nil
end
it "should not support setting next_page, previous_page, last_page, or total_pages" do
@collection.should_not respond_to(:next_page=)
@collection.should_not respond_to(:previous_page=)
@collection.should_not respond_to(:last_page=)
@collection.should_not respond_to(:total_pages=)
end
end
end

View File

@ -36,7 +36,8 @@ describe "PaginatedCollection" do
items.size.should == 2
items.current_page.should == 1
items.per_page.should == 5
%w(next_page previous_page first_page last_page total_entries).each { |a| items.send(a).should be_nil }
items.last_page.should == 1
%w(first_page next_page previous_page total_entries).each { |a| items.send(a).should be_nil }
end
it "should use the pager returned" do