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

added attribute.eq(nil)

- produces attribute IS NULL
This commit is contained in:
Nick Kallen 2008-03-16 20:47:02 -07:00
parent 9771b2f3a3
commit 89b354bf97
10 changed files with 45 additions and 11 deletions

4
TODO
View file

@ -1,8 +1,8 @@
todo:
- string passthrough:
:joins=>"INNER JOIN posts ON comments.post_id = posts.id"
:select=>"`comments`.*"
- shit this one is hard at the moment.
- need adapters for this form:
{:conditions=>["approved = ?", false]}
{:conditions=>{:approved=>false}}

View file

@ -2,4 +2,5 @@ require 'active_relation/extensions/object'
require 'active_relation/extensions/class'
require 'active_relation/extensions/array'
require 'active_relation/extensions/hash'
require 'active_relation/extensions/range'
require 'active_relation/extensions/range'
require 'active_relation/extensions/nil_class'

View file

@ -7,7 +7,7 @@ class Array
"(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")"
end
def predicate_sql
def inclusion_predicate_sql
"IN"
end
end

View file

@ -0,0 +1,5 @@
class NilClass
def equality_predicate_sql
'IS'
end
end

View file

@ -7,6 +7,10 @@ class Object
formatter.scalar self
end
def equality_predicate_sql
'='
end
def metaclass
class << self
self

View file

@ -3,7 +3,7 @@ class Range
formatter.range self.begin, self.end
end
def predicate_sql
def inclusion_predicate_sql
"BETWEEN"
end
end

View file

@ -41,7 +41,7 @@ module ActiveRelation
end
def predicate_sql
'='
operand2.equality_predicate_sql
end
end
@ -74,6 +74,8 @@ module ActiveRelation
end
class In < Binary
delegate :predicate_sql, :to => :operand2
def predicate_sql
operand2.inclusion_predicate_sql
end
end
end

View file

@ -2,7 +2,7 @@ module ActiveRelation
class Value
attr_reader :value, :relation
delegate :predicate_sql, :to => :value
delegate :inclusion_predicate_sql, :equality_predicate_sql, :to => :value
def initialize(value, relation)
@value, @relation = value, relation

View file

@ -115,7 +115,7 @@ module ActiveRelation
end
alias_method :to_s, :to_sql
def predicate_sql
def inclusion_predicate_sql
"IN"
end

View file

@ -5,8 +5,8 @@ module ActiveRelation
before do
@relation1 = Table.new(:users)
@relation2 = Table.new(:photos)
@attribute1 = @relation1[:name]
@attribute2 = @relation2[:name]
@attribute1 = @relation1[:id]
@attribute2 = @relation2[:user_id]
end
describe '==' do
@ -23,5 +23,27 @@ module ActiveRelation
Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute2, @attribute1)
end
end
describe '#to_sql' do
describe 'when relating to a non-nil value' do
it "manufactures an equality predicate" do
Equality.new(@attribute1, @attribute2).to_sql.should be_like("
`users`.`id` = `photos`.`user_id`
")
end
end
describe 'when relation to a nil value' do
before do
@nil = nil.bind(@relation1)
end
it "manufactures an is null predicate" do
Equality.new(@attribute1, @nil).to_sql.should be_like("
`users`.`id` IS NULL
")
end
end
end
end
end