From 23f8f35a2c7b44f1a26651b1f4bb9e51c17ca1d9 Mon Sep 17 00:00:00 2001 From: zmagod Date: Thu, 26 Apr 2018 11:05:02 +0200 Subject: [PATCH] a shotgun surgery that fixes searching/filtering of repository_rows [fixes SCI-2339] --- app/models/concerns/searchable_model.rb | 31 ++++++++++++++++---- app/services/repository_datatable_service.rb | 2 ++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/models/concerns/searchable_model.rb b/app/models/concerns/searchable_model.rb index 20d80fed9..859739037 100644 --- a/app/models/concerns/searchable_model.rb +++ b/app/models/concerns/searchable_model.rb @@ -32,15 +32,25 @@ module SearchableModel else a_query = Regexp.escape(query) end - a_query = '\\y(' + a_query + ')\\y' + # quick fix to enable searching by repositoy_row id + id_index = { present: false } where_str = (attrs.map.with_index do |a, i| - "(trim_html_tags(#{a})) #{like} :t#{i} OR " + if a == 'repository_rows.id' + id_index = { present: true, val: i } + "(#{a}) = :t#{i} OR " + else + "(trim_html_tags(#{a})) #{like} :t#{i} OR " + end end ).join[0..-5] vals = ( attrs.map.with_index do |_, i| - ["t#{i}".to_sym, a_query] + if id_index[:present] && id_index[:val] == i + ["t#{i}".to_sym, a_query.to_i] + else + ["t#{i}".to_sym, '\\y(' + a_query + ')\\y'] + end end ).to_h @@ -53,14 +63,25 @@ module SearchableModel if query.count(' ') > 0 unless attrs.empty? a_query = query.split.map { |a| "%#{sanitize_sql_like(a)}%" } + # quick fix to enable searching by repositoy_row id + id_index = { present: false } where_str = (attrs.map.with_index do |a, i| - "(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR " + if a == 'repository_rows.id' + id_index = { present: true, val: i } + "(#{a}) IN (:t#{i}) OR " + else + "(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR " + end end ).join[0..-5] vals = ( attrs.map.with_index do |_, i| - ["t#{i}".to_sym, a_query] + if id_index[:present] && id_index[:val] == i + ["t#{i}".to_sym, a_query.map(&:to_i)] + else + ["t#{i}".to_sym, a_query] + end end ).to_h diff --git a/app/services/repository_datatable_service.rb b/app/services/repository_datatable_service.rb index 892236e32..5106ded2c 100644 --- a/app/services/repository_datatable_service.rb +++ b/app/services/repository_datatable_service.rb @@ -124,6 +124,8 @@ class RepositoryDatatableService type = RepositoryColumn.find_by_id(id) return records unless type return select_type(type.data_type, records, id, dir) + elsif sortable_columns[column_id - 1] == 'users.full_name' + return records.joins(:created_by).order("users.full_name #{dir}") else return records.order( "#{sortable_columns[column_id - 1]} #{dir}"