class Joiner::Joins

Constants

JoinAssociation
JoinDependency

Attributes

join_association_class[RW]
joins_cache[R]
model[R]

Public Class Methods

new(model) click to toggle source
# File lib/joiner/joins.rb, line 11
def initialize(model)
  @model       = model
  @joins_cache = Set.new
end

Public Instance Methods

add_join_to(path) click to toggle source
# File lib/joiner/joins.rb, line 16
def add_join_to(path)
  return if path.empty?

  joins_cache.add path_as_hash(path)
end
alias_for(path) click to toggle source
# File lib/joiner/joins.rb, line 22
def alias_for(path)
  return model.table_name if path.empty?

  add_join_to path
  join_association_for(path).tables.first.name
end
join_values() click to toggle source
# File lib/joiner/joins.rb, line 29
def join_values
  switch_join_dependency join_association_class
  result = JoinDependency.new model, joins_cache.to_a, []
  switch_join_dependency JoinAssociation

  result
end

Private Instance Methods

join_association_for(path) click to toggle source
# File lib/joiner/joins.rb, line 41
def join_association_for(path)
  path.inject(join_values.join_root) do |node, piece|
    node.children.detect { |child| child.reflection.name == piece }
  end
end
path_as_hash(path) click to toggle source
# File lib/joiner/joins.rb, line 47
def path_as_hash(path)
  path[0..-2].reverse.inject(path.last) { |key, item| {item => key} }
end
switch_join_dependency(klass) click to toggle source
# File lib/joiner/joins.rb, line 51
def switch_join_dependency(klass)
  return unless join_association_class

  JoinDependency.send :remove_const, :JoinAssociation
  JoinDependency.const_set :JoinAssociation, klass
end