Created
June 2, 2016 23:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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