@@ -79,11 +79,30 @@ def filter_string_not_suffix(scope, attribute, value)
7979 filter_string_suffix ( scope , attribute , value , is_not : true )
8080 end
8181
82- def filter_string_match ( scope , attribute , value , is_not : false )
83- column = column_for ( scope , attribute )
84- map = value . map { |v | "%#{ v . downcase } %" }
85- clause = column . lower . matches_any ( map )
86- is_not ? scope . where . not ( clause ) : scope . where ( clause )
82+ # Arel has different match escaping behavior before rails 5.
83+ # Since rails 4.x does not expose methods to escape LIKE statements
84+ # anyway, we just don't support proper LIKE escaping in those versions.
85+ if ::ActiveRecord . version >= Gem ::Version . new ( '5.0.0' )
86+ def filter_string_match ( scope , attribute , value , is_not : false )
87+ escape_char = '\\'
88+ column = column_for ( scope , attribute )
89+ map = value . map do |v |
90+ v = v . downcase
91+ v = scope . sanitize_sql_like ( v )
92+ "%#{ v } %"
93+ end
94+ clause = column . lower . matches_any ( map , escape_char , true )
95+ is_not ? scope . where . not ( clause ) : scope . where ( clause )
96+ end
97+ else
98+ def filter_string_match ( scope , attribute , value , is_not : false )
99+ column = column_for ( scope , attribute )
100+ map = value . map do |v |
101+ "%#{ v . downcase } %"
102+ end
103+ clause = column . lower . matches_any ( map )
104+ is_not ? scope . where . not ( clause ) : scope . where ( clause )
105+ end
87106 end
88107
89108 def filter_string_not_match ( scope , attribute , value )
0 commit comments