Skip to content

Commit

Permalink
Adding base method symbolized_sti_name to activerecord base to be use…
Browse files Browse the repository at this point in the history
…d on identity map. Identity map now considers the inheritance when creating the caching keys
  • Loading branch information
richardiux committed May 6, 2011
1 parent d53b406 commit fc2823a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -830,6 +830,10 @@ def symbolized_base_class
@symbolized_base_class ||= base_class.to_s.to_sym
end

def symbolized_sti_name
@symbolized_sti_name ||= sti_name ? sti_name.to_sym : symbolized_base_class
end

# Returns the base AR subclass that this class descends from. If A
# extends AR::Base, A.base_class will return A. If B descends from A
# through some arbitrarily deep hierarchy, B.base_class will return A.
Expand Down
10 changes: 5 additions & 5 deletions activerecord/lib/active_record/identity_map.rb
Expand Up @@ -49,7 +49,7 @@ def without
end

def get(klass, primary_key)
record = repository[klass.symbolized_base_class][primary_key]
record = repository[klass.symbolized_sti_name][primary_key]

if record.is_a?(klass)
ActiveSupport::Notifications.instrument("identity.active_record",
Expand All @@ -64,15 +64,15 @@ def get(klass, primary_key)
end

def add(record)
repository[record.class.symbolized_base_class][record.id] = record
repository[record.class.symbolized_sti_name][record.id] = record
end

def remove(record)
repository[record.class.symbolized_base_class].delete(record.id)
repository[record.class.symbolized_sti_name].delete(record.id)
end

def remove_by_id(symbolized_base_class, id)
repository[symbolized_base_class].delete(id)
def remove_by_id(symbolized_sti_name, id)
repository[symbolized_sti_name].delete(id)
end

def clear
Expand Down
10 changes: 6 additions & 4 deletions activerecord/test/cases/identity_map_test.rb
Expand Up @@ -143,8 +143,9 @@ def test_inherited_without_type_attribute_without_identity_map

def test_inherited_with_type_attribute_without_identity_map
ActiveRecord::IdentityMap.without do
c1 = comments(:sub_special_comment)
c2 = Comment.find(c1.id)
c = comments(:sub_special_comment)
c1 = SubSpecialComment.find(c.id)
c2 = Comment.find(c.id)
assert_same(c1.class, c2.class)
end
end
Expand All @@ -156,8 +157,9 @@ def test_inherited_without_type_attribute
end

def test_inherited_with_type_attribute
c1 = comments(:sub_special_comment)
c2 = Comment.find(c1.id)
c = comments(:sub_special_comment)
c1 = SubSpecialComment.find(c.id)
c2 = Comment.find(c.id)
assert_same(c1, c2)
end

Expand Down

0 comments on commit fc2823a

Please sign in to comment.