Skip to content

Instantly share code, notes, and snippets.

@antoniosb
Created June 2, 2016 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoniosb/bdc374a81c51ff48bf8b1eb3a165db4e to your computer and use it in GitHub Desktop.
Save antoniosb/bdc374a81c51ff48bf8b1eb3a165db4e to your computer and use it in GitHub Desktop.
Monkey patching Arel v3.0.2 to provide a predicate that uses binary like. So we can use it to create a Ransack predicate to use on search forms.
module Arel
module Visitors
class ToSql
private
def visit_Arel_Nodes_BinaryMatches o
"#{visit o.left} LIKE BINARY #{visit o.right}"
end
end
end
end
module Arel
module Visitors
class DepthFirst
alias :visit_Arel_Nodes_BinaryMatches :binary
end
end
end
module Arel
module Visitors
class Dot
alias :visit_Arel_Nodes_BinaryMatches :binary
end
end
end
module Arel
module Nodes
class BinaryMatches < Binary
end
end
end
module Arel
module Predications
def binary_matches other, escape = nil, case_sensitive = false
Nodes::BinaryMatches.new self, other
end
end
end
Ransack.configure { |c| c.add_predicate 'bin_cont', arel_predicate: 'binary_matches', formatter: proc { |v| "%#{v}%" }, compounds: false }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment