Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support polymorphic has many #10

Merged
merged 2 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ class CreateVersionAssociations < ActiveRecord::Migration<%= migration_version %
t.integer :version_id
t.string :foreign_key_name, null: false
t.integer :foreign_key_id
t.string :foreign_type, null: false
end
add_index :version_associations, [:version_id]
add_index :version_associations,
%i(foreign_key_name foreign_key_id),
%i(foreign_key_name foreign_key_id foreign_type),
name: "index_version_associations_on_foreign_key"
end

Expand Down
11 changes: 7 additions & 4 deletions lib/paper_trail_association_tracking/record_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def save_habtm_associations(version)
::PaperTrail::VersionAssociation.create(
version_id: version.transaction_id,
foreign_key_name: a.name,
foreign_key_id: id
foreign_key_id: id,
foreign_type: a.klass
)
end
end
Expand Down Expand Up @@ -168,12 +169,14 @@ def save_bt_association(assoc, version)
}

if assoc.options[:polymorphic]
associated_record = @record.send(assoc.name) if @record.send(assoc.foreign_type)
if associated_record && ::PaperTrail.request.enabled_for_model?(associated_record.class)
assoc_version_args[:foreign_key_id] = associated_record.id
foreign_type = @record.send(assoc.foreign_type)
if foreign_type && ::PaperTrail.request.enabled_for_model?(foreign_type.constantize)
assoc_version_args[:foreign_key_id] = @record.send(assoc.foreign_key)
assoc_version_args[:foreign_type] = foreign_type
end
elsif ::PaperTrail.request.enabled_for_model?(assoc.klass)
assoc_version_args[:foreign_key_id] = @record.send(assoc.foreign_key)
assoc_version_args[:foreign_type] = assoc.klass
end

if assoc_version_args.key?(:foreign_key_id)
Expand Down
1 change: 1 addition & 0 deletions lib/paper_trail_association_tracking/reifiers/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def load_versions_for_hm_association(assoc, model, version_table, tx_id, version
select("MIN(version_id)").
where("foreign_key_name = ?", assoc.foreign_key).
where("foreign_key_id = ?", model.id).
where("foreign_type = ?", model.class.name).
where("#{version_table}.item_type = ?", assoc.klass.base_class.name).
where("created_at >= ? OR transaction_id = ?", version_at, tx_id).
group("item_id").
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy_app/app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ class Book < ActiveRecord::Base
has_many :editorships, dependent: :destroy
has_many :editors, through: :editorships

has_many :notes, as: :object

has_paper_trail
end
4 changes: 4 additions & 0 deletions spec/dummy_app/app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Note < ActiveRecord::Base
belongs_to :object, polymorphic: true
has_paper_trail
end
2 changes: 2 additions & 0 deletions spec/dummy_app/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Person < ActiveRecord::Base
has_one :thing
has_one :thing_2, class_name: "Thing"

has_many :notes, as: :object

if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :mentor, class_name: "Person", foreign_key: :mentor_id, optional: true
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ def up
t.integer :version_id
t.string :foreign_key_name, null: false
t.integer :foreign_key_id
t.string :foreign_type, null: false
end
add_index :version_associations, [:version_id]
add_index :version_associations,
%i[foreign_key_name foreign_key_id],
%i[foreign_key_name foreign_key_id foreign_type],
name: "index_version_associations_on_foreign_key"

create_table :wotsits, force: true do |t|
Expand Down Expand Up @@ -214,6 +215,12 @@ def up
t.integer :parent_id
t.integer :partner_id
end

create_table :notes, force: true do |t|
t.string :body
t.string :object_type
t.integer :object_id
end
end

def down
Expand Down
23 changes: 23 additions & 0 deletions spec/models/note_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Note, type: :model, versioning: true do
it "baseline test setup" do
expect(Note.new).to be_versioned
end

describe "#object" do
it "can be reified" do
person = Person.create!(name: "Marielle")
note = Note.create!(body: "Note on Marielle", object: person)

note.update_attributes!(body: "Modified note")
person.update_attributes!(name: "Modified")

reified_note = note.versions.last.reify(belongs_to: true)
expect(reified_note.body).to eq("Note on Marielle")
expect(reified_note.object.name).to eq("Marielle")
end
end
end
17 changes: 17 additions & 0 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,21 @@
)
end
end

describe "#notes" do
it "can be reified" do
person = Person.create!(id: 1, name: "Jessica")
book = Book.create!(id: 1, title: "La Chute")
person_note = Note.create!(body: "Some note on person", object: person)
book_note = Note.create!(body: "Some note on book", object: book)

person.update_attributes!(name: "Jennyfer")
book_note.update_attributes!(body: "Modified note on book")
person_note.update_attributes!(body: "Modified note on person")

reified_person = person.versions.last.reify(has_many: true)
expect(reified_person.notes.length).to eq(1)
expect(reified_person.notes.first.body).to eq("Some note on person")
end
end
end