-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue with primary key other than
id
(#868)
* support reifying using primary_key field other than 'id' * custom_primary_key_record - model to use a uuid primary key, with a weird default scope * test custom_primary_key_record also with destroyed record * name index on custom_primary_key_record_versions item; default is too long for mysql * specify custom_pk_record uuid as a string primary key in a way that AR 3 supports - previous way worked with 4 and 5 but not 3 * update schema for custom_primary_key_records * note fix in changelog
- Loading branch information
Showing
7 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") } | ||
|
||
before_create do | ||
self.uuid ||= SecureRandom.uuid | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters