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.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
feat(glossary): add the actors that keep the glossary by hand
Guido Boyke7c4b9f0cbe11
diff --git a/.github/workflows/guards.yml b/.github/workflows/guards.ymlindex 13e7fa7..fc79877 100644--- a/.github/workflows/guards.yml+++ b/.github/workflows/guards.yml@@ -347,6 +347,7 @@ jobs:spec/models/glossary_term_spec.rbspec/models/term_correction_spec.rbspec/requests/organisations/roles_glossary_right_spec.rb+ spec/services/actors/merge_glossary_terms_spec.rbspec/requests/organisations/people_spec.rbspec/requests/organisations/shown_email_spec.rbspec/requests/user_preferences_email_visibility_spec.rbdiff --git a/app/services/actors/destroy_glossary_term.rb b/app/services/actors/destroy_glossary_term.rbnew file mode 100644index 0000000..0d59c7e--- /dev/null+++ b/app/services/actors/destroy_glossary_term.rb@@ -0,0 +1,28 @@+# frozen_string_literal: true++# app/services/actors/destroy_glossary_term.rb+#+# Deletes one term of the glossary with its heard forms (K4a, plan §3.3).+# Rights are the caller's (GlossaryPolicy, manage_glossary).+module Actors+ class DestroyGlossaryTerm < BaseActor+ def initialize(term:, current_user:)+ super()+ @term = term+ @current_user = current_user+ end++ # A destroy leaves no surviving primary record.+ def record+ nil+ end++ def perform+ @term.destroy!+ end++ private++ attr_reader :current_user+ end+enddiff --git a/app/services/actors/destroy_term_correction.rb b/app/services/actors/destroy_term_correction.rbnew file mode 100644index 0000000..effc66b--- /dev/null+++ b/app/services/actors/destroy_term_correction.rb@@ -0,0 +1,29 @@+# 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+enddiff --git a/app/services/actors/merge_glossary_terms.rb b/app/services/actors/merge_glossary_terms.rbnew file mode 100644index 0000000..8fd994d--- /dev/null+++ b/app/services/actors/merge_glossary_terms.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true++# app/services/actors/merge_glossary_terms.rb+#+# Merges one glossary term into another (K4a, plan §6): two spellings of the+# same thing stood as two terms. The heard forms of the source move to the+# target, then the source is deleted. The target keeps its spelling, its+# category and its status, so no spelling can collide. Rights are the+# caller's (GlossaryPolicy, manage_glossary); the caller resolves the target+# 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.+module Actors+ class MergeGlossaryTerms < BaseActor+ def initialize(source:, target:, current_user:)+ super()+ @source = source+ @target = target+ @current_user = current_user+ end++ def record+ @target+ end++ def perform+ refuse(:same_term) if @source.id == @target.id+ refuse(:other_glossary) unless @source.organisation_id == @target.organisation_id++ lock_both+ TermCorrection.where(glossary_term_id: @source.id).update_all(glossary_term_id: @target.id, updated_at: Time.current)+ @target.touch+ @source.destroy!+ end++ private++ attr_reader :current_user++ def refuse(code)+ raise ActorError.new(I18n.t("controllers.glossary.merge.#{code}"), code: code)+ end++ # One query, rows locked in the order of their ids; the locked rows+ # replace the given ones, so the merge works on what is stored now.+ def lock_both+ locked = GlossaryTerm.where(id: [@source.id, @target.id]).order(:id).lock.index_by(&:id)+ @source = locked[@source.id]+ @target = locked[@target.id]+ raise ActorError.new(I18n.t('controllers.glossary.not_found'), code: :not_found) unless @source && @target+ end+ end+enddiff --git a/app/services/actors/save_glossary_term.rb b/app/services/actors/save_glossary_term.rbnew file mode 100644index 0000000..fa13a83--- /dev/null+++ b/app/services/actors/save_glossary_term.rb@@ -0,0 +1,59 @@+# frozen_string_literal: true++# app/services/actors/save_glossary_term.rb+#+# Creates or changes one term of the glossary by hand (K4a): its spelling and+# its category. Rights are the caller's (GlossaryPolicy, manage_glossary).+#+# * A term written by hand is confirmed and remembers who created it; the+# status changes only through Actors::SetGlossaryTermStatus.+# * One spelling once per organisation (GlossaryTerm#term_not_taken). Two+# 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.+module Actors+ class SaveGlossaryTerm < BaseActor+ ATTRIBUTES = %i[term category].freeze++ def initialize(organisation:, params:, current_user:, id: nil)+ super()+ @organisation = organisation+ @params = params.to_h.symbolize_keys+ @current_user = current_user+ @term = find_term(id)+ end++ def record+ @term+ end++ def perform+ raise ActorError.new(I18n.t('controllers.glossary.not_found'), code: :not_found) if @term.nil?++ @term.assign_attributes(@params.slice(*ATTRIBUTES))+ if @term.new_record?+ @term.status = 'confirmed'+ @term.created_by = @current_user+ end+ save_term+ end++ private++ attr_reader :current_user++ def save_term+ @term.save!+ rescue ActiveRecord::RecordNotUnique+ @term.errors.add(:term, :taken)+ raise ActorError.new(@term.errors.full_messages, code: :validation_failed)+ end++ def find_term(id)+ return GlossaryTerm.new(organisation: @organisation) if id.blank?++ GlossaryTerm.where(organisation: @organisation).find_by(id: id)+ end+ end+enddiff --git a/app/services/actors/set_glossary_term_status.rb b/app/services/actors/set_glossary_term_status.rbnew file mode 100644index 0000000..dbe2ffa--- /dev/null+++ b/app/services/actors/set_glossary_term_status.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++# app/services/actors/set_glossary_term_status.rb+#+# Confirms or rejects one term of the glossary (K4a, E5). Rights are the+# 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.+module Actors+ class SetGlossaryTermStatus < BaseActor+ SETTABLE = %w[confirmed rejected].freeze++ def initialize(term:, status:, current_user:)+ super()+ @term = term+ @status = status.to_s+ @current_user = current_user+ end++ def record+ @term+ end++ def perform+ raise ActorError.new(I18n.t('controllers.glossary.status.not_allowed'), code: :invalid_status) unless SETTABLE.include?(@status)++ @term.update!(status: @status)+ end++ private++ attr_reader :current_user+ end+enddiff --git a/config/locales/views/glossar.de.yml b/config/locales/views/glossar.de.ymlnew file mode 100644index 0000000..c9b7871--- /dev/null+++ b/config/locales/views/glossar.de.yml@@ -0,0 +1,25 @@+---+de:+ controllers:+ glossary:+ not_found: "Diesen Begriff gibt es nicht mehr."+ create:+ success: "Begriff angelegt."+ update:+ success: "Begriff gespeichert."+ destroy:+ success: "Begriff gelöscht."+ confirm:+ success: "Begriff bestätigt."+ reject:+ success: "Begriff verworfen."+ status:+ not_allowed: "Diesen Status kann man hier nicht setzen."+ merge:+ success: "Begriffe zusammengeführt."+ 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:+ destroy:+ success: "Gehörte Form gelöscht."diff --git a/config/locales/views/glossar.en.yml b/config/locales/views/glossar.en.ymlnew file mode 100644index 0000000..76c9ff7--- /dev/null+++ b/config/locales/views/glossar.en.yml@@ -0,0 +1,25 @@+---+en:+ controllers:+ glossary:+ not_found: "This term no longer exists."+ create:+ success: "Term created."+ update:+ success: "Term saved."+ destroy:+ success: "Term deleted."+ confirm:+ success: "Term confirmed."+ reject:+ success: "Term rejected."+ status:+ not_allowed: "This status cannot be set here."+ merge:+ success: "Terms merged."+ 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:+ destroy:+ success: "Heard form deleted."diff --git a/spec/services/actors/destroy_glossary_term_spec.rb b/spec/services/actors/destroy_glossary_term_spec.rbnew file mode 100644index 0000000..b82ef91--- /dev/null+++ b/spec/services/actors/destroy_glossary_term_spec.rb@@ -0,0 +1,26 @@+# frozen_string_literal: true++require 'rails_helper'++# Deleting a glossary term takes its heard forms along (K4a, plan §3.3).+RSpec.describe Actors::DestroyGlossaryTerm, type: :service do+ let(:organisation) { create(:organisation) }+ let(:writer) { create(:user) }++ before { ActsAsTenant.current_tenant = organisation }+ after { ActsAsTenant.current_tenant = nil }++ it 'deletes the term and its heard forms, and no other term' do+ term = create(:glossary_term, organisation: organisation)+ correction = create(:term_correction, organisation: organisation, glossary_term: term)+ other = create(:glossary_term, organisation: organisation)+ kept = create(:term_correction, organisation: organisation, glossary_term: other)++ result = described_class.call(term: term, current_user: writer)++ expect(result).to be_success+ expect(GlossaryTerm.where(id: term.id)).not_to exist+ expect(TermCorrection.where(id: correction.id)).not_to exist+ expect(TermCorrection.where(id: kept.id)).to exist+ end+enddiff --git a/spec/services/actors/destroy_term_correction_spec.rb b/spec/services/actors/destroy_term_correction_spec.rbnew file mode 100644index 0000000..4148a3e--- /dev/null+++ b/spec/services/actors/destroy_term_correction_spec.rb@@ -0,0 +1,25 @@+# 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+enddiff --git a/spec/services/actors/merge_glossary_terms_spec.rb b/spec/services/actors/merge_glossary_terms_spec.rbnew file mode 100644index 0000000..185f6ad--- /dev/null+++ b/spec/services/actors/merge_glossary_terms_spec.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++require 'rails_helper'++# Merging two terms of the glossary (K4a, plan §6): the heard forms move to+# the target, the source goes; never a term into itself, never across+# organisations. Invented words and names only.+RSpec.describe Actors::MergeGlossaryTerms, :ci_guard, type: :service do+ let(:organisation) { create(:organisation) }+ let(:other_organisation) { create(:organisation) }+ let(:writer) { create(:user) }++ before { ActsAsTenant.current_tenant = organisation }+ after { ActsAsTenant.current_tenant = nil }++ let(:project) { create(:project, organisation: organisation) }+ let(:person) { create(:person, organisation: organisation) }+ let(:source) { create(:glossary_term, organisation: organisation, term: 'Lala Base', category: 'domain') }+ let(:target) { create(:glossary_term, :proposed, organisation: organisation, term: 'Lalabase', category: 'product') }++ def merge(from = source, into = target)+ described_class.call(source: from, target: into, current_user: writer)+ end++ it 'moves every heard form of the source to the target, with speaker, project and place' do+ moved = create(:term_correction, organisation: organisation, glossary_term: source, heard: 'lala bass', project: project,+ person: person, line_index: 2, char_offset: 5)+ second = create(:term_correction, organisation: organisation, glossary_term: source, heard: 'la la base')+ own = create(:term_correction, organisation: organisation, glossary_term: target, heard: 'lalabas')++ result = merge++ expect(result).to be_success+ expect(result.resource).to eq(target)+ expect(target.term_corrections.reload).to contain_exactly(moved, second, own)+ moved.reload+ expect([moved.heard, moved.project_id, moved.person_id, moved.line_index, moved.char_offset])+ .to eq(['lala bass', project.id, person.id, 2, 5])+ end++ it 'deletes the source and keeps spelling, category and status of the target' do+ merge++ expect(GlossaryTerm.where(id: source.id)).not_to exist+ target.reload+ expect([target.term, target.category, target.status]).to eq(%w[Lalabase product proposed])+ end++ it 'refuses a term into itself and changes nothing' do+ create(:term_correction, organisation: organisation, glossary_term: source)++ result = merge(source, source)++ expect(result).not_to be_success+ expect(result.code).to eq(:same_term)+ expect(result.errors.join).to eq(I18n.t('controllers.glossary.merge.same_term'))+ expect(source.reload.term_corrections.count).to eq(1)+ end++ it 'refuses a term of another organisation and changes nothing' do+ create(:term_correction, organisation: organisation, glossary_term: source)+ foreign = ActsAsTenant.with_tenant(other_organisation) { create(:glossary_term, organisation: other_organisation) }++ result = merge(source, foreign)++ expect(result).not_to be_success+ expect(result.code).to eq(:other_glossary)+ expect(GlossaryTerm.where(id: source.id)).to exist+ expect(TermCorrection.unscoped.where(glossary_term_id: foreign.id)).not_to exist+ end++ it 'refuses a target deleted in the meantime' do+ create(:term_correction, organisation: organisation, glossary_term: source)+ gone = target+ GlossaryTerm.where(id: gone.id).delete_all++ result = merge(source, gone)++ expect(result).not_to be_success+ expect(result.code).to eq(:not_found)+ expect(source.reload.term_corrections.count).to eq(1)+ end+enddiff --git a/spec/services/actors/save_glossary_term_spec.rb b/spec/services/actors/save_glossary_term_spec.rbnew file mode 100644index 0000000..51f9814--- /dev/null+++ b/spec/services/actors/save_glossary_term_spec.rb@@ -0,0 +1,95 @@+# frozen_string_literal: true++require 'rails_helper'++# Creating and changing a glossary term by hand (K4a). Invented words only.+RSpec.describe Actors::SaveGlossaryTerm, type: :service do+ let(:organisation) { create(:organisation) }+ let(:other_organisation) { create(:organisation) }+ let(:writer) { create(:user) }++ before { ActsAsTenant.current_tenant = organisation }+ after { ActsAsTenant.current_tenant = nil }++ def save(params, id: nil)+ described_class.call(organisation: organisation, params: params, current_user: writer, id: id)+ end++ it 'creates a confirmed term with its category and its creator' do+ result = save({ term: ' Lala Base ', category: 'product' })++ expect(result).to be_success+ term = result.resource+ expect([term.term, term.normalized_term, term.category, term.status]).to eq(['Lala Base', 'lala base', 'product', 'confirmed'])+ expect(term.created_by).to eq(writer)+ expect(term.organisation).to eq(organisation)+ end++ it 'takes neither a status nor a creator from the params' do+ other = create(:user)+ result = save({ term: 'Lalabase', status: 'rejected', created_by_id: other.id, organisation_id: other_organisation.id })++ expect(result.resource.reload.status).to eq('confirmed')+ expect(result.resource.created_by).to eq(writer)+ expect(result.resource.organisation).to eq(organisation)+ end++ it 'changes spelling and category of a term, and keeps its status' do+ term = create(:glossary_term, :proposed, organisation: organisation, term: 'Lalabse')++ result = save({ term: 'Lalabase', category: '' }, id: term.id)++ expect(result).to be_success+ expect(term.reload.term).to eq('Lalabase')+ expect(term.category).to be_nil+ expect(term.status).to eq('proposed')+ end++ it 'refuses a spelling that is already in the glossary, with a message' do+ create(:glossary_term, organisation: organisation, term: 'Lalabase')++ result = save({ term: 'LALABASE' })++ expect(result).not_to be_success+ expect(result.code).to eq(:validation_failed)+ expect(result.errors.join).to include(I18n.t('activerecord.errors.models.glossary_term.attributes.term.taken'))+ end++ it 'refuses a rename onto another term, with a message' do+ create(:glossary_term, organisation: organisation, term: 'Lalabase')+ term = create(:glossary_term, organisation: organisation, term: 'Lala Base')++ result = save({ term: 'lalabase' }, id: term.id)++ expect(result).not_to be_success+ expect(term.reload.term).to eq('Lala Base')+ end++ it 'answers a lost race on the unique index with the same refusal' do+ allow_any_instance_of(GlossaryTerm).to receive(:term_not_taken)+ create(:glossary_term, organisation: organisation, term: 'Lalabase')++ result = save({ term: 'lalabase' })++ expect(result).not_to be_success+ expect(result.code).to eq(:validation_failed)+ expect(result.errors.join).to include(I18n.t('activerecord.errors.models.glossary_term.attributes.term.taken'))+ end++ it 'refuses an unknown category' do+ result = save({ term: 'Lalabase', category: 'planet' })++ expect(result).not_to be_success+ expect(GlossaryTerm.count).to eq(0)+ end++ it 'finds no term of another organisation' do+ foreign = ActsAsTenant.with_tenant(other_organisation) { create(:glossary_term, organisation: other_organisation, term: 'Fremd') }++ result = save({ term: 'Gekapert' }, id: foreign.id)++ expect(result).not_to be_success+ expect(result.code).to eq(:not_found)+ expect(GlossaryTerm.unscoped.find(foreign.id).term).to eq('Fremd')+ end+enddiff --git a/spec/services/actors/set_glossary_term_status_spec.rb b/spec/services/actors/set_glossary_term_status_spec.rbnew file mode 100644index 0000000..9209e2e--- /dev/null+++ b/spec/services/actors/set_glossary_term_status_spec.rb@@ -0,0 +1,39 @@+# frozen_string_literal: true++require 'rails_helper'++# Confirming and rejecting a glossary term (K4a, E5).+RSpec.describe Actors::SetGlossaryTermStatus, 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, :proposed, organisation: organisation) }++ it 'confirms a proposal' do+ expect(described_class.call(term: term, status: 'confirmed', current_user: writer)).to be_success+ expect(term.reload.status).to eq('confirmed')+ end++ it 'rejects a proposal and can confirm it again later' do+ described_class.call(term: term, status: 'rejected', current_user: writer)+ expect(term.reload.status).to eq('rejected')++ described_class.call(term: term, status: 'confirmed', current_user: writer)+ expect(term.reload.status).to eq('confirmed')+ end++ it 'never proposes, and refuses any other value' do+ confirmed = create(:glossary_term, organisation: organisation)++ %w[proposed maybe].each do |status|+ result = described_class.call(term: confirmed, status: status, current_user: writer)++ expect(result).not_to be_success+ expect(result.code).to eq(:invalid_status)+ end+ expect(confirmed.reload.status).to eq('confirmed')+ end+end