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
14 changes: 12 additions & 2 deletions app/helpers/names/histories_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ 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'
'Placement'
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
Expand All @@ -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
Expand Down
15 changes: 13 additions & 2 deletions app/models/name/public_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions app/models/name_paratype.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/controllers/names/histories_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions test/helpers/names/histories_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions test/models/name/public_history_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Loading