-
Notifications
You must be signed in to change notification settings - Fork 899
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
issue with primary key other than id
#868
Changes from all commits
afa8781
4d9ebd5
59d564c
ce2ce84
65e2f72
a2dc186
e8bad18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "rails_helper" | ||
|
||
describe CustomPrimaryKeyRecord, type: :model do | ||
it { is_expected.to be_versioned } | ||
|
||
describe "#versions" do | ||
it "returns instances of CustomPrimaryKeyRecordVersion", versioning: true do | ||
custom_primary_key_record = described_class.create! | ||
custom_primary_key_record.update_attributes!(name: "bob") | ||
version = custom_primary_key_record.versions.last | ||
expect(version).to be_a(CustomPrimaryKeyRecordVersion) | ||
version_from_db = CustomPrimaryKeyRecordVersion.last | ||
expect(version_from_db.reify).to be_a(CustomPrimaryKeyRecord) | ||
custom_primary_key_record.destroy | ||
expect(CustomPrimaryKeyRecordVersion.last.reify).to be_a(CustomPrimaryKeyRecord) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "securerandom" | ||
class CustomPrimaryKeyRecord < ActiveRecord::Base | ||
self.primary_key = :uuid | ||
|
||
has_paper_trail class_name: "CustomPrimaryKeyRecordVersion" | ||
# this unusual default_scope is to test the case of the Version#item association | ||
# not returning the item due to unmatched default_scope on the model. | ||
default_scope -> { where(name: "custom_primary_key_record") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of this default_scope? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is to prevent the record from being returned on
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok that makes sense. Please add your explanation as a comment above the Also, please add a line in |
||
|
||
before_create do | ||
self.uuid ||= SecureRandom.uuid | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class CustomPrimaryKeyRecordVersion < PaperTrail::Version | ||
self.table_name = "custom_primary_key_record_versions" | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part looks good to me.