Skip to content

Commit 9a92bbb

Browse files
committed
Extract method, add tests
1 parent e742eb6 commit 9a92bbb

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/ajax-datatables-rails/datatable/column.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Column
1010
sqlite: 'TEXT',
1111
sqlite3: 'TEXT',
1212
oracle: 'VARCHAR2(4000)',
13-
oracleenhanced: 'VARCHAR2(4000)',
13+
oracleenhanced: 'VARCHAR2(4000)'
1414
}.freeze
1515

1616
attr_reader :datatable, :index, :options
@@ -38,21 +38,24 @@ def source
3838
@view_column[:source]
3939
end
4040

41-
# Add formater option to allow modification of the value
42-
# before passing it to the database
43-
def formater
44-
@view_column[:formater]
41+
def table
42+
model.respond_to?(:arel_table) ? model.arel_table : model
4543
end
4644

47-
def table
48-
model = source.split('.').first.constantize
49-
model.arel_table rescue model
45+
def model
46+
source.split('.').first.constantize
5047
end
5148

5249
def field
5350
source.split('.').last.to_sym
5451
end
5552

53+
# Add formater option to allow modification of the value
54+
# before passing it to the database
55+
def formater
56+
@view_column[:formater]
57+
end
58+
5659
def formated_value
5760
formater ? formater.call(search.value) : search.value
5861
end

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,44 @@
2929
expect(column.data).to eq('username')
3030
end
3131

32+
describe 'data' do
33+
it 'should return the data from params' do
34+
expect(column.data).to eq 'username'
35+
end
36+
end
37+
38+
describe 'source' do
39+
it 'should return the data source from view_column' do
40+
expect(column.source).to eq 'User.username'
41+
end
42+
end
43+
44+
describe 'table' do
45+
context 'with ActiveRecord ORM' do
46+
it 'should return the corresponding AR table' do
47+
expect(column.table).to eq User.arel_table
48+
end
49+
end
50+
context 'with other ORM' do
51+
it 'should return the corresponding model' do
52+
expect(User).to receive(:respond_to?).with(:arel_table).and_return(false)
53+
expect(column.table).to eq User
54+
end
55+
end
56+
end
57+
58+
describe 'model' do
59+
it 'should return the corresponding AR model' do
60+
expect(column.model).to eq User
61+
end
62+
end
63+
64+
describe 'field' do
65+
it 'should return the corresponding field in DB' do
66+
expect(column.field).to eq :username
67+
end
68+
end
69+
3270
describe '#search' do
3371
it 'child class' do
3472
expect(column.search).to be_a(AjaxDatatablesRails::Datatable::SimpleSearch)

0 commit comments

Comments
 (0)