Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

fix: review round 2 of #113, delete a heard form whole and lock every write on its term

Guido Boykee79c0577805d
diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css
index 84d8d06..0e7e8b9 100644
--- a/app/assets/stylesheets/application.tailwind.css
+++ b/app/assets/stylesheets/application.tailwind.css
@@ -539,6 +539,7 @@
.glossary-heard { @apply flex flex-col gap-3 border-t border-cream-200 pt-4; }
.glossary-heard__groups { @apply flex flex-col gap-3; }
.glossary-heard__form { @apply flex items-baseline gap-2 text-sm font-medium text-heading; }
+ .glossary-heard__delete { margin-inline-start: auto; }
.glossary-heard__count { @apply text-xs font-normal text-muted; }
.glossary-heard__list { @apply mt-1 flex flex-col; }
.glossary-heard__row { @apply flex flex-wrap items-baseline gap-x-3 gap-y-0.5 border-b border-cream-200 py-1.5 text-[13px] text-heading; }
diff --git a/app/controllers/organisations/glossary_controller.rb b/app/controllers/organisations/glossary_controller.rb
index dd2a8ad..1e749db 100644
--- a/app/controllers/organisations/glossary_controller.rb
+++ b/app/controllers/organisations/glossary_controller.rb
@@ -107,14 +107,15 @@ module Organisations
land(result, notice: 'controllers.glossary.merge.success', lane: { term_id: target.id }, failure_lane: { term_id: @term.id })
end
- # One heard form of this term, learnt wrong. The write right first (403
- # before the heard form is looked up), then only a form of this term.
- def destroy_correction
+ # One heard form of this term, learnt wrong, with every correction of it,
+ # named by one of those corrections. The write right first (403 before the
+ # correction is looked up), then only a correction of this term.
+ def destroy_heard_form
authorize @term, :update?
correction = policy_scope(TermCorrection).where(glossary_term_id: @term.id).find(params[:correction_id])
authorize correction, :destroy?
- result = Actors::DestroyTermCorrection.call(correction: correction, current_user: current_user)
- land(result, notice: 'controllers.glossary.correction.destroy.success', lane: { term_id: @term.id },
+ result = Actors::DestroyHeardForm.call(correction: correction, current_user: current_user)
+ land(result, notice: 'controllers.glossary.heard_form.destroy.success', lane: { term_id: @term.id },
failure_lane: { term_id: @term.id })
end
diff --git a/app/services/CLAUDE.md b/app/services/CLAUDE.md
index 2208776..a881962 100644
--- a/app/services/CLAUDE.md
+++ b/app/services/CLAUDE.md
@@ -165,9 +165,12 @@ Verzeichnis-Übersicht (Auswahl):
Glossar (K4a, 04.11.2026): `save_glossary_term` (anlegen/ändern, von Hand immer
`confirmed`; eine verlorene Wette auf den Unique-Index ist dieselbe Ablehnung),
`set_glossary_term_status` (nur bestätigen/verwerfen, nie vorschlagen),
- `destroy_glossary_term`, `destroy_term_correction`, `merge_glossary_terms` (sperrt beide
- Zeilen in EINER Abfrage nach ID, die gehörten Formen wandern zum Ziel, das Ziel behält
- Schreibweise, Kategorie und Status). Das Ziel löst der Aufrufer über den Policy-Scope auf.
+ `destroy_glossary_term`, `destroy_heard_form` (eine gehörte Form mit ALLEN ihren
+ Korrekturen am Begriff), `merge_glossary_terms` (sperrt beide Zeilen in EINER Abfrage nach
+ ID, die gehörten Formen wandern zum Ziel, das Ziel behält Schreibweise, Kategorie und
+ Status). Das Ziel löst der Aufrufer über den Policy-Scope auf. ⚠️ EINE Sperr-Reihenfolge:
+ jeder Glossar-Write sperrt zuerst den Begriff (`glossary/term_lock.rb`, ein weggefallener
+ Begriff ist `not_found`); andersherum blockierten sich Löschen und Zusammenführen.
Registrierung (Website-Schnitt W2, 15.09.2026): `create_signup` ist die EINE
Selbst-Registrierung für das Formular und später den Provisioning-Endpunkt. Tier 2: Sprache
und tenant-freier Rahmen umschließen den ganzen Lauf (die Bestätigungsmail rendert beim
diff --git a/app/services/actors/destroy_glossary_term.rb b/app/services/actors/destroy_glossary_term.rb
index b00f84e..beaf83d 100644
--- a/app/services/actors/destroy_glossary_term.rb
+++ b/app/services/actors/destroy_glossary_term.rb
@@ -5,11 +5,9 @@
# Deletes one term of the glossary with its heard forms (K4a, plan §3.3).
# Rights are the caller's (GlossaryPolicy, manage_glossary).
#
-# The term row is locked first, the order Actors::MergeGlossaryTerms locks in:
-# a delete and a merge of the same term at once then wait for each other.
-# Deleting the heard forms first held their rows while waiting for the term
-# the merge held, and the merge waited for those rows (deadlock; review round 1
-# of #113). A term deleted in the meantime is refused as gone.
+# The term row is locked first (Glossary::TermLock, the one lock order of the
+# glossary writes): a delete and a merge of the same term at once wait for each
+# other. A term deleted in the meantime is refused as gone.
module Actors
class DestroyGlossaryTerm < BaseActor
def initialize(term:, current_user:)
@@ -24,18 +22,12 @@ module Actors
end
def perform
- lock_term
+ Glossary::TermLock.call(term: @term)
@term.destroy!
end
private
attr_reader :current_user
-
- def lock_term
- @term.lock!
- rescue ActiveRecord::RecordNotFound
- raise ActorError.new(I18n.t('controllers.glossary.not_found'), code: :not_found)
- end
end
end
diff --git a/app/services/actors/destroy_heard_form.rb b/app/services/actors/destroy_heard_form.rb
new file mode 100644
index 0000000..abb153a
--- /dev/null
+++ b/app/services/actors/destroy_heard_form.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+# app/services/actors/destroy_heard_form.rb
+#
+# Deletes one heard form of a glossary term that was learnt wrong (K4a, plan
+# §6): every correction of that form at this term, named by one of them, so
+# the form is no longer learnt at all. The term and its other forms stay, also
+# when the term is left without any heard form. Rights are the caller's
+# (GlossaryPolicy, manage_glossary).
+#
+# The term is locked first (Glossary::TermLock). A form or a term gone in the
+# meantime (deleted, or merged into another term) is refused as gone.
+module Actors
+ class DestroyHeardForm < BaseActor
+ def initialize(correction:, current_user:)
+ super()
+ @correction = correction
+ @current_user = current_user
+ end
+
+ # A destroy leaves no surviving primary record.
+ def record
+ nil
+ end
+
+ def perform
+ Glossary::TermLock.call(term: @correction.glossary_term)
+ deleted = TermCorrection.where(glossary_term_id: @correction.glossary_term_id,
+ normalized_heard: @correction.normalized_heard).delete_all
+ raise ActorError.new(I18n.t('controllers.glossary.heard_form.not_found'), code: :not_found) if deleted.zero?
+ end
+
+ private
+
+ attr_reader :current_user
+ end
+end
diff --git a/app/services/actors/destroy_term_correction.rb b/app/services/actors/destroy_term_correction.rb
deleted file mode 100644
index effc66b..0000000
--- a/app/services/actors/destroy_term_correction.rb
+++ /dev/null
@@ -1,29 +0,0 @@
-# frozen_string_literal: true
-
-# app/services/actors/destroy_term_correction.rb
-#
-# Deletes one heard form of a glossary term, one that was learnt wrong (K4a).
-# The term stays, also without any heard form. Rights are the caller's
-# (GlossaryPolicy, manage_glossary).
-module Actors
- class DestroyTermCorrection < BaseActor
- def initialize(correction:, current_user:)
- super()
- @correction = correction
- @current_user = current_user
- end
-
- # A destroy leaves no surviving primary record.
- def record
- nil
- end
-
- def perform
- @correction.destroy!
- end
-
- private
-
- attr_reader :current_user
- end
-end
diff --git a/app/services/actors/merge_glossary_terms.rb b/app/services/actors/merge_glossary_terms.rb
index 8fd994d..62f7602 100644
--- a/app/services/actors/merge_glossary_terms.rb
+++ b/app/services/actors/merge_glossary_terms.rb
@@ -10,9 +10,11 @@
# through the policy scope, never from a raw id.
#
# Refused: a term into itself, and two terms of different organisations. Both
-# rows are locked in the order of their ids, so two merges of the same pair
-# at once wait for each other instead of deadlocking; a term deleted in the
-# meantime is refused as gone, and nothing moves.
+# rows are locked in one statement in the order of their ids (the lock order
+# of Glossary::TermLock), so two merges of the same pair at once, or a merge
+# and another write on one of the terms, wait for each other instead of
+# deadlocking; a term deleted in the meantime is refused as gone, and nothing
+# moves.
module Actors
class MergeGlossaryTerms < BaseActor
def initialize(source:, target:, current_user:)
diff --git a/app/services/actors/save_glossary_term.rb b/app/services/actors/save_glossary_term.rb
index fa13a83..c5dca19 100644
--- a/app/services/actors/save_glossary_term.rb
+++ b/app/services/actors/save_glossary_term.rb
@@ -11,7 +11,8 @@
# saves of the same spelling at once pass the validation; the unique index
# refuses the second, and that is the same refusal, not a crash.
# * The id is looked up inside the organisation: an id of another one is no
-# term at all.
+# term at all. A saved term is locked before it changes (Glossary::TermLock),
+# so one deleted or merged away in the meantime is refused as gone.
module Actors
class SaveGlossaryTerm < BaseActor
ATTRIBUTES = %i[term category].freeze
@@ -31,6 +32,7 @@ module Actors
def perform
raise ActorError.new(I18n.t('controllers.glossary.not_found'), code: :not_found) if @term.nil?
+ Glossary::TermLock.call(term: @term) if @term.persisted?
@term.assign_attributes(@params.slice(*ATTRIBUTES))
if @term.new_record?
@term.status = 'confirmed'
diff --git a/app/services/actors/set_glossary_term_status.rb b/app/services/actors/set_glossary_term_status.rb
index dbe2ffa..71ef5e4 100644
--- a/app/services/actors/set_glossary_term_status.rb
+++ b/app/services/actors/set_glossary_term_status.rb
@@ -6,7 +6,8 @@
# caller's (GlossaryPolicy, manage_glossary). A proposal comes only from a
# correction (K4b) or an extraction (K5), never from here: SETTABLE has no
# proposed. A rejected term stays, so it is not proposed again; confirming it
-# later is allowed.
+# later is allowed. The term is locked first (Glossary::TermLock), so a term
+# deleted or merged away in the meantime is refused as gone.
module Actors
class SetGlossaryTermStatus < BaseActor
SETTABLE = %w[confirmed rejected].freeze
@@ -25,6 +26,7 @@ module Actors
def perform
raise ActorError.new(I18n.t('controllers.glossary.status.not_allowed'), code: :invalid_status) unless SETTABLE.include?(@status)
+ Glossary::TermLock.call(term: @term)
@term.update!(status: @status)
end
diff --git a/app/services/glossary/term_lock.rb b/app/services/glossary/term_lock.rb
new file mode 100644
index 0000000..e31fde9
--- /dev/null
+++ b/app/services/glossary/term_lock.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+# The one lock order of the glossary writes (K4a): the term row first. Every
+# actor that changes a term or its heard forms takes this lock before it
+# writes anything, and Actors::MergeGlossaryTerms takes both of its rows in one
+# statement in the order of their ids. Two writes on the same term then wait
+# for each other instead of deadlocking (review round 1 of #113: a delete held
+# the heard forms and waited for the term a merge held).
+#
+# The row is read again under the lock. A term deleted or merged away in the
+# meantime is refused as gone (code :not_found), never written as a success
+# on a row that no longer exists (review round 2 of #113).
+module Glossary
+ class TermLock
+ # term may be nil: a heard form whose term is gone reads its term as nil.
+ def self.call(term:)
+ gone! unless term
+ term.lock!
+ rescue ActiveRecord::RecordNotFound
+ gone!
+ end
+
+ def self.gone!
+ raise Actors::ActorError.new(I18n.t('controllers.glossary.not_found'), code: :not_found)
+ end
+ private_class_method :gone!
+ end
+end
diff --git a/app/views/organisations/glossary/_correction.html.erb b/app/views/organisations/glossary/_correction.html.erb
index 057c540..8f38aec 100644
--- a/app/views/organisations/glossary/_correction.html.erb
+++ b/app/views/organisations/glossary/_correction.html.erb
@@ -1,9 +1,9 @@
-<%# locals: (correction:, term:, meetings:, can_write:) %>
+<%# locals: (correction:, meetings:) %>
<%# One correction of a heard form: who spoke (a name, a link only with
view_people), the project it came from (a name for every reader of the
glossary), the meeting (title and link only when the reader may read it,
- meetings holds those), the date, how often it was replaced, and delete for
- those who may keep the glossary. %>
+ meetings holds those), the date and how often it was replaced. Deleting
+ is the whole heard form's (_detail). %>
<% speaker = glossary_speaker_name(correction) %>
<% speaker_url = glossary_speaker_url(correction) if speaker %>
<% meeting_title = meetings[correction.meeting_id] %>
@@ -30,15 +30,4 @@
· <%= t('views.organisations.glossary.show.replaced', count: correction.replaced_count) %>
<% end %>
</span>
- <% if can_write %>
- <%= render Neo::Buttons::ButtonComponent.new(
- variant: :ghost, size: :small, icon: 'trash',
- title: t('views.organisations.glossary.show.delete_heard', heard: correction.heard),
- confirm: t('views.organisations.glossary.show.delete_heard_confirm', heard: correction.heard, term: term.term),
- form: { action: correction_organisation_glossary_term_path(@current_organisation, term, correction_id: correction.id),
- method: :delete, params: list_state, data: { turbo_frame: '_top' } }
- ) do %>
- <span class="sr-only"><%= t('views.organisations.glossary.show.delete_heard', heard: correction.heard) %></span>
- <% end %>
- <% end %>
</li>
diff --git a/app/views/organisations/glossary/_detail.html.erb b/app/views/organisations/glossary/_detail.html.erb
index 2639787..8bd111e 100644
--- a/app/views/organisations/glossary/_detail.html.erb
+++ b/app/views/organisations/glossary/_detail.html.erb
@@ -33,14 +33,28 @@
<ul class="glossary-heard__groups">
<% groups.each do |group| %>
<li class="glossary-heard__group">
- <p class="glossary-heard__form">
+ <div class="glossary-heard__form">
<%= t('views.organisations.glossary.quoted', text: group.first.heard) %>
<span class="glossary-heard__count"><%= t('views.organisations.glossary.show.times', count: group.size) %></span>
- </p>
+ <% if can_write %>
+ <%# The whole form goes, with every correction of it (plan §6: a form learnt wrong). %>
+ <span class="glossary-heard__delete">
+ <%= render Neo::Buttons::ButtonComponent.new(
+ variant: :ghost, size: :small, icon: 'trash',
+ title: t('views.organisations.glossary.show.delete_heard', heard: group.first.heard),
+ confirm: t('views.organisations.glossary.show.delete_heard_confirm', heard: group.first.heard, term: term.term,
+ count: group.size),
+ form: { action: heard_form_organisation_glossary_term_path(@current_organisation, term, correction_id: group.first.id),
+ method: :delete, params: list_state, data: { turbo_frame: '_top' } }
+ ) do %>
+ <span class="sr-only"><%= t('views.organisations.glossary.show.delete_heard', heard: group.first.heard) %></span>
+ <% end %>
+ </span>
+ <% end %>
+ </div>
<ul class="glossary-heard__list">
<% group.each do |correction| %>
- <%= render 'organisations/glossary/correction', correction: correction, term: term, meetings: meetings,
- can_write: can_write %>
+ <%= render 'organisations/glossary/correction', correction: correction, meetings: meetings %>
<% end %>
</ul>
</li>
@@ -76,7 +90,7 @@
<div>
<%= render Neo::Buttons::ButtonComponent.new(
variant: :danger, size: :small, icon: 'trash',
- confirm: t('views.organisations.glossary.show.delete_confirm', term: term.term, count: corrections.size),
+ confirm: t('views.organisations.glossary.show.delete_confirm', term: term.term, count: groups.size),
form: { action: organisation_glossary_term_path(@current_organisation, term), method: :delete,
params: list_state, data: { turbo_frame: '_top' } }
) do %>
diff --git a/config/locales/views/glossar.de.yml b/config/locales/views/glossar.de.yml
index 197ad10..ee1a7bf 100644
--- a/config/locales/views/glossar.de.yml
+++ b/config/locales/views/glossar.de.yml
@@ -20,7 +20,8 @@ de:
same_term: "Ein Begriff lässt sich nicht mit sich selbst zusammenführen."
other_glossary: "Beide Begriffe müssen im selben Glossar stehen."
no_target: "Bitte einen Begriff wählen, mit dem zusammengeführt wird."
- correction:
+ heard_form:
+ not_found: "Diese gehörte Form gibt es nicht mehr."
destroy:
success: "Gehörte Form gelöscht."
views:
@@ -81,7 +82,9 @@ de:
one: "1-mal ersetzt"
other: "%{count}-mal ersetzt"
delete_heard: "Gehörte Form „%{heard}“ löschen"
- delete_heard_confirm: "Die gehörte Form „%{heard}“ bei „%{term}“ löschen?"
+ delete_heard_confirm:
+ one: "Die gehörte Form „%{heard}“ bei „%{term}“ löschen?"
+ other: "Die gehörte Form „%{heard}“ bei „%{term}“ löschen, mit allen %{count} Korrekturen?"
merge_label: "Mit anderem Begriff zusammenführen"
merge_hint: "Die gehörten Formen wandern zu dem gewählten Begriff, dieser Begriff wird gelöscht."
merge_choose: "Begriff wählen"
@@ -93,7 +96,7 @@ de:
zero: "„%{term}“ löschen?"
one: "„%{term}“ und seine gehörte Form löschen?"
other: "„%{term}“ und seine %{count} gehörten Formen löschen?"
- read_only: "Nur lesen: Pflegen dürfen Verwalter und wer das Recht „Glossar pflegen“ hat."
+ read_only: "Du kannst das Glossar ansehen, aber nicht pflegen."
form:
new_title: "Neuer Begriff"
edit_title: "„%{term}“ bearbeiten"
diff --git a/config/locales/views/glossar.en.yml b/config/locales/views/glossar.en.yml
index dd441a3..d0972d4 100644
--- a/config/locales/views/glossar.en.yml
+++ b/config/locales/views/glossar.en.yml
@@ -20,7 +20,8 @@ en:
same_term: "A term cannot be merged with itself."
other_glossary: "Both terms must be in the same glossary."
no_target: "Please choose the term to merge with."
- correction:
+ heard_form:
+ not_found: "This heard form no longer exists."
destroy:
success: "Heard form deleted."
views:
@@ -81,7 +82,9 @@ en:
one: "replaced once"
other: "replaced %{count} times"
delete_heard: "Delete heard form “%{heard}”"
- delete_heard_confirm: "Delete the heard form “%{heard}” of “%{term}”?"
+ delete_heard_confirm:
+ one: "Delete the heard form “%{heard}” of “%{term}”?"
+ other: "Delete the heard form “%{heard}” of “%{term}”, with all %{count} corrections?"
merge_label: "Merge with another term"
merge_hint: "The heard forms move to the chosen term, and this term is deleted."
merge_choose: "Choose a term"
@@ -93,7 +96,7 @@ en:
zero: "Delete “%{term}”?"
one: "Delete “%{term}” and its heard form?"
other: "Delete “%{term}” and its %{count} heard forms?"
- read_only: "Read only: admins and holders of the right “Keep the glossary” can change it."
+ read_only: "You can view the glossary, but not change it."
form:
new_title: "New term"
edit_title: "Edit “%{term}”"
diff --git a/config/routes.rb b/config/routes.rb
index e997fef..ab90326 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -339,13 +339,13 @@ Rails.application.routes.draw do
# of the people directory. The terms on the left, a term or the form in the
# lane; show, new and edit answer the lane frame, a full visit lands on the
# index with ?term_id= or ?new=term. Confirm, reject, merge and deleting one
- # heard form are writes on a term.
+ # heard form (named by one of its corrections) are writes on a term.
resources :glossary_terms, path: 'glossary', controller: 'glossary' do
member do
patch :confirm
patch :reject
post :merge
- delete 'corrections/:correction_id', action: :destroy_correction, as: :correction
+ delete 'heard/:correction_id', action: :destroy_heard_form, as: :heard_form
end
end
diff --git a/spec/requests/organisations/glossary_spec.rb b/spec/requests/organisations/glossary_spec.rb
index 51ff2cf..8ff201a 100644
--- a/spec/requests/organisations/glossary_spec.rb
+++ b/spec/requests/organisations/glossary_spec.rb
@@ -58,7 +58,7 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
# Every write of the page, one example each: Prosopite reads requests sent
# from one line of a loop as an N+1.
def self.write_names
- %w[create update destroy confirm reject merge destroy_correction]
+ %w[create update destroy confirm reject merge destroy_heard_form]
end
# [verb, path, params] of a write, against the given term.
@@ -70,7 +70,7 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
'confirm' => [:patch, confirm_organisation_glossary_term_path(organisation, target), {}],
'reject' => [:patch, reject_organisation_glossary_term_path(organisation, target), {}],
'merge' => [:post, merge_organisation_glossary_term_path(organisation, target), { target_id: proposal.id }],
- 'destroy_correction' => [:delete, correction_organisation_glossary_term_path(organisation, target, correction_id: open_correction.id), {}]
+ 'destroy_heard_form' => [:delete, heard_form_organisation_glossary_term_path(organisation, target, correction_id: open_correction.id), {}]
}.fetch(name)
end
@@ -162,8 +162,12 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(response.body).not_to include(edit_organisation_glossary_term_path(organisation, term))
expect(response.body).not_to include(confirm_organisation_glossary_term_path(organisation, proposal))
expect(response.body).not_to include(merge_organisation_glossary_term_path(organisation, term))
- expect(response.body).not_to include(correction_organisation_glossary_term_path(organisation, term, correction_id: open_correction.id))
+ expect(response.body).not_to include(heard_form_organisation_glossary_term_path(organisation, term, correction_id: open_correction.id))
expect(response.body).to include(I18n.t('views.organisations.glossary.show.read_only'))
+ page = Nokogiri::HTML(response.body)
+ expect(page.css('input[name="_method"][value="delete"]')).to be_empty
+ expect(page.text).not_to include(I18n.t('views.organisations.glossary.show.delete'))
+ expect(page.css('.glossary-heard__group')).to be_present
end
it 'opens no form with ?new=term on a full visit' do
@@ -174,6 +178,13 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(response.body).not_to include('id="glossary_term_term"')
end
+ it 'names ?new=term as unresolved when it opens no form' do
+ get organisation_glossary_terms_path(organisation, new: 'term')
+
+ data = Nokogiri::HTML(response.body).at_css('.glossary-page')['data-layout-toggle-unresolved-selection-value']
+ expect(JSON.parse(data)).to eq('new' => 'term')
+ end
+
it 'opens no form with ?new=term in the lane frame' do
get organisation_glossary_terms_path(organisation, new: 'term'), headers: frame_headers
@@ -184,7 +195,7 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
# The write right comes before the heard form is looked up: 403, not 404.
it 'is refused deleting a heard form of another term with 403' do
- delete correction_organisation_glossary_term_path(organisation, proposal, correction_id: open_correction.id)
+ delete heard_form_organisation_glossary_term_path(organisation, proposal, correction_id: open_correction.id)
expect(response).to have_http_status(:forbidden)
expect(TermCorrection.where(id: open_correction.id)).to exist
@@ -473,16 +484,46 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(glossary_state).to eq(before)
end
- it 'deletes one heard form and keeps the term' do
- delete correction_organisation_glossary_term_path(organisation, term, correction_id: open_correction.id)
+ # Review round 2 of #113: a heard form learnt wrong goes with every
+ # correction of it at this term, not one correction at a time.
+ it 'deletes one heard form with all its corrections and keeps the term and its other forms' do
+ heard_again = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'Lala Bass')
+
+ delete heard_form_organisation_glossary_term_path(organisation, term, correction_id: open_correction.id)
expect(response).to redirect_to(organisation_glossary_terms_path(organisation, term_id: term.id))
- expect(TermCorrection.where(id: open_correction.id)).not_to exist
+ expect(flash[:notice]).to eq(I18n.t('controllers.glossary.heard_form.destroy.success'))
+ expect(TermCorrection.where(id: [open_correction.id, heard_again.id])).not_to exist
expect(TermCorrection.where(id: closed_correction.id)).to exist
+ expect(GlossaryTerm.where(id: term.id)).to exist
+ end
+
+ it 'offers the delete once per heard form, and asks with the number of its corrections' do
+ create(:term_correction, organisation: organisation, glossary_term: term, heard: 'Lala Bass')
+
+ get organisation_glossary_term_path(organisation, term), headers: frame_headers
+
+ page = Nokogiri::HTML(response.body)
+ forms = page.css('.glossary-heard__group form[action*="/heard/"]')
+ expect(forms.size).to eq(2)
+ expect(forms.map { |form| form['data-turbo-confirm'] }).to include(
+ I18n.t('views.organisations.glossary.show.delete_heard_confirm', heard: 'Lala Bass', term: 'Lalabase', count: 2)
+ )
+ expect(page.css('.glossary-heard__row form')).to be_empty
+ end
+
+ it 'counts heard forms, not corrections, when it asks before deleting the term' do
+ create(:term_correction, organisation: organisation, glossary_term: term, heard: 'Lala Bass')
+
+ get organisation_glossary_term_path(organisation, term), headers: frame_headers
+
+ confirm = Nokogiri::HTML(response.body).css("form[action=\"#{organisation_glossary_term_path(organisation, term)}\"]")
+ .filter_map { |form| form['data-turbo-confirm'] }
+ expect(confirm).to eq([I18n.t('views.organisations.glossary.show.delete_confirm', term: 'Lalabase', count: 2)])
end
it 'answers 404 for a heard form of another term' do
- delete correction_organisation_glossary_term_path(organisation, proposal, correction_id: open_correction.id)
+ delete heard_form_organisation_glossary_term_path(organisation, proposal, correction_id: open_correction.id)
expect(response).to have_http_status(:not_found)
expect(TermCorrection.where(id: open_correction.id)).to exist
@@ -502,7 +543,7 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(response.body).to include(confirm_organisation_glossary_term_path(organisation, proposal),
reject_organisation_glossary_term_path(organisation, proposal),
merge_organisation_glossary_term_path(organisation, term),
- correction_organisation_glossary_term_path(organisation, term, correction_id: open_correction.id))
+ heard_form_organisation_glossary_term_path(organisation, term, correction_id: open_correction.id))
end
end
@@ -621,6 +662,35 @@ RSpec.describe 'Organisations::Glossary', :ci_guard, type: :request do
expect(response.body).to include('id="glossary_term_term"')
end
+ # The lane parameters the server did not open go to layout-toggle, which
+ # takes them out of the address (review round 2 of #113).
+ it 'names an unknown term_id as unresolved, and none for an opened term' do
+ unknown = SecureRandom.uuid
+ get organisation_glossary_terms_path(organisation, term_id: unknown)
+
+ data = Nokogiri::HTML(response.body).at_css('.glossary-page')['data-layout-toggle-unresolved-selection-value']
+ expect(JSON.parse(data)).to eq('term_id' => unknown)
+
+ get organisation_glossary_terms_path(organisation, term_id: term.id)
+
+ expect(Nokogiri::HTML(response.body).at_css('.glossary-page')['data-layout-toggle-unresolved-selection-value']).to be_nil
+ end
+
+ it 'keeps the phone view through a refused save' do
+ post organisation_glossary_terms_path(organisation), params: { glossary_term: { term: 'LALABASE' }, viewport: 'mobile' }
+
+ expect(response).to have_http_status(:unprocessable_content)
+ page = Nokogiri::HTML(response.body)
+ expect(page.css('.people-mobile-grid')).to be_present
+ expect(page.css('form.people-form input[type="hidden"][name="viewport"][value="mobile"]')).to be_present
+ end
+
+ it 'keeps the phone view through a write' do
+ patch confirm_organisation_glossary_term_path(organisation, proposal), params: { viewport: 'mobile' }
+
+ expect(response).to redirect_to(organisation_glossary_terms_path(organisation, viewport: 'mobile'))
+ end
+
it 'answers a lane request of the index with the lane alone' do
get organisation_glossary_terms_path(organisation, term_id: term.id), headers: frame_headers
diff --git a/spec/services/actors/destroy_heard_form_spec.rb b/spec/services/actors/destroy_heard_form_spec.rb
new file mode 100644
index 0000000..cfe07ae
--- /dev/null
+++ b/spec/services/actors/destroy_heard_form_spec.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# Deleting a heard form that was learnt wrong (K4a, plan §6): every correction
+# of that form at this term goes, the term and its other forms stay. Review
+# round 2 of #113: deleting one correction left the form learnt as long as
+# another correction of it remained.
+RSpec.describe Actors::DestroyHeardForm, type: :service do
+ let(:organisation) { create(:organisation) }
+ let(:writer) { create(:user) }
+
+ before { ActsAsTenant.current_tenant = organisation }
+ after { ActsAsTenant.current_tenant = nil }
+
+ let(:term) { create(:glossary_term, organisation: organisation) }
+ let(:other_term) { create(:glossary_term, organisation: organisation) }
+
+ it 'deletes every correction of the form at this term, whatever its spelling, and nothing else' do
+ wrong = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lala bass')
+ wrong_again = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'Lala Bass')
+ right = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'la la base')
+ same_form_elsewhere = create(:term_correction, organisation: organisation, glossary_term: other_term, heard: 'lala bass')
+
+ result = described_class.call(correction: wrong, current_user: writer)
+
+ expect(result).to be_success
+ expect(TermCorrection.where(id: [wrong.id, wrong_again.id])).not_to exist
+ expect(TermCorrection.where(id: [right.id, same_form_elsewhere.id]).count).to eq(2)
+ expect(GlossaryTerm.where(id: term.id)).to exist
+ end
+
+ it 'answers a form deleted in the meantime as gone' do
+ gone = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lala bass')
+ TermCorrection.where(id: gone.id).delete_all
+
+ result = described_class.call(correction: gone, current_user: writer)
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:not_found)
+ expect(result.errors).to eq([I18n.t('controllers.glossary.heard_form.not_found')])
+ end
+
+ it 'answers a term deleted in the meantime as gone, read fresh as the controller reads it' do
+ correction = TermCorrection.find(create(:term_correction, organisation: organisation, glossary_term: term).id)
+ GlossaryTerm.where(id: term.id).delete_all
+
+ result = described_class.call(correction: correction, current_user: writer)
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:not_found)
+ end
+
+ it 'locks the term before it deletes' do
+ correction = create(:term_correction, organisation: organisation, glossary_term: term)
+ statements = []
+ callback = ->(*, payload) { statements << payload[:sql] }
+
+ ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
+ described_class.call(correction: correction, current_user: writer)
+ end
+
+ lock = statements.index { |sql| sql.include?('FROM "glossary_terms"') && sql.include?('FOR UPDATE') }
+ delete = statements.index { |sql| sql.start_with?('DELETE FROM "term_corrections"') }
+ expect([lock, delete]).to all(be_present)
+ expect(lock).to be < delete
+ end
+end
diff --git a/spec/services/actors/destroy_term_correction_spec.rb b/spec/services/actors/destroy_term_correction_spec.rb
deleted file mode 100644
index 4148a3e..0000000
--- a/spec/services/actors/destroy_term_correction_spec.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-# frozen_string_literal: true
-
-require 'rails_helper'
-
-# Deleting one heard form that was learnt wrong (K4a); the term stays.
-RSpec.describe Actors::DestroyTermCorrection, type: :service do
- let(:organisation) { create(:organisation) }
- let(:writer) { create(:user) }
-
- before { ActsAsTenant.current_tenant = organisation }
- after { ActsAsTenant.current_tenant = nil }
-
- it 'deletes the heard form and keeps the term and its other forms' do
- term = create(:glossary_term, organisation: organisation)
- wrong = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'lala bass')
- right = create(:term_correction, organisation: organisation, glossary_term: term, heard: 'la la base')
-
- result = described_class.call(correction: wrong, current_user: writer)
-
- expect(result).to be_success
- expect(TermCorrection.where(id: wrong.id)).not_to exist
- expect(TermCorrection.where(id: right.id)).to exist
- expect(GlossaryTerm.where(id: term.id)).to exist
- end
-end
diff --git a/spec/services/actors/merge_glossary_terms_spec.rb b/spec/services/actors/merge_glossary_terms_spec.rb
index 185f6ad..1d89a60 100644
--- a/spec/services/actors/merge_glossary_terms_spec.rb
+++ b/spec/services/actors/merge_glossary_terms_spec.rb
@@ -80,4 +80,22 @@ RSpec.describe Actors::MergeGlossaryTerms, :ci_guard, type: :service do
expect(result.code).to eq(:not_found)
expect(source.reload.term_corrections.count).to eq(1)
end
+
+ # The one lock order of the glossary (Glossary::TermLock): rows in the order
+ # of their ids, in one statement, before any heard form moves.
+ it 'locks both terms in one statement ordered by id, before it moves anything' do
+ create(:term_correction, organisation: organisation, glossary_term: source)
+ statements = []
+ callback = ->(*, payload) { statements << payload[:sql] }
+
+ ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
+ merge
+ end
+
+ lock = statements.index { |sql| sql.include?('FROM "glossary_terms"') && sql.include?('FOR UPDATE') }
+ move = statements.index { |sql| sql.start_with?('UPDATE "term_corrections"') }
+ expect([lock, move]).to all(be_present)
+ expect(statements[lock]).to include('ORDER BY "glossary_terms"."id"')
+ expect(lock).to be < move
+ end
end
diff --git a/spec/services/actors/save_glossary_term_spec.rb b/spec/services/actors/save_glossary_term_spec.rb
index 51f9814..c0dcf57 100644
--- a/spec/services/actors/save_glossary_term_spec.rb
+++ b/spec/services/actors/save_glossary_term_spec.rb
@@ -83,6 +83,19 @@ RSpec.describe Actors::SaveGlossaryTerm, type: :service do
expect(GlossaryTerm.count).to eq(0)
end
+ # Review round 2 of #113: the update ran on a stale row and reported success.
+ it 'answers a term deleted between lookup and save as gone' do
+ term = create(:glossary_term, organisation: organisation, term: 'Lalabse')
+ actor = described_class.new(organisation: organisation, params: { term: 'Lalabase' }, current_user: writer, id: term.id)
+ GlossaryTerm.where(id: term.id).delete_all
+
+ result = actor.call
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:not_found)
+ expect(result.errors).to eq([I18n.t('controllers.glossary.not_found')])
+ end
+
it 'finds no term of another organisation' do
foreign = ActsAsTenant.with_tenant(other_organisation) { create(:glossary_term, organisation: other_organisation, term: 'Fremd') }
diff --git a/spec/services/actors/set_glossary_term_status_spec.rb b/spec/services/actors/set_glossary_term_status_spec.rb
index 9209e2e..80fffa6 100644
--- a/spec/services/actors/set_glossary_term_status_spec.rb
+++ b/spec/services/actors/set_glossary_term_status_spec.rb
@@ -36,4 +36,28 @@ RSpec.describe Actors::SetGlossaryTermStatus, type: :service do
end
expect(confirmed.reload.status).to eq('confirmed')
end
+
+ # Review round 2 of #113: the update ran on a stale row and reported success.
+ it 'answers a term deleted in the meantime as gone' do
+ GlossaryTerm.where(id: term.id).delete_all
+
+ result = described_class.call(term: term, status: 'confirmed', current_user: writer)
+
+ expect(result).not_to be_success
+ expect(result.code).to eq(:not_found)
+ end
+
+ it 'locks the term before it changes it' do
+ statements = []
+ callback = ->(*, payload) { statements << payload[:sql] }
+
+ ActiveSupport::Notifications.subscribed(callback, 'sql.active_record') do
+ described_class.call(term: term, status: 'confirmed', current_user: writer)
+ end
+
+ lock = statements.index { |sql| sql.include?('FROM "glossary_terms"') && sql.include?('FOR UPDATE') }
+ update = statements.index { |sql| sql.start_with?('UPDATE "glossary_terms"') }
+ expect([lock, update]).to all(be_present)
+ expect(lock).to be < update
+ end
end