1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
require 'active_record'
require 'active_support/ordered_hash'
class Joiner::Joins
attr_reader :model
def initialize(model)
@model = model
@joins_cache = Set.new
end
def add_join_to(path)
return if path.empty?
joins_cache.add path_as_hash(path)
end
def alias_for(path)
return model.table_name if path.empty?
add_join_to path
association_for(path).table.name
end
def join_values
Joiner::JoinDependency.new(
model, table, joins_cache.to_a, Arel::Nodes::OuterJoin
)
end
private
attr_reader :joins_cache
def alias_tracker
Joiner::AliasTracker.create(
model.connection, table.name, []
)
end
def association_for(path)
join_values.join_association_for path, alias_tracker
end
def path_as_hash(path)
path[0..-2].reverse.inject(path.last) { |key, item| {item => key} }
end
def table
@table ||= model.arel_table
end
end
|