Skip to content

Commit

Permalink
Merge pull request #779 from airblade/martin_issue_777
Browse files Browse the repository at this point in the history
Change reify_has_many_through to consider :source
  • Loading branch information
jaredbeck committed Apr 20, 2016
2 parents a140a9e + 7b40cc4 commit 9ecb2ba
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) -
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/reifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def reify_has_many_through(transaction_id, associations, model, options = {})
}
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.
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/models/authorship.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Authorship < ActiveRecord::Base
belongs_to :book
belongs_to :person
belongs_to :author, class_name: "Person"
has_paper_trail
end
2 changes: 1 addition & 1 deletion test/dummy/app/models/book.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/models/person.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def up

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|
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

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|
Expand Down
4 changes: 2 additions & 2 deletions test/unit/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 9ecb2ba

Please sign in to comment.