|
13 | 13 | allow_filter :first_name_prefix do |scope, value| |
14 | 14 | scope.where(['first_name like ?', "#{value}%"]) |
15 | 15 | end |
| 16 | + allow_filter :active |
16 | 17 | end |
17 | 18 | end |
18 | 19 |
|
|
26 | 27 | expect(scope.resolve.map(&:id)).to eq([author1.id]) |
27 | 28 | end |
28 | 29 |
|
| 30 | + context 'when filter is a "string nil"' do |
| 31 | + before do |
| 32 | + params[:filter] = { first_name: 'nil' } |
| 33 | + author2.update_attribute(:first_name, nil) |
| 34 | + end |
| 35 | + |
| 36 | + it 'converts to a real nil' do |
| 37 | + ids = scope.resolve.map(&:id) |
| 38 | + expect(ids).to eq([author2.id]) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + context 'when filter is a "string null"' do |
| 43 | + before do |
| 44 | + params[:filter] = { first_name: 'null' } |
| 45 | + author2.update_attribute(:first_name, nil) |
| 46 | + end |
| 47 | + |
| 48 | + it 'converts to a real nil' do |
| 49 | + ids = scope.resolve.map(&:id) |
| 50 | + expect(ids).to eq([author2.id]) |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context 'when filter is a "string boolean"' do |
| 55 | + before do |
| 56 | + params[:filter] = { active: 'true' } |
| 57 | + author2.update_attribute(:active, false) |
| 58 | + end |
| 59 | + |
| 60 | + it 'automatically casts to a real boolean' do |
| 61 | + ids = scope.resolve.map(&:id) |
| 62 | + expect(ids.length).to eq(3) |
| 63 | + expect(ids).to_not include(author2.id) |
| 64 | + end |
| 65 | + |
| 66 | + context 'and multiple are passed' do |
| 67 | + before do |
| 68 | + params[:filter] = { active: 'true,false' } |
| 69 | + end |
| 70 | + |
| 71 | + it 'still works' do |
| 72 | + ids = scope.resolve.map(&:id) |
| 73 | + expect(ids.length).to eq(4) |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + |
29 | 78 | context 'when filter is an integer' do |
30 | 79 | before do |
31 | 80 | params[:filter] = { id: author1.id } |
|
0 commit comments