diff --git a/app/models/concerns/versioned.rb b/app/models/concerns/versioned.rb index 1469a259..e326eb8e 100644 --- a/app/models/concerns/versioned.rb +++ b/app/models/concerns/versioned.rb @@ -6,6 +6,7 @@ module Versioned class_attribute :versioned_attribute_sets, default: [] class_attribute :versioned_owner_association class_attribute :always_versioned_attributes, default: [] + class_attribute :versioned_never_attributes, default: %w[created_at updated_at] after_create :record_create_version after_update :record_update_version @@ -24,6 +25,12 @@ def always_versioned(*attributes) self.always_versioned_attributes = always_versioned_attributes + attributes.map(&:to_s) end + # Changes to these attributes alone add no version, and update versions + # omit them, e.g. cached responses from external services. + def versioned_never(*attributes) + self.versioned_never_attributes = versioned_never_attributes + attributes.map(&:to_s) + end + # Versions belong to the history of this association's record rather than # their own, e.g. a placement's versions are part of its name's history. def versioned_owner(association) @@ -87,9 +94,9 @@ def record_version(operation, changeset) # Create and delete versions change every attribute from or to nothing, # retaining the full attributes: { 'rank' => [nil, 'species'] } on create and # { 'rank' => ['species', nil] } on delete. - # Only update changesets omit timestamps. + # Only update changesets omit timestamps and never versioned attributes. def versioned_changes - changeset = versioned_saved_changes.except('created_at', 'updated_at') + changeset = versioned_saved_changes.except(*versioned_never_attributes) # A polymorphic ID is only meaningful together with its record type. polymorphic_sets = self.class.reflect_on_all_associations(:belongs_to) diff --git a/app/models/name.rb b/app/models/name.rb index 30dacbea..62377ee8 100644 --- a/app/models/name.rb +++ b/app/models/name.rb @@ -165,6 +165,10 @@ class Name < ApplicationRecord ETYMOLOGY_COLUMNS = column_names.grep(/\Aetymology_/).freeze versioned_together(:name, *ETYMOLOGY_COLUMNS) + versioned_never( + :itis_json, :itis_at, :irmng_json, :irmng_at, + :col_json, :col_at, :gbif_json, :gbif_at + ) attr_accessor :only_display attr_accessor :nomenclatural_type_entry diff --git a/test/models/name/versioned_test.rb b/test/models/name/versioned_test.rb index 77c46b5a..4f4ea5f5 100644 --- a/test/models/name/versioned_test.rb +++ b/test/models/name/versioned_test.rb @@ -49,6 +49,33 @@ class Name::VersionedTest < ActiveSupport::TestCase end end + test 'updating only external service data does not add a version' do + @name.save! + + assert_no_difference -> { @name.versions.count } do + @name.update!(gbif_json: '{"results":[]}', gbif_at: Time.current) + end + end + + test 'updating omits timestamps and external service data from the changeset' do + @name.save! + + @name.update!(syllabication: 'co.li', gbif_json: '{"results":[]}', gbif_at: Time.current) + + assert_equal %w[syllabication], @name.versions.last.changeset.keys + end + + test 'updating a workflow timestamp adds a version' do + @name.save! + validated_at = Time.current + + assert_difference -> { @name.versions.count } do + @name.update!(validated_at: validated_at) + end + + assert_equal %w[validated_at], @name.versions.last.changeset.keys + end + test 'changing an etymology description snapshots valued etymology attributes' do @name.assign_attributes( etymology_xx_lang: 'N.L.', etymology_xx_description: 'of the colon'