diff --git a/.ameba.yml b/.ameba.yml new file mode 100644 index 000000000..8a7c469d6 --- /dev/null +++ b/.ameba.yml @@ -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. diff --git a/spec/sql/select_spec.cr b/spec/sql/select_spec.cr index 90b6f470d..bfe04b88e 100644 --- a/spec/sql/select_spec.cr +++ b/spec/sql/select_spec.cr @@ -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 diff --git a/src/clear/sql/query/with_pagination.cr b/src/clear/sql/query/with_pagination.cr index be85ac7fc..6f980852c 100644 --- a/src/clear/sql/query/with_pagination.cr +++ b/src/clear/sql/query/with_pagination.cr @@ -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 @@ -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