Skip to content

Instantly share code, notes, and snippets.

@avit
Created September 21, 2013 01:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avit/6646003 to your computer and use it in GitHub Desktop.
Save avit/6646003 to your computer and use it in GitHub Desktop.
Allow Ransack to include NULL values in "does not equal" searches.
module Arel
module Predications
def not_eq other
if other.is_a? Nodes::Not
Nodes::Equality.new self, other.value
else
Nodes::NotEqual.new self, other
end
end
def eq other
if other.is_a? Nodes::Not
Nodes::NotEqual.new self, other.value
else
Nodes::Equality.new self, other
end
end
def matches other
if other.is_a? Nodes::Not
Nodes::DoesNotMatch.new self, other.value
else
Nodes::Matches.new self, other
end
end
def does_not_match other
if other.is_a? Nodes::Not
Nodes::Matches.new self, other.value
else
Nodes::DoesNotMatch.new self, other
end
end
end
end
# Include NULL values in NOT queries
# https://github.com/ernie/ransack/issues/123
module Ransack
module Constants
HACKED_PREDICATES = [
['not_eq', {:arel_predicate => 'eq_any', :formatter => proc { |v| [Arel::Nodes::Not.new(v), nil] }}],
['does_not_match', {:arel_predicate => 'matches_any', :formatter => proc { |v| [Arel::Nodes::Not.new(v), nil] }}]
]
AREL_PREDICATES.delete('not_eq')
AREL_PREDICATES.delete('does_not_match')
DERIVED_PREDICATES.concat(HACKED_PREDICATES)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment