组织、项目、课程等在配置中添加成员时,对特殊字符'_'、‘/’添加转义,使能够搜索

This commit is contained in:
ouyangxuhua 2016-03-24 10:14:25 +08:00
parent 1579a86f9f
commit 3fc988757c
1 changed files with 3 additions and 3 deletions

View File

@ -42,9 +42,9 @@ class Principal < ActiveRecord::Base
if q.blank? if q.blank?
where({}) where({})
else else
pattern = "%#{q}%" pattern = "%#{q}%".gsub("/","//").gsub("_","/_")
# sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") # sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
sql= "LOWER(concat(lastname,firstname)) LIKE LOWER(:p) or LOWER(login) LIKE LOWER(:p) or LOWER(mail) LIKE LOWER(:p)" sql= "LOWER(concat(lastname,firstname)) LIKE LOWER(:p) or LOWER(login) LIKE LOWER(:p) or LOWER(mail) LIKE LOWER(:p) escape '/'"
params = {:p => pattern} params = {:p => pattern}
if q =~ /^(.+)\s+(.+)$/ if q =~ /^(.+)\s+(.+)$/
a, b = "#{$1}%", "#{$2}%" a, b = "#{$1}%", "#{$2}%"
@ -52,7 +52,7 @@ class Principal < ActiveRecord::Base
sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))" sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
params.merge!(:a => a, :b => b) params.merge!(:a => a, :b => b)
end end
where(sql, params) p where(sql, params)
end end
} }