diff --git a/app/helpers/names/histories_helper.rb b/app/helpers/names/histories_helper.rb index a9efde97..f383ea81 100644 --- a/app/helpers/names/histories_helper.rb +++ b/app/helpers/names/histories_helper.rb @@ -40,8 +40,9 @@ def history_record_reference(record_class, id) end # Label a link's changes with what it links to. Publication links identify - # their publication, even once deleted. Placement headings stay neutral - # because a placement's current parent may differ from earlier edits. + # their publication and paratypes their strain, even once deleted. Placement + # headings stay neutral because a placement's current parent may differ from + # earlier edits. def history_record_heading(version) case version.record_type when 'Placement' @@ -49,6 +50,9 @@ def history_record_heading(version) when 'PublicationName' publication_id = version.changeset['publication_id'].compact.first safe_join(['Publication: ', history_record_reference(Publication, publication_id)]) + when 'NameParatype' + strain_id = version.changeset['nomenclatural_type_id'].compact.first + safe_join(['Paratype: ', history_paratype_reference(strain_id)]) else raise ArgumentError, "Unsupported history record heading: #{version.record_type}" end @@ -75,6 +79,12 @@ def history_attribute_values(model, changes, attribute) end end + # Paratypes are always strains, linked by their title as on the name page. + def history_paratype_reference(strain_id) + strain = Strain.find_by(id: strain_id) + strain ? link_to(strain.title(sup: false), strain) : "Strain ##{strain_id} (deleted)" + end + def history_polymorphic_reference(type, id) # Resolve only model classes, not any constant a recorded type string names. record_class = type&.safe_constantize diff --git a/app/models/name/public_history.rb b/app/models/name/public_history.rb index aef0c9ce..6274a12e 100644 --- a/app/models/name/public_history.rb +++ b/app/models/name/public_history.rb @@ -13,6 +13,7 @@ class Name::PublicHistory # Only these rich texts of the name appear in the public history. RICH_TEXT_NAMES = %w[description etymology_text].freeze PLACEMENT_KEYS = %w[parent_id incertae_sedis publication_id preferred].freeze + PARATYPE_KEYS = %w[nomenclatural_type_type nomenclatural_type_id publication_id].freeze def initialize(name) @name = name @@ -35,6 +36,8 @@ def changes_for(version) publication_changes_for(version) when 'Placement' placement_changes_for(version) + when 'NameParatype' + paratype_changes_for(version) when 'ActionText::RichText' rich_text_changes_for(version) else @@ -78,15 +81,23 @@ def placement_changes_for(version) link_record_changes(version, PLACEMENT_KEYS) end + def paratype_changes_for(version) + link_record_changes(version, PARATYPE_KEYS) + end + # Creating and deleting a link are shown only as a change of 'linked': # { 'linked' => [false, true] } - # Updates show the changed attributes: + # Updates show the changed attributes, omitting always versioned ones that + # did not change: # { 'emends' => [true, false] } def link_record_changes(version, keys) case version.operation when 'create' then { 'linked' => [false, true] } when 'delete' then { 'linked' => [true, false] } - else polymorphic_references(version.record_type.constantize, version.changeset.slice(*keys)) + else + model = version.record_type.constantize + polymorphic_references(model, version.changeset.slice(*keys)) + .reject { |_, values| values.first == values.last } end end diff --git a/app/models/name_paratype.rb b/app/models/name_paratype.rb index 4c0e61c8..631420e9 100644 --- a/app/models/name_paratype.rb +++ b/app/models/name_paratype.rb @@ -1,4 +1,8 @@ class NameParatype < ApplicationRecord + include Versioned + versioned_owner(:name) + always_versioned(:nomenclatural_type_type, :nomenclatural_type_id) + belongs_to(:name) belongs_to(:publication) belongs_to(:nomenclatural_type, polymorphic: true) diff --git a/test/controllers/names/histories_controller_test.rb b/test/controllers/names/histories_controller_test.rb index 30992bf3..eb28bbd9 100644 --- a/test/controllers/names/histories_controller_test.rb +++ b/test/controllers/names/histories_controller_test.rb @@ -14,6 +14,17 @@ class Names::HistoriesControllerTest < ActionDispatch::IntegrationTest assert_select '.name-history-version', count: 2 end + test 'renders paratype history under its strain' do + name = names(:escherichia_coli) + strain = strains(:two) + NameParatype.create!(name: name, nomenclatural_type: strain, publication: publications(:two)) + + get name_history_path(name) + + assert_response :success + assert_select '.name-history-record th', text: "Paratype: #{strain.title(sup: false)}" + end + test 'does not reveal private name history' do name = Name.create!( name: 'E. coli', rank: 'species', status: 5, created_by: users(:contributor) diff --git a/test/helpers/names/histories_helper_test.rb b/test/helpers/names/histories_helper_test.rb index 980ead6c..d60d4fd5 100644 --- a/test/helpers/names/histories_helper_test.rb +++ b/test/helpers/names/histories_helper_test.rb @@ -95,4 +95,23 @@ class Names::HistoriesHelperTest < ActionView::TestCase assert_equal heading, history_record_heading(version), version.operation end end + + test 'labels each version of a deleted paratype with its strain' do + strain = strains(:two) + paratype = NameParatype.create!( + name: names(:escherichia_coli), nomenclatural_type: strain, publication: publications(:one) + ) + paratype.update!(publication: publications(:two)) + paratype.destroy! + + heading = "Paratype: #{link_to(strain.title(sup: false), strain)}" + Version.where(record: paratype).each do |version| + assert_equal heading, history_record_heading(version), version.operation + end + + strain.destroy! + + assert_equal "Paratype: Strain ##{strain.id} (deleted)", + history_record_heading(Version.where(record: paratype).last) + end end diff --git a/test/models/name/public_history_test.rb b/test/models/name/public_history_test.rb index af373620..e0719a32 100644 --- a/test/models/name/public_history_test.rb +++ b/test/models/name/public_history_test.rb @@ -120,6 +120,31 @@ class Name::PublicHistoryTest < ActiveSupport::TestCase assert_equal({ 'linked' => [true, false] }, latest_change) end + test 'shows linking a paratype' do + NameParatype.create!(name: @name, nomenclatural_type: strains(:two), publication: publications(:one)) + + assert_equal({ 'linked' => [false, true] }, latest_paratype_change) + end + + test 'shows changing a paratype publication without its unchanged strain' do + paratype = NameParatype.create!( + name: @name, nomenclatural_type: strains(:two), publication: publications(:one) + ) + paratype.update!(publication: publications(:two)) + + assert_equal({ 'publication_id' => [publications(:one).id, publications(:two).id] }, + latest_paratype_change) + end + + test 'shows unlinking a paratype' do + paratype = NameParatype.create!( + name: @name, nomenclatural_type: strains(:two), publication: publications(:one) + ) + paratype.destroy! + + assert_equal({ 'linked' => [true, false] }, latest_paratype_change) + end + test 'lists name and relationship changes together newest first' do PublicationName.create!(name: @name, publication: publications(:one)) @name.update!(authority: 'Castellani and Chalmers 1919') @@ -143,4 +168,10 @@ class Name::PublicHistoryTest < ActiveSupport::TestCase def latest_change @history.changes_for(@history.versions(page: 1).first) end + + # Creating a paratype also links its publication, which adds its own version. + def latest_paratype_change + version = @history.versions(page: 1).find { |v| v.record_type == 'NameParatype' } + @history.changes_for(version) + end end