Skip to content

Instantly share code, notes, and snippets.

@byllc
Created January 27, 2011 14:25
Show Gist options
  • Save byllc/798563 to your computer and use it in GitHub Desktop.
Save byllc/798563 to your computer and use it in GitHub Desktop.
#split the query data for each of hte query data records attached to this filter
def parse
or_match = /\s+or\s+/i
and_group = /\s+and\s+/i
not_group = /^\s*not\s+/i
quote_group = /"\w+"/i
or_pieces = []
query_values = []
or_split = query_string.split(or_match)
or_split.each do |query_group|
raw_values = query_group.split(and_group)
or_pieces << raw_values.map do |val|
operator = if ((val =~ not_group) == 0)
"NOT ILIKE"
else
"ILIKE"
end
"#{filter_field.query_operand} #{operator} ?"
end.join(" AND ")
query_values << raw_values.map do |g|
g.gsub!(not_group,"")
g.match(quote_group).present? ? g.gsub("\"","") : "%#{g}%"
end
end
["(#{or_pieces.join(") OR (")})"] + query_values.flatten
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment