Skip to content

Commit

Permalink
Pass a type caster when aliasing tables for joins
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrif committed Dec 29, 2014
1 parent 848cba1 commit 7931c96
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/associations/alias_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def initialize(connection, aliases)
@connection = connection
end

def aliased_table_for(table_name, aliased_name)
def aliased_table_for(table_name, aliased_name, **table_options)

This comment has been minimized.

Copy link
@tenderlove

tenderlove Dec 29, 2014

Member

Can we change this to explicit kwargs? Callers of this method must now understand the API for Arel::Table. I'd rather that this factory method hide how Arel::Table is constructed.

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Dec 29, 2014

Member

Yes. Seems a better option if it is possible.

This comment has been minimized.

Copy link
@sgrif

sgrif Dec 30, 2014

Author Contributor

Sure.

This comment has been minimized.

Copy link
@tenderlove

tenderlove Dec 30, 2014

Member

❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️

if aliases[table_name].zero?
# If it's zero, we can have our table_name
aliases[table_name] = 1
Arel::Table.new(table_name)
Arel::Table.new(table_name, table_options)
else
# Otherwise, we need to use an alias
aliased_name = connection.table_alias_for(aliased_name)
Expand All @@ -73,7 +73,7 @@ def aliased_table_for(table_name, aliased_name)
else
aliased_name
end
Arel::Table.new(table_name).alias(table_alias)
Arel::Table.new(table_name, table_options).alias(table_alias)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def construct_tables(chain, klass, refl, alias_tracker)
chain.map do |reflection|
alias_tracker.aliased_table_for(
table_name_for(reflection, klass, refl),
table_alias_for(reflection, refl, reflection != refl)
table_alias_for(reflection, refl, reflection != refl),
type_caster: klass.type_caster,
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def self.walk_tree(associations, hash)
#
def initialize(base, associations, joins)
@alias_tracker = AliasTracker.create(base.connection, joins)
@alias_tracker.aliased_table_for(base.table_name, base.table_name) # Updates the count for base.table_name to 1
@alias_tracker.aliased_table_for(base.table_name, base.table_name, type_caster: base.type_caster) # Updates the count for base.table_name to 1
tree = self.class.make_tree associations
@join_root = JoinBase.new base, build(tree, base)
@join_root.children.each { |child| construct_tables! @join_root, child }
Expand Down Expand Up @@ -186,9 +186,13 @@ def make_inner_joins(parent, child)

def table_aliases_for(parent, node)
node.reflection.chain.map { |reflection|
if reflection.klass
type_caster = reflection.klass.type_caster
end
alias_tracker.aliased_table_for(
reflection.table_name,
table_alias_for(reflection, parent, reflection != node.reflection)
table_alias_for(reflection, parent, reflection != node.reflection),
type_caster: type_caster,
)
}
end
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def predicate_builder # :nodoc:
@predicate_builder ||= PredicateBuilder.new(table_metadata)
end

def type_caster # :nodoc:
TypeCaster::Map.new(self)
end

private

def relation # :nodoc:
Expand All @@ -267,10 +271,6 @@ def relation # :nodoc:
def table_metadata # :nodoc:
TableMetadata.new(self, arel_table)
end

def type_caster # :nodoc:
TypeCaster::Map.new(self)
end
end

# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
Expand Down

0 comments on commit 7931c96

Please sign in to comment.