Skip to content

Commit a05aa9a

Browse files
committed
Change paginator configuration
Instead of boolean predicate methods, just use one `.paginator` method that returns the paginator name symbolized. Signed-off-by: David Celis <[email protected]>
1 parent fbfd005 commit a05aa9a

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ Link: <http://localhost:3000/movies?page=1>; rel="first">,
8484
# ...
8585
```
8686

87+
## Testing
88+
89+
```bash
90+
PAGINATOR=kaminari bundle exec rspec
91+
PAGINATOR=will_paginate bundle exec rspec
92+
```
93+
8794
## Contributing
8895

8996
1. Fork it

lib/api-pagination.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33

44
module ApiPagination
55
class << self
6-
attr_writer :kaminari
7-
attr_writer :will_paginate
8-
9-
def kaminari?() !!@kaminari end
10-
def will_paginate?() !!@will_paginate end
6+
attr_reader :paginator
117

128
def paginate(collection, options = {}, &block)
139
options[:page] ||= 1
1410
options[:per_page] ||= 10
1511

16-
if ApiPagination.kaminari?
12+
case ApiPagination.paginator
13+
when :kaminari
1714
collection.page(options[:page]).per(options[:per_page]).tap(&block)
18-
elsif ApiPagination.will_paginate?
15+
when :will_paginate
1916
collection.paginate(:page => options[:page], :per_page => options[:per_page]).tap(&block)
2017
end
2118
end

lib/api-pagination/hooks.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ def first_page?() !previous_page end
2626
def last_page?() !next_page end
2727
end
2828

29-
ApiPagination.will_paginate = true
29+
ApiPagination.instance_variable_set(:@paginator, :will_paginate)
3030
end
3131

3232
begin; require 'kaminari'; rescue LoadError; end
3333
if defined?(Kaminari)
34-
ApiPagination.kaminari = true
34+
ApiPagination.instance_variable_set(:@paginator, :kaminari)
3535
end
3636

3737
STDERR.puts <<-EOC unless defined?(Kaminari) || defined?(WillPaginate)

spec/spec_helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ def paginate(options = {})
2626
end
2727
end
2828

29-
ApiPagination.kaminari = ENV['PAGINATOR'] == 'kaminari'
30-
ApiPagination.will_paginate = ENV['PAGINATOR'] == 'will_paginate'
29+
if ENV['PAGINATOR']
30+
ApiPagination.instance_variable_set(:@paginator, ENV['PAGINATOR'].to_sym)
31+
else
32+
warn 'No PAGINATOR set. Defaulting to kaminari. To test against will_paginate, run `PAGINATOR=will_paginate bundle exec rspec`'
33+
ApiPagination.instance_variable_set(:@paginator, :kaminari)
34+
end
3135

3236
RSpec.configure do |config|
3337
config.include Rack::Test::Methods

0 commit comments

Comments
 (0)