Skip to content

Commit

Permalink
adding in paginate method for proper limit and offset calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink committed Jun 20, 2018
1 parent b32d59d commit 1f7c562
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions spec/model/model_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,36 @@ module ModelSpec
end
end
end

context "with pagination" do
it "can pull the next 5 users from page 2" do
temporary do
reinit

15.times do |x|
User.create!({first_name: "user#{x}"})
end

users = User.query.paginate(page: 2, per_page: 5)
users.map(&.first_name).should eq ["user5", "user6", "user7", "user8", "user9"]
users.total_entries.should eq 15
end
end

it "can paginate with where clause" do
temporary do
reinit
last_names = ["smith", "jones"]
15.times do |x|
last_name = last_names[x % 2]?
User.create!({first_name: "user#{x}", last_name: last_name})
end

users = User.query.where { last_name == "smith" }.paginate(page: 1, per_page: 5)
users.map(&.first_name).should eq ["user0", "user2", "user4", "user6", "user8"]
users.total_entries.should eq 8
end
end
end
end
end
17 changes: 17 additions & 0 deletions src/clear/sql/query/with_pagination.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
module Clear::SQL::Query::WithPagination
DEFAULT_LIMIT = 50
DEFAULT_PAGE = 1

macro included
property total_entries : Int64? = nil
end

# Maybe this goes on the Collection?
def paginate(page : Int32 = DEFAULT_PAGE, per_page : Int32 = DEFAULT_LIMIT)
# Need to clear these values to get total count first
clear_limit.clear_offset
# TODO: this fails. Maybe something with clear_select?
# @total_entries = count
@total_entries = Clear::SQL.connection.scalar("SELECT COUNT(*) #{print_froms} #{print_wheres}").as(Int64)

# Calculate proper offset and set limit
page = page < 1 ? 1 : page
@limit = per_page.to_i64
@offset = (per_page * (page - 1)).to_i64
change!
end

def per_page
limit
end
Expand Down

0 comments on commit 1f7c562

Please sign in to comment.