1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

extracted conditionals concerning the "externalizing" of relations under a join.

This commit is contained in:
Nick Kallen 2008-02-18 20:20:51 -08:00
parent 2a91d80753
commit ae217d5816
7 changed files with 46 additions and 33 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -88,6 +88,10 @@ module ActiveRelation
def aggregation?
false
end
def alias?
false
end
def to_sql(strategy = Sql::Select.new)
strategy.select [

View file

@ -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

View file

@ -7,16 +7,9 @@ module ActiveRelation
@alias_relation = Alias.new(@relation, :foo)
end
describe '#prefix_for' do
it "delegates to the underlying relation" do
@alias_relation.prefix_for(@relation[:id]).should == :users
end
end
describe '#aliased_prefix_for' do
describe '#alias' do
it "returns the alias" do
@alias_relation.aliased_prefix_for(@relation[:id]).should == :foo
@alias_relation.aliased_prefix_for(:does_not_exist).should be_nil
@alias_relation.alias.should == :foo
end
end
end

View file

@ -52,12 +52,6 @@ module ActiveRelation
end
end
describe '#aliased_prefix_for' do
it "returns the table name" do
@relation.aliased_prefix_for(@relation[:id]).should == :users
end
end
describe '#attributes' do
it 'manufactures attributes corresponding to columns in the table' do
@relation.attributes.should == [