diff --git a/app/models/concerns/searchable_model.rb b/app/models/concerns/searchable_model.rb index e2f7daf34..20d80fed9 100644 --- a/app/models/concerns/searchable_model.rb +++ b/app/models/concerns/searchable_model.rb @@ -68,12 +68,13 @@ module SearchableModel end else unless attrs.empty? - id_index = 0 # quick fix to enable searching by repositoy_row id + # quick fix to enable searching by repositoy_row id + id_index = { present: false } where_str = (attrs.map.with_index do |a, i| if a == 'repository_rows.id' - id_index = i - "#{a} = :t#{i} OR " + id_index = { present: true, val: i } + "(#{a}) = :t#{i} OR " else "(trim_html_tags(#{a})) #{like} :t#{i} OR " end @@ -81,7 +82,7 @@ module SearchableModel ).join[0..-5] vals = ( attrs.map.with_index do |_, i| - if id_index == i + if id_index[:present] && id_index[:val] == i ["t#{i}".to_sym, sanitize_sql_like(query).to_i] else ["t#{i}".to_sym, "%#{sanitize_sql_like(query.to_s)}%"] diff --git a/spec/controllers/repository_rows_controller_spec.rb b/spec/controllers/repository_rows_controller_spec.rb index 0ee849e91..feb487cb0 100644 --- a/spec/controllers/repository_rows_controller_spec.rb +++ b/spec/controllers/repository_rows_controller_spec.rb @@ -62,7 +62,7 @@ describe RepositoryRowsController, type: :controller do describe 'json object' do it 'returns a valid object' do - params = { order: { 0 => { column: '3', dir: 'asc' } }, + params = { order: { 0 => { column: '4', dir: 'asc' } }, drow: '0', search: { value: '' }, length: '10', @@ -77,7 +77,7 @@ describe RepositoryRowsController, type: :controller do describe 'pagination' do it 'returns first 10 records' do - params = { order: { 0 => { column: '3', dir: 'asc' } }, + params = { order: { 0 => { column: '4', dir: 'asc' } }, drow: '0', search: { value: '' }, length: '10', @@ -86,11 +86,11 @@ describe RepositoryRowsController, type: :controller do get :index, params: params, format: :json response_body = JSON.parse(response.body) expect(response_body['data'].length).to eq 10 - expect(response_body['data'].first['2']).to eq 'row (0)' + expect(response_body['data'].first['3']).to eq 'row (0)' end it 'returns next 10 records' do - params = { order: { 0 => { column: '3', dir: 'asc' } }, + params = { order: { 0 => { column: '4', dir: 'asc' } }, drow: '0', search: { value: '' }, length: '10', @@ -99,11 +99,11 @@ describe RepositoryRowsController, type: :controller do get :index, params: params, format: :json response_body = JSON.parse(response.body) expect(response_body['data'].length).to eq 10 - expect(response_body['data'].first['2']).to eq 'row (10)' + expect(response_body['data'].first['3']).to eq 'row (10)' end it 'returns first 25 records' do - params = { order: { 0 => { column: '2', dir: 'desc' } }, + params = { order: { 0 => { column: '4', dir: 'desc' } }, drow: '0', search: { value: '' }, length: '25', diff --git a/spec/services/repository_datatable_service_spec.rb b/spec/services/repository_datatable_service_spec.rb index e32b13f19..2a5f9816a 100644 --- a/spec/services/repository_datatable_service_spec.rb +++ b/spec/services/repository_datatable_service_spec.rb @@ -52,7 +52,7 @@ describe RepositoryDatatableService do context 'object' do let(:params) do - { order: { 0 => { column: '2', dir: 'asc' } }, + { order: { 0 => { column: '3', dir: 'asc' } }, search: { value: 'row' } } end @@ -65,7 +65,7 @@ describe RepositoryDatatableService do contitions = subject.send(:build_conditions, params) expect(contitions[:search_value]).to eq 'row' expect(contitions[:order_by_column]).to eq( - { column: 2, dir: 'asc' } + { column: 3, dir: 'asc' } ) end end @@ -73,7 +73,7 @@ describe RepositoryDatatableService do describe '#sortable_columns' do it 'returns an array of all columns that are sortable' do columns = subject.send(:sortable_columns) - expect(columns.length).to eq 5 + expect(columns.length).to eq 6 end end @@ -92,7 +92,7 @@ describe RepositoryDatatableService do describe 'ordering' do it 'is ordered by row name asc' do - params = { order: { 0 => { column: '2', dir: 'asc' } }, + params = { order: { 0 => { column: '3', dir: 'asc' } }, search: { value: '' } } subject = RepositoryDatatableService.new(repository, params, @@ -102,7 +102,7 @@ describe RepositoryDatatableService do end it 'is ordered by row name desc' do - params = { order: { 0 => { column: '2', dir: 'desc' } }, + params = { order: { 0 => { column: '3', dir: 'desc' } }, search: { value: '' } } subject = RepositoryDatatableService.new(repository, params, @@ -121,7 +121,7 @@ describe RepositoryDatatableService do end it 'returns only the searched entity' do - params = { order: { 0 => { column: '2', dir: 'desc' } }, + params = { order: { 0 => { column: '4', dir: 'desc' } }, search: { value: 'test' } } subject = RepositoryDatatableService.new(repository, params, diff --git a/spec/support/api/schemas/repository_row_datatables.json b/spec/support/api/schemas/repository_row_datatables.json index 0a73d82f6..5c209e998 100644 --- a/spec/support/api/schemas/repository_row_datatables.json +++ b/spec/support/api/schemas/repository_row_datatables.json @@ -8,13 +8,14 @@ "data": { "type": "array", "items":{ - "required": ["DT_RowId", "1", "2", "3", "4", "recordEditUrl", "recordUpdateUrl", "recordInfoUrl"], + "required": ["DT_RowId", "1", "2", "3", "4", "5", "recordEditUrl", "recordUpdateUrl", "recordInfoUrl"], "properties": { "DT_RowId": { "type": "integer" }, "1": { "type": "string" }, - "2": { "type": "string" }, + "2": { "type": "integer" }, "3": { "type": "string" }, "4": { "type": "string" }, + "5": { "type": "string" }, "recordEditUrl": { "type": "string" }, "recordUpdateUrl": { "type": "string" }, "recordInfoUrl": { "type": "string" }