aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/where_clause.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-25 17:06:13 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-25 17:06:13 -0700
commit2ae49dd073a8037ef7ea69e7b1f5507ff514a051 (patch)
tree668077cd4a39b96b50853444756a2a5e886872e2 /activerecord/lib/active_record/relation/where_clause.rb
parent2da8f2154b2f4c6beac5e50225742beb3caea996 (diff)
downloadrails-2ae49dd073a8037ef7ea69e7b1f5507ff514a051.tar.gz
rails-2ae49dd073a8037ef7ea69e7b1f5507ff514a051.tar.bz2
rails-2ae49dd073a8037ef7ea69e7b1f5507ff514a051.zip
Move `where.not` logic into `WhereClause`
Diffstat (limited to 'activerecord/lib/active_record/relation/where_clause.rb')
-rw-r--r--activerecord/lib/active_record/relation/where_clause.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb
index d1469d0a7a..676b0d47d3 100644
--- a/activerecord/lib/active_record/relation/where_clause.rb
+++ b/activerecord/lib/active_record/relation/where_clause.rb
@@ -30,6 +30,10 @@ module ActiveRecord
binds == other.binds
end
+ def invert
+ WhereClause.new(inverted_parts, binds)
+ end
+
def self.empty
new([], [])
end
@@ -60,6 +64,25 @@ module ActiveRecord
conflicts.map! { |node| node.name.to_s }
binds.reject { |col, _| conflicts.include?(col.name) }
end
+
+ def inverted_parts
+ parts.map { |node| invert_predicate(node) }
+ end
+
+ def invert_predicate(node)
+ case node
+ when NilClass
+ raise ArgumentError, 'Invalid argument for .where.not(), got nil.'
+ when Arel::Nodes::In
+ Arel::Nodes::NotIn.new(node.left, node.right)
+ when Arel::Nodes::Equality
+ Arel::Nodes::NotEqual.new(node.left, node.right)
+ when String
+ Arel::Nodes::Not.new(Arel::Nodes::SqlLiteral.new(node))
+ else
+ Arel::Nodes::Not.new(node)
+ end
+ end
end
end
end