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.

Edit the additional details of a person in the form

Guido Boyke51ee0df975d2
diff --git a/app/assets/stylesheets/application.tailwind.css b/app/assets/stylesheets/application.tailwind.css
index ecce0ee..096b956 100644
--- a/app/assets/stylesheets/application.tailwind.css
+++ b/app/assets/stylesheets/application.tailwind.css
@@ -502,5 +502,15 @@
.people-input--area { @apply resize-y leading-normal; }
.people-input--locked { @apply bg-cream-100 text-cream-700; }
.people-form__actions { @apply flex items-center gap-4 pt-0.5; }
+ /* The additional details (PersonDetail): kind, label and remove button on
+ one line, the value below across the first two columns. */
+ .people-form__details { @apply m-0 flex min-w-0 flex-col gap-2 border-0 p-0; }
+ .people-form__details legend { @apply mb-1 p-0 text-xs text-muted; }
+ .people-form__detail-rows { @apply flex flex-col gap-2; }
+ .people-detail-row { grid-template-columns: minmax(0, 8.5rem) minmax(0, 1fr) auto; @apply grid items-center gap-x-2 gap-y-1.5 border-b border-cream-200 pb-2; }
+ .people-detail-row__value { grid-column: 1 / 3; }
+ .people-form__add-detail { @apply inline-flex items-center gap-1.5 self-start; }
+ .people-form__add-detail[hidden], .people-form__hint[hidden] { display: none; }
+ .people-form__hint { @apply text-xs text-muted; }
.people-page :is(a, button, input, select, textarea, li[tabindex]):focus-visible { outline: 2px solid rgb(var(--ink)); outline-offset: 2px; }
}
diff --git a/app/helpers/people_helper.rb b/app/helpers/people_helper.rb
index 1b75b81..8c7aac5 100644
--- a/app/helpers/people_helper.rb
+++ b/app/helpers/people_helper.rb
@@ -143,6 +143,29 @@ module PeopleHelper
t("views.organisations.people.details.kinds.#{kind}")
end
+ # The kinds of the form's select: [name, kind], in PersonDetail's order.
+ def person_detail_kind_options
+ PersonDetail::KINDS.map { |kind| [person_detail_kind_name(kind), kind] }
+ end
+
+ # The value field and the label suggestions of a kind in the form. The same
+ # map goes to person_details_controller, which switches a row by it; an
+ # unknown kind (a hand-made request shown again) gets a plain text field.
+ def person_detail_field(kind)
+ kind = PersonDetail::KINDS.include?(kind) ? kind : 'text'
+ { input: PersonDetail::INPUT_TYPES.fetch(kind), maxlength: PersonDetail.max_value_length(kind),
+ list: ("person-detail-suggestions-#{kind}" if person_detail_suggestions(kind).any?) }
+ end
+
+ def person_detail_fields_config
+ PersonDetail::KINDS.index_with { |kind| person_detail_field(kind) }
+ end
+
+ # The label suggestions of a kind (a datalist each: "Mobil", "Fax", ...).
+ def person_detail_suggestions(kind)
+ Array(t("views.organisations.people.form.detail_suggestions.#{kind}", default: []))
+ end
+
# The value of a detail as the lane shows it.
def person_detail_value(detail)
date = detail.date
diff --git a/app/javascript/controllers/person_details_controller.js b/app/javascript/controllers/person_details_controller.js
new file mode 100644
index 0000000..34507b2
--- /dev/null
+++ b/app/javascript/controllers/person_details_controller.js
@@ -0,0 +1,75 @@
+// The additional details of a person in the people form (PersonDetail): add a
+// row from the template, remove one, and switch the value field and the label
+// suggestions when the kind changes. The kinds, their inputs and the limit come
+// from the server as values; nothing here names a kind.
+//
+// Without JavaScript the stored rows still render and save; the add button
+// stays hidden, because only adding needs this controller.
+import { Controller } from "@hotwired/stimulus"
+import { nextIndex, fillIndex, canAdd, valueField } from "person_details"
+
+export default class extends Controller {
+ static targets = ["list", "row", "template", "add", "limit"]
+ static values = { max: Number, kinds: Object }
+
+ connect() {
+ this.update()
+ }
+
+ add(event) {
+ event.preventDefault()
+ if (!canAdd(this.rowTargets.length, this.maxValue)) return
+
+ const index = nextIndex(this.rowTargets.map((row) => row.dataset.index))
+ this.listTarget.insertAdjacentHTML("beforeend", fillIndex(this.templateTarget.innerHTML, index))
+ this.update()
+ this.rowTargets[this.rowTargets.length - 1]?.querySelector("select")?.focus()
+ }
+
+ remove(event) {
+ event.preventDefault()
+ const row = this.rowTargets.find((candidate) => candidate.contains(event.currentTarget))
+ if (!row) return
+
+ row.remove()
+ this.update()
+ if (this.addTarget.hidden) return
+ this.addTarget.focus()
+ }
+
+ changeKind(event) {
+ const row = this.rowTargets.find((candidate) => candidate.contains(event.currentTarget))
+ if (!row) return
+
+ const field = valueField(this.kindsValue, event.currentTarget.value)
+ const label = row.querySelector("[data-person-details-label]")
+ if (label && field.list) label.setAttribute("list", field.list)
+ else label?.removeAttribute("list")
+
+ const current = row.querySelector("[data-person-details-value]")
+ if (!current) return
+ const next = current.tagName.toLowerCase() === field.tag ? current : this.rebuild(current, field.tag)
+ if (field.type) next.type = field.type
+ if (field.maxlength) next.maxLength = field.maxlength
+ }
+
+ // A textarea for an input or the other way round, with the same name, value
+ // and wiring.
+ rebuild(current, tag) {
+ const next = document.createElement(tag)
+ for (const attribute of current.attributes) {
+ if (!["type", "rows", "value"].includes(attribute.name)) next.setAttribute(attribute.name, attribute.value)
+ }
+ next.classList.toggle("people-input--area", tag === "textarea")
+ if (tag === "textarea") next.rows = 3
+ next.value = current.value
+ current.replaceWith(next)
+ return next
+ }
+
+ update() {
+ const room = canAdd(this.rowTargets.length, this.maxValue)
+ this.addTarget.hidden = !room
+ this.limitTarget.hidden = room
+ }
+}
diff --git a/app/javascript/lib/person_details.js b/app/javascript/lib/person_details.js
new file mode 100644
index 0000000..4757735
--- /dev/null
+++ b/app/javascript/lib/person_details.js
@@ -0,0 +1,40 @@
+// What: the pure part of person_details_controller.js (the additional details
+// of a person in the people form), so node:test can pin it.
+// Why: the rows travel as an indexed hash (person[additional_details][3][...]),
+// and the server orders them by that index. A new row needs an index no other
+// row has, or two rows merge into one on the server. The kinds, their inputs
+// and the limit come from the server (PersonDetail): nothing here knows a kind.
+
+// The index of a new row: one above the highest present, 0 for none. Gaps from
+// removed rows stay; only the order matters to the server.
+export function nextIndex(indexes) {
+ const numbers = indexes
+ .map((value) => (/^\d+$/.test(String(value)) ? Number(value) : NaN))
+ .filter((number) => Number.isSafeInteger(number))
+ return numbers.length === 0 ? 0 : Math.max(...numbers) + 1
+}
+
+// The markup of the row template with its placeholder index filled in.
+export function fillIndex(html, index) {
+ return html.replaceAll("__INDEX__", String(index))
+}
+
+// Whether one more row fits under the limit.
+export function canAdd(count, max) {
+ return Number.isInteger(max) && count < max
+}
+
+// The value field of a kind, from the server's kinds map
+// ({ phone: { input: "tel", maxlength: 255, list: "..." }, ... }): a textarea
+// for a multi-line kind, else an input of the given type. An unknown kind gets
+// a plain text input without suggestions.
+export function valueField(kinds, kind) {
+ const config = (kinds && Object.hasOwn(kinds, kind) && kinds[kind]) || {}
+ const multiLine = config.input === "textarea"
+ return {
+ tag: multiLine ? "textarea" : "input",
+ type: multiLine ? null : config.input || "text",
+ maxlength: config.maxlength ?? null,
+ list: config.list || null
+ }
+}
diff --git a/app/models/CLAUDE.md b/app/models/CLAUDE.md
index 3fddb4f..4d7bdba 100644
--- a/app/models/CLAUDE.md
+++ b/app/models/CLAUDE.md
@@ -86,6 +86,14 @@ Verzeichnis-Layout nicht ablesbar — diese Karte gruppiert sie:
Zeile und damit den Link. Macht das Formular eine Kontakt-Zeile zu der eines Mitglieds,
fällt `person_id` weg, und das Zuordnen (Scope `external`) lässt Kontakt-Zeilen aus.
Reihenfolge der Chip-Zeile: Scope `in_row_order` (Seite, Panel und Neuzeichnen).
+ Seit 28.10.2026 trägt eine Person bis zu 10 **weitere Angaben** (`people.additional_details`,
+ JSONB-Liste aus `kind`/`label`/`value`). Die Regel hat EINEN Besitzer: `person_detail`
+ (kein AR-Modell): Arten, Grenzen, Normalisierung, Prüfung, Link (`#href`, nur http(s)) und
+ Suchwerte (nur Werte, nie Bezeichnungen). Formular (Arten, Eingabetyp, `MAX`), Bahn, Suche und
+ Export fragen dort; der Stimulus-Controller bekommt Arten und Grenze als Values. Das Formular
+ schickt immer `person[additional_details][sent]` mit (`FORM_MARKER`): ohne ihn ließe das
+ Entfernen der letzten Angabe die Liste stehen. Fehlt der Key ganz, bleibt die Liste
+ unberührt. Check-Constraint `people_additional_details_shape` hält Liste und Grenze.
* **Activity/Notifications**: `activity`, `user_activity`, `outbox_event`, `notification`,
`notification_preference`, `notification_subscription`, `email_log` — Services dazu unter
`app/services/notifications/` (eigene Karte dort)
diff --git a/app/views/organisations/people/_detail_row.html.erb b/app/views/organisations/people/_detail_row.html.erb
new file mode 100644
index 0000000..3db27e6
--- /dev/null
+++ b/app/views/organisations/people/_detail_row.html.erb
@@ -0,0 +1,30 @@
+<%# locals: (index:, detail:) %>
+<%# One additional detail in the form (PersonDetail): kind, label, value and
+ a remove button. The rows travel as an indexed hash; the template row
+ carries __INDEX__, which person_details_controller replaces. The value
+ field and the label suggestions follow the kind (person_detail_field). %>
+<% field = person_detail_field(detail.kind) %>
+<% name = "person[additional_details][#{index}]" %>
+<div class="people-detail-row" data-person-details-target="row" data-index="<%= index %>">
+ <%= select_tag "#{name}[kind]", options_for_select(person_detail_kind_options, detail.kind),
+ id: nil, class: 'people-input people-detail-row__kind',
+ 'aria-label': t('views.organisations.people.form.detail_kind'),
+ data: { action: 'change->person-details#changeKind' } %>
+ <%= text_field_tag "#{name}[label]", detail.label, id: nil, class: 'people-input', maxlength: PersonDetail::MAX_LABEL_LENGTH,
+ list: field[:list], autocomplete: 'off',
+ placeholder: t('views.organisations.people.form.detail_label_placeholder'),
+ 'aria-label': t('views.organisations.people.form.detail_label'),
+ data: { person_details_label: true } %>
+ <button type="button" class="people-icon-action" data-action="person-details#remove"
+ aria-label="<%= t('views.organisations.people.form.remove_detail') %>"
+ title="<%= t('views.organisations.people.form.remove_detail') %>">
+ <i class="bi bi-x-lg" aria-hidden="true"></i>
+ </button>
+ <% value_options = { id: nil, maxlength: field[:maxlength], 'aria-label': t('views.organisations.people.form.detail_value'),
+ data: { person_details_value: true } } %>
+ <% if field[:input] == 'textarea' %>
+ <%= text_area_tag "#{name}[value]", detail.value, rows: 3, class: 'people-input people-input--area people-detail-row__value', **value_options %>
+ <% else %>
+ <%= tag.input type: field[:input], name: "#{name}[value]", value: detail.value, class: 'people-input people-detail-row__value', **value_options %>
+ <% end %>
+</div>
diff --git a/app/views/organisations/people/_form.html.erb b/app/views/organisations/people/_form.html.erb
index 7a0d448..4727d74 100644
--- a/app/views/organisations/people/_form.html.erb
+++ b/app/views/organisations/people/_form.html.erb
@@ -9,7 +9,11 @@
shown, locked, and not sent. Its link is fixed, so the member select is
left out (no user_id sent: SavePerson keeps the link). A deactivated
member's row keeps its link too, but its own name and email stay editable.
- A new row and a contact may be linked. %>
+ A new row and a contact may be linked.
+
+ The additional details (PersonDetail) always send the marker
+ person[additional_details][sent] next to their rows: removing the last row
+ clears the list instead of leaving it as it was. %>
<% member = person_form_member(person) %>
<% url = person.persisted? ? organisation_person_path(@current_organisation, person) : organisation_people_path(@current_organisation) %>
<% title = person.persisted? || member ? t('views.organisations.people.form.edit_title', name: person_display_name(person: person, user: member)) : t('views.organisations.people.form.new_title') %>
@@ -82,6 +86,37 @@
placeholder: t('views.organisations.people.form.placeholders.phone') %>
</div>
+ <fieldset class="people-form__details" data-controller="person-details"
+ data-person-details-max-value="<%= PersonDetail::MAX %>"
+ data-person-details-kinds-value="<%= person_detail_fields_config.to_json %>">
+ <legend><%= Person.human_attribute_name(:additional_details) %></legend>
+ <%= hidden_field_tag "person[additional_details][#{PersonDetail::FORM_MARKER}]", '1', id: nil %>
+ <div class="people-form__detail-rows" data-person-details-target="list">
+ <% person.additional_details.each_with_index do |entry, index| %>
+ <%= render 'organisations/people/detail_row', index: index, detail: PersonDetail.new(entry) %>
+ <% end %>
+ </div>
+ <template data-person-details-target="template">
+ <%= render 'organisations/people/detail_row', index: '__INDEX__', detail: PersonDetail.new('kind' => PersonDetail::KINDS.first) %>
+ </template>
+ <button type="button" class="people-quiet-action people-form__add-detail" hidden
+ data-person-details-target="add" data-action="person-details#add">
+ <i class="bi bi-plus-lg" aria-hidden="true"></i><%= t('views.organisations.people.form.add_detail') %>
+ </button>
+ <p class="people-form__hint" hidden data-person-details-target="limit">
+ <%= t('views.organisations.people.form.details_limit', count: PersonDetail::MAX) %>
+ </p>
+ <% PersonDetail::KINDS.each do |kind| %>
+ <% suggestions = person_detail_suggestions(kind) %>
+ <% next if suggestions.empty? %>
+ <datalist id="person-detail-suggestions-<%= kind %>">
+ <% suggestions.each do |suggestion| %>
+ <option value="<%= suggestion %>"></option>
+ <% end %>
+ </datalist>
+ <% end %>
+ </fieldset>
+
<div class="people-form__field">
<%= f.label :notes %>
<%= f.text_area :notes, rows: 4, class: 'people-input people-input--area', maxlength: Person::MAX_NOTES_LENGTH,
diff --git a/config/importmap.rb b/config/importmap.rb
index 1e41978..f1725ce 100644
--- a/config/importmap.rb
+++ b/config/importmap.rb
@@ -34,6 +34,8 @@ pin 'filter_bar/menu_renderer', to: 'lib/filter_bar/menu_renderer.js'
pin 'ticket_selection', to: 'lib/ticket_selection.js'
pin 'ticket_bulk_draft', to: 'lib/ticket_bulk_draft.js'
pin 'new_tag_name', to: 'lib/new_tag_name.js'
+# Row index, limit and value field of the additional details in the people form.
+pin 'person_details', to: 'lib/person_details.js'
pin 'filter_bar/chip_renderer', to: 'lib/filter_bar/chip_renderer.js'
pin 'column_picker/fit', to: 'lib/column_picker/fit.js'
pin 'column_picker/order', to: 'lib/column_picker/order.js'
@@ -222,6 +224,7 @@ else
pin 'controllers/notification_count_listener_controller', to: 'controllers/notification_count_listener_controller.js'
pin 'controllers/pane_mark_read_delay_controller', to: 'controllers/pane_mark_read_delay_controller.js'
pin 'controllers/participant_link_modal_controller', to: 'controllers/participant_link_modal_controller.js'
+ pin 'controllers/person_details_controller', to: 'controllers/person_details_controller.js'
pin 'controllers/personal_tasks_drawer_controller', to: 'controllers/personal_tasks_drawer_controller.js'
pin 'controllers/popover_controller', to: 'controllers/popover_controller.js'
pin 'controllers/post_composer_controller', to: 'controllers/post_composer_controller.js'
diff --git a/config/locales/views/personen.de.yml b/config/locales/views/personen.de.yml
index 5dfab42..8becd39 100644
--- a/config/locales/views/personen.de.yml
+++ b/config/locales/views/personen.de.yml
@@ -125,6 +125,20 @@ de:
phone: "+49 30 1234567"
notes: "Einfacher Text, z. B. Erreichbarkeit oder Zuständigkeit"
+ detail_kind: "Art"
+ detail_label: "Bezeichnung"
+ detail_label_placeholder: "Bezeichnung, z. B. Mobil"
+ detail_value: "Wert"
+ add_detail: "Angabe hinzufügen"
+ remove_detail: "Angabe entfernen"
+ details_limit: "Höchstens %{count} weitere Angaben je Person."
+ detail_suggestions:
+ phone: ["Mobil", "Festnetz", "Fax", "Zentrale", "Privat"]
+ email: ["Privat", "Büro"]
+ url: ["Website", "LinkedIn"]
+ date: ["Geburtstag", "Vertragsbeginn"]
+ address: ["Büro", "Rechnungsadresse"]
+ text: ["Erreichbarkeit", "Assistenz"]
shared_hint: "Alle mit Zugriff auf Personen sehen diese Angaben, auch die Kundenseite."
save: "Speichern"
cancel: "Abbrechen"
diff --git a/config/locales/views/personen.en.yml b/config/locales/views/personen.en.yml
index 6b2f04d..6778dfd 100644
--- a/config/locales/views/personen.en.yml
+++ b/config/locales/views/personen.en.yml
@@ -125,6 +125,20 @@ en:
phone: "+49 30 1234567"
notes: "Plain text, e.g. availability or responsibility"
+ detail_kind: "Kind"
+ detail_label: "Label"
+ detail_label_placeholder: "Label, e.g. Mobile"
+ detail_value: "Value"
+ add_detail: "Add detail"
+ remove_detail: "Remove detail"
+ details_limit: "At most %{count} additional details per person."
+ detail_suggestions:
+ phone: ["Mobile", "Landline", "Fax", "Switchboard", "Private"]
+ email: ["Private", "Office"]
+ url: ["Website", "LinkedIn"]
+ date: ["Birthday", "Contract start"]
+ address: ["Office", "Billing address"]
+ text: ["Availability", "Assistant"]
shared_hint: "Everyone with access to people sees these details, the customer's side too."
save: "Save"
cancel: "Cancel"
diff --git a/spec/javascript/lib/person_details.test.js b/spec/javascript/lib/person_details.test.js
new file mode 100644
index 0000000..56d019b
--- /dev/null
+++ b/spec/javascript/lib/person_details.test.js
@@ -0,0 +1,50 @@
+// What: the pure part of the additional details in the people form
+// (lib/person_details.js).
+// Why: the server orders the rows by their index and merges two rows that
+// share one; the kinds and the limit come from the server, so an unknown
+// kind must not break a row.
+import { test } from "node:test"
+import assert from "node:assert/strict"
+import { nextIndex, fillIndex, canAdd, valueField } from "../../../app/javascript/lib/person_details.js"
+
+test("a new row takes the index above the highest present one", () => {
+ assert.equal(nextIndex([]), 0)
+ assert.equal(nextIndex(["0", "1"]), 2)
+ assert.equal(nextIndex(["0", "7", "3"]), 8)
+})
+
+test("ignores anything that is not an index", () => {
+ assert.equal(nextIndex(["__INDEX__", undefined, "-1", "2a", ""]), 0)
+ assert.equal(nextIndex(["4", "abc"]), 5)
+})
+
+test("fills every placeholder of the template", () => {
+ const html = '<select name="person[additional_details][__INDEX__][kind]"></select><input name="person[additional_details][__INDEX__][value]">'
+
+ assert.equal(fillIndex(html, 3), '<select name="person[additional_details][3][kind]"></select><input name="person[additional_details][3][value]">')
+})
+
+test("allows a row below the limit, not at it (n-1, n, n+1)", () => {
+ assert.equal(canAdd(9, 10), true)
+ assert.equal(canAdd(10, 10), false)
+ assert.equal(canAdd(11, 10), false)
+ assert.equal(canAdd(0, NaN), false)
+})
+
+const KINDS = {
+ phone: { input: "tel", maxlength: 255, list: "person-detail-suggestions-phone" },
+ address: { input: "textarea", maxlength: 500, list: "person-detail-suggestions-address" },
+ text: { input: "text", maxlength: 255, list: null }
+}
+
+test("gives each kind the field the server named", () => {
+ assert.deepEqual(valueField(KINDS, "phone"), { tag: "input", type: "tel", maxlength: 255, list: "person-detail-suggestions-phone" })
+ assert.deepEqual(valueField(KINDS, "address"), { tag: "textarea", type: null, maxlength: 500, list: "person-detail-suggestions-address" })
+ assert.deepEqual(valueField(KINDS, "text"), { tag: "input", type: "text", maxlength: 255, list: null })
+})
+
+test("gives an unknown kind a plain text input, also for inherited names", () => {
+ assert.deepEqual(valueField(KINDS, "fax"), { tag: "input", type: "text", maxlength: null, list: null })
+ assert.deepEqual(valueField(KINDS, "constructor"), { tag: "input", type: "text", maxlength: null, list: null })
+ assert.deepEqual(valueField(undefined, "phone"), { tag: "input", type: "text", maxlength: null, list: null })
+})
diff --git a/spec/requests/organisations/people_spec.rb b/spec/requests/organisations/people_spec.rb
index 7ffb774..20d3fc5 100644
--- a/spec/requests/organisations/people_spec.rb
+++ b/spec/requests/organisations/people_spec.rb
@@ -476,6 +476,80 @@ RSpec.describe 'Organisations::People', :ci_guard, type: :request do
expect(Activity.where(trackable: person).to_a.to_json).not_to include('5550000')
end
+ describe 'in the form' do
+ let(:stored) do
+ [{ 'kind' => 'phone', 'label' => 'Fax', 'value' => '+49 30 1234568' },
+ { 'kind' => 'date', 'label' => 'Geburtstag', 'value' => '1980-02-29' },
+ { 'kind' => 'address', 'label' => 'Büro', 'value' => "Musterstraße 1\n12345 Berlin" }]
+ end
+
+ def form_html
+ get edit_organisation_person_path(organisation, person), headers: frame_headers
+ expect(response).to have_http_status(:ok)
+ Nokogiri::HTML(response.body)
+ end
+
+ # The fields a browser without JavaScript sends: everything named in the
+ # form, outside the template, enabled.
+ def submitted_fields(html)
+ html.css('form.people-form input[name], form.people-form select[name], form.people-form textarea[name]')
+ .reject { |node| node.ancestors('template').any? || node['disabled'] || node['type'] == 'submit' }
+ .map { |node| [node['name'], node.name == 'select' ? node.at_css('option[selected]')&.[]('value') : (node['value'] || node.text)] }
+ end
+
+ before { person.update!(additional_details: stored) }
+
+ it 'renders the stored rows with the field of their kind, the marker and an empty template' do
+ html = form_html
+ rows = html.css('[data-person-details-target="list"] [data-person-details-target="row"]')
+
+ expect(rows.size).to eq(3)
+ expect(rows[0].at_css('select option[selected]')['value']).to eq('phone')
+ expect(rows[0].at_css('input[data-person-details-value]').attributes.slice('type', 'value').transform_values(&:value))
+ .to eq('type' => 'tel', 'value' => '+49 30 1234568')
+ expect(rows[0].at_css('input[data-person-details-label]')['list']).to eq('person-detail-suggestions-phone')
+ expect(rows[1].at_css('input[data-person-details-value]')['type']).to eq('date')
+ expect(rows[2].at_css('textarea[data-person-details-value]').text).to include("Musterstraße 1\n12345 Berlin")
+ expect(html.at_css("input[type=hidden][name='person[additional_details][#{PersonDetail::FORM_MARKER}]']")).to be_present
+ expect(html.at_css('template[data-person-details-target="template"]').inner_html).to include('person[additional_details][__INDEX__][kind]')
+ end
+
+ it 'hands the limit and the kinds to the controller from PersonDetail' do
+ section = form_html.at_css('[data-controller="person-details"]')
+ kinds = JSON.parse(section['data-person-details-kinds-value'])
+
+ expect(section['data-person-details-max-value']).to eq(PersonDetail::MAX.to_s)
+ expect(kinds.keys).to eq(PersonDetail::KINDS)
+ expect(kinds['address']).to include('input' => 'textarea', 'maxlength' => PersonDetail::MAX_ADDRESS_LENGTH)
+ end
+
+ it 'offers the label suggestions of every kind and a translated remove button' do
+ html = form_html
+
+ expect(html.css('datalist#person-detail-suggestions-phone option').pluck('value')).to include('Mobil', 'Fax')
+ expect(html.css('[data-person-details-target="list"] button[data-action="person-details#remove"]').pluck('aria-label').uniq)
+ .to eq([I18n.t('views.organisations.people.form.remove_detail')])
+ end
+
+ it 'saves the stored rows unchanged when sent as rendered (without JavaScript)' do
+ query = Rack::Utils.parse_nested_query(URI.encode_www_form(submitted_fields(form_html)))
+
+ patch organisation_person_path(organisation, person), params: query
+
+ expect(response).to have_http_status(:see_other)
+ expect(person.reload.additional_details).to eq(stored)
+ end
+
+ it 'shows the refused rows again, with the reason' do
+ patch_details(person, details_params(row('url', 'ftp://example.com', label: 'Server')))
+
+ expect(response).to have_http_status(:unprocessable_content)
+ value = Nokogiri::HTML(response.body).at_css('[data-person-details-target="list"] input[data-person-details-value]')
+ expect(value['value']).to eq('ftp://example.com')
+ expect(person.reload.additional_details).to eq(stored)
+ end
+ end
+
it 'answers 404 for the details of a person of another organisation and changes nothing' do
foreign = ActsAsTenant.with_tenant(other_organisation) { create(:person, organisation: other_organisation) }
sign_in admin
@@ -711,13 +785,21 @@ RSpec.describe 'Organisations::People', :ci_guard, type: :request do
describe 'preselecting a member through the address' do
before { sign_in admin }
+ # The member select is there, with nothing chosen (the kind selects of the
+ # additional details have a choice of their own).
+ def expect_no_member_preselected
+ select = Nokogiri::HTML(response.body).at_css('select[name="person[user_id]"]')
+ expect(select).to be_present
+ expect(select.css('option[selected]')).to be_empty
+ end
+
it 'does not preselect a member who already has a row' do
taken = member_with(role_with, first_name: 'Vergeben')
create(:person, organisation: organisation, user: taken)
get new_organisation_person_path(organisation, user_id: taken.id), headers: frame_headers
- expect(response.body).not_to include('selected="selected"')
+ expect_no_member_preselected
expect(response.body[/<input[^>]*id="person_name"[^>]*>/]).not_to include('readonly')
end
@@ -727,7 +809,7 @@ RSpec.describe 'Organisations::People', :ci_guard, type: :request do
get new_organisation_person_path(organisation, user_id: former.id), headers: frame_headers
- expect(response.body).not_to include('selected="selected"')
+ expect_no_member_preselected
end
end
diff --git a/spec/system/people_additional_details_spec.rb b/spec/system/people_additional_details_spec.rb
new file mode 100644
index 0000000..d8a8ea6
--- /dev/null
+++ b/spec/system/people_additional_details_spec.rb
@@ -0,0 +1,135 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# The additional details of a person in the form, with real clicks: adding a
+# row from the template, switching the kind (input type and suggestions),
+# removing, the limit of ten, saving, and the lane after the save. Only the
+# browser runs person_details_controller.
+RSpec.describe 'People additional details in the form', type: :system do
+ let(:organisation) { create(:organisation) }
+ let(:user) { create(:user, first_name: 'Anna', last_name: 'Beispiel') }
+ let(:person) { create(:person, organisation: organisation, name: 'Doris Dienstleister', phone: '+49 30 1234567') }
+
+ before do
+ Prosopite.pause
+ ActsAsTenant.current_tenant = organisation
+ role = create(:role, organisation: organisation, title: Role::ACCOUNT_ADMIN_TITLE)
+ create(:membership, user: user, organisation: organisation, role: role, active: true)
+ person
+ sign_in user
+ end
+
+ after do
+ ActsAsTenant.current_tenant = nil
+ Prosopite.resume
+ end
+
+ def lane
+ find("turbo-frame##{Organisations::PeopleController::DETAIL_FRAME_ID}")
+ end
+
+ def open_form
+ visit organisation_people_path(organisation, person_id: person.id)
+ within(lane) { click_link I18n.t('views.organisations.people.show.edit') }
+ expect(page).to have_css('[data-controller="person-details"]')
+ end
+
+ # A picture for the review, after the page transition has finished (the
+ # browser keeps painting the old page until its animations end).
+ def screenshot(name)
+ page.execute_script('document.getAnimations().forEach((animation) => animation.finish())')
+ page.driver.browser.save_screenshot(Rails.root.join("tmp/screenshots/#{name}.png").to_s)
+ end
+
+ def rows
+ all('[data-person-details-target="list"] [data-person-details-target="row"]')
+ end
+
+ def add_button
+ find_button(I18n.t('views.organisations.people.form.add_detail'))
+ end
+
+ def remove_label
+ I18n.t('views.organisations.people.form.remove_detail')
+ end
+
+ def kind_name(kind)
+ I18n.t("views.organisations.people.details.kinds.#{kind}")
+ end
+
+ it 'adds, switches, removes and saves details, and shows them in the lane' do
+ open_form
+
+ add_button.click
+ expect(rows.size).to eq(1)
+ within(rows[0]) do
+ expect(page).to have_css('input[data-person-details-value][type="tel"]')
+ find('input[data-person-details-label]').fill_in(with: 'Fax')
+ find('input[data-person-details-value]').fill_in(with: '+49 30 1234568')
+ end
+
+ add_button.click
+ within(rows[1]) do
+ find('select').select(kind_name('address'))
+ expect(page).to have_css('textarea[data-person-details-value]')
+ expect(page).to have_css('input[data-person-details-label][list="person-detail-suggestions-address"]')
+ find('input[data-person-details-label]').fill_in(with: 'Büro')
+ find('textarea[data-person-details-value]').fill_in(with: "Musterstraße 1\n12345 Berlin")
+ end
+
+ add_button.click
+ within(rows[2]) do
+ find('select').select(kind_name('url'))
+ expect(page).to have_css('input[data-person-details-value][type="url"]')
+ find('input[data-person-details-value]').fill_in(with: 'https://example.com')
+ click_button remove_label
+ end
+ expect(rows.size).to eq(2)
+
+ screenshot('people_additional_details_form')
+ click_button I18n.t('views.organisations.people.form.save')
+
+ expect(page).to have_text(I18n.t('controllers.people.update.success'))
+ expect(lane).to have_link('+49 30 1234568', href: 'tel:+49301234568')
+ expect(lane).to have_text('Fax')
+ expect(lane).to have_text("Musterstraße 1\n12345 Berlin")
+ expect(lane).to have_no_link(href: 'https://example.com')
+ expect(page).to have_no_css('[data-controller="person-details"]')
+ screenshot('people_additional_details_lane')
+ expect(person.reload.additional_details.pluck('label')).to eq(%w[Fax Büro])
+ end
+
+ it 'hides the add button at ten with a note, and shows it again after a remove' do
+ open_form
+ hint = I18n.t('views.organisations.people.form.details_limit', count: PersonDetail::MAX)
+ expect(page).to have_no_text(hint)
+
+ PersonDetail::MAX.times { add_button.click }
+
+ expect(rows.size).to eq(PersonDetail::MAX)
+ expect(page).to have_no_button(I18n.t('views.organisations.people.form.add_detail'))
+ expect(page).to have_text(hint)
+
+ within(rows.last) { click_button remove_label }
+
+ expect(rows.size).to eq(PersonDetail::MAX - 1)
+ expect(add_button).to be_visible
+ expect(page).to have_no_text(hint)
+ end
+
+ it 'clears the details when the last one is removed and saved' do
+ person.update!(additional_details: [{ 'kind' => 'text', 'label' => 'Erreichbar', 'value' => 'nur vormittags' }])
+ open_form
+ expect(rows.size).to eq(1)
+
+ within(rows[0]) { click_button remove_label }
+ click_button I18n.t('views.organisations.people.form.save')
+
+ expect(page).to have_text(I18n.t('controllers.people.update.success'))
+ expect(page).to have_no_css('[data-controller="person-details"]')
+ expect(lane).to have_text('Doris Dienstleister')
+ expect(lane).to have_no_text('nur vormittags')
+ expect(person.reload.additional_details).to eq([])
+ end
+end