Permalink
Browse files

move helper class to the top

  • Loading branch information...
1 parent db99407 commit 01da3593939ba6037a00f8eb9a2051b4b6f6f325 @tenderlove tenderlove committed Oct 21, 2013
Showing with 42 additions and 42 deletions.
  1. +42 −42 activerecord/lib/active_record/associations/join_dependency.rb
@@ -4,6 +4,47 @@ class JoinDependency # :nodoc:
autoload :JoinBase, 'active_record/associations/join_dependency/join_base'
autoload :JoinAssociation, 'active_record/associations/join_dependency/join_association'
+ class Aliases # :nodoc:
+ def initialize(tables)
+ @tables = tables
+ @alias_cache = tables.each_with_object({}) { |table,h|
+ h[table.node] = table.columns.each_with_object({}) { |column,i|
+ i[column.name] = column.alias
+ }
+ }
+ @name_and_alias_cache = tables.each_with_object({}) { |table,h|
+ h[table.node] = table.columns.map { |column|
+ [column.name, column.alias]
+ }
+ }
+ end
+
+ def columns
+ @tables.flat_map { |t| t.column_aliases }
+ end
+
+ # An array of [column_name, alias] pairs for the table
+ def column_aliases(node)
+ @name_and_alias_cache[node]
+ end
+
+ def column_alias(node, column)
+ @alias_cache[node][column]
+ end
+
+ class Table < Struct.new(:node, :columns)
+ def table
+ Arel::Nodes::TableAlias.new node.table, node.aliased_table_name
+ end
+
+ def column_aliases
+ t = table
+ columns.map { |column| t[column.name].as Arel.sql column.alias }
+ end
+ end
+ Column = Struct.new(:name, :alias)
+ end
+
attr_reader :alias_tracker, :base_klass, :join_root
def self.make_tree(associations)
@@ -73,53 +114,12 @@ def join_constraints(outer_joins)
walk join_root, oj.join_root
else
oj.join_root.children.flat_map { |child|
- make_outer_joins(join_root, child)
+ make_outer_joins join_root, child
}
end
}
end
- class Aliases
- def initialize(tables)
- @tables = tables
- @alias_cache = tables.each_with_object({}) { |table,h|
- h[table.node] = table.columns.each_with_object({}) { |column,i|
- i[column.name] = column.alias
- }
- }
- @name_and_alias_cache = tables.each_with_object({}) { |table,h|
- h[table.node] = table.columns.map { |column|
- [column.name, column.alias]
- }
- }
- end
-
- def columns
- @tables.flat_map { |t| t.column_aliases }
- end
-
- # An array of [column_name, alias] pairs for the table
- def column_aliases(node)
- @name_and_alias_cache[node]
- end
-
- def column_alias(node, column)
- @alias_cache[node][column]
- end
-
- class Table < Struct.new(:node, :columns)
- def table
- Arel::Nodes::TableAlias.new node.table, node.aliased_table_name
- end
-
- def column_aliases
- t = table
- columns.map { |column| t[column.name].as Arel.sql column.alias }
- end
- end
- Column = Struct.new(:name, :alias)
- end
-
def aliases
Aliases.new join_root.each_with_index.map { |join_part,i|
columns = join_part.column_names.each_with_index.map { |column_name,j|

0 comments on commit 01da359

Please sign in to comment.