Merge pull request #1408 from okriuchykhin/ok_SCI_2802

Search for IDs on inventory table as text [SCI-2802]
This commit is contained in:
Alex Kriuchykhin 2018-12-03 11:39:57 +01:00 committed by GitHub
commit 9621f0b372
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -37,13 +37,10 @@ module SearchableModel
a_query = Regexp.escape(query) a_query = Regexp.escape(query)
end end
# quick fix to enable searching by repositoy_row id
id_index = { present: false }
where_str = where_str =
(attrs.map.with_index do |a, i| (attrs.map.with_index do |a, i|
if a == 'repository_rows.id' if a == 'repository_rows.id'
id_index = { present: true, val: i } "CAST(#{a} AS TEXT) #{like} :t#{i} OR "
"(#{a}) = :t#{i} OR "
else else
col = options[:at_search].to_s == 'true' ? "lower(#{a})": a col = options[:at_search].to_s == 'true' ? "lower(#{a})": a
"(trim_html_tags(#{col})) #{like} :t#{i} OR " "(trim_html_tags(#{col})) #{like} :t#{i} OR "
@ -52,11 +49,7 @@ module SearchableModel
).join[0..-5] ).join[0..-5]
vals = ( vals = (
attrs.map.with_index do |_, i| attrs.map.with_index do |_, i|
if id_index[:present] && id_index[:val] == i ["t#{i}".to_sym, '\\y(' + a_query + ')\\y']
["t#{i}".to_sym, a_query.to_i]
else
["t#{i}".to_sym, '\\y(' + a_query + ')\\y']
end
end end
).to_h ).to_h
return where(where_str, vals) return where(where_str, vals)
@ -68,13 +61,10 @@ module SearchableModel
if query.count(' ') > 0 if query.count(' ') > 0
unless attrs.empty? unless attrs.empty?
a_query = query.split.map { |a| "%#{sanitize_sql_like(a)}%" } 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 = where_str =
(attrs.map.with_index do |a, i| (attrs.map.with_index do |a, i|
if a == 'repository_rows.id' if a == 'repository_rows.id'
id_index = { present: true, val: i } "CAST(#{a} AS TEXT) #{like} ANY (array[:t#{i}]) OR "
"(#{a}) IN (:t#{i}) OR "
else else
"(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR " "(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR "
end end
@ -82,11 +72,7 @@ module SearchableModel
).join[0..-5] ).join[0..-5]
vals = ( vals = (
attrs.map.with_index do |_, i| attrs.map.with_index do |_, i|
if id_index[:present] && id_index[:val] == i ["t#{i}".to_sym, a_query]
["t#{i}".to_sym, a_query.map(&:to_i)]
else
["t#{i}".to_sym, a_query]
end
end end
).to_h ).to_h
@ -94,13 +80,10 @@ module SearchableModel
end end
else else
unless attrs.empty? unless attrs.empty?
# quick fix to enable searching by repositoy_row id
id_index = { present: false }
where_str = where_str =
(attrs.map.with_index do |a, i| (attrs.map.with_index do |a, i|
if a == 'repository_rows.id' if a == 'repository_rows.id'
id_index = { present: true, val: i } "CAST(#{a} AS TEXT) #{like} :t#{i} OR "
"(#{a}) = :t#{i} OR "
else else
"(trim_html_tags(#{a})) #{like} :t#{i} OR " "(trim_html_tags(#{a})) #{like} :t#{i} OR "
end end
@ -108,11 +91,7 @@ module SearchableModel
).join[0..-5] ).join[0..-5]
vals = ( vals = (
attrs.map.with_index do |_, i| attrs.map.with_index do |_, i|
if id_index[:present] && id_index[:val] == i ["t#{i}".to_sym, "%#{sanitize_sql_like(query.to_s)}%"]
["t#{i}".to_sym, sanitize_sql_like(query).to_i]
else
["t#{i}".to_sym, "%#{sanitize_sql_like(query.to_s)}%"]
end
end end
).to_h ).to_h