From 89b354bf97d0a981376d36f2f8a7ba6a87fe2aa8 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 16 Mar 2008 20:47:02 -0700 Subject: added attribute.eq(nil) - produces attribute IS NULL --- TODO | 4 ++-- lib/active_relation/extensions.rb | 3 ++- lib/active_relation/extensions/array.rb | 2 +- lib/active_relation/extensions/nil_class.rb | 5 +++++ lib/active_relation/extensions/object.rb | 4 ++++ lib/active_relation/extensions/range.rb | 2 +- lib/active_relation/predicates.rb | 6 +++-- lib/active_relation/primitives/value.rb | 2 +- lib/active_relation/relations/relation.rb | 2 +- .../unit/predicates/equality_spec.rb | 26 ++++++++++++++++++++-- 10 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 lib/active_relation/extensions/nil_class.rb diff --git a/TODO b/TODO index 8b60e671a1..d7b57da4e2 100644 --- a/TODO +++ b/TODO @@ -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}} diff --git a/lib/active_relation/extensions.rb b/lib/active_relation/extensions.rb index 21d6724004..ded830eb8c 100644 --- a/lib/active_relation/extensions.rb +++ b/lib/active_relation/extensions.rb @@ -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' \ No newline at end of file +require 'active_relation/extensions/range' +require 'active_relation/extensions/nil_class' \ No newline at end of file diff --git a/lib/active_relation/extensions/array.rb b/lib/active_relation/extensions/array.rb index 2af5832707..793c06aad8 100644 --- a/lib/active_relation/extensions/array.rb +++ b/lib/active_relation/extensions/array.rb @@ -7,7 +7,7 @@ class Array "(" + collect { |e| e.to_sql(formatter) }.join(', ') + ")" end - def predicate_sql + def inclusion_predicate_sql "IN" end end \ No newline at end of file diff --git a/lib/active_relation/extensions/nil_class.rb b/lib/active_relation/extensions/nil_class.rb new file mode 100644 index 0000000000..729c4cada7 --- /dev/null +++ b/lib/active_relation/extensions/nil_class.rb @@ -0,0 +1,5 @@ +class NilClass + def equality_predicate_sql + 'IS' + end +end \ No newline at end of file diff --git a/lib/active_relation/extensions/object.rb b/lib/active_relation/extensions/object.rb index cd51543c91..25cef989a4 100644 --- a/lib/active_relation/extensions/object.rb +++ b/lib/active_relation/extensions/object.rb @@ -7,6 +7,10 @@ class Object formatter.scalar self end + def equality_predicate_sql + '=' + end + def metaclass class << self self diff --git a/lib/active_relation/extensions/range.rb b/lib/active_relation/extensions/range.rb index 0218a0ab44..d7329efe34 100644 --- a/lib/active_relation/extensions/range.rb +++ b/lib/active_relation/extensions/range.rb @@ -3,7 +3,7 @@ class Range formatter.range self.begin, self.end end - def predicate_sql + def inclusion_predicate_sql "BETWEEN" end end \ No newline at end of file diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index 6fcef3cfd6..22fbcd9f0b 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -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 \ No newline at end of file diff --git a/lib/active_relation/primitives/value.rb b/lib/active_relation/primitives/value.rb index aeee89dc3b..6f251d442b 100644 --- a/lib/active_relation/primitives/value.rb +++ b/lib/active_relation/primitives/value.rb @@ -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 diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 88d1bce4a8..db61fce3de 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -115,7 +115,7 @@ module ActiveRelation end alias_method :to_s, :to_sql - def predicate_sql + def inclusion_predicate_sql "IN" end diff --git a/spec/active_relation/unit/predicates/equality_spec.rb b/spec/active_relation/unit/predicates/equality_spec.rb index 499b13383d..613236ad04 100644 --- a/spec/active_relation/unit/predicates/equality_spec.rb +++ b/spec/active_relation/unit/predicates/equality_spec.rb @@ -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 \ No newline at end of file -- cgit v1.2.3