-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore
versions
DB table and add learning tests
- Loading branch information
Artur Beljajev
committed
May 17, 2019
1 parent
5c8e507
commit 647bf4c
Showing
5 changed files
with
84 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class RestoreVersions < ActiveRecord::Migration | ||
def change | ||
drop_table :versions | ||
|
||
create_table :versions do |t| | ||
t.string :item_type, :null => false | ||
t.integer :item_id, :null => false | ||
t.string :event, :null => false | ||
t.string :whodunnit | ||
t.text :object | ||
t.datetime :created_at | ||
end | ||
add_index :versions, [:item_type, :item_id] | ||
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,5 @@ | ||
class AddObjectChangesToVersions < ActiveRecord::Migration | ||
def change | ||
add_column :versions, :object_changes, :jsonb | ||
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
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,46 @@ | ||
require 'test_helper' | ||
|
||
class Post < ActiveRecord::Base | ||
has_paper_trail | ||
end | ||
|
||
class PaperTrailLearningTest < ActiveSupport::TestCase | ||
setup do | ||
ActiveRecord::Base.connection.create_table :posts do |t| | ||
t.string :title | ||
|
||
# Otherwise `touch_with_version` fails silently | ||
t.datetime :updated_at | ||
end | ||
end | ||
|
||
def test_returns_version_list | ||
@record = Post.create!(title: 'any') | ||
|
||
assert_equal 1, @record.versions.count | ||
assert_respond_to @record.versions.first, :item_id | ||
end | ||
|
||
def test_creates_new_version_upon_update | ||
@record = Post.create!(title: 'old title') | ||
original_record = @record.clone | ||
|
||
assert_difference -> { @record.versions.size } do | ||
@record.update!(title: 'new title') | ||
end | ||
version = @record.versions.last | ||
assert_equal @record.id, version.item_id | ||
assert_equal @record.class.name, version.item_type | ||
assert_equal version.reify, original_record | ||
assert_equal ['old title', 'new title'], version.object_changes['title'] | ||
assert_equal 'update', version.event | ||
end | ||
|
||
def test_touch_with_version | ||
@record = Post.create!(title: 'any') | ||
|
||
assert_difference -> { @record.versions.size } do | ||
@record.touch_with_version | ||
end | ||
end | ||
end |