Skip to content
This repository
Browse code

refactor instantiate method in base, so we remove nesting if's which …

…make the code harder to read. Minor changes to contain_all_columns in IdentityMap.
  • Loading branch information...
commit 14af11664147dc4b59de85970965b7b6d49975ac 1 parent 15a2e0d
Arturo Pie authored March 26, 2012
21  activerecord/lib/active_record/base.rb
@@ -954,23 +954,30 @@ def instantiate(record)
954 954
         record_id = sti_class.primary_key && record[sti_class.primary_key]
955 955
 
956 956
         if ActiveRecord::IdentityMap.enabled? && record_id
  957
+          instance = use_identity_map(sti_class, record_id, record)
  958
+        else
  959
+          instance = sti_class.allocate.init_with('attributes' => record)
  960
+        end
  961
+
  962
+        instance
  963
+      end
  964
+
  965
+      private
  966
+
  967
+        def use_identity_map(sti_class, record_id, record)
957 968
           if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
958 969
             record_id = record_id.to_i
959 970
           end
  971
+
960 972
           if instance = IdentityMap.get(sti_class, record_id)
961 973
             instance.reinit_with('attributes' => record)
962 974
           else
963 975
             instance = sti_class.allocate.init_with('attributes' => record)
964 976
             IdentityMap.add(instance)
965 977
           end
966  
-        else
967  
-          instance = sti_class.allocate.init_with('attributes' => record)
968  
-        end
969 978
 
970  
-        instance
971  
-      end
972  
-
973  
-      private
  979
+          instance
  980
+        end
974 981
 
975 982
         def relation #:nodoc:
976 983
           @relation ||= Relation.new(self, arel_table)
6  activerecord/lib/active_record/identity_map.rb
@@ -90,7 +90,7 @@ def get(klass, primary_key)
90 90
       end
91 91
 
92 92
       def add(record)
93  
-        repository[record.class.symbolized_sti_name][record.id] = record if contain_all_columns(record)
  93
+        repository[record.class.symbolized_sti_name][record.id] = record if contain_all_columns?(record)
94 94
       end
95 95
 
96 96
       def remove(record)
@@ -107,8 +107,8 @@ def clear
107 107
 
108 108
       private
109 109
 
110  
-        def contain_all_columns(record)
111  
-          (record.class.column_names - record.attribute_names) == []
  110
+        def contain_all_columns?(record)
  111
+          (record.class.column_names - record.attribute_names).empty?
112 112
         end
113 113
     end
114 114
 

0 notes on commit 14af116

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