public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Some performance goodness for AR associations.

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
clemens (author)
Tue Sep 02 01:31:49 -0700 2008
jeremy (committer)
Tue Sep 02 15:55:22 -0700 2008
commit  ba3ecf53b4902a9a5943f4dcf2073fe413de4778
tree    16485f0fb7ac3c6e028b4989c01d5d561c9c8d47
parent  1646e8c36495680756304b23b7301dbda9cad07a
...
878
879
880
881
 
882
883
884
 
 
885
886
887
...
994
995
996
997
 
998
999
1000
...
1002
1003
1004
1005
1006
 
 
1007
1008
1009
...
1015
1016
1017
1018
 
1019
1020
1021
...
1023
1024
1025
1026
 
1027
1028
1029
...
1038
1039
1040
1041
1042
 
 
1043
1044
1045
1046
1047
1048
1049
 
 
1050
1051
1052
...
1329
1330
1331
1332
 
1333
1334
1335
1336
1337
1338
 
1339
1340
1341
 
1342
1343
1344
 
1345
1346
1347
...
1357
1358
1359
1360
 
1361
1362
1363
...
1377
1378
1379
1380
 
1381
1382
1383
...
1444
1445
1446
1447
 
1448
1449
1450
...
1463
1464
1465
1466
 
1467
1468
1469
1470
1471
1472
1473
 
1474
1475
1476
1477
1478
1479
1480
1481
 
 
1482
1483
1484
...
1493
1494
1495
1496
 
1497
1498
1499
1500
1501
1502
1503
 
1504
1505
1506
...
1535
1536
1537
1538
 
1539
1540
1541
...
1927
1928
1929
1930
 
1931
1932
1933
...
1939
1940
1941
1942
 
1943
1944
1945
...
1976
1977
1978
1979
 
1980
1981
1982
1983
 
1984
1985
1986
...
2117
2118
2119
2120
 
2121
2122
2123
...
2135
2136
2137
2138
 
2139
2140
2141
...
878
879
880
 
881
882
 
 
883
884
885
886
887
...
994
995
996
 
997
998
999
1000
...
1002
1003
1004
 
 
1005
1006
1007
1008
1009
...
1015
1016
1017
 
1018
1019
1020
1021
...
1023
1024
1025
 
1026
1027
1028
1029
...
1038
1039
1040
 
 
1041
1042
1043
1044
1045
1046
1047
 
 
1048
1049
1050
1051
1052
...
1329
1330
1331
 
1332
1333
1334
1335
1336
1337
 
1338
1339
1340
 
1341
1342
1343
 
1344
1345
1346
1347
...
1357
1358
1359
 
1360
1361
1362
1363
...
1377
1378
1379
 
1380
1381
1382
1383
...
1444
1445
1446
 
1447
1448
1449
1450
...
1463
1464
1465
 
1466
1467
1468
1469
1470
1471
1472
 
1473
1474
1475
1476
1477
1478
1479
 
 
1480
1481
1482
1483
1484
...
1493
1494
1495
 
1496
1497
1498
1499
1500
1501
1502
 
1503
1504
1505
1506
...
1535
1536
1537
 
1538
1539
1540
1541
...
1927
1928
1929
 
1930
1931
1932
1933
...
1939
1940
1941
 
1942
1943
1944
1945
...
1976
1977
1978
 
1979
1980
1981
1982
 
1983
1984
1985
1986
...
2117
2118
2119
 
2120
2121
2122
2123
...
2135
2136
2137
 
2138
2139
2140
2141
0
@@ -878,10 +878,10 @@ module ActiveRecord
0
 
0
           method_name = "has_one_after_save_for_#{reflection.name}".to_sym
0
           define_method(method_name) do
0
-            association = instance_variable_get("#{ivar}") if instance_variable_defined?("#{ivar}")
0
+            association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
0
 
0
-            if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id)
0
-              association["#{reflection.primary_key_name}"] = id
0
+            if !association.nil? && (new_record? || association.new_record? || association[reflection.primary_key_name] != id)
0
+              association[reflection.primary_key_name] = id
0
               association.save(true)
0
             end
0
           end
0
@@ -994,7 +994,7 @@ module ActiveRecord
0
 
0
           method_name = "polymorphic_belongs_to_before_save_for_#{reflection.name}".to_sym
0
           define_method(method_name) do
