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.

feat(glossary): add glossary terms and their heard forms per organisation

Guido Boykeff833372ed6f
diff --git a/.github/workflows/guards.yml b/.github/workflows/guards.yml
index a5823ef..2d5d9f8 100644
--- a/.github/workflows/guards.yml
+++ b/.github/workflows/guards.yml
@@ -344,6 +344,8 @@ jobs:
spec/requests/organisations/meeting_assistant_seams_spec.rb
spec/requests/organisations/meetings_overview_create_spec.rb
spec/models/person_spec.rb
+ spec/models/glossary_term_spec.rb
+ spec/models/term_correction_spec.rb
spec/requests/organisations/people_spec.rb
spec/requests/organisations/shown_email_spec.rb
spec/requests/user_preferences_email_visibility_spec.rb
diff --git a/app/models/glossary_term.rb b/app/models/glossary_term.rb
new file mode 100644
index 0000000..04bc0c6
--- /dev/null
+++ b/app/models/glossary_term.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+# One term of the glossary of an organisation (K4a), in its right spelling.
+# The glossary holds for the whole organisation, not per project (E1); the
+# project stays on every heard form (TermCorrection) as its origin.
+#
+# normalized_term (GlossaryText) is unique per organisation: "Lalabase" and
+# "lalabase" are one term. status (E5): proposed, confirmed, rejected. Only
+# K4b and K5 propose; a term written by hand is confirmed. A rejected term
+# stays, so it is not proposed again.
+#
+# Rights: GlossaryPolicy (view_glossary / manage_glossary). Writes go through
+# the glossary actors (Actors::SaveGlossaryTerm and its siblings).
+class GlossaryTerm < ApplicationRecord
+ CATEGORIES = %w[person company product place domain foreign].freeze
+ STATUSES = %w[proposed confirmed rejected].freeze
+ MAX_LENGTH = 255
+
+ belongs_to :created_by, class_name: 'User', optional: true
+
+ acts_as_tenant(:organisation)
+
+ # The database removes them too (on_delete: :cascade); delete_all says the
+ # same in Rails and runs one DELETE, no callbacks.
+ has_many :term_corrections, dependent: :delete_all
+
+ enum :status, STATUSES.index_by(&:itself), validate: true
+
+ before_validation :normalize_term
+
+ validates :term, presence: true, length: { maximum: MAX_LENGTH }
+ validates :normalized_term, presence: true
+ validates :category, inclusion: { in: CATEGORIES }, allow_nil: true
+ validate :term_not_taken
+
+ # Open proposals first, then confirmed, then rejected (STATUSES), each by
+ # spelling.
+ scope :ordered, -> { in_order_of(:status, STATUSES).order(:normalized_term, :id) }
+
+ private
+
+ def normalize_term
+ self.term = GlossaryText.clean(term) unless term.nil?
+ self.normalized_term = GlossaryText.normalize(term)
+ self.category = category.presence
+ end
+
+ # One spelling once per organisation, compared in normalized form. The
+ # message stands on term, the field the writer typed. The unique index
+ # catches two saves at once (the actors map RecordNotUnique to the same
+ # message).
+ def term_not_taken
+ return if normalized_term.blank? || !will_save_change_to_normalized_term?
+ return unless self.class.where(organisation_id: organisation_id, normalized_term: normalized_term).where.not(id: id).exists?
+
+ errors.add(:term, :taken)
+ end
+end
diff --git a/app/models/glossary_text.rb b/app/models/glossary_text.rb
new file mode 100644
index 0000000..760006b
--- /dev/null
+++ b/app/models/glossary_text.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+# The ONE normalizer of the glossary (K4a): the form in which a term and a
+# heard form are compared. Unicode NFC, any whitespace (also a no-break
+# space) collapsed to one space and trimmed, lower case. Two terms that
+# differ only in this are one term per organisation (unique index on
+# glossary_terms.normalized_term).
+#
+# Deliberately NOT PersonName.normalize: that one also writes umlauts and ß
+# out (ä -> ae, ß -> ss), which would make "Maße" and "Masse" one term.
+module GlossaryText
+ # The text as it is stored and shown: NFC, whitespace collapsed and trimmed,
+ # case kept.
+ def self.clean(value)
+ value.to_s.unicode_normalize(:nfc).gsub(/[[:space:]]+/, ' ').strip
+ end
+
+ # The text as it is compared.
+ def self.normalize(value)
+ clean(value).downcase
+ end
+end
diff --git a/app/models/term_correction.rb b/app/models/term_correction.rb
new file mode 100644
index 0000000..1c34c2c
--- /dev/null
+++ b/app/models/term_correction.rb
@@ -0,0 +1,85 @@
+# frozen_string_literal: true
+
+# A heard form of a glossary term (K4a): what speech recognition wrote, who
+# spoke, in which project and where in the transcript. One row per
+# correction, not per form: "how often and by whom" is a query, not a counter
+# that can drift. K4b writes the rows; K4a shows them and deletes one that was
+# learnt wrong.
+#
+# The speaker is a person of the directory (person_id) or a member
+# (user_id), at most one of them, the rule of meeting_participants: the
+# validation one_speaker and the check constraint term_corrections_one_speaker
+# (which also catches update_all) hold it. A speaker without a link stays
+# empty.
+#
+# Deleting the meeting keeps the row with its place (line_index, char_offset,
+# transcript_digest) and only clears meeting_id (E4, on_delete: :nullify; see
+# Meeting#purge_fragment_content_embeddings). Deleting the project or the
+# person clears the origin or the speaker the same way. Deleting the term or
+# the organisation takes the row with it.
+class TermCorrection < ApplicationRecord
+ # The references to rows of an organisation, declared BEFORE acts_as_tenant:
+ # acts_as_tenant 1.0.1 checks the tenant only of references declared before
+ # the call, and only through the current tenant. same_organisation_references
+ # holds them to the row's organisation on top, with or without a tenant set.
+ TENANT_REFERENCES = %i[glossary_term project person meeting].freeze
+
+ belongs_to :glossary_term
+ belongs_to :project, optional: true
+ belongs_to :person, optional: true
+ belongs_to :meeting, optional: true
+
+ acts_as_tenant(:organisation)
+
+ # The speaker as a member, and who corrected. Users have no organisation of
+ # their own; the speaker must be a member of this one.
+ belongs_to :user, optional: true
+ belongs_to :corrected_by, class_name: 'User', optional: true
+
+ DIGEST_FORMAT = /\A\h{64}\z/
+
+ before_validation :normalize_heard
+
+ validates :heard, presence: true, length: { maximum: GlossaryTerm::MAX_LENGTH }
+ validates :normalized_heard, presence: true
+ validates :replaced_count, numericality: { only_integer: true, greater_than_or_equal_to: 1 }
+ validates :line_index, :char_offset, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true
+ validates :transcript_digest, format: { with: DIGEST_FORMAT }, allow_nil: true
+ validate :one_speaker
+ validate :same_organisation_references
+ validate :speaker_is_member, if: -> { user_id.present? && will_save_change_to_user_id? }
+
+ # Newest first within a heard form: the order of the lane.
+ scope :in_lane_order, -> { order(:normalized_heard, created_at: :desc, id: :asc) }
+
+ private
+
+ def normalize_heard
+ self.heard = GlossaryText.clean(heard) unless heard.nil?
+ self.normalized_heard = GlossaryText.normalize(heard)
+ end
+
+ def one_speaker
+ errors.add(:base, :one_speaker) if person_id.present? && user_id.present?
+ end
+
+ # Read without the tenant's scope: a reference to a row of another
+ # organisation must fail, not read as "no row" (an optional reference would
+ # pass that way and keep the foreign id).
+ def same_organisation_references
+ TENANT_REFERENCES.each do |name|
+ reflection = self.class.reflect_on_association(name)
+ id = public_send(reflection.foreign_key)
+ next if id.blank? || !will_save_change_to_attribute?(reflection.foreign_key)
+
+ owner = ActsAsTenant.without_tenant { reflection.klass.unscoped.where(id: id).pick(:organisation_id) }
+ errors.add(name, :invalid) unless owner && owner == organisation_id
+ end
+ end
+
+ def speaker_is_member
+ return if Membership.exists?(organisation_id: organisation_id, user_id: user_id)
+
+ errors.add(:user, :invalid)
+ end
+end
diff --git a/config/locales/models.de.yml b/config/locales/models.de.yml
index 605a2a3..d91f790 100644
--- a/config/locales/models.de.yml
+++ b/config/locales/models.de.yml
@@ -2,6 +2,17 @@
de:
activerecord:
attributes:
+ glossary_term:
+ term: "Begriff"
+ category: "Kategorie"
+ status: "Status"
+ term_correction:
+ heard: "Gehörte Form"
+ user: "Sprecher"
+ person: "Sprecher"
+ project: "Projekt"
+ meeting: "Meeting"
+ glossary_term: "Begriff"
organisation:
meeting_inbox_project_id: "Eingangsprojekt"
person:
@@ -80,6 +91,12 @@ de:
file: "PDF"
errors:
models:
+ glossary_term:
+ attributes:
+ term:
+ taken: "steht schon im Glossar"
+ term_correction:
+ one_speaker: "Ein Sprecher ist entweder ein Mitglied oder ein Kontakt, nie beides."
person:
attributes:
additional_details:
diff --git a/config/locales/models.en.yml b/config/locales/models.en.yml
index 52649a5..4e29e63 100644
--- a/config/locales/models.en.yml
+++ b/config/locales/models.en.yml
@@ -2,6 +2,17 @@
en:
activerecord:
attributes:
+ glossary_term:
+ term: "Term"
+ category: "Category"
+ status: "Status"
+ term_correction:
+ heard: "Heard form"
+ user: "Speaker"
+ person: "Speaker"
+ project: "Project"
+ meeting: "Meeting"
+ glossary_term: "Term"
organisation:
meeting_inbox_project_id: "Inbox project"
person:
@@ -80,6 +91,12 @@ en:
file: "PDF"
errors:
models:
+ glossary_term:
+ attributes:
+ term:
+ taken: "is already in the glossary"
+ term_correction:
+ one_speaker: "A speaker is either a member or a contact, never both."
person:
attributes:
additional_details:
diff --git a/db/migrate/20261104100000_create_glossary_terms.rb b/db/migrate/20261104100000_create_glossary_terms.rb
new file mode 100644
index 0000000..d33b689
--- /dev/null
+++ b/db/migrate/20261104100000_create_glossary_terms.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+# The glossary of an organisation (K4a): one row per term in its right
+# spelling. normalized_term (lower case, NFC, whitespace collapsed,
+# GlossaryText) is unique per organisation, so one spelling cannot stand
+# twice. status: proposed, confirmed, rejected; a rejected term stays, so it
+# is not proposed again.
+class CreateGlossaryTerms < ActiveRecord::Migration[8.1]
+ def change
+ create_table :glossary_terms, id: :uuid do |t|
+ # No index of its own: the unique index below starts with organisation_id.
+ t.references :organisation, type: :uuid, null: false, index: false, foreign_key: { on_delete: :cascade }
+ t.string :term, null: false
+ t.string :normalized_term, null: false
+ t.string :category
+ t.string :status, null: false, default: 'confirmed'
+ t.references :created_by, type: :uuid, foreign_key: { to_table: :users, on_delete: :nullify }
+ t.timestamps
+ t.index %i[organisation_id normalized_term], unique: true
+ end
+ end
+end
diff --git a/db/migrate/20261104100100_create_term_corrections.rb b/db/migrate/20261104100100_create_term_corrections.rb
new file mode 100644
index 0000000..93d98ed
--- /dev/null
+++ b/db/migrate/20261104100100_create_term_corrections.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+# A heard form of a glossary term (K4a): what speech recognition wrote, who
+# spoke, in which project and where. One row per correction, not per form:
+# "how often and by whom" is a query, not a counter that can drift.
+#
+# The speaker is a person of the directory or a member, at most one of them
+# (the rule of meeting_participants). line_index, char_offset and
+# transcript_digest let a device find the place in its own copy of the text.
+#
+# Deleting a meeting keeps its corrections with their place and only clears
+# meeting_id (on_delete: :nullify): a correction is knowledge of the
+# organisation, not content of the conversation. Deleting a project or a
+# person clears the origin or the speaker the same way.
+class CreateTermCorrections < ActiveRecord::Migration[8.1]
+ def change
+ create_table :term_corrections, id: :uuid do |t|
+ t.references :organisation, type: :uuid, null: false, foreign_key: { on_delete: :cascade }
+ # No index of its own: the index on (glossary_term_id, normalized_heard) starts with it.
+ t.references :glossary_term, type: :uuid, null: false, index: false, foreign_key: { on_delete: :cascade }
+ t.references :project, type: :uuid, foreign_key: { on_delete: :nullify }
+ t.string :heard, null: false
+ t.string :normalized_heard, null: false
+ t.references :person, type: :uuid, foreign_key: { on_delete: :nullify }
+ t.references :user, type: :uuid, foreign_key: { on_delete: :nullify }
+ t.references :meeting, type: :uuid, foreign_key: { on_delete: :nullify }
+ t.integer :line_index
+ t.integer :char_offset
+ t.string :transcript_digest
+ t.integer :replaced_count, null: false, default: 1
+ t.references :corrected_by, type: :uuid, foreign_key: { to_table: :users, on_delete: :nullify }
+ t.timestamps
+ t.index %i[glossary_term_id normalized_heard], name: 'index_term_corrections_on_term_and_heard'
+ t.check_constraint 'num_nonnulls(person_id, user_id) <= 1', name: 'term_corrections_one_speaker'
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 00440ce..658786e 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[8.1].define(version: 2026_11_01_120000) do
+ActiveRecord::Schema[8.1].define(version: 2026_11_04_100100) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
enable_extension "pgcrypto"
@@ -716,6 +716,19 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_01_120000) do
t.index ["version_token"], name: "index_fragments_on_version_token"
end
+ create_table "glossary_terms", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+ t.uuid "organisation_id", null: false
+ t.string "term", null: false
+ t.string "normalized_term", null: false
+ t.string "category"
+ t.string "status", default: "confirmed", null: false
+ t.uuid "created_by_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["created_by_id"], name: "index_glossary_terms_on_created_by_id"
+ t.index ["organisation_id", "normalized_term"], name: "index_glossary_terms_on_organisation_id_and_normalized_term", unique: true
+ end
+
create_table "imported_objects", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "organisation_id", null: false
t.string "importable_type", null: false
@@ -1677,6 +1690,32 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_01_120000) do
t.index ["user_id"], name: "index_tags_on_user_id"
end
+ create_table "term_corrections", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
+ t.uuid "organisation_id", null: false
+ t.uuid "glossary_term_id", null: false
+ t.uuid "project_id"
+ t.string "heard", null: false
+ t.string "normalized_heard", null: false
+ t.uuid "person_id"
+ t.uuid "user_id"
+ t.uuid "meeting_id"
+ t.integer "line_index"
+ t.integer "char_offset"
+ t.string "transcript_digest"
+ t.integer "replaced_count", default: 1, null: false
+ t.uuid "corrected_by_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["corrected_by_id"], name: "index_term_corrections_on_corrected_by_id"
+ t.index ["glossary_term_id", "normalized_heard"], name: "index_term_corrections_on_term_and_heard"
+ t.index ["meeting_id"], name: "index_term_corrections_on_meeting_id"
+ t.index ["organisation_id"], name: "index_term_corrections_on_organisation_id"
+ t.index ["person_id"], name: "index_term_corrections_on_person_id"
+ t.index ["project_id"], name: "index_term_corrections_on_project_id"
+ t.index ["user_id"], name: "index_term_corrections_on_user_id"
+ t.check_constraint "num_nonnulls(person_id, user_id) <= 1", name: "term_corrections_one_speaker"
+ end
+
create_table "ticket_assignments", force: :cascade do |t|
t.uuid "ticket_id", null: false
t.uuid "user_id", null: false
@@ -2075,6 +2114,8 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_01_120000) do
add_foreign_key "embedding_spaces", "organisations", on_delete: :cascade
add_foreign_key "founder_signups", "accounts"
add_foreign_key "fragments", "organisations"
+ add_foreign_key "glossary_terms", "organisations", on_delete: :cascade
+ add_foreign_key "glossary_terms", "users", column: "created_by_id", on_delete: :nullify
add_foreign_key "imported_objects", "organisations"
add_foreign_key "mcp_idempotency_keys", "personal_access_tokens"
add_foreign_key "meeting_analyses", "analysis_prompts", on_delete: :nullify
@@ -2182,6 +2223,13 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_01_120000) do
add_foreign_key "taggings", "users"
add_foreign_key "tags", "organisations"
add_foreign_key "tags", "users"
+ add_foreign_key "term_corrections", "glossary_terms", on_delete: :cascade
+ add_foreign_key "term_corrections", "meetings", on_delete: :nullify
+ add_foreign_key "term_corrections", "organisations", on_delete: :cascade
+ add_foreign_key "term_corrections", "people", on_delete: :nullify
+ add_foreign_key "term_corrections", "projects", on_delete: :nullify
+ add_foreign_key "term_corrections", "users", column: "corrected_by_id", on_delete: :nullify
+ add_foreign_key "term_corrections", "users", on_delete: :nullify
add_foreign_key "ticket_attribute_definitions", "projects"
add_foreign_key "ticket_bulk_runs", "organisations"
add_foreign_key "ticket_bulk_runs", "projects"
diff --git a/spec/factories/glossary_terms.rb b/spec/factories/glossary_terms.rb
new file mode 100644
index 0000000..29e91d3
--- /dev/null
+++ b/spec/factories/glossary_terms.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+# Invented terms only (no real customers or products in fixtures).
+FactoryBot.define do
+ factory :glossary_term do
+ sequence(:term) { |n| "Beispielwort #{n}" }
+ status { 'confirmed' }
+ association :organisation
+
+ trait :proposed do
+ status { 'proposed' }
+ end
+
+ trait :rejected do
+ status { 'rejected' }
+ end
+ end
+end
diff --git a/spec/factories/term_corrections.rb b/spec/factories/term_corrections.rb
new file mode 100644
index 0000000..96c4d58
--- /dev/null
+++ b/spec/factories/term_corrections.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+
+# A heard form of a glossary term. Invented words only.
+FactoryBot.define do
+ factory :term_correction do
+ sequence(:heard) { |n| "bei spiel wort #{n}" }
+ glossary_term { association :glossary_term, organisation: organisation }
+ association :organisation
+ end
+end
diff --git a/spec/models/glossary_term_spec.rb b/spec/models/glossary_term_spec.rb
new file mode 100644
index 0000000..a18dce7
--- /dev/null
+++ b/spec/models/glossary_term_spec.rb
@@ -0,0 +1,117 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# The glossary of an organisation (K4a). Invented words only.
+RSpec.describe GlossaryTerm, :ci_guard, type: :model do
+ let(:organisation) { create(:organisation) }
+ let(:other_organisation) { create(:organisation) }
+
+ before { ActsAsTenant.current_tenant = organisation }
+ after { ActsAsTenant.current_tenant = nil }
+
+ it 'is bound to the tenant' do
+ mine = create(:glossary_term, organisation: organisation)
+ theirs = ActsAsTenant.with_tenant(other_organisation) { create(:glossary_term, organisation: other_organisation) }
+
+ expect(described_class.all).to contain_exactly(mine)
+ expect(described_class.unscoped.where(id: theirs.id)).to exist
+ end
+
+ it 'requires a term' do
+ term = build(:glossary_term, organisation: organisation, term: " \u00A0 ")
+
+ expect(term).not_to be_valid
+ expect(term.errors[:term]).to be_present
+ end
+
+ it 'refuses a term longer than the limit' do
+ term = build(:glossary_term, organisation: organisation, term: 'a' * (described_class::MAX_LENGTH + 1))
+
+ expect(term).not_to be_valid
+ expect(build(:glossary_term, organisation: organisation, term: 'a' * described_class::MAX_LENGTH)).to be_valid
+ end
+
+ describe 'normalisation (GlossaryText)' do
+ it 'keeps the spelling, collapses any whitespace and stores the lower case form' do
+ term = create(:glossary_term, organisation: organisation, term: " Lala\u00A0 Base\t")
+
+ expect(term.term).to eq('Lala Base')
+ expect(term.normalized_term).to eq('lala base')
+ end
+
+ it 'composes to NFC, so a decomposed umlaut is the same term' do
+ create(:glossary_term, organisation: organisation, term: "Mu\u0308hlstein")
+ twin = build(:glossary_term, organisation: organisation, term: "M\u00FChlstein")
+
+ expect(twin).not_to be_valid
+ expect(twin.errors.details[:term]).to include(error: :taken)
+ end
+
+ it 'does not write umlauts or ß out, unlike PersonName' do
+ create(:glossary_term, organisation: organisation, term: 'Maße')
+
+ expect(build(:glossary_term, organisation: organisation, term: 'Masse')).to be_valid
+ expect(build(:glossary_term, organisation: organisation, term: 'Mueller')).to be_valid
+ expect(described_class.last.normalized_term).to eq('maße')
+ end
+ end
+
+ describe 'one spelling per organisation' do
+ it 'refuses a term that differs only in case and whitespace, with the message on term' do
+ create(:glossary_term, organisation: organisation, term: 'Lalabase')
+ twin = build(:glossary_term, organisation: organisation, term: ' lalaBASE ')
+
+ expect(twin).not_to be_valid
+ expect(twin.errors.details[:term]).to include(error: :taken)
+ expect(twin.errors.full_messages.join).to include(described_class.human_attribute_name(:term))
+ end
+
+ it 'allows the same term in another organisation' do
+ ActsAsTenant.with_tenant(other_organisation) { create(:glossary_term, organisation: other_organisation, term: 'Lalabase') }
+
+ expect(build(:glossary_term, organisation: organisation, term: 'Lalabase')).to be_valid
+ end
+
+ it 'keeps its own spelling valid on a later save' do
+ term = create(:glossary_term, organisation: organisation, term: 'Lalabase')
+
+ expect(term.update(category: 'product')).to be(true)
+ end
+
+ it 'is held by the unique index when the validation is skipped' do
+ create(:glossary_term, organisation: organisation, term: 'Lalabase')
+ twin = build(:glossary_term, organisation: organisation, term: 'LALABASE')
+ twin.valid? # runs the normalisation, which save(validate: false) skips
+
+ expect { twin.save!(validate: false) }.to raise_error(ActiveRecord::RecordNotUnique)
+ end
+ end
+
+ describe 'category and status' do
+ it 'stores a blank category as none' do
+ expect(create(:glossary_term, organisation: organisation, category: '').category).to be_nil
+ end
+
+ it 'accepts the six categories and refuses any other' do
+ described_class::CATEGORIES.each do |category|
+ expect(build(:glossary_term, organisation: organisation, category: category)).to be_valid
+ end
+ expect(build(:glossary_term, organisation: organisation, category: 'planet')).not_to be_valid
+ end
+
+ it 'is confirmed by default and refuses an unknown status' do
+ expect(described_class.new.status).to eq('confirmed')
+ expect(build(:glossary_term, organisation: organisation, status: 'maybe')).not_to be_valid
+ end
+ end
+
+ it 'orders open proposals first, then confirmed, then rejected, each by spelling' do
+ rejected = create(:glossary_term, :rejected, organisation: organisation, term: 'Aalwort')
+ confirmed_b = create(:glossary_term, organisation: organisation, term: 'Bergwort')
+ confirmed_a = create(:glossary_term, organisation: organisation, term: 'apfelwort')
+ proposed = create(:glossary_term, :proposed, organisation: organisation, term: 'Zielwort')
+
+ expect(described_class.ordered.to_a).to eq([proposed, confirmed_a, confirmed_b, rejected])
+ end
+end
diff --git a/spec/models/term_correction_spec.rb b/spec/models/term_correction_spec.rb
new file mode 100644
index 0000000..d894fc4
--- /dev/null
+++ b/spec/models/term_correction_spec.rb
@@ -0,0 +1,180 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# A heard form of a glossary term (K4a): tenant bound, at most one speaker,
+# references only within the organisation, and the delete rules of plan §3.3.
+# Invented words and names only.
+RSpec.describe TermCorrection, :ci_guard, type: :model do
+ let(:organisation) { create(:organisation) }
+ let(:other_organisation) { create(:organisation) }
+
+ before { ActsAsTenant.current_tenant = organisation }
+ after { ActsAsTenant.current_tenant = nil }
+
+ let(:term) { create(:glossary_term, organisation: organisation, term: 'Lalabase') }
+ let(:project) { create(:project, organisation: organisation) }
+
+ def member_of(org)
+ user = create(:user)
+ create(:membership, user: user, organisation: org)
+ user
+ end
+
+ it 'is bound to the tenant' do
+ mine = create(:term_correction, organisation: organisation, glossary_term: term)
+ theirs = ActsAsTenant.with_tenant(other_organisation) { create(:term_correction, organisation: other_organisation) }
+
+ expect(described_class.all).to contain_exactly(mine)
+ expect(described_class.unscoped.where(id: theirs.id)).to exist
+ end
+
+ it 'keeps the heard form as marked and stores its compared form' do
+ correction = create(:term_correction, organisation: organisation, glossary_term: term, heard: " Lala\u00A0 Bass ")
+
+ expect(correction.heard).to eq('Lala Bass')
+ expect(correction.normalized_heard).to eq('lala bass')
+ end
+
+ it 'requires a heard form' do
+ correction = build(:term_correction, organisation: organisation, glossary_term: term, heard: ' ')
+
+ expect(correction).not_to be_valid
+ expect(correction.errors[:heard]).to be_present
+ end
+
+ it 'checks the place and the count' do
+ expect(build(:term_correction, organisation: organisation, glossary_term: term, replaced_count: 0)).not_to be_valid
+ expect(build(:term_correction, organisation: organisation, glossary_term: term, line_index: -1)).not_to be_valid
+ expect(build(:term_correction, organisation: organisation, glossary_term: term, transcript_digest: 'abc')).not_to be_valid
+ expect(build(:term_correction, organisation: organisation, glossary_term: term, line_index: 0, char_offset: 4,
+ transcript_digest: 'a' * 64)).to be_valid
+ end
+
+ describe 'the speaker' do
+ let(:person) { create(:person, organisation: organisation) }
+ let(:member) { member_of(organisation) }
+
+ it 'is a person or a member' do
+ expect(build(:term_correction, organisation: organisation, glossary_term: term, person: person)).to be_valid
+ expect(build(:term_correction, organisation: organisation, glossary_term: term, user: member)).to be_valid
+ end
+
+ it 'is never both, in the model' do
+ correction = build(:term_correction, organisation: organisation, glossary_term: term, person: person, user: member)
+
+ expect(correction).not_to be_valid
+ expect(correction.errors[:base]).to be_present
+ end
+
+ it 'is never both, in the database' do
+ correction = create(:term_correction, organisation: organisation, glossary_term: term, person: person)
+
+ expect { described_class.where(id: correction.id).update_all(user_id: member.id) }
+ .to raise_error(ActiveRecord::StatementInvalid, /term_corrections_one_speaker/)
+ end
+
+ it 'is a member of this organisation' do
+ stranger = member_of(other_organisation)
+ correction = build(:term_correction, organisation: organisation, glossary_term: term, user: stranger)
+
+ expect(correction).not_to be_valid
+ expect(correction.errors[:user]).to be_present
+ end
+ end
+
+ describe 'references to rows of another organisation' do
+ let(:foreign) do
+ ActsAsTenant.with_tenant(other_organisation) do
+ foreign_project = create(:project, organisation: other_organisation)
+ {
+ glossary_term: create(:glossary_term, organisation: other_organisation),
+ project: foreign_project,
+ person: create(:person, organisation: other_organisation),
+ meeting: create(:meeting, organisation: other_organisation, project: foreign_project)
+ }
+ end
+ end
+
+ TermCorrection::TENANT_REFERENCES.each do |name|
+ it "refuses a #{name} of another organisation" do
+ correction = build(:term_correction, organisation: organisation, glossary_term: term)
+ correction.public_send(:"#{name}_id=", foreign.fetch(name).id)
+
+ expect(correction).not_to be_valid
+ expect(correction.errors[name]).to be_present
+ end
+
+ it "refuses a #{name} of another organisation without a tenant set" do
+ attributes = { organisation: organisation, glossary_term: term }
+ foreign_id = foreign.fetch(name).id
+ correction = ActsAsTenant.without_tenant do
+ built = build(:term_correction, **attributes)
+ built.public_send(:"#{name}_id=", foreign_id)
+ built.validate
+ built
+ end
+
+ expect(correction.errors[name]).to be_present
+ end
+ end
+ end
+
+ # Plan §3.3 (decided 04.11.2026).
+ describe 'deleting' do
+ let(:creator) { member_of(organisation) }
+ let(:meeting) { create(:meeting, organisation: organisation, project: project, creator: creator) }
+ let(:person) { create(:person, organisation: organisation) }
+ let!(:correction) do
+ create(:term_correction, organisation: organisation, glossary_term: term, project: project, meeting: meeting,
+ person: person, line_index: 3, char_offset: 12, transcript_digest: 'b' * 64)
+ end
+
+ it 'the term takes its heard forms along' do
+ term.destroy!
+
+ expect(described_class.unscoped.where(id: correction.id)).not_to exist
+ end
+
+ it 'the term takes them along in the database too' do
+ GlossaryTerm.where(id: term.id).delete_all
+
+ expect(described_class.unscoped.where(id: correction.id)).not_to exist
+ end
+
+ it 'the organisation takes terms and heard forms along' do
+ organisation.destroy!
+
+ expect(GlossaryTerm.unscoped.where(id: term.id)).not_to exist
+ expect(described_class.unscoped.where(id: correction.id)).not_to exist
+ end
+
+ # E4: a correction is knowledge of the organisation, confirmed by a person,
+ # not content of the conversation. It stays with its place; only the link
+ # to the meeting goes.
+ it 'the meeting leaves the heard form with its place and clears only meeting_id' do
+ meeting.destroy!
+ correction.reload
+
+ expect(correction.meeting_id).to be_nil
+ expect([correction.line_index, correction.char_offset, correction.transcript_digest]).to eq([3, 12, 'b' * 64])
+ expect([correction.project_id, correction.person_id, correction.glossary_term_id]).to eq([project.id, person.id, term.id])
+ end
+
+ it 'the project leaves the heard form without its origin' do
+ project.destroy!
+ correction.reload
+
+ expect(correction.project_id).to be_nil
+ expect(correction.glossary_term_id).to eq(term.id)
+ end
+
+ it 'the person leaves the heard form without its speaker' do
+ person.destroy!
+ correction.reload
+
+ expect(correction.person_id).to be_nil
+ expect(correction.meeting_id).to eq(meeting.id)
+ end
+ end
+end