Change reify_has_many_through to consider :source

Also modified test (changed authorship.person to authorship.author so that source is not the same as the class name) to verify this case.

[Fixes #777]
This commit is contained in:
Martin Skinner 2016-04-17 21:33:38 +02:00 committed by Jared Beck
parent 7733894b0d
commit 7b40cc4eb8
9 changed files with 12 additions and 10 deletions

View File

@ -31,6 +31,8 @@
### Fixed
- [#777](https://github.com/airblade/paper_trail/issues/777) -
Support HMT associations with `:source` option.
- [#731](https://github.com/airblade/paper_trail/pull/731) -
Map enums to database values before storing in `object_changes` column.
- [#715](https://github.com/airblade/paper_trail/issues/715) -

View File

@ -255,7 +255,7 @@ module PaperTrail
}
else
collection_keys = through_collection.map { |through_model|
through_model.send(assoc.association_foreign_key)
through_model.send(assoc.source_reflection.foreign_key)
}
version_id_subquery = assoc.klass.paper_trail_version_class.

View File

@ -1,5 +1,5 @@
class Authorship < ActiveRecord::Base
belongs_to :book
belongs_to :person
belongs_to :author, class_name: "Person"
has_paper_trail
end

View File

@ -1,6 +1,6 @@
class Book < ActiveRecord::Base
has_many :authorships, dependent: :destroy
has_many :authors, through: :authorships, source: :person
has_many :authors, through: :authorships
has_many :editorships, dependent: :destroy
has_many :editors, through: :editorships

View File

@ -1,5 +1,5 @@
class Person < ActiveRecord::Base
has_many :authorships, dependent: :destroy
has_many :authorships, foreign_key: :author_id, dependent: :destroy
has_many :books, through: :authorships
has_paper_trail

View File

@ -138,7 +138,7 @@ class SetUpTestTables < ActiveRecord::Migration
create_table :authorships, force: true do |t|
t.integer :book_id
t.integer :person_id
t.integer :author_id
end
create_table :people, force: true do |t|

View File

@ -27,7 +27,7 @@ ActiveRecord::Schema.define(version: 20110208155312) do
create_table "authorships", force: :cascade do |t|
t.integer "book_id"
t.integer "person_id"
t.integer "author_id"
end
create_table "banana_versions", force: :cascade do |t|

View File

@ -523,7 +523,7 @@ class AssociationsTest < ActiveSupport::TestCase
should "mark the newly associated-through for destruction" do
assert @book_0.
authorships.
detect { |as| as.person.name == "author_1" }.
detect { |as| as.author.name == "author_1" }.
marked_for_destruction?
end
end
@ -564,7 +564,7 @@ class AssociationsTest < ActiveSupport::TestCase
should "mark the newly associated-through for destruction" do
assert @book_0.
authorships.
detect { |as| as.person.name == "person_existing" }.
detect { |as| as.author.name == "person_existing" }.
marked_for_destruction?
end
end

View File

@ -1025,7 +1025,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
@book.authorships.reload.last.destroy
assert_equal 1, PaperTrail::Version.count - count
assert_equal @book, PaperTrail::Version.last.reify.book
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.person
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.author
end
should "store version on join clear" do
@ -1034,7 +1034,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
@book.authorships.reload.destroy_all
assert_equal 1, PaperTrail::Version.count - count
assert_equal @book, PaperTrail::Version.last.reify.book
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.person
assert_equal @dostoyevsky, PaperTrail::Version.last.reify.author
end
end