aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-18 20:20:51 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-02-18 20:20:51 -0800
commitae217d5816c766443c6e6737d04073f40614b564 (patch)
tree70fbc3c84e612c1f580e108fc30f50264a172fbb /lib
parent2a91d807538a4d39c5755fb83a5e9462e8056fa6 (diff)
downloadrails-ae217d5816c766443c6e6737d04073f40614b564.tar.gz
rails-ae217d5816c766443c6e6737d04073f40614b564.tar.bz2
rails-ae217d5816c766443c6e6737d04073f40614b564.zip
extracted conditionals concerning the "externalizing" of relations under a join.
Diffstat (limited to 'lib')
-rw-r--r--lib/active_relation/relations/alias.rb4
-rw-r--r--lib/active_relation/relations/compound.rb2
-rw-r--r--lib/active_relation/relations/join.rb51
-rw-r--r--lib/active_relation/relations/relation.rb4
-rw-r--r--lib/active_relation/relations/table.rb1
5 files changed, 44 insertions, 18 deletions
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