Skip to content
This repository
Browse code

Some performance goodness for AR associations.

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

0 notes on commit ba3ecf5

Please sign in to comment.
Something went wrong with that request. Please try again.