Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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.
  • Loading branch information...
commit 14af11664147dc4b59de85970965b7b6d49975ac 1 parent 15a2e0d
Arturo Pie arturopie authored
21 activerecord/lib/active_record/base.rb
View
@@ -954,23 +954,30 @@ def instantiate(record)
record_id = sti_class.primary_key && record[sti_class.primary_key]
if ActiveRecord::IdentityMap.enabled? && record_id
+ instance = use_identity_map(sti_class, record_id, record)
+ else
+ instance = sti_class.allocate.init_with('attributes' => record)
+ end
+
+ instance
+ end
+
+ 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
- else
- instance = sti_class.allocate.init_with('attributes' => record)
- end
- instance
- end
-
- private
+ instance
+ end
def relation #:nodoc:
@relation ||= Relation.new(self, arel_table)
6 activerecord/lib/active_record/identity_map.rb
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.