File: join_aliaser.rb

package info (click to toggle)
ruby-joiner 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 204 kB
  • sloc: ruby: 303; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,174 bytes parent folder | download | duplicates (2)
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
# The core logic of this class is old Rails behaviour, replicated here because
# their own alias logic has evolved, but I haven't yet found a way to make use
# of it - and besides, this is only used to generate Thinking Sphinx's
# configuration rarely - not in any web requests, so performance issues are less
# critical here.

class Joiner::JoinAliaser
  def self.call(join_root, alias_tracker)
    new(join_root, alias_tracker).call
  end

  def initialize(join_root, alias_tracker)
    @join_root = join_root
    @alias_tracker = alias_tracker
  end

  def call
    join_root.each_children do |parent, child|
      child.table = table_aliases_for(parent, child).first
    end
  end

  private

  attr_reader :join_root, :alias_tracker

  def table_aliases_for(parent, node)
    node.reflection.chain.map { |reflection|
      alias_tracker.aliased_table_for(
        reflection.table_name,
        table_alias_for(reflection, parent, reflection != node.reflection),
        reflection.klass.type_caster
      )
    }
  end

  def table_alias_for(reflection, parent, join)
    name = reflection.alias_candidate(parent.table_name)
    join ? "#{name}_join" : name
  end
end