File tree Expand file tree Collapse file tree 2 files changed +29
-5
lines changed
lib/ajax-datatables-rails/orm
spec/ajax-datatables-rails/orm Expand file tree Collapse file tree 2 files changed +29
-5
lines changed Original file line number Diff line number Diff line change @@ -26,17 +26,18 @@ def paginate_records(records)
2626 # ----------------- SEARCH HELPER METHODS --------------------
2727
2828 def build_conditions
29- if datatable . searchable?
30- build_conditions_for_datatable
31- else
32- build_conditions_for_selected_columns
29+ @criteria ||= begin
30+ criteria = [ build_conditions_for_selected_columns ]
31+ criteria << build_conditions_for_datatable if datatable . searchable?
32+ criteria . compact . reduce ( :and )
3333 end
3434 end
3535
3636 def build_conditions_for_datatable
37+ columns = searchable_columns . reject ( &:searched? )
3738 criteria = search_for . inject ( [ ] ) do |crit , atom |
3839 search = Datatable ::SimpleSearch . new ( value : atom , regex : datatable . search . regexp? )
39- crit << searchable_columns . map do |simple_column |
40+ crit << columns . map do |simple_column |
4041 simple_column . search = search
4142 simple_column . search_query
4243 end . compact . reduce ( :or )
Original file line number Diff line number Diff line change 2323 end
2424 end
2525
26+ describe '#build_conditions' do
27+ before ( :each ) do
28+ create ( :user , username : 'johndoe' , email : 'johndoe@example.com' )
29+ create ( :user , username : 'msmith' , email : 'mary.smith@example.com' )
30+ create ( :user , username : 'hsmith' , email : 'henry.smith@example.net' )
31+ end
32+
33+ context 'with column and global search' do
34+ before ( :each ) do
35+ datatable . params [ :search ] = { value : 'example.com' , regex : 'false' }
36+ datatable . params [ :columns ] [ '0' ] [ :search ] [ :value ] = 'smith'
37+ end
38+
39+ it 'return a filtered set of records' do
40+ query = datatable . build_conditions
41+ results = records . where ( query ) . map ( &:username )
42+ expect ( results ) . to include ( 'msmith' )
43+ expect ( results ) . not_to include ( 'johndoe' )
44+ expect ( results ) . not_to include ( 'hsmith' )
45+ end
46+ end
47+ end
48+
2649 describe '#build_conditions_for_datatable' do
2750 before ( :each ) do
2851 create ( :user , username : 'johndoe' , email : 'johndoe@example.com' )
You can’t perform that action at this time.
0 commit comments