diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 17:30:42 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-25 17:30:42 -0700 |
commit | d6110799c2573080897410f9836f5aa623b197ea (patch) | |
tree | bfbfc983b3f8558d8dd85a042354c8eb59f333b9 /activerecord | |
parent | 7227e4fba17f4a50e53a5486a3b956a6c7c26697 (diff) | |
download | rails-d6110799c2573080897410f9836f5aa623b197ea.tar.gz rails-d6110799c2573080897410f9836f5aa623b197ea.tar.bz2 rails-d6110799c2573080897410f9836f5aa623b197ea.zip |
Move `where_unscoping` logic over to `WhereClause`
Diffstat (limited to 'activerecord')
3 files changed, 39 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 diff --git a/activerecord/test/cases/relation/where_clause_test.rb b/activerecord/test/cases/relation/where_clause_test.rb index 569082541b..6864be2608 100644 --- a/activerecord/test/cases/relation/where_clause_test.rb +++ b/activerecord/test/cases/relation/where_clause_test.rb @@ -97,6 +97,20 @@ class ActiveRecord::Relation assert_equal expected, original.invert end + test "accept removes binary predicates referencing a given column" do + where_clause = WhereClause.new([ + table["id"].in([1, 2, 3]), + table["name"].eq(bind_param), + table["age"].gteq(bind_param), + ], [ + [column("name"), "Sean"], + [column("age"), 30], + ]) + expected = WhereClause.new([table["age"].gteq(bind_param)], [[column("age"), 30]]) + + assert_equal expected, where_clause.except("id", "name") + end + private def table |