File tree Expand file tree Collapse file tree 2 files changed +52
-12
lines changed
lib/ajax-datatables-rails
spec/ajax-datatables-rails Expand file tree Collapse file tree 2 files changed +52
-12
lines changed Original file line number Diff line number Diff line change @@ -106,17 +106,6 @@ def search_condition(column, value)
106
106
model , column = column . split ( '.' )
107
107
model = model . singularize . titleize . gsub ( / / , '' ) . constantize
108
108
109
- # because postgresql is preferred, so that's why use VARCHAR
110
- # but we we need to use CHAR typecast on mysql db adapter
111
- # or maybe it should
112
- # if :pg
113
- # elsif :mysql
114
- # else
115
- if config . db_adapter == :pg
116
- typecast = 'VARCHAR'
117
- else
118
- typecast = 'CHAR'
119
- end
120
109
casted_column = ::Arel ::Nodes ::NamedFunction . new ( 'CAST' , [ model . arel_table [ column . to_sym ] . as ( typecast ) ] )
121
110
casted_column . matches ( "%#{ value } %" )
122
111
end
@@ -129,6 +118,14 @@ def aggregate_query
129
118
conditions . compact . reduce ( :and )
130
119
end
131
120
121
+ def typecast
122
+ case config . db_adapter
123
+ when :pg then 'VARCHAR'
124
+ when :mysql2 then 'CHAR'
125
+ when :sqlite3 then 'TEXT'
126
+ end
127
+ end
128
+
132
129
def offset
133
130
( page - 1 ) * per_page
134
131
end
Original file line number Diff line number Diff line change @@ -238,6 +238,49 @@ def self.arel_table
238
238
expect ( config . db_adapter ) . to eq ( :mysql2 )
239
239
end
240
240
end
241
+
242
+ describe '#typecast' do
243
+ params = {
244
+ :draw => '5' ,
245
+ :columns => {
246
+ "0" => {
247
+ :data => '0' ,
248
+ :name => '' ,
249
+ :searchable => true ,
250
+ :orderable => true ,
251
+ :search => { :value => '' , :regex => false }
252
+ } ,
253
+ "1" => {
254
+ :data => '1' ,
255
+ :name => '' ,
256
+ :searchable => true ,
257
+ :orderable => true ,
258
+ :search => { :value => '' , :regex => false }
259
+ }
260
+ } ,
261
+ :order => { "0" => { :column => '1' , :dir => 'desc' } } ,
262
+ :start => '0' ,
263
+ :length => '10' ,
264
+ :search => { :value => '' , :regex => false } ,
265
+ '_' => '1403141483098'
266
+ }
267
+ let ( :view ) { double ( 'view' , :params => params ) }
268
+ let ( :datatable ) { AjaxDatatablesRails ::Base . new ( view ) }
269
+
270
+ it 'returns VARCHAR if :db_adapter is :pg' do
271
+ expect ( datatable . send ( :typecast ) ) . to eq ( 'VARCHAR' )
272
+ end
273
+
274
+ it 'returns CHAR if :db_adapter is :mysql2' do
275
+ allow_any_instance_of ( AjaxDatatablesRails ::Configuration ) . to receive ( :db_adapter ) { :mysql2 }
276
+ expect ( datatable . send ( :typecast ) ) . to eq ( 'CHAR' )
277
+ end
278
+
279
+ it 'returns TEXT if :db_adapter is :sqlite3' do
280
+ allow_any_instance_of ( AjaxDatatablesRails ::Configuration ) . to receive ( :db_adapter ) { :sqlite3 }
281
+ expect ( datatable . send ( :typecast ) ) . to eq ( 'TEXT' )
282
+ end
283
+ end
241
284
end
242
285
243
286
describe AjaxDatatablesRails do
@@ -255,4 +298,4 @@ def self.arel_table
255
298
end
256
299
257
300
end
258
- end
301
+ end
You can’t perform that action at this time.
0 commit comments