Skip to content
Draft
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
4 changes: 4 additions & 0 deletions app/assets/stylesheets/names.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
border-top: 0.75rem solid var(--white);
}

.name-history-record ~ .name-history-change th {
padding-left: 1.5rem;
}

.name-history-before,
.name-history-after {
white-space: pre-wrap;
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/filterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def apply_sort(query, sort_by: nil, sort_direction: nil)
# Handle special sorting cases (e.g., 'citations' for Name model)
case sort_by.to_s.downcase
when 'citations'
query.left_joins(:publication_names).group(:id).order("COUNT(publication_names.id) #{sort_direction.upcase}")
query.left_joins(:publication_names).group(:id).order("COUNT(CASE WHEN publication_names.unlinked_at IS NULL THEN publication_names.id END) #{sort_direction.upcase}")
when 'date'
# Use validated_at if available, otherwise fall back to created_at
if query.model.column_names.include?('validated_at')
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/publication_names_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def destroy
if name.assigned_in? publication
name.update(assigned_in: nil)
end
@publication_name.destroy
@publication_name.unlink!
end

redirect_to(
Expand Down Expand Up @@ -60,7 +60,7 @@ def link_name_commit
private
# Use callbacks to share common setup or constraints between actions.
def set_publication_name
@publication_name = PublicationName.find(params[:id])
@publication_name = PublicationName.linked.find(params[:id])
end

def set_publication
Expand Down
42 changes: 33 additions & 9 deletions app/helpers/names_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
module NamesHelper
def history_values(changes, attribute:)
def history_values(changes, attribute:, record_type: 'Name')
values =
case attribute
when 'etymology'
history_diff_parts(*changes.map { |attributes| Name.new(attributes).full_etymology })
when 'proposed_in_id'
changes.map { |id| history_publication_reference(id) }
when 'status'
changes.map { |code| Name.status_hash.dig(code, :name) || code }
case record_type
when 'PublicationName'
history_publication_name_values(changes)
when 'Name'
history_name_values(changes, attribute)
else
changes.any? { |value| value.is_a?(String) } ? history_diff_parts(*changes) : changes
raise ArgumentError, "Unsupported public history record type: #{record_type}"
end

values.map { |value| safe_join(Array(value)).presence || '—' }
Expand Down Expand Up @@ -109,4 +107,30 @@ def name_lineage(name, links: true, last: true, register: nil, visited: [])

private

def history_name_values(changes, attribute)
case attribute
when 'etymology'
history_diff_parts(*changes.map { |attributes| Name.new(attributes).full_etymology })
when 'proposed_in_id'
changes.map { |id| history_publication_reference(id) }
when 'status'
changes.map { |code| Name.status_hash.dig(code, :name) || code }
else
history_text_values(changes)
end
end

def history_publication_name_values(changes)
history_text_values(changes).map do |value|
case value
when true then 'Yes'
when false then 'No'
else value
end
end
end

def history_text_values(changes)
changes.any? { |value| value.is_a?(String) } ? history_diff_parts(*changes) : changes
end
end
15 changes: 15 additions & 0 deletions app/models/concerns/unlinkable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Unlinkable
extend ActiveSupport::Concern

included do
scope :linked, -> { where(unlinked_at: nil) }
end

def unlink!
update!(unlinked_at: Time.current)
end

def unlinked?
unlinked_at.present?
end
end
10 changes: 6 additions & 4 deletions app/models/name.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class Name < ApplicationRecord
has_many(:pseudonyms, dependent: :destroy)
# A deleted name no longer needs its publication links, including unlinked ones.
has_many(:publication_names, dependent: :destroy)
has_many(:publications, through: :publication_names)
has_many(:publications, -> { merge(PublicationName.linked) },
through: :publication_names)
has_many(
:name_correspondences, -> { order(:created_at) }, dependent: :destroy
)
Expand Down Expand Up @@ -663,7 +665,7 @@ def hard_to_pronounce?
end

def publication_names_ordered
publication_names.left_joins(:publication).order(journal_date: :desc)
publication_names.linked.left_joins(:publication).order(journal_date: :desc)
end

def citations
Expand Down Expand Up @@ -827,15 +829,15 @@ def corrigendum_source
end

def emended_in
publication_names.where(emends: true).map(&:publication)
publication_names.linked.where(emends: true).map(&:publication)
end

def emended_in?(publication)
emended_in.include? publication
end

def not_validly_proposed_in
publication_names.where(not_valid_proposal: true).map(&:publication)
publication_names.linked.where(not_valid_proposal: true).map(&:publication)
end

def not_validly_proposed_in?(publication)
Expand Down
59 changes: 48 additions & 11 deletions app/models/name/public_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,39 @@ def initialize(name)
@name = name
end

# Filter before paginating so each page contains public updates only.
# Load both histories and filter displayable changes in Ruby before pagination.
def versions(page:)
@name.versions
.where(operation: 'update')
.where(
'EXISTS (SELECT 1 FROM jsonb_object_keys(versions.changeset) AS key WHERE key IN (?))',
KEYS
)
.order(created_at: :desc, id: :desc)
.paginate(page: page, per_page: 25)
name_versions.or(publication_versions)
.order(created_at: :desc, id: :desc)
.select { |version| changes_for(version).any? }
.paginate(page: page, per_page: 25)
end

def changes_for(version)
case version.record_type
when 'Name'
name_changes_for(version)
when 'PublicationName'
publication_changes_for(version)
else
raise ArgumentError, "Unsupported public history record type: #{version.record_type}"
end
end

def publication_id_for(version)
@publication_ids ||= @name.publication_names.pluck(:id, :publication_id).to_h
@publication_ids.fetch(version.record_id)
end

private

def name_versions
@name.versions.where(operation: 'update')
end

# Ordinary fields keep their [before, after] pairs. Etymology fields become
# one pair of attribute snapshots for the view to render together.
def changes_for(version)
def name_changes_for(version)
changes = version.changeset.slice(*KEYS)
etymology = changes.extract!(*Name::ETYMOLOGY_COLUMNS)
if etymology.any?
Expand All @@ -35,7 +53,26 @@ def changes_for(version)
changes
end

private
def publication_versions
Version.where(
record_type: 'PublicationName', record_id: @name.publication_names.select(:id)
)
end

def publication_changes_for(version)
return { 'linked' => [false, true] } if version.operation == 'create'
return {} unless version.operation == 'update'

changes = version.changeset.dup
unlinked_at = changes.delete('unlinked_at')
return changes unless unlinked_at

# A nil unlinked_at means the publication is linked.
linked = unlinked_at.map(&:nil?)
return changes if linked.first == linked.last

{ 'linked' => linked }.merge(changes)
end

# Include the name because full_etymology derives the :xx particle from its
# last word. Older versions without a name pair use the current name.
Expand Down
3 changes: 2 additions & 1 deletion app/models/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Publication < ApplicationRecord
has_many(:publication_names, dependent: :destroy)
has_many(:authors, through: :publication_authors)
has_many(:subjects, through: :publication_subjects)
has_many(:names, through: :publication_names)
has_many(:names, -> { merge(PublicationName.linked) },
through: :publication_names)
has_many(:registers)
has_many(
:proposed_names, class_name: 'Name',
Expand Down
3 changes: 3 additions & 0 deletions app/models/publication_name.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class PublicationName < ApplicationRecord
include Unlinkable
include Versioned

belongs_to(:publication)
belongs_to(:name)

Expand Down
19 changes: 16 additions & 3 deletions app/views/names/history.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,31 @@
</thead>
<% @versions.each do |version| %>
<% changes = @history.changes_for(version) %>
<% publication_change = version.record_type == 'PublicationName' %>
<tbody class="name-history-version" data-version-id="<%= version.id %>">
<% if publication_change %>
<tr class="name-history-record">
<th class="name-history-date" scope="rowgroup" rowspan="<%= changes.size + 1 %>">
<time datetime="<%= version.created_at.iso8601 %>">
<%= version.created_at.strftime('%F %H:%M %Z') %>
</time>
</th>
<th colspan="3">
Publication: <%= history_publication_reference(@history.publication_id_for(version)) %>
</th>
</tr>
<% end %>
<% changes.each_with_index do |(attribute, values), index| %>
<% before, after = history_values(values, attribute: attribute) %>
<% before, after = history_values(values, attribute: attribute, record_type: version.record_type) %>
<tr class="name-history-change">
<% if index.zero? %>
<% if index.zero? && !publication_change %>
<th class="name-history-date" scope="rowgroup" rowspan="<%= changes.size %>">
<time datetime="<%= version.created_at.iso8601 %>">
<%= version.created_at.strftime('%F %H:%M %Z') %>
</time>
</th>
<% end %>
<th scope="row"><%= Name.human_attribute_name(attribute) %></th>
<th scope="row"><%= (publication_change ? PublicationName : Name).human_attribute_name(attribute) %></th>
<td class="name-history-before"><%= before %></td>
<td class="name-history-after"><%= after %></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/publications/_names.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% publication.publication_names.each do |publication_name| %>
<% publication.publication_names.linked.each do |publication_name| %>
<% name = publication_name.name %>
<%= link_to(
name.abbr_name, name, class: 'badge badge-pill badge-primary') %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddUnlinkedAtToPublicationNames < ActiveRecord::Migration[6.1]
def change
add_column :publication_names, :unlinked_at, :datetime
remove_index :publication_names, column: %i[publication_id name_id], unique: true
add_index :publication_names, %i[publication_id name_id],
unique: true, where: 'unlinked_at IS NULL'
end
end
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2026_09_17_100000) do
ActiveRecord::Schema.define(version: 2026_10_02_100000) do

# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
Expand Down Expand Up @@ -367,8 +367,9 @@
t.datetime "updated_at", null: false
t.boolean "emends", default: false
t.boolean "not_valid_proposal", default: false
t.datetime "unlinked_at"
t.index ["name_id"], name: "index_publication_names_on_name_id"
t.index ["publication_id", "name_id"], name: "index_publication_names_on_publication_id_and_name_id", unique: true
t.index ["publication_id", "name_id"], name: "index_publication_names_on_publication_id_and_name_id", unique: true, where: "(unlinked_at IS NULL)"
t.index ["publication_id"], name: "index_publication_names_on_publication_id"
end

Expand Down
5 changes: 3 additions & 2 deletions test/controllers/names/history_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ class NamesHistoryTest < ActionDispatch::IntegrationTest
test 'renders public history' do
name = names(:escherichia_coli)
name.update!(authority: 'Smith', etymology_xx_description: 'of the colon')
PublicationName.create!(name: name, publication: publications(:one))

get history_name_path(name)

assert_response :success
assert_select '.name-history-version', count: 1
assert_select '.name-history-change', count: 2
assert_select '.name-history-version', count: 2
end

test 'does not reveal private name history' do
name = Name.create!(
name: 'E. coli', rank: 'species', status: 5, created_by: users(:contributor)
)
name.update!(syllabication: 'co.li')
PublicationName.create!(name: name, publication: publications(:one)).unlink!

get history_name_path(name)

Expand Down
39 changes: 38 additions & 1 deletion test/helpers/names_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,56 @@ class NamesHelperTest < ActionView::TestCase
assert_equal 'coli, <ins class="text-success">&amp;</ins> E. coli <ins class="text-success">!</ins>', after
end

test 'renders proposed publication changes as citations' do
test 'renders publication changes as citations' do
publication = publications(:one)
citation = link_to(publication.short_citation, publication)
assert_equal [citation, '—'],
history_values([publication.id, nil], attribute: 'proposed_in_id')
assert_equal ['—', citation],
history_values([nil, publication.id], attribute: 'proposed_in_id')
end

test 'diffs text additions and removals in either record context' do
%w[Name PublicationName].each do |record_type|
assert_equal ['—', '<ins class="text-success">Smith &amp; Jones</ins>'],
history_values([nil, 'Smith & Jones'], attribute: 'authority', record_type: record_type)
assert_equal ['<del class="text-danger">Smith &amp; Jones</del>', '—'],
history_values(['Smith & Jones', nil], attribute: 'authority', record_type: record_type)
end
end

test 'formats name status labels without diffing them' do
assert_equal ['Draft', 'Submitted'], history_values([5, 10], attribute: 'status')
assert_equal ['—', '99'], history_values([nil, 99], attribute: 'status')
assert_equal ['5', '10'], history_values(
[5, 10], attribute: 'status', record_type: 'PublicationName'
)
end

test 'renders publication relationship values in their own record context' do
%w[linked emends not_valid_proposal].each do |attribute|
assert_equal ['No', 'Yes'], history_values(
[false, true], attribute: attribute, record_type: 'PublicationName'
)
assert_equal ['Yes', 'No'], history_values(
[true, false], attribute: attribute, record_type: 'PublicationName'
)
end
end

test 'renders a retained publication id when the publication no longer exists' do
publication = publications(:no_doi)
publication.destroy!

assert_equal ['—', "Publication ##{publication.id} (deleted)"],
history_values([nil, publication.id], attribute: 'proposed_in_id')
end

test 'preserves nonboolean publication relationship values' do
ids = [publications(:one).id, publications(:no_doi).id]

assert_equal ids.map(&:to_s), history_values(
ids, attribute: 'publication_id', record_type: 'PublicationName'
)
end
end
Loading
Loading