diff options
author | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-11 22:56:12 -0800 |
---|---|---|
committer | Nick Kallen <nkallen@nick-kallens-computer-2.local> | 2008-02-11 22:56:12 -0800 |
commit | 29d9b8e9b5899a8c52326dfca9343e79ba049d6b (patch) | |
tree | 4ca2605f94ca0d38d8aec1d689ec03713db54996 /lib | |
parent | 61d6c2c1c5215014971355892e024682ed148ebb (diff) | |
download | rails-29d9b8e9b5899a8c52326dfca9343e79ba049d6b.tar.gz rails-29d9b8e9b5899a8c52326dfca9343e79ba049d6b.tar.bz2 rails-29d9b8e9b5899a8c52326dfca9343e79ba049d6b.zip |
removing code complexity concerning attribute lookup.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/active_relation/primitives/attribute.rb | 10 | ||||
-rw-r--r-- | lib/active_relation/primitives/expression.rb | 3 | ||||
-rw-r--r-- | lib/active_relation/relations/compound.rb | 5 | ||||
-rw-r--r-- | lib/active_relation/relations/join.rb | 8 | ||||
-rw-r--r-- | lib/active_relation/relations/relation.rb | 2 | ||||
-rw-r--r-- | lib/active_relation/relations/rename.rb | 17 | ||||
-rw-r--r-- | lib/active_relation/relations/table.rb | 14 |
7 files changed, 12 insertions, 47 deletions
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index e6df5cd87c..893feefef2 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -5,6 +5,10 @@ module ActiveRelation def initialize(relation, name, aliaz = nil, ancestor = nil) @relation, @name, @alias, @ancestor = relation, name, aliaz, ancestor end + + def alias_or_name + @alias || name + end module Transformations def as(aliaz = nil) @@ -96,9 +100,11 @@ module ActiveRelation strategy.attribute prefix, name, self.alias end - private - delegate :hash, :to => :relation + def hash + relation.hash + name.hash + end + private def prefix relation.prefix_for(self) end diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb index a64219e9d1..ff07c40109 100644 --- a/lib/active_relation/primitives/expression.rb +++ b/lib/active_relation/primitives/expression.rb @@ -15,12 +15,10 @@ module ActiveRelation end def as(aliaz) - # key line -- note self Expression.new(attribute, function_sql, aliaz, self) end def to_attribute - # key line -- note self Attribute.new(relation, @alias, nil, self) end end @@ -39,7 +37,6 @@ module ActiveRelation !(history & other.send(:history)).empty? end - private def hash attribute.hash + function_sql.hash end diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 776620732d..befb7f26d2 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -8,10 +8,5 @@ module ActiveRelation def attributes relation.attributes.collect { |a| a.substitute(self) } end - - protected - def attribute_for_name(name) - relation[name].substitute(self) rescue nil - end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index 0327b5cd87..5b2efe727f 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -42,14 +42,6 @@ module ActiveRelation (relation2.send(:selects) unless relation2.aggregation?) ].compact.flatten end - - def attribute_for_name(name) - (relation1[name] || relation2[name]) - end - - def attribute_for_attribute(attribute) - (relation1[attribute] || relation2[attribute]) - end def table_sql relation1.aggregation?? relation1.to_sql(Sql::Aggregation.new) : relation1.send(:table_sql) diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index 0f770e674c..c5409bc9ff 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -109,7 +109,7 @@ module ActiveRelation end def attribute_for_name(name) - nil + attributes.detect { |a| a.alias_or_name == name } end def attribute_for_attribute(attribute) diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb index 8d92e9422c..c41cd1bdc5 100644 --- a/lib/active_relation/relations/rename.rb +++ b/lib/active_relation/relations/rename.rb @@ -19,26 +19,9 @@ module ActiveRelation relation.attributes.collect(&method(:substitute)) end - protected - def attribute_for_name(name) - case - when referring_by_autonym?(name) then nil - when referring_by_pseudonym?(name) then substitute(relation[attribute]) - else relation[name].substitute(self) rescue nil - end - end - private def substitute(attribute) (attribute =~ self.attribute ? attribute.as(pseudonym) : attribute).substitute(self) rescue nil end - - def referring_by_autonym?(name) - relation[name] == relation[attribute] - end - - def referring_by_pseudonym?(name) - name == pseudonym - end end end
\ No newline at end of file diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index 9e58404a51..d6ab45dcea 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -7,7 +7,9 @@ module ActiveRelation end def attributes - attributes_by_name.values + @attributes ||= connection.columns(name, "#{name} Columns").collect do |column| + Attribute.new(self, column.name.to_sym) + end end def qualify @@ -23,21 +25,11 @@ module ActiveRelation end protected - def attribute_for_name(name) - attributes_by_name[name.to_s] - end - def table_sql "#{quote_table_name(name)}" end private - def attributes_by_name - @attributes_by_name ||= connection.columns(name, "#{name} Columns").inject({}) do |attributes_by_name, column| - attributes_by_name.merge(column.name => Attribute.new(self, column.name.to_sym)) - end - end - def qualifications attributes.zip(attributes.collect(&:qualified_name)).to_hash end |