Skip to content

Commit

Permalink
Fix styling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Yacine Petitprez committed Jun 24, 2018
1 parent 827b5c1 commit 8a0090d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This configuration file was generated by `ameba --gen-config`
# on 2018-06-24 15:35:58 +02:00 using Ameba version 0.7.0.
# The point is for the user to remove these configuration records
# one by one as the reported problems are removed from the code base.
42 changes: 21 additions & 21 deletions spec/sql/select_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -272,72 +272,72 @@ module SelectSpec
context "when there's 1901902 records and limit of 25" do
it "sets the per_page to 25" do
r = select_request.from(:users).offset(0).limit(25)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.per_page.should eq 25
end

it "returns 1 for current_page with no limit set" do
r = select_request.from(:users)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.current_page.should eq 1
end

it "returns 5 for current_page when offset is 100" do
r = select_request.from(:users).offset(100).limit(25)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.current_page.should eq 5
end

it "returns 1 for total_pages when there's no limit" do
r = select_request.from(:users)
r.total_entries = 1901902_i64
r.total_pages.should eq 1
r.total_entries = 1_901_902_i64
r.total_pages.should eq 1
end

it "returns 76077 total_pages when 25 per_page" do
r = select_request.from(:users).offset(100).limit(25)
r.total_entries = 1901902_i64
r.total_pages.should eq 76077
r.total_entries = 1_901_902_i64
r.total_pages.should eq 76_077
end

it "returns 4 as previous_page when on page 5" do
r = select_request.from(:users).offset(100).limit(25)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.current_page.should eq 5
r.previous_page.should eq 4
r.previous_page.should eq 4
end

it "returns nil for previous_page when on page 1" do
r = select_request.from(:users).offset(0).limit(25)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.current_page.should eq 1
r.previous_page.should eq nil
r.previous_page.should eq nil
end

it "returns 6 as next_page when on page 5" do
r = select_request.from(:users).offset(100).limit(25)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.current_page.should eq 5
r.next_page.should eq 6
end

it "returns nil for next_page when on page 76077" do
r = select_request.from(:users).offset(1901900).limit(25)
r.total_entries = 1901902_i64
r.current_page.should eq 76077
r = select_request.from(:users).offset(1_901_900).limit(25)
r.total_entries = 1_901_902_i64
r.current_page.should eq 76_077
r.next_page.should eq nil
end

it "returns true for out_of_bounds? when current_page is 76078" do
r = select_request.from(:users).offset(1901925).limit(25)
r.total_entries = 1901902_i64
r.current_page.should eq 76078
r = select_request.from(:users).offset(1_901_925).limit(25)
r.total_entries = 1_901_902_i64
r.current_page.should eq 76_078
r.out_of_bounds?.should eq true
end

it "returns false for out_of_bounds? when current_page is in normal range" do
r = select_request.from(:users).offset(925).limit(25)
r.total_entries = 1901902_i64
r.total_entries = 1_901_902_i64
r.out_of_bounds?.should eq false
end
end
Expand Down
12 changes: 6 additions & 6 deletions src/clear/sql/query/with_pagination.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Clear::SQL::Query::WithPagination
DEFAULT_LIMIT = 50
DEFAULT_PAGE = 1
DEFAULT_PAGE = 1

macro included
property total_entries : Int64? = nil
end
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
@total_entries = count

# Calculate proper offset and set limit
page = page < 1 ? 1 : page
@limit = per_page.to_i64
Expand All @@ -30,7 +30,7 @@ module Clear::SQL::Query::WithPagination
(offset.as(Int64) / limit.as(Int64)) + 1
end
end

def total_pages
if limit.nil? || total_entries.nil?
1
Expand Down

0 comments on commit 8a0090d

Please sign in to comment.