File tree Expand file tree Collapse file tree 4 files changed +19
-11
lines changed
Expand file tree Collapse file tree 4 files changed +19
-11
lines changed Original file line number Diff line number Diff 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
89961 . Fork it
Original file line number Diff line number Diff line change 33
44module 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
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change @@ -26,8 +26,12 @@ def paginate(options = {})
2626 end
2727end
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
3236RSpec . configure do |config |
3337 config . include Rack ::Test ::Methods
You can’t perform that action at this time.
0 commit comments