mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Fix issue 633 re: reification of through assoc.
This commit is contained in:
parent
a2ada6c2ff
commit
3279206b9b
2 changed files with 32 additions and 2 deletions
|
@ -219,6 +219,16 @@ module PaperTrail
|
|||
through_collection.each do |through_model|
|
||||
reify_has_manys(transaction_id, through_model, options)
|
||||
end
|
||||
|
||||
# At this point, the "through" part of the association chain has
|
||||
# been reified, but not the final, "target" part. To continue our
|
||||
# example, `model.sections` (including `model.sections.paragraphs`)
|
||||
# has been loaded. However, the final "target" part of the
|
||||
# association, that is, `model.paragraphs`, has not been loaded. So,
|
||||
# we do that now.
|
||||
collection = through_collection.flat_map { |through_model|
|
||||
through_model.public_send(assoc.name.to_sym).to_a
|
||||
}
|
||||
else
|
||||
collection_keys = through_collection.map { |through_model|
|
||||
through_model.send(assoc.association_foreign_key)
|
||||
|
@ -234,8 +244,11 @@ module PaperTrail
|
|||
versions = versions_by_id(assoc.klass, version_id_subquery)
|
||||
collection = Array.new assoc.klass.where(assoc.klass.primary_key => collection_keys)
|
||||
prepare_array_for_has_many(collection, options, versions)
|
||||
model.send(assoc.name).proxy_association.target = collection
|
||||
end
|
||||
|
||||
# To continue our example above, assign to `model.paragraphs` the
|
||||
# `collection` (an array of `Paragraph`s).
|
||||
model.send(assoc.name).proxy_association.target = collection
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -628,6 +628,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|||
setup do
|
||||
assert_equal 3, @chapter.versions.size
|
||||
@section = @chapter.sections.first
|
||||
Timecop.travel 1.second.since
|
||||
@paragraph = @section.paragraphs.create :name => 'para1'
|
||||
end
|
||||
|
||||
|
@ -648,7 +649,23 @@ class AssociationsTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "destroy a section" do
|
||||
context "the version before a section is destroyed" do
|
||||
should "have the section and paragraph" do
|
||||
Timecop.travel 1.second.since
|
||||
@chapter.update_attributes(:name => CHAPTER_NAMES[3])
|
||||
assert_equal 4, @chapter.versions.size
|
||||
Timecop.travel 1.second.since
|
||||
@section.destroy
|
||||
assert_equal 4, @chapter.versions.size
|
||||
chapter_v3 = @chapter.versions[3].reify(:has_many => true)
|
||||
assert_equal CHAPTER_NAMES[2], chapter_v3.name
|
||||
assert_equal [@section], chapter_v3.sections
|
||||
assert_equal [@paragraph], chapter_v3.sections[0].paragraphs
|
||||
assert_equal [@paragraph], chapter_v3.paragraphs
|
||||
end
|
||||
end
|
||||
|
||||
context "the version after a section is destroyed" do
|
||||
should "not have any sections or paragraphs" do
|
||||
@section.destroy
|
||||
Timecop.travel 1.second.since
|
||||
|
|
Loading…
Reference in a new issue