Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for array in paginator #1108

Merged
merged 6 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions spec/paginator/backend_helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Paginatable
paginate(FakeUser::BaseQuery.new)
end

def call_array
paginate_array([1]*50)
end

def params
Params.new(@page)
end
Expand Down Expand Up @@ -78,6 +82,33 @@ describe Lucky::Paginator::BackendHelpers do
records.query.limit.should eq(25)
end

it "accept array with default" do
pages, records = Paginatable.new.call_array

pages.page.should eq(1)
pages.per_page.should eq(25)
pages.total.should eq(2)
records.size.should eq(25)
end

it "uses array with the 'page' param if given" do
pages, records = Paginatable.new(page: "2").call_array

pages.page.should eq(2)
pages.per_page.should eq(25)
pages.total.should eq(2)
records.size.should eq(25)
end

it "return empty array if page is set above array size" do
confact marked this conversation as resolved.
Show resolved Hide resolved
pages, records = Paginatable.new(page: "3").call_array

pages.page.should eq(3)
pages.per_page.should eq(25)
pages.total.should eq(2)
records.size.should eq(0)
end

it "allows overriding 'paginator_page' and 'paginator_per_page'" do
pages, records = PaginatableWithOverriddenMethods.new.call

Expand Down
42 changes: 42 additions & 0 deletions src/lucky/paginator/backend_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,48 @@ module Lucky::Paginator::BackendHelpers
{pages, updated_query}
end

# Call this in your actions to paginate an array.
#
# This method will return a `Lucky::Paginator` object and the requested page
# of items.
#
# ## Examples
#
# ```crystal
# class ListItems::Index < BrowserAction
# get "/items" do
# # The 'Array' will just show items for the requested page
# pages, items = paginate_array([1, 2, 3])
# render IndexPage, pages: pages, items: items
# end
# end
#
# class Users::IndexPage < MainLayout
# needs pages : Lucky::Paginator
# needs items : Array(Int32)
#
# def content
# # Render pagination links for the 'items' Array
# mount Lucky::Paginator::SimpleNav.new(@pages)
# end
# end
# ```
def paginate_array(
items : Array(T),
per_page : Int32 = paginator_per_page
) : Tuple(Paginator, Array(T)) forall T
pages = Paginator.new \
page: paginator_page,
per_page: per_page,
item_count: items.size,
full_path: context.request.resource

return {pages, Array(T).new} if pages.offset > items.size
confact marked this conversation as resolved.
Show resolved Hide resolved

updated_items = items[pages.offset...pages.offset + pages.per_page]
{pages, updated_items}
end

# Returns the page that was request, or `1`
#
# By default this method looks for a `page` param. It can be given as a
Expand Down