Class: ActiveRecord::Associations::AliasTracker

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

Overview

Keeps track of table aliases for ActiveRecord::Associations::ClassMethods::JoinDependency and ActiveRecord::Associations::ThroughAssociationScope

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, aliases) ⇒ AliasTracker

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



44
45
46
47
# File 'lib/active_record/associations/alias_tracker.rb', line 44

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

Instance Attribute Details

#aliasesObject (readonly)

:nodoc:



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

def aliases
  @aliases
end

#connectionObject (readonly)

:nodoc:



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

def connection
  @connection
end

Class Method Details

.create(connection, table_joins) ⇒ Object



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

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

.empty(connection) ⇒ Object



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

def self.empty(connection)
  new connection, Hash.new(0)
end

.initial_count_for(connection, name, table_joins) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_record/associations/alias_tracker.rb', line 25

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
    else
      join.left.table_name == name ? 1 : 0
    end
  end

  counts.sum
end

Instance Method Details

#aliased_name_for(table_name, aliased_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_record/associations/alias_tracker.rb', line 59

def aliased_name_for(table_name, aliased_name)
  if aliases[table_name].zero?
    # If it's zero, we can have our table_name
    aliases[table_name] = 1
    table_name
  else
    # Otherwise, we need to use an alias
    aliased_name = connection.table_alias_for(aliased_name)

    # Update the count
    aliases[aliased_name] += 1

    if aliases[aliased_name] > 1
      "#{truncate(aliased_name)}_#{aliases[aliased_name]}"
    else
      aliased_name
    end
  end
end

#aliased_table_for(table_name, aliased_name) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/active_record/associations/alias_tracker.rb', line 49

def aliased_table_for(table_name, aliased_name)
  table_alias = aliased_name_for(table_name, aliased_name)

  if table_alias == table_name
    Arel::Table.new(table_name)
  else
    Arel::Table.new(table_name).alias(table_alias)
  end
end