@@ -57,41 +57,28 @@ def filter_string_not_eql(scope, attribute, value)
5757 filter_string_eql ( scope , attribute , value , is_not : true )
5858 end
5959
60- def filter_string_prefix ( scope , attribute , value , is_not : false )
61- column = column_for ( scope , attribute )
62- map = value . map { |v | "#{ v } %" }
63- clause = column . lower . matches_any ( map )
64- is_not ? scope . where . not ( clause ) : scope . where ( clause )
65- end
66-
67- def filter_string_not_prefix ( scope , attribute , value )
68- filter_string_prefix ( scope , attribute , value , is_not : true )
69- end
70-
71- def filter_string_suffix ( scope , attribute , value , is_not : false )
72- column = column_for ( scope , attribute )
73- map = value . map { |v | "%#{ v } " }
74- clause = column . lower . matches_any ( map )
75- is_not ? scope . where . not ( clause ) : scope . where ( clause )
76- end
77-
78- def filter_string_not_suffix ( scope , attribute , value )
79- filter_string_suffix ( scope , attribute , value , is_not : true )
80- end
81-
8260 # Arel has different match escaping behavior before rails 5.
8361 # Since rails 4.x does not expose methods to escape LIKE statements
8462 # anyway, we just don't support proper LIKE escaping in those versions.
8563 if ::ActiveRecord . version >= Gem ::Version . new ( "5.0.0" )
8664 def filter_string_match ( scope , attribute , value , is_not : false )
87- escape_char = '\\'
88- column = column_for ( scope , attribute )
89- map = value . map { |v |
90- v = v . downcase
91- v = scope . sanitize_sql_like ( v )
65+ clause = sanitized_like_for ( scope , attribute , value ) do |v |
9266 "%#{ v } %"
93- }
94- clause = column . lower . matches_any ( map , escape_char , true )
67+ end
68+ is_not ? scope . where . not ( clause ) : scope . where ( clause )
69+ end
70+
71+ def filter_string_prefix ( scope , attribute , value , is_not : false )
72+ clause = sanitized_like_for ( scope , attribute , value ) do |v |
73+ "#{ v } %"
74+ end
75+ is_not ? scope . where . not ( clause ) : scope . where ( clause )
76+ end
77+
78+ def filter_string_suffix ( scope , attribute , value , is_not : false )
79+ clause = sanitized_like_for ( scope , attribute , value ) do |v |
80+ "%#{ v } "
81+ end
9582 is_not ? scope . where . not ( clause ) : scope . where ( clause )
9683 end
9784 else
@@ -103,6 +90,28 @@ def filter_string_match(scope, attribute, value, is_not: false)
10390 clause = column . lower . matches_any ( map )
10491 is_not ? scope . where . not ( clause ) : scope . where ( clause )
10592 end
93+
94+ def filter_string_prefix ( scope , attribute , value , is_not : false )
95+ column = column_for ( scope , attribute )
96+ map = value . map { |v | "#{ v } %" }
97+ clause = column . lower . matches_any ( map )
98+ is_not ? scope . where . not ( clause ) : scope . where ( clause )
99+ end
100+
101+ def filter_string_suffix ( scope , attribute , value , is_not : false )
102+ column = column_for ( scope , attribute )
103+ map = value . map { |v | "%#{ v } " }
104+ clause = column . lower . matches_any ( map )
105+ is_not ? scope . where . not ( clause ) : scope . where ( clause )
106+ end
107+ end
108+
109+ def filter_string_not_prefix ( scope , attribute , value )
110+ filter_string_prefix ( scope , attribute , value , is_not : true )
111+ end
112+
113+ def filter_string_not_suffix ( scope , attribute , value )
114+ filter_string_suffix ( scope , attribute , value , is_not : true )
106115 end
107116
108117 def filter_string_not_match ( scope , attribute , value )
@@ -298,6 +307,22 @@ def column_for(scope, name)
298307 table [ name ]
299308 end
300309 end
310+
311+ def sanitized_like_for ( scope , attribute , value , &block )
312+ escape_char = '\\'
313+ column = column_for ( scope , attribute )
314+ map = value . map do |v |
315+ v = v . downcase
316+ v = Sanitizer . sanitize_sql_like ( v )
317+ block . call v
318+ end
319+
320+ column . lower . matches_any ( map , escape_char , true )
321+ end
322+
323+ class Sanitizer
324+ extend ::ActiveRecord ::Sanitization ::ClassMethods
325+ end
301326 end
302327 end
303328end
0 commit comments