Skip to content

Commit 79ba03c

Browse files
committed
Remove dependency on view_context module, only inject request params
1 parent 5862ca9 commit 79ba03c

11 files changed

+69
-78
lines changed

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Set the controller to respond to JSON
317317
def index
318318
respond_to do |format|
319319
format.html
320-
format.json { render json: UserDatatable.new(view_context) }
320+
format.json { render json: UserDatatable.new(params) }
321321
end
322322
end
323323
```
@@ -386,7 +386,9 @@ Sometimes you'll need to use view helper methods like `link_to`, `mail_to`,
386386
To have these methods available to be used, this is the way to go:
387387

388388
```ruby
389-
class MyCustomDatatable < AjaxDatatablesRails::Base
389+
class UserDatatable < AjaxDatatablesRails::Base
390+
extend Forwardable
391+
390392
# either define them one-by-one
391393
def_delegator :@view, :check_box_tag
392394
def_delegator :@view, :link_to
@@ -398,6 +400,11 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
398400

399401
# ... other methods (view_columns, get_raw_records...)
400402

403+
def initialize(params, opts = {})
404+
@view = opts[:view_context]
405+
super
406+
end
407+
401408
# now, you'll have these methods available to be used anywhere
402409
def data
403410
records.map do |record|
@@ -412,6 +419,14 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
412419
end
413420
end
414421
end
422+
423+
# and in your controller:
424+
def index
425+
respond_to do |format|
426+
format.html
427+
format.json { render json: UserDatatable.new(params, view_context: view_context) }
428+
end
429+
end
415430
```
416431

417432
### Using view decorators
@@ -464,12 +479,7 @@ class UserDecorator < ApplicationDecorator
464479
end
465480
```
466481

