Merge pull request #638 from airblade/fix_issue_633

Fix issue 633 re: reification of through assoc.
This commit is contained in:
Jared Beck 2015-10-31 20:48:15 -04:00
commit 52745df467
3 changed files with 54 additions and 2 deletions

View File

@ -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

View File

@ -60,6 +60,14 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.boolean "deleted", default: false
end
create_table "chapters", force: :cascade do |t|
t.string "name"
end
create_table "citations", force: :cascade do |t|
t.integer "quotation_id"
end
create_table "customers", force: :cascade do |t|
t.string "name"
end
@ -114,6 +122,11 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.string "order_date"
end
create_table "paragraphs", force: :cascade do |t|
t.integer "section_id"
t.string "name"
end
create_table "people", force: :cascade do |t|
t.string "name"
t.string "time_zone"
@ -141,6 +154,15 @@ ActiveRecord::Schema.define(version: 20110208155312) do
t.string "content"
end
create_table "quotations", force: :cascade do |t|
t.integer "chapter_id"
end
create_table "sections", force: :cascade do |t|
t.integer "chapter_id"
t.string "name"
end
create_table "skippers", force: :cascade do |t|
t.string "name"
t.datetime "another_timestamp"

View File

@ -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