Skip to content

Commit

Permalink
Restore versions DB table and add learning tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Beljajev committed May 17, 2019
1 parent 5c8e507 commit 647bf4c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
15 changes: 15 additions & 0 deletions db/migrate/20190302091059_restore_versions.rb
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
5 changes: 5 additions & 0 deletions db/migrate/20190302111152_add_object_changes_to_versions.rb
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
19 changes: 18 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,13 @@ ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;

CREATE TABLE public.versions (
id integer NOT NULL,
depricated_table_but_somehow_paper_trail_tests_fails_without_it text
item_type character varying NOT NULL,
item_id integer NOT NULL,
event character varying NOT NULL,
whodunnit character varying,
object text,
created_at timestamp without time zone,
object_changes jsonb
);


Expand Down Expand Up @@ -3980,6 +3986,13 @@ CREATE INDEX index_users_on_identity_code ON public.users USING btree (identity_
CREATE INDEX index_users_on_registrar_id ON public.users USING btree (registrar_id);


--
-- Name: index_versions_on_item_type_and_item_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--

CREATE INDEX index_versions_on_item_type_and_item_id ON public.versions USING btree (item_type, item_id);


--
-- Name: index_whois_records_on_domain_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
Expand Down Expand Up @@ -4927,6 +4940,10 @@ INSERT INTO schema_migrations (version) VALUES ('20190102144032');

INSERT INTO schema_migrations (version) VALUES ('20190209150026');

INSERT INTO schema_migrations (version) VALUES ('20190302091059');

INSERT INTO schema_migrations (version) VALUES ('20190302111152');

INSERT INTO schema_migrations (version) VALUES ('20190311111718');

INSERT INTO schema_migrations (version) VALUES ('20190312211614');
Expand Down
7 changes: 0 additions & 7 deletions lib/gem_ext/paper_trail.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# the following line is required for PaperTrail >= 4.0.0 with Rails
PaperTrail::Rails::Engine.eager_load!

PaperTrail::Version.module_eval do
self.abstract_class = true
end

# Store console and rake changes in versions
if defined?(::Rails::Console)
PaperTrail.whodunnit = "console-#{`whoami`.strip}"
Expand Down
46 changes: 46 additions & 0 deletions test/learning/paper_trail_test.rb
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

0 comments on commit 647bf4c

Please sign in to comment.