0
-            association = instance_variable_get("#{ivar}") if instance_variable_defined?("#{ivar}")
0
+            association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
0
 
0
             if association && association.target
0
               if association.new_record?
0
@@ -1002,8 +1002,8 @@ module ActiveRecord
0
               end
0
 
0
               if association.updated?
0
-                self["#{reflection.primary_key_name}"] = association.id
0
-                self["#{reflection.options[:foreign_type]}"] = association.class.base_class.name.to_s
0
+                self[reflection.primary_key_name] = association.id
0
+                self[reflection.options[:foreign_type]] = association.class.base_class.name.to_s
0
               end
0
             end
0
           end
0
@@ -1015,7 +1015,7 @@ module ActiveRecord
0
 
0
           method_name = "belongs_to_before_save_for_#{reflection.name}".to_sym
0
           define_method(method_name) do
0
-            association = instance_variable_get("#{ivar}") if instance_variable_defined?("#{ivar}")
0
+            association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
0
 
0
             if !association.nil?
0
               if association.new_record?
0
@@ -1023,7 +1023,7 @@ module ActiveRecord
0
               end
0
 
0
               if association.updated?
0
-                self["#{reflection.primary_key_name}"] = association.id
0
+                self[reflection.primary_key_name] = association.id
0
               end
0
             end
0
           end
0
@@ -1038,15 +1038,15 @@ module ActiveRecord
0
 
0
           method_name = "belongs_to_counter_cache_after_create_for_#{reflection.name}".to_sym
0
           define_method(method_name) do
0
-            association = send("#{reflection.name}")
0
-            association.class.increment_counter("#{cache_column}", send("#{reflection.primary_key_name}")) unless association.nil?
0
+            association = send(reflection.name)
0
+            association.class.increment_counter(cache_column, send(reflection.primary_key_name)) unless association.nil?
0
           end
0
           after_create method_name
0
 
0
           method_name = "belongs_to_counter_cache_before_destroy_for_#{reflection.name}".to_sym
0
           define_method(method_name) do
0
-            association = send("#{reflection.name}")
0
-            association.class.decrement_counter("#{cache_column}", send("#{reflection.primary_key_name}")) unless association.nil?
0
+            association = send(reflection.name)
0
+            association.class.decrement_counter(cache_column, send(reflection.primary_key_name)) unless association.nil?
0
           end
0
           before_destroy method_name
0
 
0
@@ -1329,19 +1329,19 @@ module ActiveRecord
0
             end
0
           end
0
         end
0
-        
0
+
0
         def add_single_associated_validation_callbacks(association_name)
0
           method_name = "validate_associated_records_for_#{association_name}".to_sym
0
           define_method(method_name) do
0
             association = instance_variable_get("@#{association_name}")
0
             if !association.nil?
0
-              errors.add "#{association_name}" unless association.target.nil? || association.valid?
0
+              errors.add association_name unless association.target.nil? || association.valid?
0
             end
0
           end
0
-        
0
+
0
           validate method_name
0
         end
0
-        
0
+
0
         def add_multiple_associated_validation_callbacks(association_name)
0
           method_name = "validate_associated_records_for_#{association_name}".to_sym
0
           ivar = "@#{association_name}"
0
@@ -1357,7 +1357,7 @@ module ActiveRecord
0
               else
0
                 association.target.select { |record| record.new_record? }
0
               end.each do |record|
0
-                errors.add "#{association_name}" unless record.valid?
0
+                errors.add association_name unless record.valid?
0
               end
0
             end
0
           end
0
@@ -1377,7 +1377,7 @@ module ActiveRecord
0
 
0
           method_name = "after_create_or_update_associated_records_for_#{association_name}".to_sym
0
           define_method(method_name) do
0
-            association = instance_variable_get("#{ivar}") if instance_variable_defined?("#{ivar}")
0
+            association = instance_variable_get(ivar) if instance_variable_defined?(ivar)
0
 
0
             records_to_save = if @new_record_before_save
0
               association
0
@@ -1444,7 +1444,7 @@ module ActiveRecord
0
               when :destroy
0
                 method_name = "has_many_dependent_destroy_for_#{reflection.name}".to_sym
0
                 define_method(method_name) do
0
-                  send("#{reflection.name}").each { |o| o.destroy }
0
+                  send(reflection.name).each { |o| o.destroy }
0
                 end
