From ae217d5816c766443c6e6737d04073f40614b564 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 18 Feb 2008 20:20:51 -0800 Subject: extracted conditionals concerning the "externalizing" of relations under a join. --- lib/active_relation/relations/alias.rb | 4 +-- lib/active_relation/relations/compound.rb | 2 +- lib/active_relation/relations/join.rb | 51 ++++++++++++++++++++++--------- lib/active_relation/relations/relation.rb | 4 +++ lib/active_relation/relations/table.rb | 1 - 5 files changed, 44 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/active_relation/relations/alias.rb b/lib/active_relation/relations/alias.rb index 0bfa895951..f24b1d743c 100644 --- a/lib/active_relation/relations/alias.rb +++ b/lib/active_relation/relations/alias.rb @@ -6,8 +6,8 @@ module ActiveRelation @relation, @alias = relation, aliaz end - def aliased_prefix_for(attribute) - self[attribute] and @alias + def alias? + true end def descend(&block) diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 2839862987..094c6e29dd 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -2,7 +2,7 @@ module ActiveRelation class Compound < Relation attr_reader :relation delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, - :offset, :name, :alias, :aggregation?, :prefix_for, :aliased_prefix_for, + :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :to => :relation def attributes diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 850a773ee5..70c85bb1e0 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -19,17 +19,14 @@ module ActiveRelation end def attributes - [ - relation1.attributes.collect(&:to_attribute), - relation2.attributes.collect(&:to_attribute), - ].flatten.collect { |a| a.bind(self) } + (externalize(relation1).attributes + + externalize(relation2).attributes).collect { |a| a.bind(self) } end def prefix_for(attribute) - relation1.aliased_prefix_for(attribute) or - relation2.aliased_prefix_for(attribute) + externalize(relation1).prefix_for(attribute) or + externalize(relation2).prefix_for(attribute) end - alias_method :aliased_prefix_for, :prefix_for def descend(&block) Join.new(join_sql, relation1.descend(&block), relation2.descend(&block), *predicates.collect(&block)) @@ -37,20 +34,46 @@ module ActiveRelation protected def joins - right_table_sql = relation2.aggregation?? relation2.to_sql(Sql::Aggregation.new) : relation2.send(:table_sql) - this_join = [join_sql, right_table_sql, "ON", predicates.collect { |p| p.bind(self).to_sql(Sql::Predicate.new) }.join(' AND ')].join(" ") + this_join = [ + join_sql, + externalize(relation2).table_sql, + "ON", + predicates.collect { |p| p.bind(self).to_sql(Sql::Predicate.new) }.join(' AND ') + ].join(" ") [relation1.joins, relation2.joins, this_join].compact.join(" ") end def selects - [ - (relation1.send(:selects) unless relation1.aggregation?), - (relation2.send(:selects) unless relation2.aggregation?) - ].compact.flatten + externalize(relation1).selects + externalize(relation2).selects end def table_sql - relation1.aggregation?? relation1.to_sql(Sql::Aggregation.new) : relation1.send(:table_sql) + externalize(relation1).table_sql + end + + private + def externalize(relation) + Externalizer.new(relation) + end + + Externalizer = Struct.new(:relation) do + def table_sql + relation.aggregation?? relation.to_sql(Sql::Aggregation.new) : relation.send(:table_sql) + end + + def selects + relation.aggregation?? [] : relation.send(:selects) + end + + def attributes + relation.aggregation?? relation.attributes.collect(&:to_attribute) : relation.attributes + end + + def prefix_for(attribute) + if relation[attribute] + relation.alias?? relation.alias : relation.prefix_for(attribute) + end + end end end end \ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index e0ae882475..f20561c5b2 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -88,6 +88,10 @@ module ActiveRelation def aggregation? false end + + def alias? + false + end def to_sql(strategy = Sql::Select.new) strategy.select [ diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index 18c851c847..d0fc2b2475 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -19,7 +19,6 @@ module ActiveRelation def prefix_for(attribute) self[attribute] and name end - alias_method :aliased_prefix_for, :prefix_for protected def table_sql -- cgit v1.2.3