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

string passthrough for joins

This commit is contained in:
Nick Kallen 2008-04-11 15:22:22 -07:00
parent ad50e772d3
commit eee3a76616
9 changed files with 32 additions and 23 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
coverage/* coverage/*
*.DS_Store

View file

@ -1,8 +1,5 @@
todo: todo:
- re-evaluate bind -- does bind belong inside the relation / predicate classes or in the factory methods? - mock out database
- - mock out database
- #bind in Attribute and Expression should be doing a descend?
- try to make aggegration testing in join spec to be a bit more unit-like
- finish pending tests - finish pending tests
- test relation, table reset - test relation, table reset
- standardize quoting - standardize quoting
@ -14,12 +11,6 @@ todo:
:joins=>"INNER JOIN posts ON comments.post_id = posts.id" :joins=>"INNER JOIN posts ON comments.post_id = posts.id"
- shit this one is hard at the moment. - shit this one is hard at the moment.
- need adapters for this form:
{:conditions=>["approved = ?", false]}
{:conditions=>{:approved=>false}}
{:conditions=>{"topics.approved"=>false}}
{:conditions=>{:address=>#<Address:0x3489b3c @street="Funny Street", @country="Loony Land", @city="Scary Town">, "customers.name"=>"David1"}}
- cache expiry on write - cache expiry on write
- rewrite of querycache test in light of this - rewrite of querycache test in light of this
- rename the tion (Selection) classes so that words that don't end in tion don't seem inconsistent - rename the tion (Selection) classes so that words that don't end in tion don't seem inconsistent
@ -67,3 +58,8 @@ done:
- orderings - orderings
- relation inclusion when given an array (1,2,3,4) should quote the elements using the appropriate quoting formatter taken from the attribute - relation inclusion when given an array (1,2,3,4) should quote the elements using the appropriate quoting formatter taken from the attribute
- descend on array, along with bind written in terms of it - descend on array, along with bind written in terms of it
- re-evaluate bind -- does bind belong inside the relation / predicate classes or in the factory methods?
icebox:
- #bind in Attribute and Expression should be doing a descend?
- try to make aggegration testing in join spec to be a bit more unit-like

View file

@ -4,7 +4,7 @@ module ActiveRelation
delegate :engine, :to => :relation delegate :engine, :to => :relation
def initialize(relation, name, options = {}) def initialize(relation, name, options = {})
@relation, @name, @alias, @ancestor, @column = relation, name, options[:alias], options[:ancestor] @relation, @name, @alias, @ancestor = relation, name, options[:alias], options[:ancestor]
end end
def alias_or_name def alias_or_name

View file

@ -1,4 +1,5 @@
require 'active_relation/relations/relation' require 'active_relation/relations/relation'
require 'active_relation/relations/nil'
require 'active_relation/relations/compound' require 'active_relation/relations/compound'
require 'active_relation/relations/writing' require 'active_relation/relations/writing'
require 'active_relation/relations/table' require 'active_relation/relations/table'

View file

@ -6,7 +6,7 @@ module ActiveRelation
hash_on :relation1 hash_on :relation1
def initialize(join_sql, relation1, relation2, *predicates) def initialize(join_sql, relation1, relation2 = Nil.new, *predicates)
@join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates
end end
@ -40,9 +40,9 @@ module ActiveRelation
this_join = [ this_join = [
join_sql, join_sql,
externalize(relation2).table_sql, externalize(relation2).table_sql,
"ON", ("ON" unless predicates.blank?),
predicates.collect { |p| p.bind(self).to_sql }.join(' AND ') predicates.collect { |p| p.bind(self).to_sql }.join(' AND ')
].join(" ") ].compact.join(" ")
[relation1.joins, relation2.joins, this_join].compact.join(" ") [relation1.joins, relation2.joins, this_join].compact.join(" ")
end end

View file

@ -0,0 +1,5 @@
module ActiveRelation
class Nil < Relation
def table_sql; '' end
end
end

View file

@ -45,7 +45,7 @@ module ActiveRelation
end end
def table_sql def table_sql
"#{engine.quote_table_name(name)}" engine.quote_table_name(name)
end end
private private

View file

@ -108,7 +108,7 @@ module ActiveRelation
end end
end end
describe 'with aggregated relations' do describe 'when joining aggregated relations' do
before do before do
@aggregation = @relation2 \ @aggregation = @relation2 \
.aggregate(@relation2[:user_id], @relation2[:id].count) \ .aggregate(@relation2[:user_id], @relation2[:id].count) \
@ -158,5 +158,15 @@ module ActiveRelation
end end
end end
end end
describe 'when joining with a string' do
it "passes the string through to the where clause" do
Join.new("INNER JOIN ON asdf", @relation1).to_sql.should be_like("
SELECT `users`.`id`, `users`.`name`
FROM `users`
INNER JOIN ON asdf
")
end
end
end end
end end

View file

@ -48,12 +48,8 @@ module ActiveRelation
end end
describe 'when given a string' do describe 'when given a string' do
before do
@string = "asdf"
end
it "passes the string through to the where clause" do it "passes the string through to the where clause" do
Selection.new(@relation, @string).to_sql.should be_like(" Selection.new(@relation, 'asdf').to_sql.should be_like("
SELECT `users`.`id`, `users`.`name` SELECT `users`.`id`, `users`.`name`
FROM `users` FROM `users`
WHERE asdf WHERE asdf