0
                 before_destroy method_name
0
               when :delete_all
0
@@ -1463,22 +1463,22 @@ module ActiveRecord
0
               when :destroy
0
                 method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym
0
                 define_method(method_name) do
0
-                  association = send("#{reflection.name}")
0
+                  association = send(reflection.name)
0
                   association.destroy unless association.nil?
0
                 end
0
                 before_destroy method_name
0
               when :delete
0
                 method_name = "has_one_dependent_delete_for_#{reflection.name}".to_sym
0
                 define_method(method_name) do
0
-                  association = send("#{reflection.name}")
0
+                  association = send(reflection.name)
0
                   association.class.delete(association.id) unless association.nil?
0
                 end
0
                 before_destroy method_name
0
               when :nullify
0
                 method_name = "has_one_dependent_nullify_for_#{reflection.name}".to_sym
0
                 define_method(method_name) do
0
-                  association = send("#{reflection.name}")
0
-                  association.update_attribute("#{reflection.primary_key_name}", nil) unless association.nil?
0
+                  association = send(reflection.name)
0
+                  association.update_attribute(reflection.primary_key_name, nil) unless association.nil?
0
                 end
0
                 before_destroy method_name
0
               else
0
@@ -1493,14 +1493,14 @@ module ActiveRecord
0
               when :destroy
0
                 method_name = "belongs_to_dependent_destroy_for_#{reflection.name}".to_sym
0
                 define_method(method_name) do
0
-                  association = send("#{reflection.name}")
0
+                  association = send(reflection.name)
0
                   association.destroy unless association.nil?
0
                 end
0
                 before_destroy method_name
0
               when :delete
0
                 method_name = "belongs_to_dependent_delete_for_#{reflection.name}".to_sym
0
                 define_method(method_name) do
0
-                  association = send("#{reflection.name}")
0
+                  association = send(reflection.name)
0
                   association.class.delete(association.id) unless association.nil?
0
                 end
0
                 before_destroy method_name
0
@@ -1535,7 +1535,7 @@ module ActiveRecord
0
 
0
           create_reflection(:has_one, association_id, options, self)
0
         end
0
-        
0
+
0
         def create_has_one_through_reflection(association_id, options)
0
           options.assert_valid_keys(
0
             :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type, :validate
0
@@ -1927,7 +1927,7 @@ module ActiveRecord
0
             end
0
 
0
             def aliased_primary_key
0
-              "#{ aliased_prefix }_r0"
0
+              "#{aliased_prefix}_r0"
0
             end
0
 
0
             def aliased_table_name
0
@@ -1939,7 +1939,7 @@ module ActiveRecord
0
                 @column_names_with_alias = []
0
 
0
                 ([primary_key] + (column_names - [primary_key])).each_with_index do |column_name, i|
0
-                  @column_names_with_alias << [column_name, "#{ aliased_prefix }_r#{ i }"]
0
+                  @column_names_with_alias << [column_name, "#{aliased_prefix}_r#{i}"]
0
                 end
0
               end
0
 
0
@@ -1976,11 +1976,11 @@ module ActiveRecord
0
               @aliased_prefix     = "t#{ join_dependency.joins.size }"
0
               @parent_table_name  = parent.active_record.table_name
0
               @aliased_table_name = aliased_table_name_for(table_name)
0
-              
0
+
0
               if reflection.macro == :has_and_belongs_to_many
0
                 @aliased_join_table_name = aliased_table_name_for(reflection.options[:join_table], "_join")
0
               end
0
-        
0
+
0
               if [:has_many, :has_one].include?(reflection.macro) && reflection.options[:through]
0
                 @aliased_join_table_name = aliased_table_name_for(reflection.through_reflection.klass.table_name, "_join")
0
               end
0
@@ -2117,7 +2117,7 @@ module ActiveRecord
0
             end
0
 
0
             protected
0
-            
0
+
0
               def aliased_table_name_for(name, suffix = nil)
0
                 if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{name.downcase}\son}
0
                   @join_dependency.table_aliases[name] += 1
0
@@ -2135,7 +2135,7 @@ module ActiveRecord
0
 
0
                 name
0
               end
0
-              
0
+
0
               def pluralize(table_name)
0
                 ActiveRecord::Base.pluralize_table_names ? table_name.to_s.pluralize : table_name
0
               end

Comments