Skip to content

Commit

Permalink
Avoid casting nil to int. Fixes #1356
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jan 11, 2016
1 parent 99aadb5 commit 24ef748
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions app/models/blacklight/facet_paginator.rb
Expand Up @@ -28,7 +28,7 @@ class FacetPaginator
# :sort => 'count' or 'index', solr tokens for facet value sorting, default 'count'.
def initialize(all_facet_values, arguments = {})
# to_s.to_i will conveniently default to 0 if nil
@offset = arguments[:offset].to_s.to_i
@offset = arguments[:offset].to_s.to_i
@limit = arguments[:limit]
@sort = arguments[:sort]
@prefix = arguments[:prefix]
Expand All @@ -50,7 +50,8 @@ def prev_page
end

def current_page
if limit == 0 #check for divide by zero
# A nil limit is unlimited, thus only one page.
if limit.nil? || limit == 0 #check for divide by zero
1
else
@offset / limit + 1
Expand Down
19 changes: 15 additions & 4 deletions spec/models/blacklight/facet_paginator_spec.rb
Expand Up @@ -83,11 +83,22 @@
end

context "for a nil :limit" do
subject { described_class.new(seven_facet_values, offset: 0, limit: nil) }
it "should return all the items" do
expect(subject.items).to eq seven_facet_values
let(:paginator) { described_class.new(seven_facet_values, offset: 0, limit: nil) }

describe "#items" do
subject { paginator.items }
it { is_expected.to eq seven_facet_values }
end

describe "#last_page?" do
subject { paginator.last_page? }
it { is_expected.to be true }
end

describe "#current_page" do
subject { paginator.current_page }
it { is_expected.to eq 1 }
end
it { should be_last_page }
end

describe "#as_json" do
Expand Down

0 comments on commit 24ef748

Please sign in to comment.