From c4809d9984ab5d20759075a96ddea6096ab93802 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Dec 2010 22:41:54 -0200 Subject: use map instead of each --- activerecord/lib/active_record/base.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index ba7f6f0129..f54c9d118d 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1500,9 +1500,7 @@ MSG # Returns a hash of all the attributes with their names as keys and the values of the attributes as values. def attributes - attrs = {} - attribute_names.each { |name| attrs[name] = read_attribute(name) } - attrs + Hash[attribute_names.map { |name| [name, read_attribute(name)] }] end # Returns an #inspect-like string for the value of the -- cgit v1.2.3 From 20ba81e47d04b0ffd510c4de9c65a439b22a00b2 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 3 Dec 2010 22:45:02 -0200 Subject: We can use the keys of the @attributes hash here and avoid a method call --- activerecord/lib/active_record/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f54c9d118d..b55aaddbd7 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1500,7 +1500,7 @@ MSG # Returns a hash of all the attributes with their names as keys and the values of the attributes as values. def attributes - Hash[attribute_names.map { |name| [name, read_attribute(name)] }] + Hash[@attributes.map { |name, _| [name, read_attribute(name)] }] end # Returns an #inspect-like string for the value of the -- cgit v1.2.3 From 70ab4502749d23464f06d50779aeea0b3275d67c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 14:01:37 -0800 Subject: cleaning up custom_join_sql method --- activerecord/lib/active_record/relation/query_methods.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 0483950db7..2933d427f0 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -163,13 +163,15 @@ module ActiveRecord end def custom_join_sql(joins) - arel = table.select_manager + joins = joins.reject { |join| join.blank? } - joins.each do |join| - next if join.blank? + return if joins.empty? - @implicit_readonly = true + @implicit_readonly = true + arel = table.select_manager + + joins.each do |join| case join when Array join = Arel.sql(join.join(' ')) if array_of_strings?(join) -- cgit v1.2.3 From ef79658bd143a9243eefa90db2f9ce300f18dd0d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 14:47:21 -0800 Subject: save the AR reference rather than delegating --- .../active_record/associations/class_methods/join_dependency.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index c578845878..9f171f891d 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -6,9 +6,10 @@ module ActiveRecord module Associations module ClassMethods class JoinDependency # :nodoc: - attr_reader :join_parts, :reflections, :table_aliases + attr_reader :join_parts, :reflections, :table_aliases, :active_record def initialize(base, associations, joins) + @active_record = base @table_joins = joins || '' @join_parts = [JoinBase.new(base)] @associations = {} @@ -45,7 +46,7 @@ module ActiveRecord def count_aliases_from_table_joins(name) # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase - quoted_name = join_base.active_record.connection.quote_table_name(name.downcase).downcase + quoted_name = active_record.connection.quote_table_name(name.downcase).downcase join_sql = @table_joins.downcase join_sql.blank? ? 0 : # Table names @@ -61,11 +62,11 @@ module ActiveRecord records = rows.map { |model| primary_id = model[primary_key] parent = parents[primary_id] ||= join_base.instantiate(model) - construct(parent, @associations, join_associations.dup, model) + construct(parent, @associations, join_associations, model) parent }.uniq - remove_duplicate_results!(join_base.active_record, records, @associations) + remove_duplicate_results!(active_record, records, @associations) records end -- cgit v1.2.3 From ddbd2039615ca4376a46a97a17612732f24568cd Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 15:23:29 -0800 Subject: reduce string objects, reduce method calls --- .../active_record/associations/class_methods/join_dependency.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index 9f171f891d..34928bc764 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -10,7 +10,7 @@ module ActiveRecord def initialize(base, associations, joins) @active_record = base - @table_joins = joins || '' + @table_joins = joins @join_parts = [JoinBase.new(base)] @associations = {} @reflections = [] @@ -45,12 +45,13 @@ module ActiveRecord end def count_aliases_from_table_joins(name) + return 0 unless @table_joins + # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase quoted_name = active_record.connection.quote_table_name(name.downcase).downcase join_sql = @table_joins.downcase - join_sql.blank? ? 0 : - # Table names - join_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size + + # Table names + join_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size + # Table aliases join_sql.scan(/join(?:\s+\w+)?\s+\S+\s+#{quoted_name}\son/).size end -- cgit v1.2.3 From d6133209ce54bac430e63ec512251eae182b79e1 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 15:23:50 -0800 Subject: arel is lazy about column information, so no need to pass it around --- .../associations/class_methods/join_dependency/join_base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_base.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_base.rb index 97003c1457..67567f06df 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_base.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_base.rb @@ -13,7 +13,7 @@ module ActiveRecord end def table - Arel::Table.new(table_name, :engine => arel_engine, :columns => active_record.columns) + Arel::Table.new(table_name, arel_engine) end def aliased_table_name -- cgit v1.2.3 From 2d9d6cd4c263f639adf8e26c7d95e7772e0c4eb7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 16:48:59 -0800 Subject: passing the ast to JoinDependency --- .../associations/class_methods/join_dependency.rb | 13 +++++++++++-- activerecord/lib/active_record/relation/finder_methods.rb | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index 34928bc764..86d43f21d3 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -45,11 +45,20 @@ module ActiveRecord end def count_aliases_from_table_joins(name) - return 0 unless @table_joins + return 0 if !@table_joins || Arel::Table === @table_joins + return count_aliases_from_string(@table_joins.downcase, name) if String === @table_joins + + @table_joins.grep(Arel::Table).find_all { |table| + table.name.downcase == name + }.length + @table_joins.grep(String).map { |s| + count_aliases_from_string(s, name) + }.sum + end + + def count_aliases_from_string(join_sql, name) # quoted_name should be downcased as some database adapters (Oracle) return quoted name in uppercase quoted_name = active_record.connection.quote_table_name(name.downcase).downcase - join_sql = @table_joins.downcase # Table names join_sql.scan(/join(?:\s+\w+)?\s+#{quoted_name}\son/).size + # Table aliases diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 51ba17c9be..906ad7699c 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -196,7 +196,7 @@ module ActiveRecord def construct_relation_for_association_calculations including = (@eager_load_values + @includes_values).uniq - join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, arel.join_sql) + join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, arel.froms.first) relation = except(:includes, :eager_load, :preload) apply_join_dependency(relation, join_dependency) end -- cgit v1.2.3 From d98cb5153dad97cde82ec91a1993043cfa9bc0e4 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 17:05:15 -0800 Subject: JoinDependency is always created with an AST now --- .../associations/class_methods/join_dependency.rb | 14 ++++++++++---- activerecord/lib/active_record/relation/query_methods.rb | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index 86d43f21d3..928e8152a5 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -49,10 +49,16 @@ module ActiveRecord return count_aliases_from_string(@table_joins.downcase, name) if String === @table_joins - @table_joins.grep(Arel::Table).find_all { |table| - table.name.downcase == name - }.length + @table_joins.grep(String).map { |s| - count_aliases_from_string(s, name) + @table_joins.grep(Arel::Nodes::Join).map { |join| + right = join.right + case right + when Arel::Table + right.name.downcase == name ? 1 : 0 + when String + count_aliases_from_string(right.downcase, name) + else + 0 + end }.sum end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 2933d427f0..194ce881e0 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -185,6 +185,20 @@ module ActiveRecord arel.join_sql end + def custom_join_ast(joins) + joins = joins.reject { |join| join.blank? } + + return if joins.empty? + + @implicit_readonly = true + + head = table.create_string_join(table, joins.shift) + + joins.inject(head) do |ast, join| + ast.right = table.create_string_join(ast.right, join) + end + end + def build_arel arel = table @@ -256,7 +270,7 @@ module ActiveRecord non_association_joins = (joins - association_joins - stashed_association_joins) custom_joins = custom_join_sql(non_association_joins) - join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_joins) + join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_join_ast(non_association_joins)) join_dependency.graft(*stashed_association_joins) -- cgit v1.2.3 From 1d96d44da318faab8d213f330aebcecdbec52f5d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 Dec 2010 18:54:04 -0800 Subject: passing the ast to a table when the relation is a table --- .../associations/class_methods/join_dependency.rb | 2 -- .../lib/active_record/relation/query_methods.rb | 23 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index 928e8152a5..d508c3b7aa 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -47,8 +47,6 @@ module ActiveRecord def count_aliases_from_table_joins(name) return 0 if !@table_joins || Arel::Table === @table_joins - return count_aliases_from_string(@table_joins.downcase, name) if String === @table_joins - @table_joins.grep(Arel::Nodes::Join).map { |join| right = join.right case right diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 194ce881e0..df41f756ce 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -185,18 +185,30 @@ module ActiveRecord arel.join_sql end - def custom_join_ast(joins) + def custom_join_ast(table, joins) joins = joins.reject { |join| join.blank? } return if joins.empty? @implicit_readonly = true + joins.map! do |join| + case join + when Array + join = Arel.sql(join.join(' ')) if array_of_strings?(join) + when String + join = Arel.sql(join) + end + join + end + head = table.create_string_join(table, joins.shift) joins.inject(head) do |ast, join| ast.right = table.create_string_join(ast.right, join) end + + head end def build_arel @@ -269,8 +281,9 @@ module ActiveRecord non_association_joins = (joins - association_joins - stashed_association_joins) custom_joins = custom_join_sql(non_association_joins) + ast = custom_join_ast(relation, non_association_joins) - join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, custom_join_ast(non_association_joins)) + join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, ast) join_dependency.graft(*stashed_association_joins) @@ -280,7 +293,11 @@ module ActiveRecord relation = association.join_to(relation) end - relation.join(custom_joins) + if Arel::Table === relation + relation.from(ast || relation) + else + relation.join(custom_joins) + end end def build_select(arel, selects) -- cgit v1.2.3 From 2fd385d47163149ec74029731bc29370fa768735 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 10:29:52 -0800 Subject: reducing use of custom joins --- activerecord/lib/active_record/relation/query_methods.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index df41f756ce..705bf21eae 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -296,7 +296,12 @@ module ActiveRecord if Arel::Table === relation relation.from(ast || relation) else - relation.join(custom_joins) + if relation.froms.length > 0 && ast + ast.left = relation.froms.first + relation.from ast + else + relation.join(custom_joins) + end end end -- cgit v1.2.3 From ddd6dee04343d3731b2f2fc558501c3707cde8fb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 10:35:26 -0800 Subject: further reducing dependence on custom_joins --- activerecord/lib/active_record/relation/query_methods.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 705bf21eae..3a2cbc8dda 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -299,6 +299,8 @@ module ActiveRecord if relation.froms.length > 0 && ast ast.left = relation.froms.first relation.from ast + elsif relation.froms.length == 0 && ast + relation.from(ast) else relation.join(custom_joins) end -- cgit v1.2.3 From 3499f882cc62e891057f806cf9a09a84f97a5e5c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 10:54:24 -0800 Subject: renaming variables, making the join_ast method private --- .../lib/active_record/relation/query_methods.rb | 78 ++++++++-------------- 1 file changed, 27 insertions(+), 51 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 3a2cbc8dda..f9e5d7d925 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -162,29 +162,32 @@ module ActiveRecord @arel ||= build_arel end - def custom_join_sql(joins) - joins = joins.reject { |join| join.blank? } + def build_arel + arel = table - return if joins.empty? + arel = build_joins(arel, @joins_values) unless @joins_values.empty? - @implicit_readonly = true + arel = collapse_wheres(arel, (@where_values - ['']).uniq) - arel = table.select_manager + arel = arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty? - joins.each do |join| - case join - when Array - join = Arel.sql(join.join(' ')) if array_of_strings?(join) - when String - join = Arel.sql(join) - end + arel = arel.take(@limit_value) if @limit_value + arel = arel.skip(@offset_value) if @offset_value - arel.join(join) - end + arel = arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty? - arel.join_sql + arel = arel.order(*@order_values.uniq.reject{|o| o.blank?}) unless @order_values.empty? + + arel = build_select(arel, @select_values.uniq) + + arel = arel.from(@from_value) if @from_value + arel = arel.lock(@lock_value) if @lock_value + + arel end + private + def custom_join_ast(table, joins) joins = joins.reject { |join| join.blank? } @@ -211,32 +214,6 @@ module ActiveRecord head end - def build_arel - arel = table - - arel = build_joins(arel, @joins_values) unless @joins_values.empty? - - arel = collapse_wheres(arel, (@where_values - ['']).uniq) - - arel = arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty? - - arel = arel.take(@limit_value) if @limit_value - arel = arel.skip(@offset_value) if @offset_value - - arel = arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty? - - arel = arel.order(*@order_values.uniq.reject{|o| o.blank?}) unless @order_values.empty? - - arel = build_select(arel, @select_values.uniq) - - arel = arel.from(@from_value) if @from_value - arel = arel.lock(@lock_value) if @lock_value - - arel - end - - private - def collapse_wheres(arel, wheres) equalities = wheres.grep(Arel::Nodes::Equality) @@ -280,10 +257,9 @@ module ActiveRecord stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation) non_association_joins = (joins - association_joins - stashed_association_joins) - custom_joins = custom_join_sql(non_association_joins) - ast = custom_join_ast(relation, non_association_joins) + join_ast = custom_join_ast(relation, non_association_joins) - join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, ast) + join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, join_ast) join_dependency.graft(*stashed_association_joins) @@ -294,15 +270,15 @@ module ActiveRecord end if Arel::Table === relation - relation.from(ast || relation) + relation.from(join_ast || relation) else - if relation.froms.length > 0 && ast - ast.left = relation.froms.first - relation.from ast - elsif relation.froms.length == 0 && ast - relation.from(ast) + if relation.froms.length > 0 && join_ast + join_ast.left = relation.froms.first + relation.from join_ast + elsif relation.froms.length == 0 && join_ast + relation.from(join_ast) else - relation.join(custom_joins) + relation end end end -- cgit v1.2.3 From 17d72dd19fc7f979ae17097c3ee1e0b5c1d75fb7 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 11:02:15 -0800 Subject: adding a fixme comment --- activerecord/lib/active_record/relation/query_methods.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index f9e5d7d925..6886c7538b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -265,6 +265,7 @@ module ActiveRecord @implicit_readonly = true unless association_joins.empty? && stashed_association_joins.empty? + # FIXME: refactor this to build an AST join_dependency.join_associations.each do |association| relation = association.join_to(relation) end -- cgit v1.2.3 From f4223cc7abeeae5f4849eab27951faa55f3c6996 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 11:18:26 -0800 Subject: arel ignores the columns parameter --- .../associations/class_methods/join_dependency/join_association.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index 4dd11a7366..2c20e304d0 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -67,8 +67,7 @@ module ActiveRecord def table @table ||= Arel::Table.new( - table_name, :as => aliased_table_name, - :engine => arel_engine, :columns => active_record.columns + table_name, :as => aliased_table_name, :engine => arel_engine ) end -- cgit v1.2.3 From b8d57a0400f95abe1b87634e9cada8868ff0a22e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 11:50:16 -0800 Subject: have table_aliases call count_aliases to set the default value --- .../lib/active_record/associations/class_methods/join_dependency.rb | 4 +++- .../associations/class_methods/join_dependency/join_association.rb | 4 ---- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb index d508c3b7aa..13576e1aec 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency.rb @@ -14,7 +14,9 @@ module ActiveRecord @join_parts = [JoinBase.new(base)] @associations = {} @reflections = [] - @table_aliases = Hash.new(0) + @table_aliases = Hash.new do |h,name| + h[name] = count_aliases_from_table_joins(name) + end @table_aliases[base.table_name] = 1 build(associations) end diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index 2c20e304d0..15c1e7de55 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -77,10 +77,6 @@ module ActiveRecord protected def aliased_table_name_for(name, suffix = nil) - if @join_dependency.table_aliases[name].zero? - @join_dependency.table_aliases[name] = @join_dependency.count_aliases_from_table_joins(name) - end - if !@join_dependency.table_aliases[name].zero? # We need an alias name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" @join_dependency.table_aliases[name] += 1 -- cgit v1.2.3 From de4bd472c3298f0455de8e3862c31900184f3c31 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 11:54:45 -0800 Subject: remove code that could never be executed --- .../associations/class_methods/join_dependency/join_association.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index 15c1e7de55..9c43e9edd7 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -80,10 +80,6 @@ module ActiveRecord if !@join_dependency.table_aliases[name].zero? # We need an alias name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" @join_dependency.table_aliases[name] += 1 - if @join_dependency.table_aliases[name] == 1 # First time we've seen this name - # Also need to count the aliases from the table_aliases to avoid incorrect count - @join_dependency.table_aliases[name] += @join_dependency.count_aliases_from_table_joins(name) - end table_index = @join_dependency.table_aliases[name] name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index}" if table_index > 1 else -- cgit v1.2.3 From dcc0c9a8bb49d956476a7165f3eef6699ec4628b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 12:05:02 -0800 Subject: dry up calls to table_aliases --- .../class_methods/join_dependency/join_association.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index 9c43e9edd7..bf0190e40a 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -77,13 +77,15 @@ module ActiveRecord protected def aliased_table_name_for(name, suffix = nil) - if !@join_dependency.table_aliases[name].zero? # We need an alias + aliases = @join_dependency.table_aliases + + if !aliases[name].zero? # We need an alias name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" - @join_dependency.table_aliases[name] += 1 - table_index = @join_dependency.table_aliases[name] + aliases[name] += 1 + table_index = aliases[name] name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index}" if table_index > 1 else - @join_dependency.table_aliases[name] += 1 + aliases[name] += 1 end name -- cgit v1.2.3 From c69bd59a9cc7c0de3d6db7f8c90e73778516debf Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 12:09:00 -0800 Subject: only call active_record once --- .../associations/class_methods/join_dependency/join_association.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index bf0190e40a..09094f3ed5 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -80,10 +80,12 @@ module ActiveRecord aliases = @join_dependency.table_aliases if !aliases[name].zero? # We need an alias - name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" + connection = active_record.connection + + name = connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" aliases[name] += 1 table_index = aliases[name] - name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index}" if table_index > 1 + name = name[0, connection.table_alias_length-3] + "_#{table_index}" if table_index > 1 else aliases[name] += 1 end -- cgit v1.2.3 From 45ea60eee7e6284768b78d51dbed4a2913aa18dd Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 8 Dec 2010 12:11:43 -0800 Subject: fewer method calls, fewer code branches --- .../class_methods/join_dependency/join_association.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index 09094f3ed5..25511a092f 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -79,17 +79,16 @@ module ActiveRecord def aliased_table_name_for(name, suffix = nil) aliases = @join_dependency.table_aliases - if !aliases[name].zero? # We need an alias + if aliases[name] != 0 # We need an alias connection = active_record.connection name = connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" - aliases[name] += 1 - table_index = aliases[name] + table_index = aliases[name] + 1 name = name[0, connection.table_alias_length-3] + "_#{table_index}" if table_index > 1 - else - aliases[name] += 1 end + aliases[name] += 1 + name end -- cgit v1.2.3 From 53bbbcc6638a9df86e74e4e420840f076e7c2994 Mon Sep 17 00:00:00 2001 From: Ivan Evtukhovich Date: Thu, 9 Dec 2010 00:41:47 +0300 Subject: Fix doc about nested transaction rollback Because AR::Rollback do not reraise and inner transaction is not "real" nothing rollback at all --- activerecord/lib/active_record/transactions.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 654c475aed..181280baaa 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -141,7 +141,8 @@ module ActiveRecord # end # end # - # User.find(:all) # => empty + # User.find(:all) # => Return both Kotori and Nemu, because inner transaction do not rollback + # # without :requiers_new => true option, and Rollback exception do not reraise # # It is also possible to requires a sub-transaction by passing # :requires_new => true. If anything goes wrong, the -- cgit v1.2.3