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::JoinDependency
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
Class Method Summary collapse
-
.create(connection, initial_table, joins) ⇒ Object
:nodoc:.
- .initial_count_for(connection, name, table_joins) ⇒ Object
Instance Method Summary collapse
- #aliased_table_for(table_name, aliased_name, type_caster) ⇒ 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
46 47 48 49 |
# File 'lib/active_record/associations/alias_tracker.rb', line 46 def initialize(connection, aliases) @aliases = aliases @connection = connection end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
72 73 74 |
# File 'lib/active_record/associations/alias_tracker.rb', line 72 def aliases @aliases end |
Class Method Details
.create(connection, initial_table, joins) ⇒ Object
:nodoc:
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/active_record/associations/alias_tracker.rb', line 9 def self.create(connection, initial_table, joins) if joins.empty? aliases = Hash.new(0) else aliases = Hash.new { |h, k| h[k] = initial_count_for(connection, k, joins) } end aliases[initial_table] = 1 new(connection, aliases) end |
.initial_count_for(connection, name, table_joins) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_record/associations/alias_tracker.rb', line 21 def self.initial_count_for(connection, name, table_joins) quoted_name = nil counts = table_joins.map do |join| if join.is_a?(Arel::Nodes::StringJoin) # quoted_name should be case ignored as some database adapters (Oracle) return quoted name in uppercase quoted_name ||= connection.quote_table_name(name) # Table names + table aliases join.left.scan( /JOIN(?:\s+\w+)?\s+(?:\S+\s+)?(?:#{quoted_name}|#{name})\sON/i ).size elsif join.is_a?(Arel::Nodes::Join) join.left.name == name ? 1 : 0 elsif join.is_a?(Hash) join[name] else raise ArgumentError, "joins list should be initialized by list of Arel::Nodes::Join" end end counts.sum end |
Instance Method Details
#aliased_table_for(table_name, aliased_name, type_caster) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/active_record/associations/alias_tracker.rb', line 51 def aliased_table_for(table_name, aliased_name, type_caster) 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 |