Skip to content

Commit a1a4adb

Browse files
committed
Move Rails::Pagination inclusion to a railtie
Fixes #96
1 parent 0b1a31d commit a1a4adb

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

lib/api-pagination/hooks.rb

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
1-
begin; require 'rails'; rescue LoadError; end
2-
begin; require 'rails-api'; rescue LoadError; end
3-
4-
if defined?(Rails)
5-
module ApiPagination
6-
module Hooks
7-
def self.rails_parent_controller
8-
if Gem::Version.new(Rails.version) >= Gem::Version.new('5') || defined?(ActionController::API)
9-
ActionController::API
10-
else
11-
ActionController::Base
12-
end
13-
end
14-
end
15-
end
16-
17-
require 'rails/pagination'
18-
19-
ActiveSupport.on_load(:action_controller) do
20-
ApiPagination::Hooks.rails_parent_controller.send(:include, Rails::Pagination)
21-
end
22-
end
23-
241
begin; require 'grape'; rescue LoadError; end
252
if defined?(Grape::API)
263
require 'grape/pagination'
@@ -40,3 +17,4 @@ def self.rails_parent_controller
4017
WARNING
4118
end
4219

20+
require 'api-pagination/railtie' if defined?(Rails)

lib/api-pagination/railtie.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'rails/railtie'
2+
3+
module ApiPagination
4+
class Railtie < ::Rails::Railtie
5+
initializer :api_pagination do
6+
ActiveSupport.on_load(:action_controller) do
7+
require 'rails/pagination'
8+
9+
klass = if Rails::VERSION::MAJOR >= 5 || defined?(ActionController::API)
10+
ActionController::API
11+
else
12+
ActionController::Base
13+
end
14+
15+
klass.send(:include, Rails::Pagination)
16+
end
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)