Skip to content

Commit

Permalink
refactor the checking of the attributes of the record in IdentityMap#…
Browse files Browse the repository at this point in the history
…add, so it's more readable
  • Loading branch information
arturopie committed Mar 28, 2012
1 parent a00a42d commit 15a2e0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/identity_map.rb
Expand Up @@ -90,7 +90,7 @@ def get(klass, primary_key)
end

def add(record)
repository[record.class.symbolized_sti_name][record.id] = record if record.class.column_names - record.attribute_names == []
repository[record.class.symbolized_sti_name][record.id] = record if contain_all_columns(record)
end

def remove(record)
Expand All @@ -104,6 +104,12 @@ def remove_by_id(symbolized_sti_name, id)
def clear
repository.clear
end

private

def contain_all_columns(record)
(record.class.column_names - record.attribute_names) == []
end
end

# Reinitialize an Identity Map model object from +coder+.
Expand Down
9 changes: 5 additions & 4 deletions activerecord/test/cases/identity_map_test.rb
Expand Up @@ -404,11 +404,12 @@ def test_find_using_identity_map_respects_readonly
assert comment.save
end

def test_do_not_add_to_identity_map_if_record_do_not_contain_all_columns
post = Post.select(:id).first
comment = post.comments[0]
def test_do_not_add_to_repository_if_record_does_not_contain_all_columns
author = Author.select(:id).first
post = author.posts.first

assert_nothing_raised do
assert_not_nil comment.post.title
assert_not_nil post.author.name
end
end

Expand Down

0 comments on commit 15a2e0d

Please sign in to comment.