Permalink
Browse files

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.

Conflicts:

	activerecord/lib/active_record/base.rb
  • Loading branch information...
1 parent 714a2c8 commit 6896cd451545679a6413939fc4eae68f3cc3ba8b @arturopie arturopie committed Mar 27, 2012
Showing with 19 additions and 12 deletions.
  1. +3 −3 activerecord/lib/active_record/identity_map.rb
  2. +16 −9 activerecord/lib/active_record/inheritance.rb
@@ -90,7 +90,7 @@ def get(klass, primary_key)
end
def add(record)
- repository[record.class.symbolized_sti_name][record.id] = record if contain_all_columns(record)
+ repository[record.class.symbolized_sti_name][record.id] = record if contain_all_columns?(record)
end
def remove(record)
@@ -107,8 +107,8 @@ def clear
private
- def contain_all_columns(record)
- (record.class.column_names - record.attribute_names) == []
+ def contain_all_columns?(record)
+ (record.class.column_names - record.attribute_names).empty?
end
end
@@ -63,15 +63,7 @@ def instantiate(record)
record_id = sti_class.primary_key && record[sti_class.primary_key]
if ActiveRecord::IdentityMap.enabled? && record_id
- if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
- record_id = record_id.to_i
- end
- if instance = IdentityMap.get(sti_class, record_id)
- instance.reinit_with('attributes' => record)
- else
- instance = sti_class.allocate.init_with('attributes' => record)
- IdentityMap.add(instance)
- end
+ instance = use_identity_map(sti_class, record_id, record)
else
instance = sti_class.allocate.init_with('attributes' => record)
end
@@ -122,6 +114,21 @@ def compute_type(type_name)
private
+ def use_identity_map(sti_class, record_id, record)
+ if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
+ record_id = record_id.to_i
+ end
+
+ if instance = IdentityMap.get(sti_class, record_id)
+ instance.reinit_with('attributes' => record)
+ else
+ instance = sti_class.allocate.init_with('attributes' => record)
+ IdentityMap.add(instance)
+ end
+
+ instance
+ end
+
def find_sti_class(type_name)
if type_name.blank? || !columns_hash.include?(inheritance_column)
self

0 comments on commit 6896cd4

Please sign in to comment.