From ff45b9e9f7c4ff0fb4fdab8beb539913b876d63b Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Wed, 28 Jan 2015 14:04:26 -0700 Subject: Bring the implementation of Relation#or up to speed --- activerecord/lib/active_record/null_relation.rb | 6 +-- .../lib/active_record/relation/query_methods.rb | 44 ++++------------------ .../lib/active_record/relation/where_clause.rb | 13 +++++++ 3 files changed, 22 insertions(+), 41 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/null_relation.rb b/activerecord/lib/active_record/null_relation.rb index 802adca908..63ca29305d 100644 --- a/activerecord/lib/active_record/null_relation.rb +++ b/activerecord/lib/active_record/null_relation.rb @@ -77,11 +77,7 @@ module ActiveRecord end def or(other) - if other.is_a?(NullRelation) - super - else - other.or(self) - end + other.spawn end end end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 78ee8b4580..b0edb3b1f2 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -596,50 +596,22 @@ module ActiveRecord spawn.or!(other) end - def or!(other) - combining = group_values.any? ? :having : :where - - unless structurally_compatible?(other, combining) + def or!(other) # :nodoc: + unless structurally_compatible_for_or?(other) raise ArgumentError, 'Relation passed to #or must be structurally compatible' end - unless other.is_a?(NullRelation) - left_values = send("#{combining}_values") - right_values = other.send("#{combining}_values") - - common = left_values & right_values - mine = left_values - common - theirs = right_values - common - - if mine.any? && theirs.any? - mine = mine.map { |x| String === x ? Arel.sql(x) : x } - theirs = theirs.map { |x| String === x ? Arel.sql(x) : x } - - mine = [Arel::Nodes::And.new(mine)] if mine.size > 1 - theirs = [Arel::Nodes::And.new(theirs)] if theirs.size > 1 - - common << Arel::Nodes::Or.new(mine.first, theirs.first) - end - - send("#{combining}_values=", common) - end + self.where_clause = self.where_clause.or(other.where_clause) + self.having_clause = self.having_clause.or(other.having_clause) self end - def structurally_compatible?(other, allowed_to_vary) - Relation::SINGLE_VALUE_METHODS.all? do |name| - send("#{name}_value") == other.send("#{name}_value") - end && - (Relation::MULTI_VALUE_METHODS - [allowed_to_vary, :extending]).all? do |name| - send("#{name}_values") == other.send("#{name}_values") - end && - (extending_values - [NullRelation]) == (other.extending_values - [NullRelation]) && - !limit_value && - !offset_value && - !uniq_value + private def structurally_compatible_for_or?(other) # :nodoc: + Relation::SINGLE_VALUE_METHODS.all? { |m| send("#{m}_value") == other.send("#{m}_value") } && + (Relation::MULTI_VALUE_METHODS - [:extending]).all? { |m| send("#{m}_values") == other.send("#{m}_values") } && + (Relation::CLAUSE_METHODS - [:having, :where]).all? { |m| send("#{m}_clause") != other.send("#{m}_clause") } end - private :structurally_compatible? # Allows to specify a HAVING clause. Note that you can't use HAVING # without also specifying a GROUP clause. diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index ae5667dfd6..ce307e4f0c 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -31,6 +31,19 @@ module ActiveRecord ) end + def or(other) + if empty? + other + elsif other.empty? + self + else + WhereClause.new( + [ast.or(other.ast)], + binds + other.binds + ) + end + end + def to_h(table_name = nil) equalities = predicates.grep(Arel::Nodes::Equality) if table_name -- cgit v1.2.3