From 3d424e3dd07f71ca878dae9367a1404d13dd7973 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Fri, 24 Jan 2020 13:51:44 +0500 Subject: [PATCH 1/2] Fix ignored attributes on history models Concern method #all_versions_for(ids, time) tried to create model instance from history and raises an exception when in history object there are some columns which are ignored in model at the moment. This patch removes ignored columns from data used to create a new inctance. See #1489 --- app/models/concerns/versions.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/versions.rb b/app/models/concerns/versions.rb index 77bc484aea..47ed461ce3 100644 --- a/app/models/concerns/versions.rb +++ b/app/models/concerns/versions.rb @@ -61,7 +61,8 @@ def all_versions_for(ids, time) preceding(time + 1, true). select("distinct on (item_id) #{ver_klass.table_name}.*"). map do |ver| - o = new(ver.object) + ignored_columns = ver.item_type.constantize&.ignored_columns + o = new(ver.object&.except!(*ignored_columns)) o.version_loader = ver ver.object_changes.to_h.each { |k, v| o.public_send("#{k}=", v[-1]) } o From e4352d121a7e1fb6bb5b2be9b66f0b18d667a067 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Fri, 24 Jan 2020 14:26:30 +0500 Subject: [PATCH 2/2] Add tests for fixing ignored columns in versions --- app/models/concerns/versions.rb | 4 ++-- test/models/concerns/versions_test.rb | 28 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/models/concerns/versions_test.rb diff --git a/app/models/concerns/versions.rb b/app/models/concerns/versions.rb index 47ed461ce3..b0a82376f0 100644 --- a/app/models/concerns/versions.rb +++ b/app/models/concerns/versions.rb @@ -61,8 +61,8 @@ def all_versions_for(ids, time) preceding(time + 1, true). select("distinct on (item_id) #{ver_klass.table_name}.*"). map do |ver| - ignored_columns = ver.item_type.constantize&.ignored_columns - o = new(ver.object&.except!(*ignored_columns)) + valid_columns = ver.item_type.constantize&.column_names + o = new(ver.object&.slice(*valid_columns)) o.version_loader = ver ver.object_changes.to_h.each { |k, v| o.public_send("#{k}=", v[-1]) } o diff --git a/test/models/concerns/versions_test.rb b/test/models/concerns/versions_test.rb new file mode 100644 index 0000000000..2a54b44761 --- /dev/null +++ b/test/models/concerns/versions_test.rb @@ -0,0 +1,28 @@ +require 'test_helper' + +class VersionsTest < ActiveSupport::TestCase + + def test_if_gets_all_versions_without_error_if_ignored_column_present + @nameserver = nameservers(:shop_ns1) + @nameserver.update(hostname: 'ns99.bestnames.test') + @ignored_column_title = Nameserver.ignored_columns.first + + version = NameserverVersion.last + hash = version.object + hash[@ignored_column_title] = 123456 + version.update(object: hash) + + assert_nothing_raised do + Nameserver.all_versions_for([@nameserver.id], Time.zone.now) + end + end + + def test_if_gets_all_versions_without_error_if_no_ignored_column + @account = accounts(:cash) + @account.update(currency: 'USD') + + assert_nothing_raised do + Account.all_versions_for([@account.id], Time.zone.now) + end + end +end