Skip to content

Commit e3c7de4

Browse files
committed
add support for will_paginate; remove Kaminari dependency
There's no reason why Kaminari runtime dependency needs to be declared, as api-pagination will operate on any object that responds to: - current_page - total_pages - first_page? - last_page? This monkey-patches WillPaginate::CollectionMethods to make it quack like Kaminari::PageScopeMethods, so that the plugin supports both libraries. A dummy paginated class is provided for the purpose of tests.
1 parent 143b36e commit e3c7de4

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

api-pagination.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
1717
s.require_paths = ['lib']
1818

1919
s.add_dependency 'rails', '>= 3.0.0'
20-
s.add_dependency 'kaminari', '>= 0.13.0'
2120

2221
s.add_development_dependency 'bundler', '~> 1.3'
2322
s.add_development_dependency 'rspec-rails'

lib/api-pagination.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'api-pagination/version'
2-
require 'kaminari'
32

43
module ApiPagination
54
protected
@@ -29,3 +28,10 @@ def paginate(scope)
2928

3029
ActionController::Base.send(:include, ApiPagination) if defined?(ActionController::Base)
3130
ActionController::API.send(:include, ApiPagination) if defined?(ActionController::API)
31+
32+
if defined?(WillPaginate::CollectionMethods)
33+
WillPaginate::CollectionMethods.module_eval do
34+
def first_page?() !previous_page end
35+
def last_page?() !next_page end
36+
end
37+
end
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
class NumbersController < ApplicationController
22
after_filter only: [:index] { paginate(:numbers) }
33

4+
# quacks like Kaminari
5+
PaginatedSet = Struct.new(:current_page, :total_count) do
6+
def limit_value() 25 end
7+
8+
def total_pages
9+
total_count.zero? ? 1 : (total_count.to_f / limit_value).ceil
10+
end
11+
12+
def first_page?() current_page == 1 end
13+
def last_page?() current_page == total_pages end
14+
end
15+
416
def index
5-
@numbers = Kaminari.paginate_array((1..params[:count].to_i).to_a).page(params[:page])
17+
@numbers = PaginatedSet.new(params.fetch(:page, 1).to_i, params.fetch(:count).to_i)
618
render json: @numbers
719
end
820
end

spec/dummy/config/application.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# require "sprockets/railtie"
88

99
Bundler.require(*Rails.groups)
10-
require 'kaminari'
1110
require 'api-pagination'
1211

1312
module Dummy

0 commit comments

Comments
 (0)