Skip to content

Commit 719d623

Browse files
committed
Add AjaxDatatablesRails::ActiveRecord class
1 parent 77d5185 commit 719d623

File tree

12 files changed

+69
-44
lines changed

12 files changed

+69
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 0.5.0 (to come)
44

55
* Breaking change: Remove dependency on view_context [Issue #288](https://github.com/jbox-web/ajax-datatables-rails/issues/288)
6+
* Breaking change: Replace `config.orm = :active_record` by a class : `AjaxDatatablesRails::ActiveRecord`
67

78
## 0.4.3 (2018-06-05)
89

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,11 @@ AjaxDatatablesRails.configure do |config|
9696

9797
# Or you can use your rails environment adapter if you want a generic dev and production
9898
# config.db_adapter = Rails.configuration.database_configuration[Rails.env]['adapter'].to_sym
99-
100-
# available options for orm are: :active_record, :mongoid
101-
# config.orm = :active_record
10299
end
103100
```
104101

105102
Uncomment the `config.db_adapter` line and set the corresponding value to your database and gem. This is all you need.
106103

107-
Uncomment the `config.orm` line to set `active_record or mongoid` if included in your project. It defaults to `active_record`.
108-
109104
#### Note
110105

111106
Currently `AjaxDatatablesRails` only supports `ActiveRecord` as ORM for performing database queries.
@@ -386,7 +381,7 @@ Sometimes you'll need to use view helper methods like `link_to`, `mail_to`,
386381
To have these methods available to be used, this is the way to go:
387382

388383
```ruby
389-
class UserDatatable < AjaxDatatablesRails::Base
384+
class UserDatatable < AjaxDatatablesRails::ActiveRecord
390385
extend Forwardable
391386

392387
# either define them one-by-one
@@ -483,7 +478,7 @@ This way you don't need to inject the `view_context` in the Datatable object to
483478

484479
### Pass options to the datatable class
485480

486-
An `AjaxDatatablesRails::Base` inherited class can accept an options hash at initialization. This provides room for flexibility when required.
481+
An `AjaxDatatablesRails::ActiveRecord` inherited class can accept an options hash at initialization. This provides room for flexibility when required.
487482

488483
Example:
489484

@@ -497,7 +492,7 @@ def index
497492
end
498493

499494
# The datatable class
500-
class UnrespondedMessagesDatatable < AjaxDatatablesRails::Base
495+
class UnrespondedMessagesDatatable < AjaxDatatablesRails::ActiveRecord
501496

502497
# ... other methods (view_columns, data...)
503498

@@ -648,7 +643,7 @@ To enable the date range search, for example `created_at` :
648643

649644
### Generator Syntax
650645

651-
Also, a class that inherits from `AjaxDatatablesRails::Base` is not tied to an
646+
Also, a class that inherits from `AjaxDatatablesRails::ActiveRecord` is not tied to an
652647
existing model, module, constant or any type of class in your Rails app.
653648
You can pass a name to your datatable class like this:
654649

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module AjaxDatatablesRails
4+
class ActiveRecord < AjaxDatatablesRails::Base
5+
include AjaxDatatablesRails::ORM::ActiveRecord
6+
end
7+
end

lib/ajax-datatables-rails/base.rb

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ class Base
88
GLOBAL_SEARCH_DELIMITER = ' '
99

1010
def initialize(params, options = {})
11-
@params = params
12-
@options = options
13-
load_orm_extension
11+
@params = params
12+
@options = options
1413
@datatable = Datatable::Datatable.new(self)
1514
end
1615

@@ -108,16 +107,6 @@ def records_filtered_count
108107
filter_records(fetch_records).count(:all)
109108
end
110109

111-
# Private helper methods
112-
def load_orm_extension
113-
case AjaxDatatablesRails.config.orm
114-
when :active_record
115-
extend ORM::ActiveRecord
116-
when :mongoid
117-
nil
118-
end
119-
end
120-
121110
def global_search_delimiter
122111
GLOBAL_SEARCH_DELIMITER
123112
end

lib/ajax-datatables-rails/config.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def self.config
2222
class Configuration
2323
include ActiveSupport::Configurable
2424

25-
config_accessor(:orm) { :active_record }
2625
config_accessor(:db_adapter) { :postgresql }
2726
config_accessor(:nulls_last) { false }
2827
end

lib/ajax_datatables_rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ module AjaxDatatablesRails
1212
require 'ajax-datatables-rails/datatable/column/date_filter'
1313
require 'ajax-datatables-rails/datatable/column'
1414
require 'ajax-datatables-rails/orm/active_record'
15+
require 'ajax-datatables-rails/active_record'
1516
end

lib/generators/datatable/templates/ajax_datatables_rails_config.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,4 @@
66

77
# Or you can use your rails environment adapter if you want a generic dev and production
88
# config.db_adapter = Rails.configuration.database_configuration[Rails.env]['adapter'].to_sym
9-
10-
# available options for orm are: :active_record, :mongoid
11-
# config.orm = :active_record
129
end

lib/generators/rails/templates/datatable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class <%= datatable_name %> < AjaxDatatablesRails::Base
1+
class <%= datatable_name %> < AjaxDatatablesRails::ActiveRecord
22

33
def view_columns
44
# Declare strings in this format: ModelName.column_name

spec/ajax-datatables-rails/base_spec.rb

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,63 @@
107107
end
108108
end
109109
end
110+
111+
describe '#filter_records' do
112+
let(:records) { User.all }
113+
114+
let(:datatable) do
115+
datatable = Class.new(ComplexDatatable) do
116+
def filter_records(records)
117+
raise NotImplementedError
118+
end
119+
end
120+
datatable.new(sample_params)
121+
end
122+
123+
it 'should allow method override' do
124+
expect { datatable.filter_records(records) }.to raise_error(NotImplementedError)
125+
end
126+
end
127+
128+
describe '#sort_records' do
129+
let(:records) { User.all }
130+
131+
let(:datatable) do
132+
datatable = Class.new(ComplexDatatable) do
133+
def sort_records(records)
134+
raise NotImplementedError
135+
end
136+
end
137+
datatable.new(sample_params)
138+
end
139+
140+
it 'should allow method override' do
141+
expect { datatable.sort_records(records) }.to raise_error(NotImplementedError)
142+
end
143+
end
144+
145+
describe '#paginate_records' do
146+
let(:records) { User.all }
147+
148+
let(:datatable) do
149+
datatable = Class.new(ComplexDatatable) do
150+
def paginate_records(records)
151+
raise NotImplementedError
152+
end
153+
end
154+
datatable.new(sample_params)
155+
end
156+
157+
it 'should allow method override' do
158+
expect { datatable.paginate_records(records) }.to raise_error(NotImplementedError)
159+
end
160+
end
110161
end
111162

112163

113164
context 'Private API' do
114165
context 'when orm is not implemented' do
115-
before do
116-
allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:orm) { nil }
117-
end
118-
119-
let(:datatable) { ComplexDatatable.new(sample_params) }
166+
let(:datatable) { AjaxDatatablesRails::Base.new(sample_params) }
120167

121168
describe '#fetch_records' do
122169
it 'raises an error if it does not include an ORM module' do

spec/ajax-datatables-rails/configuration_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020
let(:config) { AjaxDatatablesRails::Configuration.new }
2121

2222
describe 'default config' do
23-
it 'default orm should :active_record' do
24-
expect(config.orm).to eq(:active_record)
25-
end
26-
2723
it 'default db_adapter should :postgresql' do
2824
expect(config.db_adapter).to eq(:postgresql)
2925
end
@@ -34,10 +30,5 @@
3430
config.db_adapter = :mysql
3531
expect(config.db_adapter).to eq(:mysql)
3632
end
37-
38-
it 'accepts a custom orm value' do
39-
config.orm = :mongoid
40-
expect(config.orm).to eq(:mongoid)
41-
end
4233
end
4334
end

0 commit comments

Comments
 (0)