467-
**Note :** On the long term it's much more cleaner than using `def_delegator` since decorators are reusable everywhere in your application :)
468-
469-
So we **strongly recommand you to use Draper decorators.** It will help keeping your DataTables class small and clean and keep focused on what they should do (mostly) : filtering records ;)
470-
471-
**Note 2 :** The `def_delegator` might disappear in a near future : [#288 [RFC] Remove dependency on view_context](https://github.com/jbox-web/ajax-datatables-rails/issues/288).
472-
You're invited to give your opinion :)
482+
This way you don't need to inject the `view_context` in the Datatable object to access helpers methods.
473483

474484
### Pass options to the datatable class
475485

@@ -482,7 +492,7 @@ Example:
482492
def index
483493
respond_to do |format|
484494
format.html
485-
format.json { render json: UserDatatable.new(view_context, user: current_user, from: 1.month.ago) }
495+
format.json { render json: UserDatatable.new(params, user: current_user, from: 1.month.ago) }
486496
end
487497
end
488498

@@ -728,15 +738,15 @@ then in your controllers :
728738
end
729739

730740
def datatable
731-
render json: PostDatatable.new(view_context)
741+
render json: PostDatatable.new(params)
732742
end
733743

734744
# UsersController
735745
def index
736746
end
737747

738748
def datatable
739-
render json: UserDatatable.new(view_context)
749+
render json: UserDatatable.new(params)
740750
end
741751
```
742752

lib/ajax-datatables-rails/base.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
module AjaxDatatablesRails
44
class Base
5-
extend Forwardable
65

7-
attr_reader :view, :options
8-
def_delegator :@view, :params
6+
attr_reader :params, :options
97

108
GLOBAL_SEARCH_DELIMITER = ' '
119

12-
def initialize(view, options = {})
13-
@view = view
10+
def initialize(params, options = {})
11+
@params = params
1412
@options = options
1513
load_orm_extension
1614
end

spec/ajax-datatables-rails/base_spec.rb

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,46 @@
33
describe AjaxDatatablesRails::Base do
44

55
describe 'an instance' do
6-
let(:view) { double('view', params: sample_params) }
7-
8-
it 'requires a view_context' do
6+
it 'requires a hash of params' do
97
expect { described_class.new }.to raise_error ArgumentError
108
end
119

1210
it 'accepts an options hash' do
13-
datatable = described_class.new(view, foo: 'bar')
11+
datatable = described_class.new(sample_params, foo: 'bar')
1412
expect(datatable.options).to eq(foo: 'bar')
1513
end
1614
end
1715

1816
context 'Public API' do
19-
let(:view) { double('view', params: sample_params) }
20-
2117
describe '#view_columns' do
2218
it 'raises an error if not defined by the user' do
23-
datatable = described_class.new(view)
19+
datatable = described_class.new(sample_params)
2420
expect { datatable.view_columns }.to raise_error NotImplementedError
2521
end
2622

2723
context 'child class implements view_columns' do
2824
it 'expects a hash based defining columns' do
29-
datatable = ComplexDatatable.new(view)
25+
datatable = ComplexDatatable.new(sample_params)
3026
expect(datatable.view_columns).to be_a(Hash)
3127
end
3228
end
3329
end
3430

3531
describe '#get_raw_records' do
3632
it 'raises an error if not defined by the user' do
37-
datatable = described_class.new(view)
33+
datatable = described_class.new(sample_params)
3834
expect { datatable.get_raw_records }.to raise_error NotImplementedError
3935
end
4036
end
4137

4238
describe '#data' do
4339
it 'raises an error if not defined by the user' do
44-
datatable = described_class.new(view)
40+
datatable = described_class.new(sample_params)
4541
expect { datatable.data }.to raise_error NotImplementedError
4642
end
4743

4844
context 'when data is defined as a hash' do
49-
let(:datatable) { ComplexDatatable.new(view) }
45+
let(:datatable) { ComplexDatatable.new(sample_params) }
5046

5147
it 'should return an array of hashes' do
5248
create_list(:user, 5)
@@ -66,7 +62,7 @@
6662
end
6763

6864
context 'when data is defined as a array' do
69-
let(:datatable) { ComplexDatatableArray.new(view) }
65+
let(:datatable) { ComplexDatatableArray.new(sample_params) }
7066

7167
it 'should return an array of arrays' do
7268
create_list(:user, 5)
@@ -87,7 +83,7 @@
8783
end
8884

8985
describe '#as_json' do
90-
let(:datatable) { ComplexDatatable.new(view) }
86+
let(:datatable) { ComplexDatatable.new(sample_params) }
9187

9288
it 'should return a hash' do
9389
create_list(:user, 5)
@@ -116,8 +112,7 @@
116112

117113
context 'Private API' do
118114

119-
let(:view) { double('view', params: sample_params) }
120-
let(:datatable) { ComplexDatatable.new(view) }
115+
let(:datatable) { ComplexDatatable.new(sample_params) }
121116

122117
before(:each) do
123118
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:orm) { nil }
@@ -150,34 +145,31 @@
150145
describe 'helper methods' do
151146
describe '#offset' do
152147
it 'defaults to 0' do
153-
default_view = double('view', params: {})
154-
datatable = described_class.new(default_view)
148+
datatable = described_class.new({})
155149
expect(datatable.datatable.send(:offset)).to eq(0)
156150
end
157151

158152
it 'matches the value on view params[:start]' do
159-
paginated_view = double('view', params: { start: '11' })
160-
datatable = described_class.new(paginated_view)
153+
datatable = described_class.new({ start: '11' })
161154
expect(datatable.datatable.send(:offset)).to eq(11)
162155
end
163156
end
164157

165158
describe '#page' do
166159
it 'calculates page number from params[:start] and #per_page' do
167-
paginated_view = double('view', params: { start: '11' })
168-
datatable = described_class.new(paginated_view)
160+
datatable = described_class.new({ start: '11' })
169161
expect(datatable.datatable.send(:page)).to eq(2)
170162
end
171163
end
172164

173165
describe '#per_page' do
174166
it 'defaults to 10' do
175-
datatable = described_class.new(view)
167+
datatable = described_class.new(sample_params)
176168
expect(datatable.datatable.send(:per_page)).to eq(10)
177169
end
178170

179171
it 'matches the value on view params[:length]' do
180-
other_view = double('view', params: { length: 20 })
172+
other_view = { length: 20 }
181173
datatable = described_class.new(other_view)
182174
expect(datatable.datatable.send(:per_page)).to eq(20)
183175
end

spec/ajax-datatables-rails/datatable/column_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
describe AjaxDatatablesRails::Datatable::Column do
44

5-
let(:view) { double('view', params: sample_params) }
6-
let(:datatable) { ComplexDatatable.new(view) }
5+
let(:datatable) { ComplexDatatable.new(sample_params) }
76

87
describe 'username column' do
98

@@ -129,7 +128,7 @@
129128
end
130129

131130
describe '#formatter' do
132-
let(:datatable) { DatatableWithFormater.new(view) }
131+
let(:datatable) { DatatableWithFormater.new(sample_params) }
133132
let(:column) { datatable.datatable.columns.find { |c| c.data == 'last_name' } }
134133

135134
it 'should be a proc' do
@@ -138,7 +137,7 @@
138137
end
139138

140139
describe '#filter' do
141-
let(:datatable) { DatatableCondProc.new(view) }
140+
let(:datatable) { DatatableCondProc.new(sample_params) }
142141
let(:column) { datatable.datatable.columns.find { |c| c.data == 'username' } }
143142

144143
it 'should be a proc' do

spec/ajax-datatables-rails/datatable/datatable_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
describe AjaxDatatablesRails::Datatable::Datatable do
44

5-
let(:view) { double('view', params: sample_params) }
6-
let(:datatable) { ComplexDatatable.new(view).datatable }
5+
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
76
let(:order_option) { {'0'=>{'column'=>'0', 'dir'=>'asc'}, '1'=>{'column'=>'1', 'dir'=>'desc'}} }
87

98
describe 'order methods' do

spec/ajax-datatables-rails/datatable/simple_order_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
describe AjaxDatatablesRails::Datatable::SimpleOrder do
44

5-
let(:view) { double('view', params: sample_params) }
6-
let(:datatable) { ComplexDatatable.new(view).datatable }
5+
let(:datatable) { ComplexDatatable.new(sample_params).datatable }
76
let(:options) { ActiveSupport::HashWithIndifferentAccess.new({'column' => '1', 'dir' => 'desc'}) }
87
let(:simple_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(datatable, options) }
98

@@ -26,7 +25,7 @@
2625
end
2726

2827
describe 'using column option' do
29-
let(:sorted_datatable) { DatatableOrderNullsLast.new(view).datatable }
28+
let(:sorted_datatable) { DatatableOrderNullsLast.new(sample_params).datatable }
3029
let(:nulls_last_order) { AjaxDatatablesRails::Datatable::SimpleOrder.new(sorted_datatable, options) }
3130

3231
it 'sql query' do

spec/ajax-datatables-rails/extended_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
describe AjaxDatatablesRails::Base do
44
describe 'it can transform search value before asking the database' do
5-
let(:view) { double('view', params: sample_params) }
6-
let(:datatable) { DatatableWithFormater.new(view) }
5+
let(:datatable) { DatatableWithFormater.new(sample_params) }
76

87
before(:each) do
98
create(:user, username: 'johndoe', email: 'johndoe@example.com', last_name: 'DOE')

0 commit comments

Comments
 (0)