11require 'spec_helper'
22
33describe AjaxDatatablesRails ::Base do
4- class Column
5- def matches ( query )
6- [ ]
7- end
8- end
9-
10- class User
11- def self . arel_table
12- { :foo => Column . new }
13- end
14- end
15-
16- class UserData
17- def self . arel_table
18- { :bar => Column . new }
19- end
20- end
214
225 params = {
236 :draw => '5' ,
@@ -103,9 +86,9 @@ def self.arel_table
10386 }
10487 )
10588 datatable = AjaxDatatablesRails ::Base . new ( sort_view )
106- datatable . stub ( :sortable_columns ) { [ 'foo' , 'bar' , 'baz' ] }
89+ allow ( datatable ) . to receive ( :sortable_columns ) { [ 'foo' , 'bar' , 'baz' ] }
10790
108- expect ( datatable . send ( :sort_column ) ) . to eq ( 'bar' )
91+ expect ( datatable . send ( :sort_column , sort_view . params [ :order ] [ "0" ] ) ) . to eq ( 'bar' )
10992 end
11093 end
11194
@@ -120,7 +103,7 @@ def self.arel_table
120103 }
121104 )
122105 datatable = AjaxDatatablesRails ::Base . new ( sorting_view )
123- expect ( datatable . send ( :sort_direction ) ) . to eq ( 'DESC' )
106+ expect ( datatable . send ( :sort_direction , sorting_view . params [ :order ] [ "0" ] ) ) . to eq ( 'DESC' )
124107 end
125108
126109 it 'can only be one option from ASC or DESC' do
@@ -133,7 +116,29 @@ def self.arel_table
133116 }
134117 )
135118 datatable = AjaxDatatablesRails ::Base . new ( sorting_view )
136- expect ( datatable . send ( :sort_direction ) ) . to eq ( 'ASC' )
119+ expect ( datatable . send ( :sort_direction , sorting_view . params [ :order ] [ "0" ] ) ) . to eq ( 'ASC' )
120+ end
121+ end
122+
123+ describe "#search_condition" do
124+ let ( :datatable ) { AjaxDatatablesRails ::Base . new ( view ) }
125+
126+ context "normal model" do
127+ it "should return arel object" do
128+ expect ( datatable . send ( :search_condition , 'users.bar' , 'bar' ) . class ) . to eq ( Arel ::Nodes ::Matches )
129+ end
130+ end
131+
132+ context "namespaced model" do
133+ it "should return arel object" do
134+ expect ( datatable . send ( :search_condition , 'statistics_sessions.bar' , 'bar' ) . class ) . to eq ( Arel ::Nodes ::Matches )
135+ end
136+ end
137+
138+ it "should raise uninitialized constant if column not exist" do
139+ expect {
140+ datatable . send ( :search_condition , 'asdusers.bar' , 'bar' ) . class
141+ } . to raise_error ( /uninitialized constant/ )
137142 end
138143 end
139144
@@ -167,20 +172,21 @@ def self.arel_table
167172
168173 describe '#sort_records' do
169174 it 'calls #order on a collection' do
170- results . should_receive ( :order )
175+ expect ( results ) . to receive ( :order )
171176 datatable . send ( :sort_records , results )
172177 end
173178 end
174179
175180 describe '#filter_records' do
176- let ( :records ) { double ( 'User' , :where => [ ] ) }
181+ let ( :records ) { double ( 'User' , :where => [ ] ) }
177182 let ( :search_view ) { double ( 'view' , :params => params ) }
178183
179184 it 'applies search like functionality on a collection' do
180185 datatable = AjaxDatatablesRails ::Base . new ( search_view )
181- datatable . stub ( :searchable_columns ) { [ 'users.foo' ] }
186+ allow ( datatable ) . to receive ( :searchable_columns ) { [ 'users.foo' ] }
182187
183- records . should_receive ( :where )
188+ expect ( records ) . to receive ( :where )
189+ records . where
184190 datatable . send ( :filter_records , records )
185191 end
186192 end
@@ -191,9 +197,10 @@ def self.arel_table
191197
192198 it 'applies search like functionality on a collection' do
193199 datatable = AjaxDatatablesRails ::Base . new ( search_view )
194- datatable . stub ( :searchable_columns ) { [ 'user_datas.bar' ] }
200+ allow ( datatable ) . to receive ( :searchable_columns ) { [ 'user_datas.bar' ] }
195201
196- records . should_receive ( :where )
202+ expect ( records ) . to receive ( :where )
203+ records . where
197204 datatable . send ( :filter_records , records )
198205 end
199206 end
0 commit comments