Class: ActiveRecord::Associations::AliasTracker
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::AliasTracker
- 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
-
#aliases ⇒ Object
readonly
:nodoc:.
-
#connection ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
- .create(connection, table_joins) ⇒ Object
- .empty(connection) ⇒ Object
- .initial_count_for(connection, name, table_joins) ⇒ Object
Instance Method Summary collapse
- #aliased_name_for(table_name, aliased_name) ⇒ Object
- #aliased_table_for(table_name, aliased_name) ⇒ Object
-
#initialize(connection, aliases) ⇒ AliasTracker
constructor
table_joins is an array of arel joins which might conflict with the aliases we assign here.
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
#aliases ⇒ Object (readonly)
:nodoc:
8 9 10 |
# File 'lib/active_record/associations/alias_tracker.rb', line 8 def aliases @aliases end |
#connection ⇒ Object (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 |