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_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
54 55 56 57 |
# File 'lib/active_record/associations/alias_tracker.rb', line 54 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 42 43 44 45 46 47 48 49 50 51 |
# 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 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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/active_record/associations/alias_tracker.rb', line 59 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) 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).alias(table_alias) end end |