Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/models/concerns/versioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/models/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/models/name/versioned_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading