aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-01-25 17:30:42 -0700
committerSean Griffin <sean@thoughtbot.com>2015-01-25 17:30:42 -0700
commitd6110799c2573080897410f9836f5aa623b197ea (patch)
treebfbfc983b3f8558d8dd85a042354c8eb59f333b9 /activerecord/lib/active_record/relation
parent7227e4fba17f4a50e53a5486a3b956a6c7c26697 (diff)
downloadrails-d6110799c2573080897410f9836f5aa623b197ea.tar.gz
rails-d6110799c2573080897410f9836f5aa623b197ea.tar.bz2
rails-d6110799c2573080897410f9836f5aa623b197ea.zip
Move `where_unscoping` logic over to `WhereClause`
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb19
-rw-r--r--activerecord/lib/active_record/relation/where_clause.rb23
2 files changed, 25 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index c21730e6ae..1568064e18 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -410,9 +410,8 @@ module ActiveRecord
raise ArgumentError, "Hash arguments in .unscope(*args) must have :where as the key."
end
- Array(target_value).each do |val|
- where_unscoping(val)
- end
+ target_values = Array(target_value).map(&:to_s)
+ self.where_clause = where_clause.except(*target_values)
end
else
raise ArgumentError, "Unrecognized scoping: #{args.inspect}. Use .unscope(where: :attribute_name) or .unscope(:order), for example."
@@ -916,20 +915,6 @@ module ActiveRecord
self.send(unscope_code, result)
end
- def where_unscoping(target_value)
- target_value = target_value.to_s
-
- self.where_values = where_values.reject do |rel|
- case rel
- when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThanOrEqual
- subrelation = (rel.left.kind_of?(Arel::Attributes::Attribute) ? rel.left : rel.right)
- subrelation.name.to_s == target_value
- end
- end
-
- self.bind_values = bind_values.reject { |col,_| col.name == target_value }
- end
-
def custom_join_ast(table, joins)
joins = joins.reject(&:blank?)
diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb
index f03f95df0b..ca9c7d19a7 100644
--- a/activerecord/lib/active_record/relation/where_clause.rb
+++ b/activerecord/lib/active_record/relation/where_clause.rb
@@ -24,6 +24,13 @@ module ActiveRecord
)
end
+ def except(*columns)
+ WhereClause.new(
+ predicates_except(columns),
+ binds_except(columns),
+ )
+ end
+
def ==(other)
other.is_a?(WhereClause) &&
predicates == other.predicates &&
@@ -83,6 +90,22 @@ module ActiveRecord
Arel::Nodes::Not.new(node)
end
end
+
+ def predicates_except(columns)
+ predicates.reject do |node|
+ case node
+ when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThanOrEqual
+ subrelation = (node.left.kind_of?(Arel::Attributes::Attribute) ? node.left : node.right)
+ columns.include?(subrelation.name.to_s)
+ end
+ end
+ end
+
+ def binds_except(columns)
+ binds.reject do |column, _|
+ columns.include?(column.name)
+ end
+ end
end
end
end