Class: ActiveRecord::Associations::AliasTracker

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/associations/alias_tracker.rb

Overview

Keeps track of table aliases for ActiveRecord::Associations::JoinDependency

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, aliases, type_caster) ⇒ AliasTracker

table_joins is an array of arel joins which might conflict with the aliases we assign here


56
57
58
59
60
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 56

def initialize(connection, aliases, type_caster)
  @aliases    = aliases
  @connection = connection
  @type_caster = type_caster
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases


7
8
9
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 7

def aliases
  @aliases
end

Class Method Details

.create(connection, initial_table, type_caster) ⇒ Object


9
10
11
12
13
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 9

def self.create(connection, initial_table, type_caster)
  aliases = Hash.new(0)
  aliases[initial_table] = 1
  new connection, aliases, type_caster
end

.create_with_joins(connection, initial_table, joins, type_caster) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 15

def self.create_with_joins(connection, initial_table, joins, type_caster)
  if joins.empty?
    create(connection, initial_table, type_caster)
  else
    aliases = Hash.new { |h, k|
      h[k] = initial_count_for(connection, k, joins)
    }
    aliases[initial_table] = 1
    new connection, aliases, type_caster
  end
end

.initial_count_for(connection, name, table_joins) ⇒ Object


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
53
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 27

def self.initial_count_for(connection, name, table_joins)
  # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase
  quoted_name = connection.quote_table_name(name).downcase

  counts = table_joins.map do |join|
    if join.is_a?(Arel::Nodes::StringJoin)      # Table names + table aliases

      join.left.downcase.scan(
        /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
      ).size
    elsif join.respond_to? :left
      join.left.table_name == name ? 1 : 0
    else
      # this branch is reached by two tests:
      #
      # activerecord/test/cases/associations/cascaded_eager_loading_test.rb:37
      #   with :posts
      #
      # activerecord/test/cases/associations/eager_test.rb:1133
      #   with :comments
      #
      0
    end
  end

  counts.sum
end

Instance Method Details

#aliased_table_for(table_name, aliased_name) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'activerecord/lib/active_record/associations/alias_tracker.rb', line 62

def aliased_table_for(table_name, aliased_name)
  if aliases[table_name].zero?    # If it's zero, we can have our table_name

    aliases[table_name] = 1
    Arel::Table.new(table_name, type_caster: @type_caster)
  else
    # Otherwise, we need to use an alias
    aliased_name = @connection.table_alias_for(aliased_name)

    # Update the count
    aliases[aliased_name] += 1

    table_alias = if aliases[aliased_name] > 1
      "#{truncate(aliased_name)}_#{aliases[aliased_name]}"
    else
      aliased_name
    end
    Arel::Table.new(table_name, type_caster: @type_caster).alias(table_alias)
  end
end