diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-16 16:09:37 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-16 16:09:37 -0800 |
commit | d5d7a8eb9935cd937441bd5233b410e5a0cfd41b (patch) | |
tree | 6da1391c74af5b6d56f139453d8801754c7147df | |
parent | c910ac7554ad4989c8dd4e942fc14ae78eeddc3d (diff) | |
download | rails-d5d7a8eb9935cd937441bd5233b410e5a0cfd41b.tar.gz rails-d5d7a8eb9935cd937441bd5233b410e5a0cfd41b.tar.bz2 rails-d5d7a8eb9935cd937441bd5233b410e5a0cfd41b.zip |
attribute is now a concrete ancestor of expression. seems logical.
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 10 | ||||
-rw-r--r-- | lib/active_relation/primitives/expression.rb | 31 | ||||
-rw-r--r-- | spec/active_relation/primitives/attribute_spec.rb | 2 | ||||
-rw-r--r-- | spec/active_relation/primitives/expression_spec.rb | 6 |
4 files changed, 24 insertions, 25 deletions
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index de25a90868..4a10dffbe5 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -34,11 +34,11 @@ module ActiveRelation end def ==(other) - self.class == other.class and - relation == other.relation and - name == other.name and - @alias == other.alias and - ancestor == other.ancestor + self.class == other.class and + relation == other.relation and + name == other.name and + @alias == other.alias and + ancestor == other.ancestor end alias_method :eql?, :== diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb index 6ff06c7f81..4d28ef108c 100644 --- a/lib/active_relation/primitives/expression.rb +++ b/lib/active_relation/primitives/expression.rb @@ -1,23 +1,24 @@ module ActiveRelation - class Expression + class Expression < Attribute include Sql::Quoting - attr_reader :attribute, :function_sql, :alias, :ancestor + attr_reader :attribute, :function_sql delegate :relation, :to => :attribute + alias_method :name, :alias def initialize(attribute, function_sql, aliaz = nil, ancestor = nil) @attribute, @function_sql, @alias, @ancestor = attribute, function_sql, aliaz, ancestor end module Transformations - def bind(new_relation) - new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) - end - def as(aliaz) Expression.new(attribute, function_sql, aliaz, self) end + def bind(new_relation) + new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) + end + def to_attribute Attribute.new(relation, @alias, nil, self) end @@ -29,20 +30,12 @@ module ActiveRelation end def ==(other) - self.class == other.class and attribute == other.attribute and function_sql == other.function_sql and ancestor == other.ancestor and @alias == other.alias + self.class == other.class and + attribute == other.attribute and + function_sql == other.function_sql and + ancestor == other.ancestor and + @alias == other.alias end alias_method :eql?, :== - - def =~(other) - !(history & other.send(:history)).empty? - end - - def hash - attribute.hash + function_sql.hash - end - - def history - [self] + (ancestor ? [ancestor, ancestor.send(:history)].flatten : []) - end end end
\ No newline at end of file diff --git a/spec/active_relation/primitives/attribute_spec.rb b/spec/active_relation/primitives/attribute_spec.rb index 6d0020d5da..535e6245b5 100644 --- a/spec/active_relation/primitives/attribute_spec.rb +++ b/spec/active_relation/primitives/attribute_spec.rb @@ -29,7 +29,7 @@ module ActiveRelation end describe '#qualify' do - it "manufactures an attribute aliased with that attributes qualified name" do + it "manufactures an attribute aliased with that attribute's qualified name" do @attribute.qualify.should == Attribute.new(@attribute.relation, @attribute.name, @attribute.qualified_name, @attribute) end end diff --git a/spec/active_relation/primitives/expression_spec.rb b/spec/active_relation/primitives/expression_spec.rb index f90ae899c3..fa854d89f9 100644 --- a/spec/active_relation/primitives/expression_spec.rb +++ b/spec/active_relation/primitives/expression_spec.rb @@ -34,6 +34,12 @@ module ActiveRelation @expression.to_attribute.should == Attribute.new(@expression.relation, @expression.alias, nil, @expression) end end + + describe '#qualify' do + it "manufactures an expression aliased with that expression's qualified name" do + @expression.qualify.should == Expression.new(@attribute, "COUNT", @expression.qualified_name, @expression) + end + end end describe '=~' do |