Skip to content

Commit 74f47c7

Browse files
committed
Moves paginator settings to Configuration object
1 parent 1e65b01 commit 74f47c7

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

lib/ajax-datatables-rails/base.rb

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class MethodNotImplementedError < StandardError; end
99
def initialize(view, options = {})
1010
@view = view
1111
@options = options
12+
load_paginator
1213
end
1314

1415
def config
@@ -197,5 +198,16 @@ def generate_sortable_displayed_columns
197198
@sortable_displayed_columns
198199
end
199200

201+
def load_paginator
202+
case config.paginator
203+
when :kaminari
204+
extend Extensions::Kaminari
205+
when :will_paginate
206+
extend Extensions::WillPaginate
207+
else
208+
extend Extensions::SimplePaginator
209+
end
210+
self
211+
end
200212
end
201213
end

lib/ajax-datatables-rails/config.rb

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ class Configuration
2020

2121
# default db_adapter is pg (postgresql)
2222
config_accessor(:db_adapter) { :pg }
23+
config_accessor(:paginator) { :simple_paginator }
2324
end
2425
end
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
AjaxDatatablesRails.configure do |config|
22
# available options for db_adapter are: :pg, :mysql2, :sqlite3
33
# config.db_adapter = :pg
4+
5+
# available options for paginator are: :simple_paginator, :kaminari, :will_paginate
6+
# config.paginator = :simple_paginator
47
end

lib/generators/rails/templates/datatable.rb

-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
class <%= @datatable_name %>Datatable < AjaxDatatablesRails::Base
2-
# uncomment the appropriate paginator module,
3-
# depending on gems available in your project.
4-
# include AjaxDatatablesRails::Extensions::Kaminari
5-
# include AjaxDatatablesRails::Extensions::WillPaginate
6-
# include AjaxDatatablesRails::Extensions::SimplePaginator
72

83
def sortable_columns
94
# Declare strings in this format: ModelName.column_name

spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,18 @@ class FooDatatable < AjaxDatatablesRails::Base
198198
let(:results) { double('Collection', :offset => [], :limit => []) }
199199
let(:view) { double('view', :params => params) }
200200
let(:datatable) { AjaxDatatablesRails::Base.new(view) }
201+
let(:records) { double('Array').as_null_object }
201202

202203
before(:each) do
203204
allow(datatable).to receive(:sortable_columns) { ['User.foo', 'User.bar'] }
204205
allow(datatable).to receive(:sortable_displayed_columns) { ["0", "1"] }
205206
end
206207

207208
describe '#paginate_records' do
208-
it 'raises a MethodNotImplementedError' do
209-
expect { datatable.send(:paginate_records, []) }.to raise_error(
210-
AjaxDatatablesRails::Base::MethodNotImplementedError
211-
)
209+
it 'defaults to Extensions::SimplePaginator#paginate_records' do
210+
allow(records).to receive_message_chain(:offset, :limit)
211+
212+
expect { datatable.send(:paginate_records, records) }.not_to raise_error
212213
end
213214
end
214215

spec/ajax-datatables-rails/kaminari_spec.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
require 'spec_helper'
22

33
class KaminariDatatable < AjaxDatatablesRails::Base
4-
include AjaxDatatablesRails::Extensions::Kaminari
54
end
65

76
describe KaminariDatatable do
7+
before(:each) do
8+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:paginator) { :kaminari }
9+
end
10+
811
describe '#paginate_records' do
912
let(:users_database) do
1013
double('User',
@@ -29,4 +32,4 @@ class KaminariDatatable < AjaxDatatablesRails::Base
2932
datatable.send(:paginate_records, records)
3033
end
3134
end
32-
end
35+
end

spec/ajax-datatables-rails/will_paginate_spec.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
require 'spec_helper'
22

33
class WillPaginateDatatable < AjaxDatatablesRails::Base
4-
include AjaxDatatablesRails::Extensions::WillPaginate
54
end
65

76
describe WillPaginateDatatable do
7+
before(:each) do
8+
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:paginator) { :will_paginate }
9+
end
10+
811
describe '#paginate_records' do
912
let(:users_database) do
1013
double('User',
@@ -22,4 +25,4 @@ class WillPaginateDatatable < AjaxDatatablesRails::Base
2225
datatable.send(:paginate_records, records)
2326
end
2427
end
25-
end
28+
end

0 commit comments

Comments
 (0)