From b14f7bd6f59f4c8a8e48cc6b78eadeceef1a6a56 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 00:42:55 -0400
Subject: [PATCH 01/37] =?UTF-8?q?Add=20affiliation=E2=86=94registration=20?=
=?UTF-8?q?link=20and=20event=20reconciled-at=20column?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Groundwork for facilitator-affiliation reconciliation: an ownership FK so
reconcile only ever touches rows the registration flow created, and a
timestamp on events recording when affiliations were last reconciled.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
...044207_add_affiliations_reconciled_at_to_events.rb | 11 +++++++++++
db/schema.rb | 1 +
2 files changed, 12 insertions(+)
create mode 100644 db/migrate/20260814044207_add_affiliations_reconciled_at_to_events.rb
diff --git a/db/migrate/20260814044207_add_affiliations_reconciled_at_to_events.rb b/db/migrate/20260814044207_add_affiliations_reconciled_at_to_events.rb
new file mode 100644
index 000000000..4f25e87ba
--- /dev/null
+++ b/db/migrate/20260814044207_add_affiliations_reconciled_at_to_events.rb
@@ -0,0 +1,11 @@
+class AddAffiliationsReconciledAtToEvents < ActiveRecord::Migration[8.1]
+ def up
+ unless column_exists?(:events, :affiliations_reconciled_at)
+ add_column :events, :affiliations_reconciled_at, :datetime, null: true
+ end
+ end
+
+ def down
+ remove_column :events, :affiliations_reconciled_at, if_exists: true
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 1581f5f64..d724f095b 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -547,6 +547,7 @@
create_table "events", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.string "abbreviation"
+ t.datetime "affiliations_reconciled_at"
t.boolean "autoshow_cost", default: true, null: false
t.boolean "autoshow_date", default: true, null: false
t.boolean "autoshow_location", default: true, null: false
From 7581c04398efcb0ffe5a85b44d076025b3e8b5cb Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 00:51:34 -0400
Subject: [PATCH 02/37] Add ReconcileFacilitatorAffiliation service
Per (person, org): keep the owned facilitator affiliation active iff they have
an attended facilitator-training registration for that org; otherwise same-day
it (end_date := start_date). Hand-created rows are left alone.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
.../reconcile_facilitator_affiliation.rb | 83 ++++++++++++
.../reconcile_facilitator_affiliation_spec.rb | 127 ++++++++++++++++++
2 files changed, 210 insertions(+)
create mode 100644 app/services/affiliation_services/reconcile_facilitator_affiliation.rb
create mode 100644 spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
diff --git a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
new file mode 100644
index 000000000..60e2bcde8
--- /dev/null
+++ b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
@@ -0,0 +1,83 @@
+module AffiliationServices
+ # Reconciles a person's **owned** facilitator affiliation for one organization
+ # against whether they actually completed a facilitator training there.
+ #
+ # "Owned" means auto-minted by the registration flow (`event_registration_id`
+ # present) — hand-created / historical rows have no link and are left alone.
+ #
+ # A person is an active facilitator of an org iff they have at least one
+ # `attended` registration to that org from a facilitator-training event. Anyone
+ # else (no_show, cancelled, incomplete_attendance, still-registered, …) is not,
+ # so we **same-day** their owned facilitator affiliation — set `end_date` to its
+ # `start_date`, which the model's `set_inactive_from_dates` turns into
+ # `inactive: true`. It preserves `start_date` and is reversible: if the person is
+ # later marked attended, a re-run clears `end_date` and reactivates the row.
+ #
+ # The decision is per (person, org) across ALL their training registrations, so
+ # no-showing one training but attending another for the same org keeps them
+ # active.
+ class ReconcileFacilitatorAffiliation
+ def self.call(person:, organization:)
+ new(person:, organization:).call
+ end
+
+ def initialize(person:, organization:)
+ @person = person
+ @organization = organization
+ end
+
+ # Apply the reconciliation. Returns the action taken (:deactivate, :reactivate,
+ # or :noop).
+ def call
+ rows = owned_facilitator_affiliations.to_a
+ return :noop if rows.empty?
+
+ completed_training? ? reactivate(rows) : deactivate(rows)
+ end
+
+ # What #call would do, without writing. Returns :deactivate, :reactivate, or :noop.
+ def plan
+ rows = owned_facilitator_affiliations.to_a
+ return :noop if rows.empty?
+
+ if completed_training?
+ rows.any? { |affiliation| !affiliation.active? } ? :reactivate : :noop
+ else
+ rows.any?(&:active?) ? :deactivate : :noop
+ end
+ end
+
+ private
+
+ def deactivate(rows)
+ active = rows.select(&:active?)
+ return :noop if active.empty?
+
+ active.each { |affiliation| affiliation.update!(end_date: affiliation.start_date || Date.current) }
+ :deactivate
+ end
+
+ def reactivate(rows)
+ ended = rows.reject(&:active?)
+ return :noop if ended.empty?
+
+ ended.each { |affiliation| affiliation.update!(end_date: nil) }
+ :reactivate
+ end
+
+ def owned_facilitator_affiliations
+ @person.affiliations.facilitators
+ .where(organization: @organization)
+ .where.not(event_registration_id: nil)
+ end
+
+ # Any `attended` registration to this org from a facilitator-training event.
+ def completed_training?
+ @person.event_registrations.attended
+ .joins(:event).where(events: { facilitator_training: true })
+ .joins(:event_registration_organizations)
+ .where(event_registration_organizations: { organization_id: @organization.id })
+ .exists?
+ end
+ end
+end
diff --git a/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb b/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
new file mode 100644
index 000000000..b429fa64f
--- /dev/null
+++ b/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
@@ -0,0 +1,127 @@
+require "rails_helper"
+
+RSpec.describe AffiliationServices::ReconcileFacilitatorAffiliation do
+ let(:person) { create(:person) }
+ let(:organization) { create(:organization) }
+
+ # A facilitator-training registration for `person` linking `organization`.
+ def training_registration(status:, ended: true)
+ event = create(:event, *(ended ? [ :ended ] : []), facilitator_training: true)
+ reg = create(:event_registration, registrant: person, event: event, status: status)
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ reg
+ end
+
+ # A "Facilitator" affiliation for (person, organization) owned by `registration`.
+ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
+ create(:affiliation,
+ person: person,
+ organization: organization,
+ title: "Facilitator",
+ start_date: start_date,
+ event_registration: registration)
+ end
+
+ describe "deactivation" do
+ it "same-days the owned facilitator affiliation when the person never attended" do
+ reg = training_registration(status: "no_show")
+ affiliation = owned_facilitator(registration: reg)
+
+ described_class.call(person: person, organization: organization)
+ affiliation.reload
+
+ expect(affiliation.end_date).to eq(affiliation.start_date)
+ expect(affiliation).to be_inactive
+ expect(affiliation).not_to be_active
+ end
+
+ %w[ incomplete_attendance registered cancelled transferred_out ].each do |status|
+ it "deactivates when the only registration is #{status}" do
+ reg = training_registration(status: status)
+ affiliation = owned_facilitator(registration: reg)
+
+ described_class.call(person: person, organization: organization)
+
+ expect(affiliation.reload).not_to be_active
+ end
+ end
+
+ it "leaves an unowned (hand-created) facilitator affiliation untouched" do
+ training_registration(status: "no_show")
+ hand_created = create(:affiliation, person: person, organization: organization,
+ title: "Facilitator", start_date: 1.month.ago.to_date)
+
+ described_class.call(person: person, organization: organization)
+
+ expect(hand_created.reload).to be_active
+ expect(hand_created.end_date).to be_nil
+ end
+ end
+
+ describe "keeping / activating" do
+ it "keeps the affiliation active when the person attended" do
+ reg = training_registration(status: "attended")
+ affiliation = owned_facilitator(registration: reg)
+
+ described_class.call(person: person, organization: organization)
+
+ expect(affiliation.reload).to be_active
+ expect(affiliation.end_date).to be_nil
+ end
+
+ it "keeps active when the person no-showed one training but attended another for the same org" do
+ no_show = training_registration(status: "no_show")
+ affiliation = owned_facilitator(registration: no_show)
+ training_registration(status: "attended")
+
+ described_class.call(person: person, organization: organization)
+
+ expect(affiliation.reload).to be_active
+ end
+
+ it "reactivates a previously same-day'd affiliation once the person is marked attended" do
+ reg = training_registration(status: "attended")
+ affiliation = owned_facilitator(registration: reg, start_date: 1.month.ago.to_date)
+ affiliation.update!(end_date: affiliation.start_date)
+ expect(affiliation.reload).not_to be_active
+
+ described_class.call(person: person, organization: organization)
+
+ expect(affiliation.reload).to be_active
+ expect(affiliation.end_date).to be_nil
+ end
+ end
+
+ describe "idempotence" do
+ it "is stable across repeated runs" do
+ reg = training_registration(status: "no_show")
+ affiliation = owned_facilitator(registration: reg)
+
+ described_class.call(person: person, organization: organization)
+ first = affiliation.reload.end_date
+ described_class.call(person: person, organization: organization)
+
+ expect(affiliation.reload.end_date).to eq(first)
+ end
+ end
+
+ describe "#plan (dry run)" do
+ it "reports :deactivate without writing" do
+ reg = training_registration(status: "no_show")
+ affiliation = owned_facilitator(registration: reg)
+
+ plan = described_class.new(person: person, organization: organization).plan
+
+ expect(plan).to eq(:deactivate)
+ expect(affiliation.reload).to be_active
+ end
+
+ it "reports :noop when there is no owned facilitator affiliation" do
+ training_registration(status: "no_show")
+
+ plan = described_class.new(person: person, organization: organization).plan
+
+ expect(plan).to eq(:noop)
+ end
+ end
+end
From 25af9f3c933359df0bfa3070a66c02d810d090d8 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 00:57:03 -0400
Subject: [PATCH 03/37] Add Reconcile affiliations bulk action with preview and
opt-out
A preview-and-confirm page (under Bulk actions on facilitator trainings) that
same-days the owned facilitator affiliation of anyone who didn't complete the
training, keeps/reactivates completers, and records when it last ran. Admins can
opt individual rows out before applying.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 2 +
.../reconcile_affiliations_controller.rb | 47 +++++++++++
app/models/event.rb | 9 +++
app/policies/event_policy.rb | 4 +
.../affiliation_services/reconcile_event.rb | 77 +++++++++++++++++++
.../reconcile_facilitator_affiliation.rb | 17 +++-
app/views/events/_bulk_actions_menu.html.erb | 3 +
.../reconcile_affiliations/index.html.erb | 63 +++++++++++++++
config/routes.rb | 2 +
.../events/reconcile_affiliations_spec.rb | 70 +++++++++++++++++
.../reconcile_facilitator_affiliation_spec.rb | 10 +++
spec/views/page_bg_class_alignment_spec.rb | 1 +
12 files changed, 301 insertions(+), 4 deletions(-)
create mode 100644 app/controllers/events/reconcile_affiliations_controller.rb
create mode 100644 app/services/affiliation_services/reconcile_event.rb
create mode 100644 app/views/events/reconcile_affiliations/index.html.erb
create mode 100644 spec/requests/events/reconcile_affiliations_spec.rb
diff --git a/AGENTS.md b/AGENTS.md
index 375ebb393..f64a8c176 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -255,6 +255,8 @@ action, or `authorize! :workshop, to: :summary?`).
### Affiliations
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
+- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. `#preview` returns the actionable `(person, org)` rows (via `ReconcileFacilitatorAffiliation#plan`) for the confirm page; `#apply(included_keys:)` reconciles the rows the admin kept and stamps the event's `affiliations_reconciled_at`.
### Sectors
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
new file mode 100644
index 000000000..b368857d4
--- /dev/null
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -0,0 +1,47 @@
+module Events
+ # The "Reconcile affiliations" bulk action: a preview-and-confirm page that
+ # brings each registrant's owned facilitator affiliation in line with whether
+ # they actually completed this facilitator training. Post-event it same-days the
+ # affiliations of non-completers; the admin can opt individual rows out before
+ # applying. Only facilitator-training events have facilitator affiliations to
+ # reconcile, so the action is limited to them.
+ class ReconcileAffiliationsController < ApplicationController
+ include AhoyTracking
+ before_action :set_event
+ before_action :require_facilitator_training
+
+ def index
+ authorize! @event, to: :reconcile_affiliations?
+ track_view("events.reconcile_affiliations", { event_id: @event.id })
+
+ @rows = AffiliationServices::ReconcileEvent.new(@event).preview
+ @event = @event.decorate
+ end
+
+ def create
+ authorize! @event, to: :reconcile_affiliations?
+
+ changed = AffiliationServices::ReconcileEvent.new(@event).apply(included_keys: params[:included])
+ redirect_to registrants_event_path(@event), notice: reconcile_notice(changed)
+ end
+
+ private
+
+ def set_event
+ @event = Event.find(params[:id])
+ end
+
+ def require_facilitator_training
+ return if @event.facilitator_training?
+
+ redirect_to registrants_event_path(@event),
+ alert: "Affiliation reconciliation applies to facilitator trainings only."
+ end
+
+ def reconcile_notice(changed)
+ return "No affiliations needed reconciling." if changed.zero?
+
+ "Reconciled #{changed} #{'affiliation'.pluralize(changed)}."
+ end
+ end
+end
diff --git a/app/models/event.rb b/app/models/event.rb
index c52483bb9..28270b6ff 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -168,6 +168,15 @@ def ended?
end_date < Time.current
end
+ # A registrant's status changed after affiliations were last reconciled, so the
+ # reconciliation may be out of date and worth re-running. False when never
+ # reconciled (nothing to be stale against).
+ def affiliations_reconciliation_stale?
+ return false unless affiliations_reconciled_at
+
+ event_registrations.where("event_registrations.updated_at > ?", affiliations_reconciled_at).exists?
+ end
+
# Whether the event shows as a full card on the events index. Unpublished
# events and events that ended more than a month ago collapse into the compact
# archive list instead of taking up a card.
diff --git a/app/policies/event_policy.rb b/app/policies/event_policy.rb
index 602da91fe..ab3457a1e 100644
--- a/app/policies/event_policy.rb
+++ b/app/policies/event_policy.rb
@@ -119,6 +119,10 @@ def bulk_payments?
manage?
end
+ def reconcile_affiliations?
+ manage?
+ end
+
def invoice?
manage?
end
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
new file mode 100644
index 000000000..1de7685f4
--- /dev/null
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -0,0 +1,77 @@
+module AffiliationServices
+ # Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
+ # the event's registrants and the organizations they linked, and reconciles each
+ # (person, org)'s owned facilitator affiliation via ReconcileFacilitatorAffiliation.
+ #
+ # `preview` returns the actionable rows (nothing is written) so the admin can see
+ # what will change and opt individual rows out. `apply` reconciles the rows the
+ # admin kept (by key) and stamps the event's `affiliations_reconciled_at`.
+ class ReconcileEvent
+ Row = Struct.new(:person, :organization, :affiliation, :action, :key, keyword_init: true)
+
+ def self.key_for(person, organization)
+ "#{person.id}:#{organization.id}"
+ end
+
+ def initialize(event)
+ @event = event
+ end
+
+ # Actionable rows (:deactivate / :reactivate) for the preview. Never writes.
+ def preview
+ pairs.filter_map do |person, organization|
+ action = ReconcileFacilitatorAffiliation.new(person:, organization:).plan
+ next if action == :noop
+
+ Row.new(
+ person:,
+ organization:,
+ affiliation: owned_facilitator(person, organization),
+ action:,
+ key: self.class.key_for(person, organization)
+ )
+ end
+ end
+
+ # Reconcile the (person, org) pairs whose keys are in `included_keys`, stamp the
+ # event, and return the number of pairs actually changed.
+ def apply(included_keys:)
+ keys = Array(included_keys).to_set
+
+ changed = pairs.count do |person, organization|
+ next false unless keys.include?(self.class.key_for(person, organization))
+
+ ReconcileFacilitatorAffiliation.call(person:, organization:) != :noop
+ end
+
+ @event.update!(affiliations_reconciled_at: Time.current)
+ changed
+ end
+
+ private
+
+ # Distinct (person, organization) pairs from the event's registrants and the
+ # organizations each linked to their registration.
+ def pairs
+ @pairs ||= begin
+ seen = Set.new
+ @event.event_registrations.includes(:registrant, :organizations).flat_map do |registration|
+ registration.organizations.filter_map do |organization|
+ key = [ registration.registrant_id, organization.id ]
+ next if seen.include?(key)
+
+ seen << key
+ [ registration.registrant, organization ]
+ end
+ end
+ end
+ end
+
+ def owned_facilitator(person, organization)
+ person.affiliations.facilitators
+ .where(organization:)
+ .where.not(event_registration_id: nil)
+ .first
+ end
+ end
+end
diff --git a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
index 60e2bcde8..db3e086cb 100644
--- a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
+++ b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
@@ -42,21 +42,30 @@ def plan
if completed_training?
rows.any? { |affiliation| !affiliation.active? } ? :reactivate : :noop
+ elsif rows.any? { |affiliation| affiliation.active? && source_training_ended?(affiliation) }
+ :deactivate
else
- rows.any?(&:active?) ? :deactivate : :noop
+ :noop
end
end
private
def deactivate(rows)
- active = rows.select(&:active?)
- return :noop if active.empty?
+ # Only same-day affiliations whose source training has actually ended. A row
+ # tied to a still-upcoming training is a legitimate assumptive/upcoming
+ # affiliation — leave it alone until that training is over.
+ ended = rows.select { |affiliation| affiliation.active? && source_training_ended?(affiliation) }
+ return :noop if ended.empty?
- active.each { |affiliation| affiliation.update!(end_date: affiliation.start_date || Date.current) }
+ ended.each { |affiliation| affiliation.update!(end_date: affiliation.start_date || Date.current) }
:deactivate
end
+ def source_training_ended?(affiliation)
+ affiliation.event_registration&.event&.ended?
+ end
+
def reactivate(rows)
ended = rows.reject(&:active?)
return :noop if ended.empty?
diff --git a/app/views/events/_bulk_actions_menu.html.erb b/app/views/events/_bulk_actions_menu.html.erb
index 72c7dac7f..4d61faffd 100644
--- a/app/views/events/_bulk_actions_menu.html.erb
+++ b/app/views/events/_bulk_actions_menu.html.erb
@@ -24,6 +24,9 @@
<% else %>
<%= link_to "Sign-ins", attendance_event_path(@event, return_to: "registrants"), class: item_class %>
<% end %>
+ <% if @event.facilitator_training? %>
+ <%= link_to "Reconcile affiliations", reconcile_affiliations_event_path(@event), class: item_class %>
+ <% end %>
<%= link_to registrants_event_path(@event, format: :csv), class: item_class, data: { turbo_frame: "_top" } do %>
Download CSV
<% end %>
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
new file mode 100644
index 000000000..a6b56c659
--- /dev/null
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -0,0 +1,63 @@
+<% content_for(:page_title, "Reconcile affiliations — #{@event.title}") %>
+<% content_for(:page_bg_class, "admin-or-owner bg-blue-100") %>
+
+ Facilitator affiliations are created optimistically when someone registers for a training. This step brings
+ them in line with who actually attended: anyone who didn't complete the training has their
+ auto-created facilitator affiliation same-dayed (its end date is set to its start date, so it
+ no longer counts as active). Someone later marked attended is reactivated on the next run.
+
+
+ Only affiliations this app created from a registration are touched — hand-entered affiliations are always left
+ alone. Uncheck a row to spare it this time.
+
+ <% if @event.affiliations_reconciled_at %>
+
Last reconciled <%= @event.affiliations_reconciled_at.to_fs(:long) %>.
+ <% end %>
+ <% if @event.affiliations_reconciliation_stale? %>
+
Attendance has changed since the last reconciliation — re-run to bring affiliations up to date.
+ <% end %>
+
+
+ <% if @rows.empty? %>
+
+ Nothing to reconcile — every facilitator affiliation already matches its attendance.
+
diff --git a/config/routes.rb b/config/routes.rb
index b4ad751b3..135f6b00c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -203,6 +203,8 @@
get :recipients
post :feature_recipient_shoutout
get :bulk_payments, to: "events/bulk_payments#index"
+ get :reconcile_affiliations, to: "events/reconcile_affiliations#index"
+ post :reconcile_affiliations, to: "events/reconcile_affiliations#create"
get :preview_reminder
patch :preview
post :copy_registration_form
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
new file mode 100644
index 000000000..b7b80b361
--- /dev/null
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -0,0 +1,70 @@
+require "rails_helper"
+
+RSpec.describe "Events::ReconcileAffiliations", type: :request do
+ let(:admin) { create(:user, :admin) }
+ let(:organization) { create(:organization) }
+ let(:event) { create(:event, :ended, facilitator_training: true) }
+
+ # A registrant of `event` who linked `organization`, with an owned facilitator
+ # affiliation created (as the registration flow would).
+ def registrant_with_affiliation(status:)
+ person = create(:person)
+ reg = create(:event_registration, event: event, registrant: person, status: status)
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ affiliation = create(:affiliation, person: person, organization: organization,
+ title: "Facilitator", start_date: 1.month.ago.to_date,
+ event_registration: reg)
+ [ person, affiliation ]
+ end
+
+ before { sign_in admin }
+
+ describe "GET index" do
+ it "previews the no-show as a deactivation, checked by default" do
+ person, _affiliation = registrant_with_affiliation(status: "no_show")
+
+ get reconcile_affiliations_event_path(event)
+
+ expect(response).to have_http_status(:ok)
+ expect(response.body).to include(person.name)
+ expect(response.body).to include("Will be deactivated")
+ end
+
+ it "redirects for a non-training event" do
+ non_training = create(:event, :ended, facilitator_training: false)
+
+ get reconcile_affiliations_event_path(non_training)
+
+ expect(response).to redirect_to(registrants_event_path(non_training))
+ end
+
+ it "denies a non-admin" do
+ sign_in create(:user)
+
+ get reconcile_affiliations_event_path(event)
+
+ expect(response).not_to have_http_status(:ok)
+ end
+ end
+
+ describe "POST create" do
+ it "deactivates the included non-completer and stamps the event" do
+ _person, affiliation = registrant_with_affiliation(status: "no_show")
+ key = AffiliationServices::ReconcileEvent.key_for(affiliation.person, organization)
+
+ post reconcile_affiliations_event_path(event), params: { included: [ key ] }
+
+ expect(response).to redirect_to(registrants_event_path(event))
+ expect(affiliation.reload).not_to be_active
+ expect(event.reload.affiliations_reconciled_at).to be_present
+ end
+
+ it "spares an opted-out row" do
+ _person, affiliation = registrant_with_affiliation(status: "no_show")
+
+ post reconcile_affiliations_event_path(event), params: { included: [] }
+
+ expect(affiliation.reload).to be_active
+ end
+ end
+end
diff --git a/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb b/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
index b429fa64f..d910e4bbb 100644
--- a/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
+++ b/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
@@ -46,6 +46,16 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
end
end
+ it "leaves an assumptive affiliation alone while its training is still upcoming" do
+ reg = training_registration(status: "registered", ended: false)
+ affiliation = owned_facilitator(registration: reg, start_date: Date.current)
+
+ described_class.call(person: person, organization: organization)
+
+ expect(affiliation.reload).to be_active
+ expect(affiliation.end_date).to be_nil
+ end
+
it "leaves an unowned (hand-created) facilitator affiliation untouched" do
training_registration(status: "no_show")
hand_created = create(:affiliation, person: person, organization: organization,
diff --git a/spec/views/page_bg_class_alignment_spec.rb b/spec/views/page_bg_class_alignment_spec.rb
index 19beebcdb..122918401 100644
--- a/spec/views/page_bg_class_alignment_spec.rb
+++ b/spec/views/page_bg_class_alignment_spec.rb
@@ -121,6 +121,7 @@
"app/views/events/signins.html.erb" => "admin-or-owner bg-blue-100",
"app/views/events/sample_ticket.html.erb" => "admin-or-owner bg-blue-100",
"app/views/events/bulk_payments/index.html.erb" => "admin-or-owner bg-blue-100",
+ "app/views/events/reconcile_affiliations/index.html.erb" => "admin-or-owner bg-blue-100",
"app/views/events/edit_staff.html.erb" => "admin-or-owner bg-blue-100",
"app/views/events/recipients.html.erb" => "admin-or-owner bg-blue-100",
"app/views/events/registrants.html.erb" => "admin-or-owner bg-blue-100",
From 2a618a607d84ea3300095fa8735460c8701f271a Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 01:15:47 -0400
Subject: [PATCH 04/37] Date facilitator affiliation to the training day; heal
missing affiliations on reconcile
Start the created facilitator affiliation on the actual training date rather than
the first of its month. Extend the Reconcile affiliations action to also create
missing facilitator affiliations (pre-event for anyone, post-event for attendees),
shown as opt-out-able 'Will be created' rows alongside the deactivations.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 2 +-
.../affiliation_services/reconcile_event.rb | 91 ++++++++++++++-----
.../reconcile_facilitator_affiliation.rb | 19 ++--
.../reconcile_affiliations/index.html.erb | 15 ++-
.../events/reconcile_affiliations_spec.rb | 23 +++++
5 files changed, 112 insertions(+), 38 deletions(-)
diff --git a/AGENTS.md b/AGENTS.md
index f64a8c176..9508899d0 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -256,7 +256,7 @@ action, or `authorize! :workshop, to: :summary?`).
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. `#preview` returns the actionable `(person, org)` rows (via `ReconcileFacilitatorAffiliation#plan`) for the confirm page; `#apply(included_keys:)` reconciles the rows the admin kept and stamps the event's `affiliations_reconciled_at`.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Per `(person, org)` it decides `:create` (heal a missing facilitator affiliation — pre-event for anyone, post-event only for attendees), `:deactivate`, or `:reactivate`. `#preview` returns the actionable rows for the confirm page; `#apply(included_keys:)` performs the rows the admin kept (creating via `CreateFromRegistration`, otherwise via `ReconcileFacilitatorAffiliation`) and stamps the event's `affiliations_reconciled_at`.
### Sectors
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 1de7685f4..1c95e8f2e 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -1,13 +1,19 @@
module AffiliationServices
# Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
- # the event's registrants and the organizations they linked, and reconciles each
- # (person, org)'s owned facilitator affiliation via ReconcileFacilitatorAffiliation.
+ # the event's registrants and the organizations they linked, and for each
+ # (person, org) works out what should happen to their facilitator affiliation:
#
- # `preview` returns the actionable rows (nothing is written) so the admin can see
- # what will change and opt individual rows out. `apply` reconciles the rows the
- # admin kept (by key) and stamps the event's `affiliations_reconciled_at`.
+ # :create — no facilitator affiliation exists yet but one should (heal a
+ # missing affiliation): pre-event for any registrant, post-event
+ # only for those who attended.
+ # :deactivate — an owned affiliation whose (ended) training they didn't complete.
+ # :reactivate — an owned affiliation same-dayed earlier, now attended.
+ #
+ # `preview` returns the actionable rows without writing so the admin can see them
+ # and opt individual rows out; `apply(included_keys:)` performs the kept rows and
+ # stamps the event's `affiliations_reconciled_at`.
class ReconcileEvent
- Row = Struct.new(:person, :organization, :affiliation, :action, :key, keyword_init: true)
+ Row = Struct.new(:person, :organization, :registration, :affiliation, :action, :key, keyword_init: true)
def self.key_for(person, organization)
"#{person.id}:#{organization.id}"
@@ -17,15 +23,34 @@ def initialize(event)
@event = event
end
- # Actionable rows (:deactivate / :reactivate) for the preview. Never writes.
def preview
- pairs.filter_map do |person, organization|
- action = ReconcileFacilitatorAffiliation.new(person:, organization:).plan
+ rows
+ end
+
+ # Apply the rows whose keys are in `included_keys`, stamp the event, and return
+ # the number of pairs actually changed.
+ def apply(included_keys:)
+ keys = Array(included_keys).to_set
+
+ changed = rows.count do |row|
+ keys.include?(row.key) && apply_row(row)
+ end
+
+ @event.update!(affiliations_reconciled_at: Time.current)
+ changed
+ end
+
+ private
+
+ def rows
+ @rows ||= pairs.filter_map do |person, organization, registration|
+ action = action_for(person, organization)
next if action == :noop
Row.new(
person:,
organization:,
+ registration:,
affiliation: owned_facilitator(person, organization),
action:,
key: self.class.key_for(person, organization)
@@ -33,25 +58,45 @@ def preview
end
end
- # Reconcile the (person, org) pairs whose keys are in `included_keys`, stamp the
- # event, and return the number of pairs actually changed.
- def apply(included_keys:)
- keys = Array(included_keys).to_set
+ def action_for(person, organization)
+ reconcile = ReconcileFacilitatorAffiliation.new(person:, organization:).plan
+ return reconcile unless reconcile == :noop
- changed = pairs.count do |person, organization|
- next false unless keys.include?(self.class.key_for(person, organization))
+ create_needed?(person, organization) ? :create : :noop
+ end
- ReconcileFacilitatorAffiliation.call(person:, organization:) != :noop
- end
+ def apply_row(row)
+ return apply_create(row) if row.action == :create
- @event.update!(affiliations_reconciled_at: Time.current)
- changed
+ ReconcileFacilitatorAffiliation.call(person: row.person, organization: row.organization) != :noop
end
- private
+ def apply_create(row)
+ AffiliationServices::CreateFromRegistration.call(
+ person: row.person,
+ organization: row.organization,
+ facilitator_training: true,
+ training_date: @event.start_date,
+ event_registration: row.registration
+ )
+ true
+ end
+
+ # A facilitator affiliation should exist but doesn't. Skip when an owned one
+ # already exists (reconcile handles it — including a deliberately same-dayed
+ # no-show we must not resurrect) or when a hand-created active-or-pending one
+ # already covers it. Otherwise create it pre-event for anyone, post-event only
+ # for those who attended.
+ def create_needed?(person, organization)
+ facilitators = person.affiliations.facilitators.where(organization:)
+ return false if facilitators.where.not(event_registration_id: nil).exists?
+ return false if facilitators.active_or_pending.exists?
+
+ !@event.ended? || ReconcileFacilitatorAffiliation.new(person:, organization:).completed_training?
+ end
- # Distinct (person, organization) pairs from the event's registrants and the
- # organizations each linked to their registration.
+ # Distinct (person, organization, registration) triples from the event's
+ # registrants and the organizations each linked to their registration.
def pairs
@pairs ||= begin
seen = Set.new
@@ -61,7 +106,7 @@ def pairs
next if seen.include?(key)
seen << key
- [ registration.registrant, organization ]
+ [ registration.registrant, organization, registration ]
end
end
end
diff --git a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
index db3e086cb..acc3bf54b 100644
--- a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
+++ b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
@@ -49,6 +49,16 @@ def plan
end
end
+ # Whether the person has any `attended` registration to this org from a
+ # facilitator-training event — i.e. actually became a facilitator there.
+ def completed_training?
+ @person.event_registrations.attended
+ .joins(:event).where(events: { facilitator_training: true })
+ .joins(:event_registration_organizations)
+ .where(event_registration_organizations: { organization_id: @organization.id })
+ .exists?
+ end
+
private
def deactivate(rows)
@@ -79,14 +89,5 @@ def owned_facilitator_affiliations
.where(organization: @organization)
.where.not(event_registration_id: nil)
end
-
- # Any `attended` registration to this org from a facilitator-training event.
- def completed_training?
- @person.event_registrations.attended
- .joins(:event).where(events: { facilitator_training: true })
- .joins(:event_registration_organizations)
- .where(event_registration_organizations: { organization_id: @organization.id })
- .exists?
- end
end
end
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index a6b56c659..4bcdf4441 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -10,10 +10,10 @@
- Facilitator affiliations are created optimistically when someone registers for a training. This step brings
- them in line with who actually attended: anyone who didn't complete the training has their
- auto-created facilitator affiliation same-dayed (its end date is set to its start date, so it
- no longer counts as active). Someone later marked attended is reactivated on the next run.
+ This step brings facilitator affiliations in line with who registered and attended. Before the training it
+ creates any missing facilitator affiliations for linked organizations. After the training it
+ same-days the affiliation of anyone who didn't attend (its end date is set to its start
+ date, so it no longer counts as active), and reactivates anyone later marked attended.
Only affiliations this app created from a registration are touched — hand-entered affiliations are always left
@@ -41,10 +41,15 @@
<%= row.person.name %>— <%= row.organization.name %>
- <% if row.action == :deactivate %>
+ <% case row.action %>
+ <% when :deactivate %>
Will be deactivated
+ <% when :create %>
+
+ Will be created
+
<% else %>
Will be reactivated
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index b7b80b361..3d5b29e5a 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -30,6 +30,17 @@ def registrant_with_affiliation(status:)
expect(response.body).to include("Will be deactivated")
end
+ it "previews a missing affiliation as a creation before the event" do
+ upcoming = create(:event, facilitator_training: true, start_date: 3.days.from_now, end_date: 5.days.from_now)
+ person = create(:person)
+ reg = create(:event_registration, event: upcoming, registrant: person, status: "registered")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+
+ get reconcile_affiliations_event_path(upcoming)
+
+ expect(response.body).to include("Will be created")
+ end
+
it "redirects for a non-training event" do
non_training = create(:event, :ended, facilitator_training: false)
@@ -66,5 +77,17 @@ def registrant_with_affiliation(status:)
expect(affiliation.reload).to be_active
end
+
+ it "creates a missing affiliation before the event when included" do
+ upcoming = create(:event, facilitator_training: true, start_date: 3.days.from_now, end_date: 5.days.from_now)
+ person = create(:person)
+ reg = create(:event_registration, event: upcoming, registrant: person, status: "registered")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ key = AffiliationServices::ReconcileEvent.key_for(person, organization)
+
+ expect {
+ post reconcile_affiliations_event_path(upcoming), params: { included: [ key ] }
+ }.to change { person.affiliations.facilitators.where(organization: organization).count }.by(1)
+ end
end
end
From 68633437e2302f847dd4df422600e49fee968f9d Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 07:49:12 -0400
Subject: [PATCH 05/37] Reconcile non-training facilitator affiliations and
offer delete-instead
On a non-training event, the Reconcile affiliations action now deletes
facilitator affiliations that were auto-created off it (job affiliations are
left alone), shown as opt-out-able 'Will be deleted' rows. Same-day rows also
gain a per-row 'Delete instead' checkbox. The action is now available on every
event, not just trainings.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 2 +-
.../reconcile_affiliations_controller.rb | 23 ++--
.../affiliation_services/reconcile_event.rb | 100 ++++++++++++------
.../reconcile_facilitator_affiliation.rb | 20 ++--
app/views/events/_bulk_actions_menu.html.erb | 4 +-
.../reconcile_affiliations/index.html.erb | 37 +++++--
.../events/reconcile_affiliations_spec.rb | 35 +++++-
7 files changed, 150 insertions(+), 71 deletions(-)
diff --git a/AGENTS.md b/AGENTS.md
index 9508899d0..5a2a62613 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -256,7 +256,7 @@ action, or `authorize! :workshop, to: :summary?`).
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Per `(person, org)` it decides `:create` (heal a missing facilitator affiliation — pre-event for anyone, post-event only for attendees), `:deactivate`, or `:reactivate`. `#preview` returns the actionable rows for the confirm page; `#apply(included_keys:)` performs the rows the admin kept (creating via `CreateFromRegistration`, otherwise via `ReconcileFacilitatorAffiliation`) and stamps the event's `affiliations_reconciled_at`.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Per `(person, org)` it decides, for facilitator trainings, `:create` (heal a missing facilitator affiliation — pre-event for anyone, post-event only for attendees), `:deactivate`, or `:reactivate`; for non-training events, `:delete` (remove a facilitator affiliation auto-created off this event, leaving job affiliations). `#preview` returns the actionable rows for the confirm page; `#apply(included_keys:, delete_keys:)` performs the rows the admin kept — creating via `CreateFromRegistration`, deactivating/reactivating via `ReconcileFacilitatorAffiliation`, or deleting (a `:deactivate` row whose key is in `delete_keys` is deleted instead of same-dayed) — and stamps the event's `affiliations_reconciled_at`.
### Sectors
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
index b368857d4..f5d1d3391 100644
--- a/app/controllers/events/reconcile_affiliations_controller.rb
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -1,14 +1,13 @@
module Events
# The "Reconcile affiliations" bulk action: a preview-and-confirm page that
- # brings each registrant's owned facilitator affiliation in line with whether
- # they actually completed this facilitator training. Post-event it same-days the
- # affiliations of non-completers; the admin can opt individual rows out before
- # applying. Only facilitator-training events have facilitator affiliations to
- # reconcile, so the action is limited to them.
+ # brings each registrant's owned facilitator affiliation in line with reality.
+ # For a facilitator training it creates missing affiliations, same-days
+ # non-completers, and reactivates late attendees; for a non-training event it
+ # removes facilitator affiliations that were auto-created off it. The admin can
+ # opt individual rows out (and, for same-day rows, delete instead) before applying.
class ReconcileAffiliationsController < ApplicationController
include AhoyTracking
before_action :set_event
- before_action :require_facilitator_training
def index
authorize! @event, to: :reconcile_affiliations?
@@ -21,7 +20,10 @@ def index
def create
authorize! @event, to: :reconcile_affiliations?
- changed = AffiliationServices::ReconcileEvent.new(@event).apply(included_keys: params[:included])
+ changed = AffiliationServices::ReconcileEvent.new(@event).apply(
+ included_keys: params[:included] || [],
+ delete_keys: params[:delete] || []
+ )
redirect_to registrants_event_path(@event), notice: reconcile_notice(changed)
end
@@ -31,13 +33,6 @@ def set_event
@event = Event.find(params[:id])
end
- def require_facilitator_training
- return if @event.facilitator_training?
-
- redirect_to registrants_event_path(@event),
- alert: "Affiliation reconciliation applies to facilitator trainings only."
- end
-
def reconcile_notice(changed)
return "No affiliations needed reconciling." if changed.zero?
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 1c95e8f2e..aa6bc2ee6 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -1,17 +1,22 @@
module AffiliationServices
# Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
- # the event's registrants and the organizations they linked, and for each
- # (person, org) works out what should happen to their facilitator affiliation:
+ # the event's registrants and the organizations they linked, and per (person,
+ # org) works out what should happen to their **owned** facilitator affiliation
+ # (job affiliations are never touched):
#
- # :create — no facilitator affiliation exists yet but one should (heal a
- # missing affiliation): pre-event for any registrant, post-event
- # only for those who attended.
- # :deactivate — an owned affiliation whose (ended) training they didn't complete.
- # :reactivate — an owned affiliation same-dayed earlier, now attended.
+ # :create — facilitator training, none exists yet but one should (pre-event
+ # for anyone, post-event only for attendees).
+ # :deactivate — facilitator training, an owned affiliation whose (ended)
+ # training they didn't complete. The admin may choose to delete
+ # it instead of same-daying it (see `delete_keys`).
+ # :reactivate — facilitator training, an owned affiliation same-dayed earlier,
+ # now attended.
+ # :delete — NOT a facilitator training: an owned facilitator affiliation was
+ # auto-created off this event and shouldn't exist, so remove it.
#
# `preview` returns the actionable rows without writing so the admin can see them
- # and opt individual rows out; `apply(included_keys:)` performs the kept rows and
- # stamps the event's `affiliations_reconciled_at`.
+ # and opt individual rows out; `apply` performs the kept rows and stamps the
+ # event's `affiliations_reconciled_at`.
class ReconcileEvent
Row = Struct.new(:person, :organization, :registration, :affiliation, :action, :key, keyword_init: true)
@@ -27,13 +32,15 @@ def preview
rows
end
- # Apply the rows whose keys are in `included_keys`, stamp the event, and return
- # the number of pairs actually changed.
- def apply(included_keys:)
- keys = Array(included_keys).to_set
+ # Apply the rows whose keys are in `included_keys`. For :deactivate rows whose
+ # key is also in `delete_keys`, delete the affiliation instead of same-daying
+ # it. Stamps the event and returns the number of pairs actually changed.
+ def apply(included_keys:, delete_keys: [])
+ included = Array(included_keys).to_set
+ delete_instead = Array(delete_keys).to_set
changed = rows.count do |row|
- keys.include?(row.key) && apply_row(row)
+ included.include?(row.key) && perform(row, delete_instead: delete_instead.include?(row.key))
end
@event.update!(affiliations_reconciled_at: Time.current)
@@ -43,32 +50,50 @@ def apply(included_keys:)
private
def rows
- @rows ||= pairs.filter_map do |person, organization, registration|
- action = action_for(person, organization)
- next if action == :noop
-
- Row.new(
- person:,
- organization:,
- registration:,
- affiliation: owned_facilitator(person, organization),
- action:,
- key: self.class.key_for(person, organization)
- )
+ @rows ||= pairs.filter_map { |person, organization, registration| build_row(person, organization, registration) }
+ end
+
+ def build_row(person, organization, registration)
+ if @event.facilitator_training?
+ action = training_action(person, organization)
+ return if action == :noop
+
+ affiliation = action == :create ? nil : owned_facilitator(person, organization)
+ else
+ affiliation = owned_facilitator_from_event(person, organization)
+ return if affiliation.nil?
+
+ action = :delete
end
+
+ Row.new(person:, organization:, registration:, affiliation:, action:, key: self.class.key_for(person, organization))
end
- def action_for(person, organization)
+ def training_action(person, organization)
reconcile = ReconcileFacilitatorAffiliation.new(person:, organization:).plan
return reconcile unless reconcile == :noop
create_needed?(person, organization) ? :create : :noop
end
- def apply_row(row)
- return apply_create(row) if row.action == :create
-
- ReconcileFacilitatorAffiliation.call(person: row.person, organization: row.organization) != :noop
+ def perform(row, delete_instead:)
+ case row.action
+ when :create
+ apply_create(row)
+ true
+ when :delete
+ row.affiliation.destroy!
+ true
+ when :deactivate
+ service = ReconcileFacilitatorAffiliation.new(person: row.person, organization: row.organization)
+ targets = service.deactivatable_affiliations
+ return false if targets.empty?
+
+ delete_instead ? targets.each(&:destroy!) : service.call
+ true
+ else # :reactivate
+ ReconcileFacilitatorAffiliation.call(person: row.person, organization: row.organization) != :noop
+ end
end
def apply_create(row)
@@ -79,7 +104,6 @@ def apply_create(row)
training_date: @event.start_date,
event_registration: row.registration
)
- true
end
# A facilitator affiliation should exist but doesn't. Skip when an owned one
@@ -118,5 +142,17 @@ def owned_facilitator(person, organization)
.where.not(event_registration_id: nil)
.first
end
+
+ # An owned facilitator affiliation that was auto-created off *this* (non-training)
+ # event — the row a non-training reconcile removes. Hand-created rows (no link)
+ # and affiliations from other events are left alone.
+ def owned_facilitator_from_event(person, organization)
+ person.affiliations.facilitators
+ .where(organization:)
+ .where.not(event_registration_id: nil)
+ .joins(:event_registration)
+ .where(event_registrations: { event_id: @event.id })
+ .first
+ end
end
end
diff --git a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
index acc3bf54b..b4c837651 100644
--- a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
+++ b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
@@ -42,7 +42,7 @@ def plan
if completed_training?
rows.any? { |affiliation| !affiliation.active? } ? :reactivate : :noop
- elsif rows.any? { |affiliation| affiliation.active? && source_training_ended?(affiliation) }
+ elsif deactivatable_affiliations.any?
:deactivate
else
:noop
@@ -59,16 +59,20 @@ def completed_training?
.exists?
end
+ # The owned facilitator affiliations #call would same-day: active, and tied to a
+ # training that has already ended. Exposed so the bulk action can offer "delete
+ # instead of same-day" over the exact same set.
+ def deactivatable_affiliations
+ owned_facilitator_affiliations.select { |affiliation| affiliation.active? && source_training_ended?(affiliation) }
+ end
+
private
- def deactivate(rows)
- # Only same-day affiliations whose source training has actually ended. A row
- # tied to a still-upcoming training is a legitimate assumptive/upcoming
- # affiliation — leave it alone until that training is over.
- ended = rows.select { |affiliation| affiliation.active? && source_training_ended?(affiliation) }
- return :noop if ended.empty?
+ def deactivate(_rows)
+ targets = deactivatable_affiliations
+ return :noop if targets.empty?
- ended.each { |affiliation| affiliation.update!(end_date: affiliation.start_date || Date.current) }
+ targets.each { |affiliation| affiliation.update!(end_date: affiliation.start_date || Date.current) }
:deactivate
end
diff --git a/app/views/events/_bulk_actions_menu.html.erb b/app/views/events/_bulk_actions_menu.html.erb
index 4d61faffd..8a598d2d1 100644
--- a/app/views/events/_bulk_actions_menu.html.erb
+++ b/app/views/events/_bulk_actions_menu.html.erb
@@ -24,9 +24,7 @@
<% else %>
<%= link_to "Sign-ins", attendance_event_path(@event, return_to: "registrants"), class: item_class %>
<% end %>
- <% if @event.facilitator_training? %>
- <%= link_to "Reconcile affiliations", reconcile_affiliations_event_path(@event), class: item_class %>
- <% end %>
+ <%= link_to "Reconcile affiliations", reconcile_affiliations_event_path(@event), class: item_class %>
<%= link_to registrants_event_path(@event, format: :csv), class: item_class, data: { turbo_frame: "_top" } do %>
Download CSV
<% end %>
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 4bcdf4441..b6ca9f0b7 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -9,12 +9,19 @@
Reconcile affiliations
-
- This step brings facilitator affiliations in line with who registered and attended. Before the training it
- creates any missing facilitator affiliations for linked organizations. After the training it
- same-days the affiliation of anyone who didn't attend (its end date is set to its start
- date, so it no longer counts as active), and reactivates anyone later marked attended.
-
+ <% if @event.facilitator_training? %>
+
+ This step brings facilitator affiliations in line with who registered and attended. Before the training it
+ creates any missing facilitator affiliations for linked organizations. After the training it
+ same-days the affiliation of anyone who didn't attend (its end date is set to its start
+ date, so it no longer counts as active), and reactivates anyone later marked attended.
+
+ <% else %>
+
+ This event isn't a facilitator training, so any facilitator affiliation auto-created from it shouldn't exist.
+ This deletes those. Job affiliations are left untouched.
+
+ <% end %>
Only affiliations this app created from a registration are touched — hand-entered affiliations are always left
alone. Uncheck a row to spare it this time.
@@ -35,17 +42,25 @@
<%= form_with url: reconcile_affiliations_event_path(@event), method: :post do %>
<% @rows.each do |row| %>
-
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index 3d5b29e5a..7b6851571 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -41,12 +41,17 @@ def registrant_with_affiliation(status:)
expect(response.body).to include("Will be created")
end
- it "redirects for a non-training event" do
+ it "previews a facilitator affiliation on a non-training event as a deletion" do
non_training = create(:event, :ended, facilitator_training: false)
+ person = create(:person)
+ reg = create(:event_registration, event: non_training, registrant: person, status: "attended")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: 1.month.ago.to_date, event_registration: reg)
get reconcile_affiliations_event_path(non_training)
- expect(response).to redirect_to(registrants_event_path(non_training))
+ expect(response.body).to include("Will be deleted")
end
it "denies a non-admin" do
@@ -89,5 +94,31 @@ def registrant_with_affiliation(status:)
post reconcile_affiliations_event_path(upcoming), params: { included: [ key ] }
}.to change { person.affiliations.facilitators.where(organization: organization).count }.by(1)
end
+
+ it "deletes instead of same-daying when the delete option is checked" do
+ _person, affiliation = registrant_with_affiliation(status: "no_show")
+ key = AffiliationServices::ReconcileEvent.key_for(affiliation.person, organization)
+
+ post reconcile_affiliations_event_path(event), params: { included: [ key ], delete: [ key ] }
+
+ expect(Affiliation.exists?(affiliation.id)).to be(false)
+ end
+
+ it "deletes a facilitator affiliation auto-created off a non-training event, keeping the job affiliation" do
+ non_training = create(:event, :ended, facilitator_training: false)
+ person = create(:person)
+ reg = create(:event_registration, event: non_training, registrant: person, status: "attended")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ facilitator = create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: 1.month.ago.to_date, event_registration: reg)
+ job = create(:affiliation, person: person, organization: organization, title: "Counselor",
+ event_registration: reg)
+ key = AffiliationServices::ReconcileEvent.key_for(person, organization)
+
+ post reconcile_affiliations_event_path(non_training), params: { included: [ key ] }
+
+ expect(Affiliation.exists?(facilitator.id)).to be(false)
+ expect(Affiliation.exists?(job.id)).to be(true)
+ end
end
end
From a0cbd02635544339f3a1b87910c9e3a1edf00b9e Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 08:02:56 -0400
Subject: [PATCH 06/37] Show full reconcile picture: reasons for no-action rows
and attendance status
Preview now lists every registrant-org pair, grouped by action, and adds a
'Not reconciled' section explaining why each is left alone, with attendance
status shown per row.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
.../affiliation_services/reconcile_event.rb | 137 ++++++++++--------
.../reconcile_affiliations/index.html.erb | 102 ++++++++-----
.../events/reconcile_affiliations_spec.rb | 11 ++
3 files changed, 151 insertions(+), 99 deletions(-)
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index aa6bc2ee6..5f53babeb 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -1,24 +1,30 @@
module AffiliationServices
# Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
- # the event's registrants and the organizations they linked, and per (person,
- # org) works out what should happen to their **owned** facilitator affiliation
- # (job affiliations are never touched):
+ # the event's registrants and the organizations they linked, and classifies each
+ # (person, org) so the confirm page can show exactly what will (and won't) happen
+ # to their **owned** facilitator affiliation. Job affiliations are never touched.
#
+ # Actions:
# :create — facilitator training, none exists yet but one should (pre-event
# for anyone, post-event only for attendees).
# :deactivate — facilitator training, an owned affiliation whose (ended)
- # training they didn't complete. The admin may choose to delete
- # it instead of same-daying it (see `delete_keys`).
+ # training they didn't complete. The admin may delete it instead
+ # of same-daying it (see `delete_keys`).
# :reactivate — facilitator training, an owned affiliation same-dayed earlier,
# now attended.
- # :delete — NOT a facilitator training: an owned facilitator affiliation was
- # auto-created off this event and shouldn't exist, so remove it.
+ # :delete — NOT a facilitator training: facilitator affiliation(s)
+ # auto-created off this event that shouldn't exist.
+ # :noop — nothing to do; the row carries a `reason` for the page.
#
- # `preview` returns the actionable rows without writing so the admin can see them
- # and opt individual rows out; `apply` performs the kept rows and stamps the
- # event's `affiliations_reconciled_at`.
+ # `preview` returns every pair (actionable and not) so the admin sees the full
+ # picture; `apply` performs the kept actionable rows and stamps the event's
+ # `affiliations_reconciled_at`.
class ReconcileEvent
- Row = Struct.new(:person, :organization, :registration, :affiliation, :action, :key, keyword_init: true)
+ Row = Struct.new(:person, :organization, :registration, :affiliation, :action, :reason, :key, keyword_init: true) do
+ def actionable?
+ action != :noop
+ end
+ end
def self.key_for(person, organization)
"#{person.id}:#{organization.id}"
@@ -32,15 +38,15 @@ def preview
rows
end
- # Apply the rows whose keys are in `included_keys`. For :deactivate rows whose
- # key is also in `delete_keys`, delete the affiliation instead of same-daying
- # it. Stamps the event and returns the number of pairs actually changed.
+ # Apply the actionable rows whose keys are in `included_keys`. For :deactivate
+ # rows whose key is also in `delete_keys`, delete the affiliation instead of
+ # same-daying it. Stamps the event and returns the number of pairs changed.
def apply(included_keys:, delete_keys: [])
included = Array(included_keys).to_set
delete_instead = Array(delete_keys).to_set
changed = rows.count do |row|
- included.include?(row.key) && perform(row, delete_instead: delete_instead.include?(row.key))
+ row.actionable? && included.include?(row.key) && perform(row, delete_instead: delete_instead.include?(row.key))
end
@event.update!(affiliations_reconciled_at: Time.current)
@@ -50,30 +56,45 @@ def apply(included_keys:, delete_keys: [])
private
def rows
- @rows ||= pairs.filter_map { |person, organization, registration| build_row(person, organization, registration) }
+ @rows ||= pairs.map do |person, organization, registration|
+ action, reason, affiliation = classify(person, organization)
+ Row.new(person:, organization:, registration:, affiliation:, action:, reason:, key: self.class.key_for(person, organization))
+ end
+ end
+
+ def classify(person, organization)
+ owned = owned_facilitators(person, organization)
+ @event.facilitator_training? ? classify_training(person, organization, owned) : classify_non_training(person, organization, owned)
end
- def build_row(person, organization, registration)
- if @event.facilitator_training?
- action = training_action(person, organization)
- return if action == :noop
+ def classify_training(person, organization, owned)
+ attended = completed_training?(person, organization)
- affiliation = action == :create ? nil : owned_facilitator(person, organization)
- else
- affiliation = owned_facilitator_from_event(person, organization)
- return if affiliation.nil?
+ if owned.any?
+ return [ :reactivate, nil, owned.find { |a| !a.active? } ] if attended && owned.any? { |a| !a.active? }
+ return [ :noop, "Active — attended", owned.first ] if attended
- action = :delete
- end
+ deactivatable = owned.select { |a| a.active? && source_ended?(a) }
+ return [ :deactivate, nil, deactivatable.first ] if deactivatable.any?
+ return [ :noop, "Already deactivated — didn't attend", owned.first ] if owned.none?(&:active?)
- Row.new(person:, organization:, registration:, affiliation:, action:, key: self.class.key_for(person, organization))
+ [ :noop, "Training hasn't ended yet", owned.first ]
+ elsif hand_facilitator?(person, organization)
+ [ :noop, "Hand-entered affiliation — left alone", nil ]
+ elsif !@event.ended? || attended
+ [ :create, nil, nil ]
+ else
+ [ :noop, "Didn't attend — no affiliation to create", nil ]
+ end
end
- def training_action(person, organization)
- reconcile = ReconcileFacilitatorAffiliation.new(person:, organization:).plan
- return reconcile unless reconcile == :noop
+ def classify_non_training(person, organization, owned)
+ from_event = owned.select { |a| a.event_registration&.event_id == @event.id }
+ return [ :delete, nil, from_event.first ] if from_event.any?
+ return [ :noop, "Facilitator affiliation from another event — left alone", owned.first ] if owned.any?
+ return [ :noop, "Hand-entered affiliation — left alone", nil ] if hand_facilitator?(person, organization)
- create_needed?(person, organization) ? :create : :noop
+ [ :noop, "No facilitator affiliation", nil ]
end
def perform(row, delete_instead:)
@@ -82,7 +103,7 @@ def perform(row, delete_instead:)
apply_create(row)
true
when :delete
- row.affiliation.destroy!
+ destroy_from_event(row.person, row.organization)
true
when :deactivate
service = ReconcileFacilitatorAffiliation.new(person: row.person, organization: row.organization)
@@ -106,17 +127,30 @@ def apply_create(row)
)
end
- # A facilitator affiliation should exist but doesn't. Skip when an owned one
- # already exists (reconcile handles it — including a deliberately same-dayed
- # no-show we must not resurrect) or when a hand-created active-or-pending one
- # already covers it. Otherwise create it pre-event for anyone, post-event only
- # for those who attended.
- def create_needed?(person, organization)
- facilitators = person.affiliations.facilitators.where(organization:)
- return false if facilitators.where.not(event_registration_id: nil).exists?
- return false if facilitators.active_or_pending.exists?
+ def destroy_from_event(person, organization)
+ owned_facilitators(person, organization)
+ .select { |a| a.event_registration&.event_id == @event.id }
+ .each(&:destroy!)
+ end
+
+ def completed_training?(person, organization)
+ ReconcileFacilitatorAffiliation.new(person:, organization:).completed_training?
+ end
- !@event.ended? || ReconcileFacilitatorAffiliation.new(person:, organization:).completed_training?
+ def source_ended?(affiliation)
+ affiliation.event_registration&.event&.ended?
+ end
+
+ def hand_facilitator?(person, organization)
+ person.affiliations.facilitators.where(organization:, event_registration_id: nil).active_or_pending.exists?
+ end
+
+ def owned_facilitators(person, organization)
+ person.affiliations.facilitators
+ .where(organization:)
+ .where.not(event_registration_id: nil)
+ .includes(event_registration: :event)
+ .to_a
end
# Distinct (person, organization, registration) triples from the event's
@@ -135,24 +169,5 @@ def pairs
end
end
end
-
- def owned_facilitator(person, organization)
- person.affiliations.facilitators
- .where(organization:)
- .where.not(event_registration_id: nil)
- .first
- end
-
- # An owned facilitator affiliation that was auto-created off *this* (non-training)
- # event — the row a non-training reconcile removes. Hand-created rows (no link)
- # and affiliations from other events are left alone.
- def owned_facilitator_from_event(person, organization)
- person.affiliations.facilitators
- .where(organization:)
- .where.not(event_registration_id: nil)
- .joins(:event_registration)
- .where(event_registrations: { event_id: @event.id })
- .first
- end
end
end
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index b6ca9f0b7..fdcdfdd26 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -34,50 +34,76 @@
<% end %>
- Nothing to reconcile — every facilitator affiliation already matches its attendance.
+ No registrants have linked an organization, so there's nothing to reconcile.
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index 7b6851571..424f1dc2e 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -54,6 +54,17 @@ def registrant_with_affiliation(status:)
expect(response.body).to include("Will be deleted")
end
+ it "lists a no-action registrant under Not reconciled with the reason and attendance status" do
+ person, _affiliation = registrant_with_affiliation(status: "attended")
+
+ get reconcile_affiliations_event_path(event)
+
+ expect(response.body).to include("Not reconciled")
+ expect(response.body).to include("Active — attended")
+ expect(response.body).to include("Attended")
+ expect(response.body).to include(person.name)
+ end
+
it "denies a non-admin" do
sign_in create(:user)
From c4db320d7004e764dddb175bb9f6a5f305945ca4 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 08:06:45 -0400
Subject: [PATCH 07/37] Redesign reconcile page: group by person,
per-affiliation, editable attendance
Preview now groups actionable rows by person with the shared editable attendance
chip and a note of their other-org facilitator affiliations; each facilitator
affiliation is an individual row showing its date range with an Edit link to the
person page. 'Not reconciled' is a collapsible section grouped by reason
(hand-entered last), each reason collapsible too.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 2 +-
.../reconcile_affiliations_controller.rb | 5 +-
app/decorators/affiliation_decorator.rb | 8 +
.../affiliation_services/reconcile_event.rb | 202 +++++++++---------
.../reconcile_affiliations/index.html.erb | 124 ++++++-----
.../events/reconcile_affiliations_spec.rb | 11 +-
6 files changed, 189 insertions(+), 163 deletions(-)
diff --git a/AGENTS.md b/AGENTS.md
index 5a2a62613..8f8b3aa05 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -256,7 +256,7 @@ action, or `authorize! :workshop, to: :summary?`).
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Per `(person, org)` it decides, for facilitator trainings, `:create` (heal a missing facilitator affiliation — pre-event for anyone, post-event only for attendees), `:deactivate`, or `:reactivate`; for non-training events, `:delete` (remove a facilitator affiliation auto-created off this event, leaving job affiliations). `#preview` returns the actionable rows for the confirm page; `#apply(included_keys:, delete_keys:)` performs the rows the admin kept — creating via `CreateFromRegistration`, deactivating/reactivating via `ReconcileFacilitatorAffiliation`, or deleting (a `:deactivate` row whose key is in `delete_keys` is deleted instead of same-dayed) — and stamps the event's `affiliations_reconciled_at`.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** tied to an org a registrant linked, classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. `#actionable_person_groups` groups the actionable rows by person (with their attendance registration and other-org facilitator affiliations for context) for the confirm page; `#skipped_reason_sections` groups the no-action rows by reason (hand-entered last). `#apply(included_keys:, delete_keys:)` performs the kept rows (keys are `aff:` or `create::`; a `:deactivate` row also in `delete_keys` is deleted instead of same-dayed) and stamps `affiliations_reconciled_at`. Job affiliations are never touched.
### Sectors
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
index f5d1d3391..85296e7de 100644
--- a/app/controllers/events/reconcile_affiliations_controller.rb
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -13,7 +13,10 @@ def index
authorize! @event, to: :reconcile_affiliations?
track_view("events.reconcile_affiliations", { event_id: @event.id })
- @rows = AffiliationServices::ReconcileEvent.new(@event).preview
+ reconcile = AffiliationServices::ReconcileEvent.new(@event)
+ @person_groups = reconcile.actionable_person_groups
+ @skipped_sections = reconcile.skipped_reason_sections
+ @has_rows = reconcile.any_rows?
@event = @event.decorate
end
diff --git a/app/decorators/affiliation_decorator.rb b/app/decorators/affiliation_decorator.rb
index 86f3f24dc..2fb8221ea 100644
--- a/app/decorators/affiliation_decorator.rb
+++ b/app/decorators/affiliation_decorator.rb
@@ -2,4 +2,12 @@ class AffiliationDecorator < ApplicationDecorator
def detail(length: nil)
"#{person.full_name}: #{title.presence || position} - #{organization.name}"
end
+
+ # Compact "started – ended" range for the affiliation, e.g. "Sep 17, 2026 – present".
+ # Reads "no start date" when unset so a blank date isn't silently omitted.
+ def date_range
+ start = start_date ? h.l(start_date, format: :long) : "no start date"
+ finish = end_date ? h.l(end_date, format: :long) : "present"
+ "#{start} – #{finish}"
+ end
end
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 5f53babeb..a369a1502 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -1,51 +1,63 @@
module AffiliationServices
# Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
- # the event's registrants and the organizations they linked, and classifies each
- # (person, org) so the confirm page can show exactly what will (and won't) happen
- # to their **owned** facilitator affiliation. Job affiliations are never touched.
+ # the event's registrants and, for each facilitator affiliation tied to an org
+ # they linked, works out what should happen to it (job affiliations are never
+ # touched). Produces one row per affiliation so each is individually actionable.
#
# Actions:
# :create — facilitator training, none exists yet but one should (pre-event
# for anyone, post-event only for attendees).
- # :deactivate — facilitator training, an owned affiliation whose (ended)
- # training they didn't complete. The admin may delete it instead
- # of same-daying it (see `delete_keys`).
- # :reactivate — facilitator training, an owned affiliation same-dayed earlier,
- # now attended.
- # :delete — NOT a facilitator training: facilitator affiliation(s)
- # auto-created off this event that shouldn't exist.
- # :noop — nothing to do; the row carries a `reason` for the page.
+ # :deactivate — facilitator training, owned, its (ended) training wasn't
+ # completed. The admin may delete it instead of same-daying it.
+ # :reactivate — facilitator training, owned, same-dayed earlier, now attended.
+ # :delete — NOT a facilitator training: an owned affiliation auto-created
+ # off this event that shouldn't exist.
+ # :noop — nothing to do; the row carries a `reason`.
#
- # `preview` returns every pair (actionable and not) so the admin sees the full
- # picture; `apply` performs the kept actionable rows and stamps the event's
- # `affiliations_reconciled_at`.
+ # `actionable_person_groups` groups the actionable rows by person (with their
+ # attendance registration and other-org facilitator affiliations for context);
+ # `skipped_reason_sections` groups the no-action rows by reason (hand-entered
+ # last). `apply` performs the kept actionable rows and stamps the event.
class ReconcileEvent
- Row = Struct.new(:person, :organization, :registration, :affiliation, :action, :reason, :key, keyword_init: true) do
+ HAND_ENTERED = "Hand-entered affiliation — left alone".freeze
+
+ Row = Struct.new(:person, :registration, :organization, :affiliation, :action, :reason, :key, keyword_init: true) do
def actionable?
action != :noop
end
end
- def self.key_for(person, organization)
- "#{person.id}:#{organization.id}"
- end
-
def initialize(event)
@event = event
end
- def preview
- rows
+ # Actionable rows grouped by person: [{ person:, registration:, rows:,
+ # other_facilitators: }]. `other_facilitators` are the person's active
+ # facilitator affiliations with orgs they did NOT link on this event.
+ def actionable_person_groups
+ all_rows.select(&:actionable?).group_by(&:person).map do |person, rows|
+ { person:, registration: rows.first.registration, rows:, other_facilitators: other_facilitators(person) }
+ end
+ end
+
+ # No-action rows grouped by reason, hand-entered last: [[reason, [rows]]].
+ def skipped_reason_sections
+ grouped = all_rows.reject(&:actionable?).group_by(&:reason)
+ grouped.keys.sort_by { |reason| [ reason == HAND_ENTERED ? 1 : 0, reason ] }.map { |reason| [ reason, grouped[reason] ] }
+ end
+
+ def any_rows?
+ all_rows.any?
end
# Apply the actionable rows whose keys are in `included_keys`. For :deactivate
# rows whose key is also in `delete_keys`, delete the affiliation instead of
- # same-daying it. Stamps the event and returns the number of pairs changed.
+ # same-daying it. Stamps the event and returns the number of rows changed.
def apply(included_keys:, delete_keys: [])
included = Array(included_keys).to_set
delete_instead = Array(delete_keys).to_set
- changed = rows.count do |row|
+ changed = all_rows.count do |row|
row.actionable? && included.include?(row.key) && perform(row, delete_instead: delete_instead.include?(row.key))
end
@@ -55,82 +67,78 @@ def apply(included_keys:, delete_keys: [])
private
- def rows
- @rows ||= pairs.map do |person, organization, registration|
- action, reason, affiliation = classify(person, organization)
- Row.new(person:, organization:, registration:, affiliation:, action:, reason:, key: self.class.key_for(person, organization))
+ def all_rows
+ @all_rows ||= registrations_by_person.flat_map do |person, registrations|
+ registration = registrations.first
+ linked_organizations(registrations).flat_map { |organization| rows_for(person, registration, organization) }
end
end
- def classify(person, organization)
- owned = owned_facilitators(person, organization)
- @event.facilitator_training? ? classify_training(person, organization, owned) : classify_non_training(person, organization, owned)
+ def rows_for(person, registration, organization)
+ attended = completed_training?(person, organization)
+ facilitators = person.affiliations.facilitators
+ .where(organization:)
+ .includes(event_registration: :event)
+ .to_a
+
+ rows = facilitators.map { |affiliation| affiliation_row(person, registration, organization, affiliation, attended) }
+ rows << create_row(person, registration, organization, attended) if facilitators.empty? && @event.facilitator_training?
+ rows.compact
end
- def classify_training(person, organization, owned)
- attended = completed_training?(person, organization)
+ def affiliation_row(person, registration, organization, affiliation, attended)
+ action, reason = classify_affiliation(affiliation, attended)
+ Row.new(person:, registration:, organization:, affiliation:, action:, reason:, key: "aff:#{affiliation.id}")
+ end
+
+ def classify_affiliation(affiliation, attended)
+ owned = affiliation.event_registration_id.present?
- if owned.any?
- return [ :reactivate, nil, owned.find { |a| !a.active? } ] if attended && owned.any? { |a| !a.active? }
- return [ :noop, "Active — attended", owned.first ] if attended
+ unless @event.facilitator_training?
+ return [ :delete, nil ] if owned && affiliation.event_registration&.event_id == @event.id
+ return [ :noop, "Facilitator affiliation from another event" ] if owned
- deactivatable = owned.select { |a| a.active? && source_ended?(a) }
- return [ :deactivate, nil, deactivatable.first ] if deactivatable.any?
- return [ :noop, "Already deactivated — didn't attend", owned.first ] if owned.none?(&:active?)
+ return [ :noop, HAND_ENTERED ]
+ end
+
+ return [ :noop, HAND_ENTERED ] unless owned
- [ :noop, "Training hasn't ended yet", owned.first ]
- elsif hand_facilitator?(person, organization)
- [ :noop, "Hand-entered affiliation — left alone", nil ]
- elsif !@event.ended? || attended
- [ :create, nil, nil ]
+ if attended
+ affiliation.active? ? [ :noop, "Active — attended" ] : [ :reactivate, nil ]
+ elsif affiliation.active? && source_ended?(affiliation)
+ [ :deactivate, nil ]
+ elsif affiliation.active?
+ [ :noop, "Training hasn't ended yet" ]
else
- [ :noop, "Didn't attend — no affiliation to create", nil ]
+ [ :noop, "Already deactivated — didn't attend" ]
end
end
- def classify_non_training(person, organization, owned)
- from_event = owned.select { |a| a.event_registration&.event_id == @event.id }
- return [ :delete, nil, from_event.first ] if from_event.any?
- return [ :noop, "Facilitator affiliation from another event — left alone", owned.first ] if owned.any?
- return [ :noop, "Hand-entered affiliation — left alone", nil ] if hand_facilitator?(person, organization)
-
- [ :noop, "No facilitator affiliation", nil ]
+ def create_row(person, registration, organization, attended)
+ if !@event.ended? || attended
+ Row.new(person:, registration:, organization:, affiliation: nil, action: :create, reason: nil,
+ key: "create:#{person.id}:#{organization.id}")
+ else
+ Row.new(person:, registration:, organization:, affiliation: nil, action: :noop,
+ reason: "Didn't attend — no affiliation created", key: "none:#{person.id}:#{organization.id}")
+ end
end
def perform(row, delete_instead:)
case row.action
when :create
- apply_create(row)
- true
+ AffiliationServices::CreateFromRegistration.call(
+ person: row.person, organization: row.organization, facilitator_training: true,
+ training_date: @event.start_date, event_registration: row.registration
+ )
when :delete
- destroy_from_event(row.person, row.organization)
- true
+ row.affiliation.destroy!
when :deactivate
- service = ReconcileFacilitatorAffiliation.new(person: row.person, organization: row.organization)
- targets = service.deactivatable_affiliations
- return false if targets.empty?
-
- delete_instead ? targets.each(&:destroy!) : service.call
- true
- else # :reactivate
- ReconcileFacilitatorAffiliation.call(person: row.person, organization: row.organization) != :noop
+ delete_instead ? row.affiliation.destroy! : row.affiliation.update!(end_date: row.affiliation.start_date || Date.current)
+ when :reactivate
+ row.affiliation.update!(end_date: nil)
end
- end
-
- def apply_create(row)
- AffiliationServices::CreateFromRegistration.call(
- person: row.person,
- organization: row.organization,
- facilitator_training: true,
- training_date: @event.start_date,
- event_registration: row.registration
- )
- end
-
- def destroy_from_event(person, organization)
- owned_facilitators(person, organization)
- .select { |a| a.event_registration&.event_id == @event.id }
- .each(&:destroy!)
+ true
end
def completed_training?(person, organization)
@@ -141,33 +149,23 @@ def source_ended?(affiliation)
affiliation.event_registration&.event&.ended?
end
- def hand_facilitator?(person, organization)
- person.affiliations.facilitators.where(organization:, event_registration_id: nil).active_or_pending.exists?
+ def other_facilitators(person)
+ person.affiliations.active.facilitators
+ .where.not(organization_id: linked_org_ids(person))
+ .includes(:organization)
+ .to_a
end
- def owned_facilitators(person, organization)
- person.affiliations.facilitators
- .where(organization:)
- .where.not(event_registration_id: nil)
- .includes(event_registration: :event)
- .to_a
+ def linked_org_ids(person)
+ linked_organizations(registrations_by_person[person]).map(&:id)
end
- # Distinct (person, organization, registration) triples from the event's
- # registrants and the organizations each linked to their registration.
- def pairs
- @pairs ||= begin
- seen = Set.new
- @event.event_registrations.includes(:registrant, :organizations).flat_map do |registration|
- registration.organizations.filter_map do |organization|
- key = [ registration.registrant_id, organization.id ]
- next if seen.include?(key)
-
- seen << key
- [ registration.registrant, organization, registration ]
- end
- end
- end
+ def linked_organizations(registrations)
+ registrations.flat_map(&:organizations).uniq
+ end
+
+ def registrations_by_person
+ @registrations_by_person ||= @event.event_registrations.includes(:registrant, :organizations).group_by(&:registrant)
end
end
end
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index fdcdfdd26..dad86ce0a 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -13,8 +13,8 @@
This step brings facilitator affiliations in line with who registered and attended. Before the training it
creates any missing facilitator affiliations for linked organizations. After the training it
- same-days the affiliation of anyone who didn't attend (its end date is set to its start
- date, so it no longer counts as active), and reactivates anyone later marked attended.
+ same-days the affiliation of anyone who didn't attend, and reactivates anyone later marked
+ attended. Job affiliations are never touched.
<% else %>
@@ -22,88 +22,108 @@
This deletes those. Job affiliations are left untouched.
<% end %>
-
- Only affiliations this app created from a registration are touched — hand-entered affiliations are always left
- alone. Uncheck a row to spare it this time.
-
+
Only affiliations this app created from a registration are touched — hand-entered ones are always left alone.
<% if @event.affiliations_reconciled_at %>
Last reconciled <%= @event.affiliations_reconciled_at.to_fs(:long) %>.
<% end %>
- <% if @event.affiliations_reconciliation_stale? %>
-
Attendance has changed since the last reconciliation — re-run to bring affiliations up to date.
+ <% if @person_groups.any? && @event.affiliations_reconciliation_stale? %>
+
Attendance has changed since the last reconciliation — apply again below to bring affiliations up to date.
diff --git a/spec/decorators/affiliation_decorator_spec.rb b/spec/decorators/affiliation_decorator_spec.rb
new file mode 100644
index 000000000..bb67da60b
--- /dev/null
+++ b/spec/decorators/affiliation_decorator_spec.rb
@@ -0,0 +1,23 @@
+require "rails_helper"
+
+RSpec.describe AffiliationDecorator do
+ describe "#date_range" do
+ it "reads 'present' when there is no end date" do
+ affiliation = build(:affiliation, start_date: Date.new(2026, 10, 13), end_date: nil)
+
+ expect(affiliation.decorate.date_range).to eq("Oct 13, 2026 – present")
+ end
+
+ it "shows both dates when the affiliation has ended" do
+ affiliation = build(:affiliation, start_date: Date.new(2026, 10, 13), end_date: Date.new(2026, 10, 13))
+
+ expect(affiliation.decorate.date_range).to eq("Oct 13, 2026 – Oct 13, 2026")
+ end
+
+ it "reads 'no start date' when the start date is unset" do
+ affiliation = build(:affiliation, start_date: nil, end_date: nil)
+
+ expect(affiliation.decorate.date_range).to eq("no start date – present")
+ end
+ end
+end
From 98a8c4f0ed5608681371472561acc5fa7615eb48 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 08:20:12 -0400
Subject: [PATCH 09/37] Reconcile all facilitator affiliations incl.
hand-entered; clearer action controls
Reconcile every facilitator affiliation for a linked org (not just app-created),
gated to post-event so a pre-event run never deactivates and with per-row opt-out.
Move the include checkbox into the action chip so it's clear checking it performs
that action, move the other-org facilitator note below the rows, link org/dates to
the specific affiliation anchor and names to the registration, and strengthen the
Not reconciled section headers (open by default, expand/collapse all).
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 2 +-
.../affiliation_services/reconcile_event.rb | 44 ++++++-----
.../reconcile_affiliations/index.html.erb | 77 +++++++++++--------
.../events/reconcile_affiliations_spec.rb | 22 ++++++
4 files changed, 91 insertions(+), 54 deletions(-)
diff --git a/AGENTS.md b/AGENTS.md
index 8f8b3aa05..84ad056e0 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -256,7 +256,7 @@ action, or `authorize! :workshop, to: :summary?`).
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** tied to an org a registrant linked, classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. `#actionable_person_groups` groups the actionable rows by person (with their attendance registration and other-org facilitator affiliations for context) for the confirm page; `#skipped_reason_sections` groups the no-action rows by reason (hand-entered last). `#apply(included_keys:, delete_keys:)` performs the kept rows (keys are `aff:` or `create::`; a `:deactivate` row also in `delete_keys` is deleted instead of same-dayed) and stamps `affiliations_reconciled_at`. Job affiliations are never touched.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** for an org a registrant linked — **hand-entered rows included, not just app-created ones** — classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. Deactivation is gated to post-event (owned rows on their source training's `ended?`, hand-entered on this event's `ended?`), so a pre-event run never deactivates. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#apply(included_keys:, delete_keys:)` performs the kept rows (keys are `aff:` or `create::`; a `:deactivate` row also in `delete_keys` is deleted instead of same-dayed) and stamps `affiliations_reconciled_at`. Job affiliations are never touched.
### Sectors
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index a369a1502..958e38f8b 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -16,11 +16,10 @@ module AffiliationServices
#
# `actionable_person_groups` groups the actionable rows by person (with their
# attendance registration and other-org facilitator affiliations for context);
- # `skipped_reason_sections` groups the no-action rows by reason (hand-entered
- # last). `apply` performs the kept actionable rows and stamps the event.
+ # `skipped_reason_sections` groups the no-action rows by reason. `apply` performs
+ # the kept actionable rows and stamps the event. Every facilitator affiliation for
+ # a linked org is reconciled — hand-entered rows included, not just app-created ones.
class ReconcileEvent
- HAND_ENTERED = "Hand-entered affiliation — left alone".freeze
-
Row = Struct.new(:person, :registration, :organization, :affiliation, :action, :reason, :key, keyword_init: true) do
def actionable?
action != :noop
@@ -43,7 +42,7 @@ def actionable_person_groups
# No-action rows grouped by reason, hand-entered last: [[reason, [rows]]].
def skipped_reason_sections
grouped = all_rows.reject(&:actionable?).group_by(&:reason)
- grouped.keys.sort_by { |reason| [ reason == HAND_ENTERED ? 1 : 0, reason ] }.map { |reason| [ reason, grouped[reason] ] }
+ grouped.keys.sort.map { |reason| [ reason, grouped[reason] ] }
end
def any_rows?
@@ -75,14 +74,24 @@ def all_rows
end
def rows_for(person, registration, organization)
- attended = completed_training?(person, organization)
facilitators = person.affiliations.facilitators
.where(organization:)
.includes(event_registration: :event)
.to_a
+ unless @event.facilitator_training?
+ # A non-training event confers no facilitation, so it only removes
+ # facilitator affiliations that were auto-created off it.
+ return facilitators.filter_map do |affiliation|
+ next unless affiliation.event_registration&.event_id == @event.id
+
+ Row.new(person:, registration:, organization:, affiliation:, action: :delete, reason: nil, key: "aff:#{affiliation.id}")
+ end
+ end
+
+ attended = completed_training?(person, organization)
rows = facilitators.map { |affiliation| affiliation_row(person, registration, organization, affiliation, attended) }
- rows << create_row(person, registration, organization, attended) if facilitators.empty? && @event.facilitator_training?
+ rows << create_row(person, registration, organization, attended) if facilitators.empty?
rows.compact
end
@@ -91,21 +100,14 @@ def affiliation_row(person, registration, organization, affiliation, attended)
Row.new(person:, registration:, organization:, affiliation:, action:, reason:, key: "aff:#{affiliation.id}")
end
+ # Reconciles EVERY facilitator affiliation for the org — hand-entered ones
+ # included, not just app-created rows. Deactivation only applies once the
+ # governing training has ended (a hand-entered row has no source training, so
+ # it's gated on this event ending) — so a pre-event run never deactivates.
def classify_affiliation(affiliation, attended)
- owned = affiliation.event_registration_id.present?
-
- unless @event.facilitator_training?
- return [ :delete, nil ] if owned && affiliation.event_registration&.event_id == @event.id
- return [ :noop, "Facilitator affiliation from another event" ] if owned
-
- return [ :noop, HAND_ENTERED ]
- end
-
- return [ :noop, HAND_ENTERED ] unless owned
-
if attended
affiliation.active? ? [ :noop, "Active — attended" ] : [ :reactivate, nil ]
- elsif affiliation.active? && source_ended?(affiliation)
+ elsif affiliation.active? && deactivation_ready?(affiliation)
[ :deactivate, nil ]
elsif affiliation.active?
[ :noop, "Training hasn't ended yet" ]
@@ -114,6 +116,10 @@ def classify_affiliation(affiliation, attended)
end
end
+ def deactivation_ready?(affiliation)
+ affiliation.event_registration_id ? source_ended?(affiliation) : @event.ended?
+ end
+
def create_row(person, registration, organization, attended)
if !@event.ended? || attended
Row.new(person:, registration:, organization:, affiliation: nil, action: :create, reason: nil,
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 463a9c27a..967d8ec4c 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -22,7 +22,7 @@
This deletes those. Job affiliations are left untouched.
<% end %>
-
Only affiliations this app created from a registration are touched — hand-entered ones are always left alone.
+
Every facilitator affiliation for a linked organization is reconciled against attendance — including hand-entered ones. Review each row and uncheck any you want to leave as-is.
<% if @event.affiliations_reconciled_at %>
Last reconciled <%= @event.affiliations_reconciled_at.to_fs(:long) %>.
<% end %>
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index 86d698f58..71ee917a3 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -65,6 +65,17 @@ def registrant_with_affiliation(status:)
expect(response.body).to include(person.name)
end
+ it "reconciles a hand-entered (unowned) facilitator affiliation too" do
+ person = create(:person)
+ reg = create(:event_registration, event: event, registrant: person, status: "no_show")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ create(:affiliation, person: person, organization: organization, title: "Facilitator", start_date: 1.year.ago.to_date)
+
+ get reconcile_affiliations_event_path(event)
+
+ expect(response.body).to include("Will be deactivated")
+ end
+
it "denies a non-admin" do
sign_in create(:user)
@@ -113,6 +124,17 @@ def registrant_with_affiliation(status:)
expect(Affiliation.exists?(affiliation.id)).to be(false)
end
+ it "deactivates a hand-entered facilitator affiliation when included" do
+ person = create(:person)
+ reg = create(:event_registration, event: event, registrant: person, status: "no_show")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ hand_entered = create(:affiliation, person: person, organization: organization, title: "Facilitator", start_date: 1.year.ago.to_date)
+
+ post reconcile_affiliations_event_path(event), params: { included: [ "aff:#{hand_entered.id}" ] }
+
+ expect(hand_entered.reload).not_to be_active
+ end
+
it "deletes a facilitator affiliation auto-created off a non-training event, keeping the job affiliation" do
non_training = create(:event, :ended, facilitator_training: false)
person = create(:person)
From 438dc23600d7b1aca1fa44980569916639791e98 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 08:24:39 -0400
Subject: [PATCH 10/37] Action toggles as buttons with error-red on select,
hover tooltips, header note
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Style the include/delete-instead controls as buttons that only turn error-red when
selected (peer-checked, no JS); add hover tooltips explaining deactivate/delete
(delete as bullets: this affiliation only, job + other-org affiliations untouched).
Move the 'Also a facilitator at …' note beside the name, truncated and linking to
the single affiliation anchor (or the affiliations section when several). Order the
Not reconciled sections with 'Active — attended' second-to-last.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
.../affiliation_services/reconcile_event.rb | 13 +++++-
.../reconcile_affiliations/_tooltip.html.erb | 17 +++++++
.../reconcile_affiliations/index.html.erb | 44 ++++++++++++-------
3 files changed, 56 insertions(+), 18 deletions(-)
create mode 100644 app/views/events/reconcile_affiliations/_tooltip.html.erb
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 958e38f8b..7cd9816cb 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -39,10 +39,19 @@ def actionable_person_groups
end
end
- # No-action rows grouped by reason, hand-entered last: [[reason, [rows]]].
+ # No-action rows grouped by reason: [[reason, [rows]]]. "Active — attended" sorts
+ # second-to-last and the trivial "no affiliation" bucket last; the rest alphabetical.
def skipped_reason_sections
grouped = all_rows.reject(&:actionable?).group_by(&:reason)
- grouped.keys.sort.map { |reason| [ reason, grouped[reason] ] }
+ grouped.keys.sort_by { |reason| [ reason_rank(reason), reason ] }.map { |reason| [ reason, grouped[reason] ] }
+ end
+
+ def reason_rank(reason)
+ case reason
+ when "Active — attended" then 8
+ when "Didn't attend — no affiliation created" then 9
+ else 0
+ end
end
def any_rows?
diff --git a/app/views/events/reconcile_affiliations/_tooltip.html.erb b/app/views/events/reconcile_affiliations/_tooltip.html.erb
new file mode 100644
index 000000000..582555db3
--- /dev/null
+++ b/app/views/events/reconcile_affiliations/_tooltip.html.erb
@@ -0,0 +1,17 @@
+<%# Hover explanation for a reconcile action. `kind` is the action symbol. %>
+
+ <% case kind %>
+ <% when :deactivate %>
+ Ends this facilitator affiliation as of today (sets its end date to its start date) so it no longer counts as active. The record is kept and reactivates if the person is later marked attended.
+ <% when :delete %>
+
Permanently deletes this facilitator affiliation. Everything else stays as-is:
+
+
Job affiliations for this org
+
Facilitator affiliations for other orgs
+
+ <% when :create %>
+ Creates the facilitator affiliation for this organization.
+ <% when :reactivate %>
+ Clears the end date so this facilitator affiliation counts as active again.
+ <% end %>
+
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 967d8ec4c..1b74df0ae 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -46,15 +46,31 @@
<%= form_with url: reconcile_affiliations_event_path(@event), method: :post do %>
<% if row.action == :deactivate %>
-
- <%= check_box_tag "delete[]", row.key, false, id: "delete_#{row.key}", class: "h-4 w-4 rounded border-gray-300 text-red-600" %>
- Delete instead
+
+ <%= check_box_tag "delete[]", row.key, false, id: "delete_#{row.key}", class: "peer sr-only" %>
+ Delete instead
+ <%= render "tooltip", kind: :delete %>
<% end %>
- <%# Checkbox lives inside the action chip so it's clear that checking it performs that action. %>
-
- <%= check_box_tag "included[]", row.key, true, id: "included_#{row.key}", class: "h-4 w-4 rounded border-gray-300 text-blue-600" %>
- <%= heading %>
+ <%# Hidden checkbox toggles a button-styled label: it only shows the action color when selected. %>
+
+ <%= check_box_tag "included[]", row.key, true, id: "included_#{row.key}", class: "peer sr-only" %>
+ <%= heading %>
+ <%= render "tooltip", kind: row.action %>
<% end %>
-
- <% if group[:other_facilitators].any? %>
-
- Also a facilitator at <%= group[:other_facilitators].map { |a| a.organization.name }.to_sentence %>.
-
- <% end %>
<% end %>
From 35efe36975b74a1797d6313cea82e87f7b6e027f Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 08:31:05 -0400
Subject: [PATCH 11/37] Move Collapse all into the Not reconciled header; more
space between sections
Co-Authored-By: Claude Opus 4.8 (1M context)
---
.../events/reconcile_affiliations/index.html.erb | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 1b74df0ae..cf3335c76 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -111,15 +111,13 @@
<% if @skipped_sections.any? %>
<% skipped_count = @skipped_sections.sum { |(_reason, rows)| rows.size } %>
-
-
+
+
Not reconciled (<%= skipped_count %>)
+
-
-
-
-
+
<% @skipped_sections.each do |reason, rows| %>
From 8247cf48d519396b922a7b499d830914a7a8873f Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:06:13 -0400
Subject: [PATCH 12/37] Show checkbox inside action buttons; make
deactivate/delete mutually exclusive
Move the checkbox back inside each button (has-[:checked] colors the whole button
on select, error-red for deactivate/delete). Add an exclusive-checkboxes Stimulus
controller so checking 'Delete instead' clears 'Will be deactivated' and vice versa;
apply now treats a delete key as delete regardless of the include key.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
.../exclusive_checkboxes_controller.js | 17 ++++++++++++
.../affiliation_services/reconcile_event.rb | 19 +++++++++++---
.../reconcile_affiliations/index.html.erb | 26 +++++++++----------
3 files changed, 45 insertions(+), 17 deletions(-)
create mode 100644 app/frontend/javascript/controllers/exclusive_checkboxes_controller.js
diff --git a/app/frontend/javascript/controllers/exclusive_checkboxes_controller.js b/app/frontend/javascript/controllers/exclusive_checkboxes_controller.js
new file mode 100644
index 000000000..cd165d589
--- /dev/null
+++ b/app/frontend/javascript/controllers/exclusive_checkboxes_controller.js
@@ -0,0 +1,17 @@
+import { Controller } from "@hotwired/stimulus"
+
+// Connects to data-controller="exclusive-checkboxes"
+// Makes a small group of checkboxes mutually exclusive — like radios, but any can
+// be left unchecked. Checking one clears the others in the group (e.g. "Delete
+// instead" and "Will be deactivated" are two choices for the same row).
+export default class extends Controller {
+ static targets = ["box"]
+
+ select(event) {
+ if (!event.target.checked) return
+
+ this.boxTargets.forEach((box) => {
+ if (box !== event.target) box.checked = false
+ })
+ }
+}
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 7cd9816cb..9bab55a53 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -63,10 +63,19 @@ def any_rows?
# same-daying it. Stamps the event and returns the number of rows changed.
def apply(included_keys:, delete_keys: [])
included = Array(included_keys).to_set
- delete_instead = Array(delete_keys).to_set
+ deletes = Array(delete_keys).to_set
changed = all_rows.count do |row|
- row.actionable? && included.include?(row.key) && perform(row, delete_instead: delete_instead.include?(row.key))
+ next false unless row.actionable?
+
+ if deletes.include?(row.key) && row.affiliation
+ row.affiliation.destroy!
+ true
+ elsif included.include?(row.key)
+ perform(row)
+ else
+ false
+ end
end
@event.update!(affiliations_reconciled_at: Time.current)
@@ -139,7 +148,7 @@ def create_row(person, registration, organization, attended)
end
end
- def perform(row, delete_instead:)
+ def perform(row)
case row.action
when :create
AffiliationServices::CreateFromRegistration.call(
@@ -149,7 +158,9 @@ def perform(row, delete_instead:)
when :delete
row.affiliation.destroy!
when :deactivate
- delete_instead ? row.affiliation.destroy! : row.affiliation.update!(end_date: row.affiliation.start_date || Date.current)
+ # Same-day it: end_date = the affiliation's own start_date (start_date itself
+ # is never changed), which the model turns into inactive.
+ row.affiliation.update!(end_date: row.affiliation.start_date || Date.current)
when :reactivate
row.affiliation.update!(end_date: nil)
end
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index cf3335c76..a0ced04a2 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -47,12 +47,12 @@
From c51b6259afb9255e3b0cea9312474865bc0940f5 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:18:17 -0400
Subject: [PATCH 13/37] Two-step reconcile: Preview changes then confirmation
screen
'Preview changes' now posts to a confirmation screen that shows exactly which
affiliations get created/reactivated/deactivated/deleted (actioned rows only),
with Go back to edit (selections restored) or Perform changes. Add per-row
instructions under the action buttons and a header row with a warning that checked
boxes change affiliations. New exclusive-checkboxes controller registered.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 4 +-
.../reconcile_affiliations_controller.rb | 15 ++++++
.../affiliation_services/reconcile_event.rb | 17 ++++++
.../reconcile_affiliations/confirm.html.erb | 52 +++++++++++++++++++
.../reconcile_affiliations/index.html.erb | 44 +++++++++++-----
config/routes.rb | 3 +-
.../events/reconcile_affiliations_spec.rb | 35 ++++++++++---
spec/views/page_bg_class_alignment_spec.rb | 1 +
8 files changed, 148 insertions(+), 23 deletions(-)
create mode 100644 app/views/events/reconcile_affiliations/confirm.html.erb
diff --git a/AGENTS.md b/AGENTS.md
index 84ad056e0..c47619465 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -71,7 +71,7 @@ This codebase (Rails 8.1)
| Directory | Purpose |
|---|---|
| `app/frontend/entrypoints/` | Vite entry points (application.js, application.css) |
-| `app/frontend/javascript/controllers/` | Stimulus controllers (77) |
+| `app/frontend/javascript/controllers/` | Stimulus controllers (78) |
| `app/frontend/javascript/rhino/` | Rich text editor customizations (mentions, grid) |
| `app/frontend/stylesheets/` | Tailwind CSS and component styles |
@@ -256,7 +256,7 @@ action, or `authorize! :workshop, to: :summary?`).
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** for an org a registrant linked — **hand-entered rows included, not just app-created ones** — classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. Deactivation is gated to post-event (owned rows on their source training's `ended?`, hand-entered on this event's `ended?`), so a pre-event run never deactivates. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#apply(included_keys:, delete_keys:)` performs the kept rows (keys are `aff:` or `create::`; a `:deactivate` row also in `delete_keys` is deleted instead of same-dayed) and stamps `affiliations_reconciled_at`. Job affiliations are never touched.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** for an org a registrant linked — **hand-entered rows included, not just app-created ones** — classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. Deactivation is gated to post-event (owned rows on their source training's `ended?`, hand-entered on this event's `ended?`), so a pre-event run never deactivates. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#planned_changes(included_keys:, delete_keys:)` returns the concrete `Change`s for the confirmation screen; `#apply(included_keys:, delete_keys:)` performs the kept rows (keys are `aff:` or `create::`; a `delete_keys` entry deletes that affiliation instead of same-daying it) and stamps `affiliations_reconciled_at`. Job affiliations are never touched. The controller is a two-step flow: `index` (edit) → `confirm` (preview, no writes) → `create` (perform).
### Sectors
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
index 85296e7de..8c5f8d504 100644
--- a/app/controllers/events/reconcile_affiliations_controller.rb
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -17,9 +17,24 @@ def index
@person_groups = reconcile.actionable_person_groups
@skipped_sections = reconcile.skipped_reason_sections
@has_rows = reconcile.any_rows?
+ # Restore the admin's selections when they come back from the confirm screen.
+ @pre_included = params[:included]
+ @pre_delete = Array(params[:delete]).to_set
@event = @event.decorate
end
+ # Step 2: show exactly what "Perform changes" will do (no writes yet).
+ def confirm
+ authorize! @event, to: :reconcile_affiliations?
+
+ @included = Array(params[:included])
+ @delete = Array(params[:delete])
+ @changes = AffiliationServices::ReconcileEvent.new(@event).planned_changes(included_keys: @included, delete_keys: @delete)
+ @event = @event.decorate
+
+ redirect_to reconcile_affiliations_event_path(@event), notice: "Nothing selected to change." and return if @changes.empty?
+ end
+
def create
authorize! @event, to: :reconcile_affiliations?
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 9bab55a53..2308b4718 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -58,6 +58,23 @@ def any_rows?
all_rows.any?
end
+ Change = Struct.new(:person, :organization, :affiliation, :action, keyword_init: true)
+
+ # The concrete changes the given selection will make, for the confirmation
+ # screen: a `:delete` key wins over its include (delete instead of same-day).
+ def planned_changes(included_keys:, delete_keys: [])
+ included = Array(included_keys).to_set
+ deletes = Array(delete_keys).to_set
+
+ all_rows.select(&:actionable?).filter_map do |row|
+ if deletes.include?(row.key) && row.affiliation
+ Change.new(person: row.person, organization: row.organization, affiliation: row.affiliation, action: :delete)
+ elsif included.include?(row.key)
+ Change.new(person: row.person, organization: row.organization, affiliation: row.affiliation, action: row.action)
+ end
+ end
+ end
+
# Apply the actionable rows whose keys are in `included_keys`. For :deactivate
# rows whose key is also in `delete_keys`, delete the affiliation instead of
# same-daying it. Stamps the event and returns the number of rows changed.
diff --git a/app/views/events/reconcile_affiliations/confirm.html.erb b/app/views/events/reconcile_affiliations/confirm.html.erb
new file mode 100644
index 000000000..de5d63cb3
--- /dev/null
+++ b/app/views/events/reconcile_affiliations/confirm.html.erb
@@ -0,0 +1,52 @@
+<% content_for(:page_title, "Confirm affiliation changes — #{@event.title}") %>
+<% content_for(:page_bg_class, "admin-or-owner bg-blue-100") %>
+
+
+ <%= link_to "← Go back to edit", reconcile_affiliations_event_path(@event, included: @included, delete: @delete), class: "text-sm text-gray-500 hover:text-gray-700" %>
+
+
+
Confirm affiliation changes
+
+ Performing will make the <%= @changes.size %> <%= "change".pluralize(@changes.size) %> below. Nothing else is affected.
+
+
+ <% sections = {
+ create: [ "Create", "bg-blue-50 text-blue-800 border-blue-200", "A new facilitator affiliation is created for this organization." ],
+ reactivate: [ "Reactivate", "bg-green-50 text-green-800 border-green-200", "The end date is cleared so the facilitator affiliation is active again." ],
+ deactivate: [ "Deactivate", "bg-red-50 text-red-800 border-red-200", "The facilitator affiliation is same-dayed (ended today) so it no longer counts as active. Reversible." ],
+ delete: [ "Delete", "bg-red-100 text-red-900 border-red-300", "The facilitator affiliation is permanently deleted. Job and other-org affiliations are untouched." ]
+ } %>
+
+
+ <% sections.each do |action, (label, header_class, description)| %>
+ <% action_changes = @changes.select { |change| change.action == action } %>
+ <% next if action_changes.empty? %>
+
+
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index f13b7a438..c8c94613a 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -85,6 +85,19 @@ def registrant_with_affiliation(status:)
end
end
+ describe "toggling attendance from the reconcile page" do
+ it "stays on the reconcile page with a flash instead of leaving for the roster" do
+ person, _affiliation = registrant_with_affiliation(status: "no_show")
+ registration = person.event_registrations.first
+
+ patch event_registration_path(registration, return_to: "reconcile_affiliations"),
+ params: { event_registration: { status: "attended" } }
+
+ expect(response).to redirect_to(reconcile_affiliations_event_path(event))
+ expect(flash[:notice]).to be_present
+ end
+ end
+
describe "POST confirm (preview changes)" do
it "shows the selected change without writing" do
_person, affiliation = registrant_with_affiliation(status: "no_show")
From 1c342408a1015516b023cde437b3c4e1dcb5f344 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:29:27 -0400
Subject: [PATCH 15/37] Register exclusive-checkboxes controller; clearer
deactivate instruction
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The Stimulus manifest is explicit, so the new exclusive-checkboxes controller was
never loaded — register it and use an explicit change event so Delete instead and
Will be deactivated actually clear each other. Reword the deactivate row note to
spell out the two options (mark Attended = permanent, uncheck = one-time).
Co-Authored-By: Claude Opus 4.8 (1M context)
---
app/frontend/javascript/controllers/index.js | 3 +++
app/views/events/reconcile_affiliations/index.html.erb | 6 +++---
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/app/frontend/javascript/controllers/index.js b/app/frontend/javascript/controllers/index.js
index 64be221b9..9cc001dc6 100644
--- a/app/frontend/javascript/controllers/index.js
+++ b/app/frontend/javascript/controllers/index.js
@@ -84,6 +84,9 @@ application.register("dropdown", DropdownController)
import ExpandAllController from "./expand_all_controller"
application.register("expand-all", ExpandAllController)
+import ExclusiveCheckboxesController from "./exclusive_checkboxes_controller"
+application.register("exclusive-checkboxes", ExclusiveCheckboxesController)
+
import FilePreviewController from "./file_preview_controller"
application.register("file-preview", FilePreviewController)
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 0f5826f2d..727ed2485 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -62,7 +62,7 @@
<% action_notes = {
create: "Uncheck to skip creating this facilitator affiliation.",
reactivate: "Uncheck to leave this facilitator affiliation inactive.",
- deactivate: "Change attendance to Attended, or uncheck, to keep this facilitator affiliation active.",
+ deactivate: "To keep this facilitator affiliation active: mark them Attended (permanent), or just uncheck this box (one-time).",
delete: "Uncheck to keep this facilitator affiliation."
} %>
<% button_base = "group relative inline-flex items-center gap-2 rounded-md border border-gray-300 bg-white px-3 py-1.5 text-xs font-medium text-gray-500 shadow-sm cursor-pointer hover:bg-gray-50" %>
@@ -100,13 +100,13 @@
data-controller="exclusive-checkboxes"<% end %>>
<% if row.action == :deactivate %>
- <%= check_box_tag "delete[]", row.key, delete_checked, id: "delete_#{row.key}", class: "h-4 w-4 rounded border-gray-300 text-red-600", data: { exclusive_checkboxes_target: "box", action: "exclusive-checkboxes#select" } %>
+ <%= check_box_tag "delete[]", row.key, delete_checked, id: "delete_#{row.key}", class: "h-4 w-4 rounded border-gray-300 text-red-600", data: { exclusive_checkboxes_target: "box", action: "change->exclusive-checkboxes#select" } %>
Delete instead
<%= render "tooltip", kind: :delete %>
<% end %>
- <%= check_box_tag "included[]", row.key, included_checked, id: "included_#{row.key}", class: "h-4 w-4 rounded border-gray-300 text-blue-600", data: (row.action == :deactivate ? { exclusive_checkboxes_target: "box", action: "exclusive-checkboxes#select" } : {}) %>
+ <%= check_box_tag "included[]", row.key, included_checked, id: "included_#{row.key}", class: "h-4 w-4 rounded border-gray-300 text-blue-600", data: (row.action == :deactivate ? { exclusive_checkboxes_target: "box", action: "change->exclusive-checkboxes#select" } : {}) %>
<%= heading %>
<%= render "tooltip", kind: row.action %>
From bdedbf725afb7e487c2f3eccba317790b3bb8393 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:31:45 -0400
Subject: [PATCH 16/37] Render deactivate row note as two lines
---
.../events/reconcile_affiliations/index.html.erb | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 727ed2485..f8de48972 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -60,10 +60,10 @@
delete: "has-[:checked]:bg-red-50 has-[:checked]:text-red-700 has-[:checked]:border-red-300"
} %>
<% action_notes = {
- create: "Uncheck to skip creating this facilitator affiliation.",
- reactivate: "Uncheck to leave this facilitator affiliation inactive.",
- deactivate: "To keep this facilitator affiliation active: mark them Attended (permanent), or just uncheck this box (one-time).",
- delete: "Uncheck to keep this facilitator affiliation."
+ create: [ "Uncheck to skip creating this facilitator affiliation." ],
+ reactivate: [ "Uncheck to leave this facilitator affiliation inactive." ],
+ deactivate: [ "Mark Attended to keep active (permanent),", "or uncheck this box to keep active (one-time)." ],
+ delete: [ "Uncheck to keep this facilitator affiliation." ]
} %>
<% button_base = "group relative inline-flex items-center gap-2 rounded-md border border-gray-300 bg-white px-3 py-1.5 text-xs font-medium text-gray-500 shadow-sm cursor-pointer hover:bg-gray-50" %>
@@ -111,7 +111,11 @@
<%= render "tooltip", kind: row.action %>
-
<%= action_notes[row.action] %>
+
+ <% action_notes[row.action].each do |line| %>
+
<%= line %>
+ <% end %>
+
<% end %>
From ff530037784d015ec3356747072880d0f4625e73 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:33:01 -0400
Subject: [PATCH 17/37] Reword deactivate note to 'To keep Affiliation active:
Mark as Attended or uncheck this box'
---
app/views/events/reconcile_affiliations/index.html.erb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index f8de48972..a4aadc961 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -62,7 +62,7 @@
<% action_notes = {
create: [ "Uncheck to skip creating this facilitator affiliation." ],
reactivate: [ "Uncheck to leave this facilitator affiliation inactive." ],
- deactivate: [ "Mark Attended to keep active (permanent),", "or uncheck this box to keep active (one-time)." ],
+ deactivate: [ "To keep Affiliation active:", "Mark as Attended or uncheck this box" ],
delete: [ "Uncheck to keep this facilitator affiliation." ]
} %>
<% button_base = "group relative inline-flex items-center gap-2 rounded-md border border-gray-300 bg-white px-3 py-1.5 text-xs font-medium text-gray-500 shadow-sm cursor-pointer hover:bg-gray-50" %>
From 55567893f7036f1db530c52c7140e75adc66c619 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:34:54 -0400
Subject: [PATCH 18/37] Fix Preview changes: turbo:false so the confirm page
renders on POST; note says 'both boxes'
Turbo ignores a 200 HTML render on a form POST (only 4xx/5xx render), so the
confirmation screen never showed. Submit the preview form with turbo disabled.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
app/views/events/reconcile_affiliations/index.html.erb | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index a4aadc961..36b230363 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -50,7 +50,8 @@
Checked boxes change facilitator affiliations
- <%= form_with url: reconcile_affiliations_event_path(@event), method: :post do %>
+ <%# turbo: false so the POST renders the confirmation page (Turbo ignores a 200 HTML render on POST). %>
+ <%= form_with url: reconcile_affiliations_event_path(@event), method: :post, data: { turbo: false } do %>
<% @person_groups.each do |group| %>
<% checked_class = {
@@ -62,7 +63,7 @@
<% action_notes = {
create: [ "Uncheck to skip creating this facilitator affiliation." ],
reactivate: [ "Uncheck to leave this facilitator affiliation inactive." ],
- deactivate: [ "To keep Affiliation active:", "Mark as Attended or uncheck this box" ],
+ deactivate: [ "To keep Affiliation active:", "Mark as Attended or uncheck both boxes" ],
delete: [ "Uncheck to keep this facilitator affiliation." ]
} %>
<% button_base = "group relative inline-flex items-center gap-2 rounded-md border border-gray-300 bg-white px-3 py-1.5 text-xs font-medium text-gray-500 shadow-sm cursor-pointer hover:bg-gray-50" %>
From e727595337f70ecab54babb96f898e68cebdfd4e Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 09:53:05 -0400
Subject: [PATCH 19/37] Radio outcomes with Keep-active; fix nested-form bug;
scroll to item on attendance toggle
Replace the deactivate/delete checkboxes with a radio group per row
(Deactivate/Delete/Keep active, and action/keep for the others), styled as the same
buttons via has-[:checked]. Radios are natively mutually exclusive, so remove the
exclusive-checkboxes Stimulus controller and the per-row instruction note.
Fix the real reason 'Preview changes' did nothing: the attendance chip's form was
nested inside the reconcile form (invalid HTML), so the submit/inputs fell outside
it. Render the reconcile form standalone and join the radios/submit via the HTML
form= attribute. Switch the params to an outcome map { row.key => choice }.
Toggling attendance now scrolls back to that item's anchor, not the top.
Co-Authored-By: Claude Opus 4.8 (1M context)
---
AGENTS.md | 4 +-
.../event_registrations_controller.rb | 2 +-
.../reconcile_affiliations_controller.rb | 22 +--
.../exclusive_checkboxes_controller.js | 17 --
app/frontend/javascript/controllers/index.js | 3 -
.../affiliation_services/reconcile_event.rb | 57 +++----
.../_attendance_status_badge.html.erb | 2 +-
.../reconcile_affiliations/_tooltip.html.erb | 2 +
.../reconcile_affiliations/confirm.html.erb | 7 +-
.../reconcile_affiliations/index.html.erb | 147 ++++++++----------
.../events/reconcile_affiliations_spec.rb | 31 ++--
11 files changed, 127 insertions(+), 167 deletions(-)
delete mode 100644 app/frontend/javascript/controllers/exclusive_checkboxes_controller.js
diff --git a/AGENTS.md b/AGENTS.md
index c47619465..133491718 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -71,7 +71,7 @@ This codebase (Rails 8.1)
| Directory | Purpose |
|---|---|
| `app/frontend/entrypoints/` | Vite entry points (application.js, application.css) |
-| `app/frontend/javascript/controllers/` | Stimulus controllers (78) |
+| `app/frontend/javascript/controllers/` | Stimulus controllers (77) |
| `app/frontend/javascript/rhino/` | Rich text editor customizations (mentions, grid) |
| `app/frontend/stylesheets/` | Tailwind CSS and component styles |
@@ -256,7 +256,7 @@ action, or `authorize! :workshop, to: :summary?`).
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** for an org a registrant linked — **hand-entered rows included, not just app-created ones** — classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. Deactivation is gated to post-event (owned rows on their source training's `ended?`, hand-entered on this event's `ended?`), so a pre-event run never deactivates. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#planned_changes(included_keys:, delete_keys:)` returns the concrete `Change`s for the confirmation screen; `#apply(included_keys:, delete_keys:)` performs the kept rows (keys are `aff:` or `create::`; a `delete_keys` entry deletes that affiliation instead of same-daying it) and stamps `affiliations_reconciled_at`. Job affiliations are never touched. The controller is a two-step flow: `index` (edit) → `confirm` (preview, no writes) → `create` (perform).
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** for an org a registrant linked — **hand-entered rows included, not just app-created ones** — classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. Deactivation is gated to post-event (owned rows on their source training's `ended?`, hand-entered on this event's `ended?`), so a pre-event run never deactivates. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#planned_changes(outcome:)` and `#apply(outcome:)` take an `outcome` map `{ row.key => choice }` (choice is the action or "keep") — the confirm screen previews planned `Change`s, apply performs them and stamps `affiliations_reconciled_at`. Job affiliations are never touched. The controller is a two-step flow: `index` (edit) → `confirm` (preview, no writes) → `create` (perform).
### Sectors
diff --git a/app/controllers/event_registrations_controller.rb b/app/controllers/event_registrations_controller.rb
index 80d807ae8..6422552cf 100644
--- a/app/controllers/event_registrations_controller.rb
+++ b/app/controllers/event_registrations_controller.rb
@@ -121,7 +121,7 @@ def update
when "onboarding" then redirect_to helpers.onboarding_event_row_path(@event_registration.event, @event_registration.id), notice: notice, status: :see_other
when "attendees" then redirect_to attendees_events_path, notice: notice, status: :see_other
when "roster" then redirect_to roster_event_path(@event_registration.event), notice: notice, status: :see_other
- when "reconcile_affiliations" then redirect_to reconcile_affiliations_event_path(@event_registration.event), notice: notice, status: :see_other
+ when "reconcile_affiliations" then redirect_to reconcile_affiliations_event_path(@event_registration.event, anchor: helpers.dom_id(@event_registration, :attendance_status)), notice: notice, status: :see_other
# Two ways back to the recipients page: the shout-outs section (the
# feature-a-shout-out flow) or the recipient's own card (their name).
when "recipients" then redirect_to recipients_event_path(@event_registration.event, anchor: "shout-outs"), notice: notice, status: :see_other
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
index 8c5f8d504..d434e1188 100644
--- a/app/controllers/events/reconcile_affiliations_controller.rb
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -17,9 +17,8 @@ def index
@person_groups = reconcile.actionable_person_groups
@skipped_sections = reconcile.skipped_reason_sections
@has_rows = reconcile.any_rows?
- # Restore the admin's selections when they come back from the confirm screen.
- @pre_included = params[:included]
- @pre_delete = Array(params[:delete]).to_set
+ # Restore the admin's per-row radio choices when they come back from confirm.
+ @pre_outcome = params[:outcome]
@event = @event.decorate
end
@@ -27,9 +26,8 @@ def index
def confirm
authorize! @event, to: :reconcile_affiliations?
- @included = Array(params[:included])
- @delete = Array(params[:delete])
- @changes = AffiliationServices::ReconcileEvent.new(@event).planned_changes(included_keys: @included, delete_keys: @delete)
+ @outcome = outcome_params
+ @changes = AffiliationServices::ReconcileEvent.new(@event).planned_changes(outcome: @outcome)
@event = @event.decorate
redirect_to reconcile_affiliations_event_path(@event), notice: "Nothing selected to change." and return if @changes.empty?
@@ -38,10 +36,7 @@ def confirm
def create
authorize! @event, to: :reconcile_affiliations?
- changed = AffiliationServices::ReconcileEvent.new(@event).apply(
- included_keys: params[:included] || [],
- delete_keys: params[:delete] || []
- )
+ changed = AffiliationServices::ReconcileEvent.new(@event).apply(outcome: outcome_params)
redirect_to registrants_event_path(@event), notice: reconcile_notice(changed)
end
@@ -51,6 +46,13 @@ def set_event
@event = Event.find(params[:id])
end
+ # `outcome` is a { row.key => choice } map with dynamic keys; the service only
+ # acts on known choices, so the actual values are validated downstream.
+ def outcome_params
+ outcome = params[:outcome]
+ outcome.respond_to?(:permit!) ? outcome.permit!.to_h : {}
+ end
+
def reconcile_notice(changed)
return "No affiliations needed reconciling." if changed.zero?
diff --git a/app/frontend/javascript/controllers/exclusive_checkboxes_controller.js b/app/frontend/javascript/controllers/exclusive_checkboxes_controller.js
deleted file mode 100644
index cd165d589..000000000
--- a/app/frontend/javascript/controllers/exclusive_checkboxes_controller.js
+++ /dev/null
@@ -1,17 +0,0 @@
-import { Controller } from "@hotwired/stimulus"
-
-// Connects to data-controller="exclusive-checkboxes"
-// Makes a small group of checkboxes mutually exclusive — like radios, but any can
-// be left unchecked. Checking one clears the others in the group (e.g. "Delete
-// instead" and "Will be deactivated" are two choices for the same row).
-export default class extends Controller {
- static targets = ["box"]
-
- select(event) {
- if (!event.target.checked) return
-
- this.boxTargets.forEach((box) => {
- if (box !== event.target) box.checked = false
- })
- }
-}
diff --git a/app/frontend/javascript/controllers/index.js b/app/frontend/javascript/controllers/index.js
index 9cc001dc6..64be221b9 100644
--- a/app/frontend/javascript/controllers/index.js
+++ b/app/frontend/javascript/controllers/index.js
@@ -84,9 +84,6 @@ application.register("dropdown", DropdownController)
import ExpandAllController from "./expand_all_controller"
application.register("expand-all", ExpandAllController)
-import ExclusiveCheckboxesController from "./exclusive_checkboxes_controller"
-application.register("exclusive-checkboxes", ExclusiveCheckboxesController)
-
import FilePreviewController from "./file_preview_controller"
application.register("file-preview", FilePreviewController)
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index 2308b4718..dffa15930 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -60,39 +60,30 @@ def any_rows?
Change = Struct.new(:person, :organization, :affiliation, :action, keyword_init: true)
- # The concrete changes the given selection will make, for the confirmation
- # screen: a `:delete` key wins over its include (delete instead of same-day).
- def planned_changes(included_keys:, delete_keys: [])
- included = Array(included_keys).to_set
- deletes = Array(delete_keys).to_set
+ # Each row's outcome is one radio choice keyed by row.key: the action itself
+ # (deactivate/delete/reactivate/create) or "keep" (do nothing).
+ ACTION_FOR_CHOICE = { "deactivate" => :deactivate, "delete" => :delete, "reactivate" => :reactivate, "create" => :create }.freeze
+
+ # The concrete changes the given `outcome` map will make, for the confirmation
+ # screen. `outcome` is `{ row.key => choice }`.
+ def planned_changes(outcome:)
+ outcome = outcome.to_h
all_rows.select(&:actionable?).filter_map do |row|
- if deletes.include?(row.key) && row.affiliation
- Change.new(person: row.person, organization: row.organization, affiliation: row.affiliation, action: :delete)
- elsif included.include?(row.key)
- Change.new(person: row.person, organization: row.organization, affiliation: row.affiliation, action: row.action)
- end
+ action = ACTION_FOR_CHOICE[outcome[row.key]]
+ next unless action
+
+ Change.new(person: row.person, organization: row.organization, affiliation: row.affiliation, action:)
end
end
- # Apply the actionable rows whose keys are in `included_keys`. For :deactivate
- # rows whose key is also in `delete_keys`, delete the affiliation instead of
- # same-daying it. Stamps the event and returns the number of rows changed.
- def apply(included_keys:, delete_keys: [])
- included = Array(included_keys).to_set
- deletes = Array(delete_keys).to_set
+ # Apply each row's chosen outcome, stamp the event, and return the number of
+ # rows actually changed ("keep"/unknown choices are no-ops).
+ def apply(outcome:)
+ outcome = outcome.to_h
changed = all_rows.count do |row|
- next false unless row.actionable?
-
- if deletes.include?(row.key) && row.affiliation
- row.affiliation.destroy!
- true
- elsif included.include?(row.key)
- perform(row)
- else
- false
- end
+ row.actionable? && perform_outcome(row, outcome[row.key])
end
@event.update!(affiliations_reconciled_at: Time.current)
@@ -165,21 +156,23 @@ def create_row(person, registration, organization, attended)
end
end
- def perform(row)
- case row.action
- when :create
+ def perform_outcome(row, choice)
+ case choice
+ when "create"
AffiliationServices::CreateFromRegistration.call(
person: row.person, organization: row.organization, facilitator_training: true,
training_date: @event.start_date, event_registration: row.registration
)
- when :delete
+ when "delete"
row.affiliation.destroy!
- when :deactivate
+ when "deactivate"
# Same-day it: end_date = the affiliation's own start_date (start_date itself
# is never changed), which the model turns into inactive.
row.affiliation.update!(end_date: row.affiliation.start_date || Date.current)
- when :reactivate
+ when "reactivate"
row.affiliation.update!(end_date: nil)
+ else
+ return false # "keep" or unknown
end
true
end
diff --git a/app/views/event_registrations/_attendance_status_badge.html.erb b/app/views/event_registrations/_attendance_status_badge.html.erb
index 5b8e5b931..a9f40128e 100644
--- a/app/views/event_registrations/_attendance_status_badge.html.erb
+++ b/app/views/event_registrations/_attendance_status_badge.html.erb
@@ -1,6 +1,6 @@
<% deco = registration.decorate %>
<% badge_return_to = local_assigns.fetch(:return_to, nil) %>
-
diff --git a/app/views/events/reconcile_affiliations/_tooltip.html.erb b/app/views/events/reconcile_affiliations/_tooltip.html.erb
index 582555db3..a41b75283 100644
--- a/app/views/events/reconcile_affiliations/_tooltip.html.erb
+++ b/app/views/events/reconcile_affiliations/_tooltip.html.erb
@@ -13,5 +13,7 @@
Creates the facilitator affiliation for this organization.
<% when :reactivate %>
Clears the end date so this facilitator affiliation counts as active again.
+ <% when :keep %>
+ Leaves this facilitator affiliation exactly as it is — no change.
<% end %>
diff --git a/app/views/events/reconcile_affiliations/confirm.html.erb b/app/views/events/reconcile_affiliations/confirm.html.erb
index de5d63cb3..885b8287d 100644
--- a/app/views/events/reconcile_affiliations/confirm.html.erb
+++ b/app/views/events/reconcile_affiliations/confirm.html.erb
@@ -2,7 +2,7 @@
<% content_for(:page_bg_class, "admin-or-owner bg-blue-100") %>
- <%= link_to "← Go back to edit", reconcile_affiliations_event_path(@event, included: @included, delete: @delete), class: "text-sm text-gray-500 hover:text-gray-700" %>
+ <%= link_to "← Go back to edit", reconcile_affiliations_event_path(@event, outcome: @outcome), class: "text-sm text-gray-500 hover:text-gray-700" %>
Confirm affiliation changes
@@ -42,10 +42,9 @@
- <%= link_to "← Go back to edit", reconcile_affiliations_event_path(@event, included: @included, delete: @delete), class: "text-sm text-gray-500 hover:text-gray-700" %>
+ <%= link_to "← Go back to edit", reconcile_affiliations_event_path(@event, outcome: @outcome), class: "text-sm text-gray-500 hover:text-gray-700" %>
<%= form_with url: perform_reconcile_affiliations_event_path(@event), method: :post do %>
- <% @included.each do |key| %><%= hidden_field_tag "included[]", key %><% end %>
- <% @delete.each do |key| %><%= hidden_field_tag "delete[]", key %><% end %>
+ <% @outcome.each do |key, value| %><%= hidden_field_tag "outcome[#{key}]", value %><% end %>
<%= submit_tag "Perform changes", class: "btn btn-primary" %>
<% end %>
- <%# turbo: false so the POST renders the confirmation page (Turbo ignores a 200 HTML render on POST). %>
- <%= form_with url: reconcile_affiliations_event_path(@event), method: :post, data: { turbo: false } do %>
-
- <%= render "event_registrations/attendance_status_badge", registration: group[:registration], return_to: "reconcile_affiliations" %>
+
+ <%# Standalone form (turbo:false so the POST renders the confirm page). The cards
+ live OUTSIDE it — the attendance chip renders its own form and nesting forms is
+ invalid — so the radios and submit join this form via the HTML form= attribute. %>
+ <%= form_with url: reconcile_affiliations_event_path(@event), method: :post, id: "reconcile_form", data: { turbo: false } do %><% end %>
+
+
<% end %>
<% if @skipped_sections.any? %>
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index c8c94613a..0a0b2aa3c 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -93,7 +93,7 @@ def registrant_with_affiliation(status:)
patch event_registration_path(registration, return_to: "reconcile_affiliations"),
params: { event_registration: { status: "attended" } }
- expect(response).to redirect_to(reconcile_affiliations_event_path(event))
+ expect(response).to redirect_to(reconcile_affiliations_event_path(event, anchor: "attendance_status_event_registration_#{registration.id}"))
expect(flash[:notice]).to be_present
end
end
@@ -102,7 +102,7 @@ def registrant_with_affiliation(status:)
it "shows the selected change without writing" do
_person, affiliation = registrant_with_affiliation(status: "no_show")
- post reconcile_affiliations_event_path(event), params: { included: [ "aff:#{affiliation.id}" ] }
+ post reconcile_affiliations_event_path(event), params: { outcome: { "aff:#{affiliation.id}" => "deactivate" } }
expect(response).to have_http_status(:ok)
expect(response.body).to include("Confirm affiliation changes")
@@ -111,60 +111,59 @@ def registrant_with_affiliation(status:)
end
it "redirects back when nothing is selected" do
- registrant_with_affiliation(status: "no_show")
+ _person, affiliation = registrant_with_affiliation(status: "no_show")
- post reconcile_affiliations_event_path(event), params: { included: [] }
+ post reconcile_affiliations_event_path(event), params: { outcome: { "aff:#{affiliation.id}" => "keep" } }
expect(response).to redirect_to(reconcile_affiliations_event_path(event))
end
end
describe "POST perform" do
- it "deactivates the included non-completer and stamps the event" do
+ it "deactivates the chosen non-completer and stamps the event" do
_person, affiliation = registrant_with_affiliation(status: "no_show")
- post perform_reconcile_affiliations_event_path(event), params: { included: [ "aff:#{affiliation.id}" ] }
+ post perform_reconcile_affiliations_event_path(event), params: { outcome: { "aff:#{affiliation.id}" => "deactivate" } }
expect(response).to redirect_to(registrants_event_path(event))
expect(affiliation.reload).not_to be_active
expect(event.reload.affiliations_reconciled_at).to be_present
end
- it "spares an opted-out row" do
+ it "spares a row set to keep" do
_person, affiliation = registrant_with_affiliation(status: "no_show")
- post perform_reconcile_affiliations_event_path(event), params: { included: [] }
+ post perform_reconcile_affiliations_event_path(event), params: { outcome: { "aff:#{affiliation.id}" => "keep" } }
expect(affiliation.reload).to be_active
end
- it "creates a missing affiliation before the event when included" do
+ it "creates a missing affiliation before the event when chosen" do
upcoming = create(:event, facilitator_training: true, start_date: 3.days.from_now, end_date: 5.days.from_now)
person = create(:person)
reg = create(:event_registration, event: upcoming, registrant: person, status: "registered")
create(:event_registration_organization, event_registration: reg, organization: organization)
expect {
- post perform_reconcile_affiliations_event_path(upcoming), params: { included: [ "create:#{person.id}:#{organization.id}" ] }
+ post perform_reconcile_affiliations_event_path(upcoming), params: { outcome: { "create:#{person.id}:#{organization.id}" => "create" } }
}.to change { person.affiliations.facilitators.where(organization: organization).count }.by(1)
end
- it "deletes instead of same-daying when the delete option is checked" do
+ it "deletes when the delete outcome is chosen" do
_person, affiliation = registrant_with_affiliation(status: "no_show")
- key = "aff:#{affiliation.id}"
- post perform_reconcile_affiliations_event_path(event), params: { included: [ key ], delete: [ key ] }
+ post perform_reconcile_affiliations_event_path(event), params: { outcome: { "aff:#{affiliation.id}" => "delete" } }
expect(Affiliation.exists?(affiliation.id)).to be(false)
end
- it "deactivates a hand-entered facilitator affiliation when included" do
+ it "deactivates a hand-entered facilitator affiliation when chosen" do
person = create(:person)
reg = create(:event_registration, event: event, registrant: person, status: "no_show")
create(:event_registration_organization, event_registration: reg, organization: organization)
hand_entered = create(:affiliation, person: person, organization: organization, title: "Facilitator", start_date: 1.year.ago.to_date)
- post perform_reconcile_affiliations_event_path(event), params: { included: [ "aff:#{hand_entered.id}" ] }
+ post perform_reconcile_affiliations_event_path(event), params: { outcome: { "aff:#{hand_entered.id}" => "deactivate" } }
expect(hand_entered.reload).not_to be_active
end
@@ -179,7 +178,7 @@ def registrant_with_affiliation(status:)
job = create(:affiliation, person: person, organization: organization, title: "Counselor",
event_registration: reg)
- post perform_reconcile_affiliations_event_path(non_training), params: { included: [ "aff:#{facilitator.id}" ] }
+ post perform_reconcile_affiliations_event_path(non_training), params: { outcome: { "aff:#{facilitator.id}" => "delete" } }
expect(Affiliation.exists?(facilitator.id)).to be(false)
expect(Affiliation.exists?(job.id)).to be(true)
From 3781f7bec70248154e2152a623b94663f5f9be7e Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 10:09:20 -0400
Subject: [PATCH 20/37] Fix Brakeman: read outcome params as a plain hash
instead of permit!
Co-Authored-By: Claude Opus 4.8 (1M context)
---
.../events/reconcile_affiliations_controller.rb | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
index d434e1188..997ff3c81 100644
--- a/app/controllers/events/reconcile_affiliations_controller.rb
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -46,11 +46,13 @@ def set_event
@event = Event.find(params[:id])
end
- # `outcome` is a { row.key => choice } map with dynamic keys; the service only
- # acts on known choices, so the actual values are validated downstream.
+ # `outcome` is a { row.key => choice } map with dynamic keys, read as a plain
+ # string hash (never mass-assigned); the service only acts on known choices.
def outcome_params
- outcome = params[:outcome]
- outcome.respond_to?(:permit!) ? outcome.permit!.to_h : {}
+ raw = params[:outcome]
+ return {} unless raw.respond_to?(:each_pair)
+
+ raw.each_pair.map { |key, value| [ key.to_s, value.to_s ] }.to_h
end
def reconcile_notice(changed)
From 619953b4b157c80ebd7c70cfc27a873b2e21dc95 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 13:36:54 -0400
Subject: [PATCH 21/37] Reorder deactivate outcomes: Keep active, Delete,
Deactivate; clearer labels
Co-Authored-By: Claude Opus 4.8 (1M context)
---
app/views/events/reconcile_affiliations/index.html.erb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 490ddda64..34af58341 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -48,7 +48,7 @@
<% outcome_options = {
create: [ [ "create", "Will be created", :blue ], [ "keep", "Skip", :gray ] ],
reactivate: [ [ "reactivate", "Will be reactivated", :green ], [ "keep", "Leave inactive", :gray ] ],
- deactivate: [ [ "deactivate", "Will be deactivated", :red ], [ "delete", "Delete instead", :red ], [ "keep", "Keep active", :green ] ],
+ deactivate: [ [ "keep", "Keep active", :green ], [ "delete", "Delete affiliation", :red ], [ "deactivate", "Deactivate affiliation", :red ] ],
delete: [ [ "delete", "Will be deleted", :red ], [ "keep", "Keep", :gray ] ]
} %>
<% button_base = "group relative inline-flex items-center gap-2 rounded-md border border-gray-300 bg-white px-3 py-1.5 text-xs font-medium text-gray-500 shadow-sm cursor-pointer hover:bg-gray-50" %>
From 3cf438c630b51937db0ec84842de5de06ff6feba Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Fri, 14 Aug 2026 19:04:06 -0400
Subject: [PATCH 22/37] Update reconcile specs for renamed 'Deactivate
affiliation' label
Co-Authored-By: Claude Opus 4.8 (1M context)
---
spec/requests/events/reconcile_affiliations_spec.rb | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index 0a0b2aa3c..2b4d53286 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -27,7 +27,7 @@ def registrant_with_affiliation(status:)
expect(response).to have_http_status(:ok)
expect(response.body).to include(person.name)
- expect(response.body).to include("Will be deactivated")
+ expect(response.body).to include("Deactivate affiliation")
end
it "previews a missing affiliation as a creation before the event" do
@@ -73,7 +73,7 @@ def registrant_with_affiliation(status:)
get reconcile_affiliations_event_path(event)
- expect(response.body).to include("Will be deactivated")
+ expect(response.body).to include("Deactivate affiliation")
end
it "denies a non-admin" do
From 0e2313c0c1d3504d381d6a29f188c9e745108d1f Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Tue, 18 Aug 2026 08:09:39 -0400
Subject: [PATCH 23/37] Let an explicit inactive flag override the date-derived
one
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Same-daying an affiliation on or after its start date left it active: the
callback always recomputed `inactive` from the dates, and a row ending today
still reads as active. Admins also had no way to set it — the column was
permitted everywhere but had no field, and ticking it alongside a date edit was
silently overwritten.
Co-Authored-By: Claude Opus 5 (1M context)
---
app/models/affiliation.rb | 4 ++++
app/views/organizations/show.html.erb | 2 +-
spec/models/affiliation_spec.rb | 7 +++++++
3 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/app/models/affiliation.rb b/app/models/affiliation.rb
index 94f9fd650..04afb9e25 100644
--- a/app/models/affiliation.rb
+++ b/app/models/affiliation.rb
@@ -163,7 +163,11 @@ def sole_address_id_for_new_organization
addresses.first.id if addresses&.one?
end
+ # Derives `inactive` from the dates, unless this save sets it explicitly — an
+ # admin's tick, or a same-day deactivation whose end_date (today, or the future
+ # start of an upcoming affiliation) the date rule alone would still call active.
def set_inactive_from_dates
+ return if inactive_changed?
return unless end_date_changed? || start_date_changed?
self.inactive = end_date.present? && end_date < Date.current
diff --git a/app/views/organizations/show.html.erb b/app/views/organizations/show.html.erb
index ac5c94225..26a4500ef 100644
--- a/app/views/organizations/show.html.erb
+++ b/app/views/organizations/show.html.erb
@@ -125,7 +125,7 @@
Affiliations
<% active_affiliations = @organization.affiliations
- .select { |a| !a.inactive? && (a.end_date.nil? || a.end_date >= Date.current) && a.person.present? }
+ .select { |a| a.active? && a.person.present? }
.sort_by { |a| [a.person.first_name.to_s.downcase, a.person.last_name.to_s.downcase] } %>
<% grouped = active_affiliations.group_by(&:person) %>
<% if grouped.any? %>
diff --git a/spec/models/affiliation_spec.rb b/spec/models/affiliation_spec.rb
index fa1ff6a1a..5bb50b6da 100644
--- a/spec/models/affiliation_spec.rb
+++ b/spec/models/affiliation_spec.rb
@@ -291,6 +291,13 @@
op.update!(title: "New Title")
expect(op.reload.inactive).to be true
end
+
+ it 'keeps an explicitly set flag the dates would not derive' do
+ op.update!(start_date: Date.current, end_date: Date.current, inactive: true)
+
+ expect(op.reload.inactive).to be true
+ expect(op).not_to be_active
+ end
end
describe "reassigning the organization" do
From 787f7fced8d69bca0dfaa48e117ef141bba817f8 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Tue, 18 Aug 2026 08:09:45 -0400
Subject: [PATCH 24/37] Move the reconcile rules into a per-person classifier
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The classification lived in ReconcileEvent while ReconcileFacilitatorAffiliation
held a second, owned-only copy that nothing but its own spec reached — two
implementations of the same rules, already disagreeing on hand-entered rows.
ReconcilePerson is now the only place a decision is made; ReconcileEvent
iterates it and keeps the keys, grouping and timestamp. Owned-vs-all becomes an
argument, so the per-person reconciler is callable on its own (e.g. from an
attendance change) without re-deriving anything.
Co-Authored-By: Claude Opus 5 (1M context)
---
AGENTS.md | 4 +-
.../affiliation_services/reconcile_event.rb | 121 ++++----------
.../reconcile_facilitator_affiliation.rb | 97 -----------
.../affiliation_services/reconcile_person.rb | 158 ++++++++++++++++++
.../reconcile_affiliations/_tooltip.html.erb | 2 +-
.../reconcile_affiliations/confirm.html.erb | 2 +-
...ation_spec.rb => reconcile_person_spec.rb} | 104 ++++++++++--
7 files changed, 279 insertions(+), 209 deletions(-)
delete mode 100644 app/services/affiliation_services/reconcile_facilitator_affiliation.rb
create mode 100644 app/services/affiliation_services/reconcile_person.rb
rename spec/services/affiliation_services/{reconcile_facilitator_affiliation_spec.rb => reconcile_person_spec.rb} (53%)
diff --git a/AGENTS.md b/AGENTS.md
index 133491718..2188ce926 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -255,8 +255,8 @@ action, or `authorize! :workshop, to: :summary?`).
### Affiliations
- `AffiliationServices::CreateFromRegistration` — On registration / org linking, creates a "job affiliation" with the typed title (when present) plus a standing "Facilitator" affiliation, in one transaction. Skips the facilitator one only when the person already has an active-or-pending affiliation titled exactly "Facilitator" with that org (a current one or one dated to a future training); an ended facilitator affiliation gets a fresh second one. Dedupe is by title + org + dates, so a job title like "Lead Facilitator" still gets its own Facilitator affiliation. Accepts an optional `organization_address:` and sets it on every affiliation it creates (the registrant's typed agency address, upserted onto the org); when an affiliation already exists and is skipped, it backfills that address onto the existing one only if it has none (an admin-set address is never overwritten)
-- `AffiliationServices::ReconcileFacilitatorAffiliation` — Per `(person, organization)`, brings the person's **owned** facilitator affiliation (created by the registration flow, i.e. `event_registration_id` present) in line with attendance: keeps it active when they have any `attended` facilitator-training registration to that org; otherwise **same-days** it (`end_date := start_date`, which the model turns into `inactive: true`) once its source training has ended. Reactivates a previously same-dayed row when the person is later marked attended. Hand-created (unowned) rows are never touched. `#plan` returns the action (`:deactivate` / `:reactivate` / `:noop`) without writing.
-- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Builds one row **per facilitator affiliation** for an org a registrant linked — **hand-entered rows included, not just app-created ones** — classifying each `:create` / `:deactivate` / `:reactivate` (facilitator trainings) or `:delete` (facilitator affiliation auto-created off a non-training event), else `:noop` with a reason. Deactivation is gated to post-event (owned rows on their source training's `ended?`, hand-entered on this event's `ended?`), so a pre-event run never deactivates. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#planned_changes(outcome:)` and `#apply(outcome:)` take an `outcome` map `{ row.key => choice }` (choice is the action or "keep") — the confirm screen previews planned `Change`s, apply performs them and stamps `affiliations_reconciled_at`. Job affiliations are never touched. The controller is a two-step flow: `index` (edit) → `confirm` (preview, no writes) → `create` (perform).
+- `AffiliationServices::ReconcilePerson` — **The single classifier** for facilitator affiliations, per `(person, organization)` in the context of one `event:`. `#plan` returns a `Decision` (`affiliation`, `action`, `reason`) per affiliation in scope, plus a create/no-create decision when the person has none — no writes. Actions: `:create` (pre-event for anyone, post-event only for attendees), `:deactivate` (**same-days** it — `end_date := start_date` plus an explicit `inactive: true`, since the model's date rule alone still reads a row ending today or later as active), `:reactivate`, `:delete` (non-training event: a row auto-created off it), or `:noop` with a reason. Completion is "any `attended` facilitator-training registration to that org", so no-showing one training but attending another keeps them active; deactivation waits for the governing training to end, so a pre-event run never deactivates. `#perform(action, affiliation:)` applies one decision, `#call` applies them all. `include_unowned:` is the auto-vs-manual gate — false (default) touches only rows the registration flow minted, true reconciles hand-entered rows too.
+- `AffiliationServices::ReconcileEvent` — Event-level orchestration for the "Reconcile affiliations" bulk action. Walks the event's registrants and their linked orgs, iterating `ReconcilePerson` (with `include_unowned: true`, one memoized instance per person+org) and turning its decisions into individually-selectable rows — every rule lives in `ReconcilePerson`, every key/grouping/timestamp concern here. `#actionable_person_groups` groups actionable rows by person (with attendance registration and other-org facilitator affiliations for context); `#skipped_reason_sections` groups no-action rows by reason. `#planned_changes(outcome:)` and `#apply(outcome:)` take an `outcome` map `{ row.key => choice }` (choice is the action or "keep") — the confirm screen previews planned `Change`s, apply performs them and stamps `affiliations_reconciled_at`. Job affiliations are never touched. The controller is a two-step flow: `index` (edit) → `confirm` (preview, no writes) → `create` (perform).
### Sectors
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index dffa15930..b95187408 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -1,18 +1,9 @@
module AffiliationServices
# Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
- # the event's registrants and, for each facilitator affiliation tied to an org
- # they linked, works out what should happen to it (job affiliations are never
- # touched). Produces one row per affiliation so each is individually actionable.
- #
- # Actions:
- # :create — facilitator training, none exists yet but one should (pre-event
- # for anyone, post-event only for attendees).
- # :deactivate — facilitator training, owned, its (ended) training wasn't
- # completed. The admin may delete it instead of same-daying it.
- # :reactivate — facilitator training, owned, same-dayed earlier, now attended.
- # :delete — NOT a facilitator training: an owned affiliation auto-created
- # off this event that shouldn't exist.
- # :noop — nothing to do; the row carries a `reason`.
+ # the event's registrants and, for each organization they linked, asks
+ # `ReconcilePerson` what should happen to their facilitator affiliations there —
+ # that class holds every rule; this one turns its decisions into reviewable,
+ # individually-selectable rows. Job affiliations are never touched.
#
# `actionable_person_groups` groups the actionable rows by person (with their
# attendance registration and other-org facilitator affiliations for context);
@@ -48,8 +39,8 @@ def skipped_reason_sections
def reason_rank(reason)
case reason
- when "Active — attended" then 8
- when "Didn't attend — no affiliation created" then 9
+ when ReconcilePerson::ACTIVE_ATTENDED then 8
+ when ReconcilePerson::NOT_ATTENDED then 9
else 0
end
end
@@ -95,94 +86,42 @@ def apply(outcome:)
def all_rows
@all_rows ||= registrations_by_person.flat_map do |person, registrations|
registration = registrations.first
- linked_organizations(registrations).flat_map { |organization| rows_for(person, registration, organization) }
- end
- end
-
- def rows_for(person, registration, organization)
- facilitators = person.affiliations.facilitators
- .where(organization:)
- .includes(event_registration: :event)
- .to_a
-
- unless @event.facilitator_training?
- # A non-training event confers no facilitation, so it only removes
- # facilitator affiliations that were auto-created off it.
- return facilitators.filter_map do |affiliation|
- next unless affiliation.event_registration&.event_id == @event.id
-
- Row.new(person:, registration:, organization:, affiliation:, action: :delete, reason: nil, key: "aff:#{affiliation.id}")
+ linked_organizations(registrations).flat_map do |organization|
+ reconciler(person, registration, organization).plan.map do |decision|
+ row_for(person, registration, organization, decision)
+ end
end
end
-
- attended = completed_training?(person, organization)
- rows = facilitators.map { |affiliation| affiliation_row(person, registration, organization, affiliation, attended) }
- rows << create_row(person, registration, organization, attended) if facilitators.empty?
- rows.compact
- end
-
- def affiliation_row(person, registration, organization, affiliation, attended)
- action, reason = classify_affiliation(affiliation, attended)
- Row.new(person:, registration:, organization:, affiliation:, action:, reason:, key: "aff:#{affiliation.id}")
- end
-
- # Reconciles EVERY facilitator affiliation for the org — hand-entered ones
- # included, not just app-created rows. Deactivation only applies once the
- # governing training has ended (a hand-entered row has no source training, so
- # it's gated on this event ending) — so a pre-event run never deactivates.
- def classify_affiliation(affiliation, attended)
- if attended
- affiliation.active? ? [ :noop, "Active — attended" ] : [ :reactivate, nil ]
- elsif affiliation.active? && deactivation_ready?(affiliation)
- [ :deactivate, nil ]
- elsif affiliation.active?
- [ :noop, "Training hasn't ended yet" ]
- else
- [ :noop, "Already deactivated — didn't attend" ]
- end
end
- def deactivation_ready?(affiliation)
- affiliation.event_registration_id ? source_ended?(affiliation) : @event.ended?
+ def row_for(person, registration, organization, decision)
+ Row.new(person:, registration:, organization:, affiliation: decision.affiliation,
+ action: decision.action, reason: decision.reason,
+ key: row_key(person, organization, decision))
end
- def create_row(person, registration, organization, attended)
- if !@event.ended? || attended
- Row.new(person:, registration:, organization:, affiliation: nil, action: :create, reason: nil,
- key: "create:#{person.id}:#{organization.id}")
- else
- Row.new(person:, registration:, organization:, affiliation: nil, action: :noop,
- reason: "Didn't attend — no affiliation created", key: "none:#{person.id}:#{organization.id}")
- end
+ # Stable per-row identity for the outcome map: the affiliation itself when there
+ # is one, else the (person, org) pair the row would create an affiliation for.
+ def row_key(person, organization, decision)
+ return "aff:#{decision.affiliation.id}" if decision.affiliation
+
+ "#{decision.action == :create ? 'create' : 'none'}:#{person.id}:#{organization.id}"
end
def perform_outcome(row, choice)
- case choice
- when "create"
- AffiliationServices::CreateFromRegistration.call(
- person: row.person, organization: row.organization, facilitator_training: true,
- training_date: @event.start_date, event_registration: row.registration
- )
- when "delete"
- row.affiliation.destroy!
- when "deactivate"
- # Same-day it: end_date = the affiliation's own start_date (start_date itself
- # is never changed), which the model turns into inactive.
- row.affiliation.update!(end_date: row.affiliation.start_date || Date.current)
- when "reactivate"
- row.affiliation.update!(end_date: nil)
- else
- return false # "keep" or unknown
- end
- true
- end
+ action = ACTION_FOR_CHOICE[choice]
+ return false unless action
- def completed_training?(person, organization)
- ReconcileFacilitatorAffiliation.new(person:, organization:).completed_training?
+ reconciler(row.person, row.registration, row.organization).perform(action, affiliation: row.affiliation)
end
- def source_ended?(affiliation)
- affiliation.event_registration&.event&.ended?
+ # One reconciler per (person, org) — reused for both planning and applying so the
+ # attendance lookup behind each decision runs once.
+ def reconciler(person, registration, organization)
+ @reconcilers ||= {}
+ @reconcilers[[ person.id, organization.id ]] ||= ReconcilePerson.new(
+ person:, organization:, event: @event, registration:, include_unowned: true
+ )
end
def other_facilitators(person)
diff --git a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb b/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
deleted file mode 100644
index b4c837651..000000000
--- a/app/services/affiliation_services/reconcile_facilitator_affiliation.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-module AffiliationServices
- # Reconciles a person's **owned** facilitator affiliation for one organization
- # against whether they actually completed a facilitator training there.
- #
- # "Owned" means auto-minted by the registration flow (`event_registration_id`
- # present) — hand-created / historical rows have no link and are left alone.
- #
- # A person is an active facilitator of an org iff they have at least one
- # `attended` registration to that org from a facilitator-training event. Anyone
- # else (no_show, cancelled, incomplete_attendance, still-registered, …) is not,
- # so we **same-day** their owned facilitator affiliation — set `end_date` to its
- # `start_date`, which the model's `set_inactive_from_dates` turns into
- # `inactive: true`. It preserves `start_date` and is reversible: if the person is
- # later marked attended, a re-run clears `end_date` and reactivates the row.
- #
- # The decision is per (person, org) across ALL their training registrations, so
- # no-showing one training but attending another for the same org keeps them
- # active.
- class ReconcileFacilitatorAffiliation
- def self.call(person:, organization:)
- new(person:, organization:).call
- end
-
- def initialize(person:, organization:)
- @person = person
- @organization = organization
- end
-
- # Apply the reconciliation. Returns the action taken (:deactivate, :reactivate,
- # or :noop).
- def call
- rows = owned_facilitator_affiliations.to_a
- return :noop if rows.empty?
-
- completed_training? ? reactivate(rows) : deactivate(rows)
- end
-
- # What #call would do, without writing. Returns :deactivate, :reactivate, or :noop.
- def plan
- rows = owned_facilitator_affiliations.to_a
- return :noop if rows.empty?
-
- if completed_training?
- rows.any? { |affiliation| !affiliation.active? } ? :reactivate : :noop
- elsif deactivatable_affiliations.any?
- :deactivate
- else
- :noop
- end
- end
-
- # Whether the person has any `attended` registration to this org from a
- # facilitator-training event — i.e. actually became a facilitator there.
- def completed_training?
- @person.event_registrations.attended
- .joins(:event).where(events: { facilitator_training: true })
- .joins(:event_registration_organizations)
- .where(event_registration_organizations: { organization_id: @organization.id })
- .exists?
- end
-
- # The owned facilitator affiliations #call would same-day: active, and tied to a
- # training that has already ended. Exposed so the bulk action can offer "delete
- # instead of same-day" over the exact same set.
- def deactivatable_affiliations
- owned_facilitator_affiliations.select { |affiliation| affiliation.active? && source_training_ended?(affiliation) }
- end
-
- private
-
- def deactivate(_rows)
- targets = deactivatable_affiliations
- return :noop if targets.empty?
-
- targets.each { |affiliation| affiliation.update!(end_date: affiliation.start_date || Date.current) }
- :deactivate
- end
-
- def source_training_ended?(affiliation)
- affiliation.event_registration&.event&.ended?
- end
-
- def reactivate(rows)
- ended = rows.reject(&:active?)
- return :noop if ended.empty?
-
- ended.each { |affiliation| affiliation.update!(end_date: nil) }
- :reactivate
- end
-
- def owned_facilitator_affiliations
- @person.affiliations.facilitators
- .where(organization: @organization)
- .where.not(event_registration_id: nil)
- end
- end
-end
diff --git a/app/services/affiliation_services/reconcile_person.rb b/app/services/affiliation_services/reconcile_person.rb
new file mode 100644
index 000000000..2db0bacfe
--- /dev/null
+++ b/app/services/affiliation_services/reconcile_person.rb
@@ -0,0 +1,158 @@
+module AffiliationServices
+ # Decides what should happen to one person's facilitator affiliations with one
+ # organization, in the context of one event. This is the single classifier —
+ # `ReconcileEvent` iterates it across an event's registrants, and it can be
+ # called on its own for a single person (e.g. after an attendance change).
+ #
+ # A person is a facilitator of an org iff they have at least one `attended`
+ # registration to that org from a facilitator-training event. The decision spans
+ # ALL their training registrations for the org, so no-showing one training but
+ # attending another keeps them active.
+ #
+ # Actions:
+ # :create — facilitator training, none exists yet but one should (pre-event
+ # for anyone, post-event only for attendees).
+ # :deactivate — facilitator training, its (ended) training wasn't completed.
+ # Same-days the row: `end_date := start_date` plus an explicit
+ # `inactive: true`, since the model's date rule alone still reads
+ # a row ending today or later as active. Preserves `start_date`
+ # and is reversible.
+ # :reactivate — facilitator training, same-dayed earlier, now attended.
+ # :delete — NOT a facilitator training: an affiliation auto-created off
+ # this event that shouldn't exist.
+ # :noop — nothing to do; the decision carries a `reason`.
+ #
+ # `include_unowned:` is the auto-vs-manual gate. False (the default) touches only
+ # rows the registration flow minted (`event_registration_id` present), leaving
+ # hand-created / historical rows alone. The bulk page passes true — an admin
+ # reviewing every row is expected to reconcile hand-entered ones too.
+ class ReconcilePerson
+ Decision = Struct.new(:affiliation, :action, :reason, keyword_init: true) do
+ def actionable?
+ action != :noop
+ end
+ end
+
+ ACTIVE_ATTENDED = "Active — attended".freeze
+ TRAINING_PENDING = "Training hasn't ended yet".freeze
+ ALREADY_DEACTIVATED = "Already deactivated — didn't attend".freeze
+ NOT_ATTENDED = "Didn't attend — no affiliation created".freeze
+
+ def self.call(person:, organization:, event:, registration: nil, include_unowned: false)
+ new(person:, organization:, event:, registration:, include_unowned:).call
+ end
+
+ def initialize(person:, organization:, event:, registration: nil, include_unowned: false)
+ @person = person
+ @organization = organization
+ @event = event
+ @registration = registration
+ @include_unowned = include_unowned
+ end
+
+ # One Decision per facilitator affiliation in scope, plus a create/no-create
+ # decision when the person has none. No writes.
+ def plan
+ @plan ||= @event.facilitator_training? ? training_plan : non_training_plan
+ end
+
+ # Perform one planned action. Returns whether anything changed, so callers can
+ # count real changes rather than attempted ones.
+ def perform(action, affiliation: nil)
+ return false if affiliation.nil? && action != :create
+
+ case action
+ when :create then create_affiliation
+ when :delete then affiliation.destroy!
+ when :deactivate then affiliation.update!(end_date: affiliation.start_date || Date.current, inactive: true)
+ when :reactivate then affiliation.update!(end_date: nil, inactive: false)
+ else return false
+ end
+ true
+ end
+
+ # Apply every actionable decision. Returns the actions taken.
+ def call
+ plan.select(&:actionable?).filter_map do |decision|
+ decision.action if perform(decision.action, affiliation: decision.affiliation)
+ end
+ end
+
+ private
+
+ # Whether the person has any `attended` registration to this org from a
+ # facilitator-training event — i.e. actually became a facilitator there.
+ def completed_training?
+ return @completed_training if defined?(@completed_training)
+
+ @completed_training = @person.event_registrations.attended
+ .joins(:event).where(events: { facilitator_training: true })
+ .joins(:event_registration_organizations)
+ .where(event_registration_organizations: { organization_id: @organization.id })
+ .exists?
+ end
+
+ # A non-training event confers no facilitation, so it only removes facilitator
+ # affiliations that were auto-created off this event. Rows minted elsewhere —
+ # and hand-entered ones, which carry no link — are none of its business.
+ def non_training_plan
+ facilitator_affiliations.filter_map do |affiliation|
+ next unless affiliation.event_registration&.event_id == @event.id
+
+ Decision.new(affiliation:, action: :delete)
+ end
+ end
+
+ def training_plan
+ decisions = reconcilable_affiliations.map { |affiliation| classify(affiliation) }
+ decisions << creation_decision if facilitator_affiliations.empty? && @registration
+ decisions
+ end
+
+ def classify(affiliation)
+ if completed_training?
+ return Decision.new(affiliation:, action: :noop, reason: ACTIVE_ATTENDED) if affiliation.active?
+
+ Decision.new(affiliation:, action: :reactivate)
+ elsif !affiliation.active?
+ Decision.new(affiliation:, action: :noop, reason: ALREADY_DEACTIVATED)
+ elsif deactivation_ready?(affiliation)
+ Decision.new(affiliation:, action: :deactivate)
+ else
+ Decision.new(affiliation:, action: :noop, reason: TRAINING_PENDING)
+ end
+ end
+
+ # Deactivation waits for the governing training to end, so a pre-event run never
+ # deactivates. A hand-entered row has no source training, so it waits on this event.
+ def deactivation_ready?(affiliation)
+ affiliation.event_registration_id ? affiliation.event_registration&.event&.ended? : @event.ended?
+ end
+
+ def creation_decision
+ return Decision.new(action: :create) if !@event.ended? || completed_training?
+
+ Decision.new(action: :noop, reason: NOT_ATTENDED)
+ end
+
+ def create_affiliation
+ CreateFromRegistration.call(
+ person: @person, organization: @organization, facilitator_training: true,
+ training_date: @event.start_date, event_registration: @registration
+ )
+ end
+
+ def reconcilable_affiliations
+ return facilitator_affiliations if @include_unowned
+
+ facilitator_affiliations.select(&:event_registration_id)
+ end
+
+ def facilitator_affiliations
+ @facilitator_affiliations ||= @person.affiliations.facilitators
+ .where(organization: @organization)
+ .includes(event_registration: :event)
+ .to_a
+ end
+ end
+end
diff --git a/app/views/events/reconcile_affiliations/_tooltip.html.erb b/app/views/events/reconcile_affiliations/_tooltip.html.erb
index a41b75283..478a8014c 100644
--- a/app/views/events/reconcile_affiliations/_tooltip.html.erb
+++ b/app/views/events/reconcile_affiliations/_tooltip.html.erb
@@ -2,7 +2,7 @@
<% case kind %>
<% when :deactivate %>
- Ends this facilitator affiliation as of today (sets its end date to its start date) so it no longer counts as active. The record is kept and reactivates if the person is later marked attended.
+ Ends this facilitator affiliation — its end date is set to its own start date and it's marked inactive — so it no longer counts as active. The record is kept and reactivates if the person is later marked attended.
<% when :delete %>
Permanently deletes this facilitator affiliation. Everything else stays as-is:
diff --git a/app/views/events/reconcile_affiliations/confirm.html.erb b/app/views/events/reconcile_affiliations/confirm.html.erb
index 885b8287d..dc2785ea5 100644
--- a/app/views/events/reconcile_affiliations/confirm.html.erb
+++ b/app/views/events/reconcile_affiliations/confirm.html.erb
@@ -13,7 +13,7 @@
<% sections = {
create: [ "Create", "bg-blue-50 text-blue-800 border-blue-200", "A new facilitator affiliation is created for this organization." ],
reactivate: [ "Reactivate", "bg-green-50 text-green-800 border-green-200", "The end date is cleared so the facilitator affiliation is active again." ],
- deactivate: [ "Deactivate", "bg-red-50 text-red-800 border-red-200", "The facilitator affiliation is same-dayed (ended today) so it no longer counts as active. Reversible." ],
+ deactivate: [ "Deactivate", "bg-red-50 text-red-800 border-red-200", "The facilitator affiliation is same-dayed (end date set to its start date) and marked inactive. Reversible." ],
delete: [ "Delete", "bg-red-100 text-red-900 border-red-300", "The facilitator affiliation is permanently deleted. Job and other-org affiliations are untouched." ]
} %>
diff --git a/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb b/spec/services/affiliation_services/reconcile_person_spec.rb
similarity index 53%
rename from spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
rename to spec/services/affiliation_services/reconcile_person_spec.rb
index d910e4bbb..c6b404a4a 100644
--- a/spec/services/affiliation_services/reconcile_facilitator_affiliation_spec.rb
+++ b/spec/services/affiliation_services/reconcile_person_spec.rb
@@ -1,6 +1,6 @@
require "rails_helper"
-RSpec.describe AffiliationServices::ReconcileFacilitatorAffiliation do
+RSpec.describe AffiliationServices::ReconcilePerson do
let(:person) { create(:person) }
let(:organization) { create(:organization) }
@@ -22,12 +22,16 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
event_registration: registration)
end
+ def reconcile(registration, **options)
+ described_class.call(person: person, organization: organization, event: registration.event, **options)
+ end
+
describe "deactivation" do
it "same-days the owned facilitator affiliation when the person never attended" do
reg = training_registration(status: "no_show")
affiliation = owned_facilitator(registration: reg)
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
affiliation.reload
expect(affiliation.end_date).to eq(affiliation.start_date)
@@ -40,32 +44,54 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
reg = training_registration(status: status)
affiliation = owned_facilitator(registration: reg)
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
expect(affiliation.reload).not_to be_active
end
end
+ it "deactivates on the day a one-day training ends, when the affiliation starts that same day" do
+ event = create(:event, facilitator_training: true, start_date: 3.hours.ago,
+ end_date: 1.hour.ago, registration_close_date: 4.hours.ago)
+ reg = create(:event_registration, registrant: person, event: event, status: "no_show")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ affiliation = owned_facilitator(registration: reg, start_date: Date.current)
+
+ reconcile(reg)
+
+ expect(affiliation.reload).not_to be_active
+ end
+
it "leaves an assumptive affiliation alone while its training is still upcoming" do
reg = training_registration(status: "registered", ended: false)
affiliation = owned_facilitator(registration: reg, start_date: Date.current)
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
expect(affiliation.reload).to be_active
expect(affiliation.end_date).to be_nil
end
it "leaves an unowned (hand-created) facilitator affiliation untouched" do
- training_registration(status: "no_show")
+ reg = training_registration(status: "no_show")
hand_created = create(:affiliation, person: person, organization: organization,
title: "Facilitator", start_date: 1.month.ago.to_date)
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
expect(hand_created.reload).to be_active
expect(hand_created.end_date).to be_nil
end
+
+ it "reconciles a hand-created affiliation when the caller opts in" do
+ reg = training_registration(status: "no_show")
+ hand_created = create(:affiliation, person: person, organization: organization,
+ title: "Facilitator", start_date: 1.month.ago.to_date)
+
+ reconcile(reg, include_unowned: true)
+
+ expect(hand_created.reload).not_to be_active
+ end
end
describe "keeping / activating" do
@@ -73,7 +99,7 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
reg = training_registration(status: "attended")
affiliation = owned_facilitator(registration: reg)
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
expect(affiliation.reload).to be_active
expect(affiliation.end_date).to be_nil
@@ -84,7 +110,7 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
affiliation = owned_facilitator(registration: no_show)
training_registration(status: "attended")
- described_class.call(person: person, organization: organization)
+ reconcile(no_show)
expect(affiliation.reload).to be_active
end
@@ -95,21 +121,38 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
affiliation.update!(end_date: affiliation.start_date)
expect(affiliation.reload).not_to be_active
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
expect(affiliation.reload).to be_active
expect(affiliation.end_date).to be_nil
end
end
+ describe "creating" do
+ it "creates the missing facilitator affiliation for an attendee" do
+ reg = training_registration(status: "attended")
+
+ expect { described_class.call(person: person, organization: organization, event: reg.event, registration: reg) }
+ .to change { person.affiliations.facilitators.where(organization: organization).count }.by(1)
+ end
+
+ it "proposes nothing when the caller passes no registration to own the new row" do
+ reg = training_registration(status: "attended")
+
+ plan = described_class.new(person: person, organization: organization, event: reg.event).plan
+
+ expect(plan).to be_empty
+ end
+ end
+
describe "idempotence" do
it "is stable across repeated runs" do
reg = training_registration(status: "no_show")
affiliation = owned_facilitator(registration: reg)
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
first = affiliation.reload.end_date
- described_class.call(person: person, organization: organization)
+ reconcile(reg)
expect(affiliation.reload.end_date).to eq(first)
end
@@ -120,18 +163,45 @@ def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
reg = training_registration(status: "no_show")
affiliation = owned_facilitator(registration: reg)
- plan = described_class.new(person: person, organization: organization).plan
+ plan = described_class.new(person: person, organization: organization, event: reg.event).plan
- expect(plan).to eq(:deactivate)
+ expect(plan.map(&:action)).to eq([ :deactivate ])
expect(affiliation.reload).to be_active
end
- it "reports :noop when there is no owned facilitator affiliation" do
- training_registration(status: "no_show")
+ it "reports the reason a row needs no action" do
+ reg = training_registration(status: "attended")
+ owned_facilitator(registration: reg)
- plan = described_class.new(person: person, organization: organization).plan
+ plan = described_class.new(person: person, organization: organization, event: reg.event).plan
- expect(plan).to eq(:noop)
+ expect(plan.map(&:action)).to eq([ :noop ])
+ expect(plan.first.reason).to eq(described_class::ACTIVE_ATTENDED)
+ expect(plan.first).not_to be_actionable
+ end
+
+ it "plans nothing when there is no owned facilitator affiliation" do
+ reg = training_registration(status: "no_show")
+
+ plan = described_class.new(person: person, organization: organization, event: reg.event).plan
+
+ expect(plan).to be_empty
+ end
+ end
+
+ describe "a non-training event" do
+ it "deletes only the facilitator affiliation auto-created off that event" do
+ event = create(:event, :ended, facilitator_training: false)
+ reg = create(:event_registration, registrant: person, event: event, status: "attended")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ off_this_event = owned_facilitator(registration: reg)
+ hand_created = create(:affiliation, person: person, organization: organization,
+ title: "Facilitator", start_date: 2.years.ago.to_date)
+
+ reconcile(reg)
+
+ expect(Affiliation.exists?(off_this_event.id)).to be(false)
+ expect(hand_created.reload).to be_active
end
end
end
From ea6500cd1c3df98615ac501e3f5354411c1a2ad3 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Tue, 18 Aug 2026 08:09:49 -0400
Subject: [PATCH 25/37] Re-render the reconcile row when its attendance changes
The badge's Turbo submit answered with a stream that swapped only the status
chip, so the row kept offering its pre-toggle action and the new return_to
redirect never ran. Pages that pass a return_to now opt out of Turbo and get the
full re-render; the registrants and onboarding pages keep the inline swap.
Also lists the bulk action on the Features & tips seed and drops the "uncheck"
wording left over from before the row controls became radios.
Co-Authored-By: Claude Opus 5 (1M context)
---
.../_attendance_status_badge.html.erb | 6 ++++-
.../reconcile_affiliations/index.html.erb | 2 +-
config/features.yml | 13 ++++++++++
.../events/reconcile_affiliations_spec.rb | 25 +++++++++++++++++++
4 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/app/views/event_registrations/_attendance_status_badge.html.erb b/app/views/event_registrations/_attendance_status_badge.html.erb
index a9f40128e..fccb183ec 100644
--- a/app/views/event_registrations/_attendance_status_badge.html.erb
+++ b/app/views/event_registrations/_attendance_status_badge.html.erb
@@ -1,7 +1,11 @@
<% deco = registration.decorate %>
<% badge_return_to = local_assigns.fetch(:return_to, nil) %>
- <%= form_with model: registration, url: event_registration_path(registration, return_to: badge_return_to), method: :patch, data: { turbo_frame: "_top" } do |f| %>
+ <%# Without a `return_to` the update answers with a turbo_stream that swaps just this
+ badge. A page whose surrounding content depends on the status (the reconcile
+ actions) passes one and opts out of Turbo, so it gets the redirect and re-renders. %>
+ <%= form_with model: registration, url: event_registration_path(registration, return_to: badge_return_to),
+ method: :patch, data: { turbo_frame: "_top", turbo: (false if badge_return_to) } do |f| %>
<%= f.select :status,
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 34af58341..8fd1e9703 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -22,7 +22,7 @@
This deletes those. Job affiliations are left untouched.
<% end %>
-
Every facilitator affiliation for a linked organization is reconciled against attendance — including hand-entered ones. Review each row and uncheck any you want to leave as-is.
+
Every facilitator affiliation for a linked organization is reconciled against attendance — including hand-entered ones. Review each row and pick an outcome: the suggested action is preselected, and every row also has a leave-as-is option.
<% if @event.affiliations_reconciled_at %>
Last reconciled <%= @event.affiliations_reconciled_at.to_fs(:long) %>.
<% end %>
diff --git a/config/features.yml b/config/features.yml
index 157f3473c..5060f8b2d 100644
--- a/config/features.yml
+++ b/config/features.yml
@@ -277,6 +277,19 @@
pro_tips:
- "Super-admins can edit any feature in place (rich descriptions with screenshots) and click \"Sync latest updates\" to pull in newly-shipped ones."
+- name: "Reconcile affiliations after a training"
+ area: events
+ display_status: admin_facing
+ released_on: 2026-08-18
+ action_path: "/events/1/reconcile_affiliations"
+ pr_number: 2195
+ summary: >-
+ A bulk action on an event that brings facilitator affiliations in line with who
+ attended — creating missing ones, ending them for people who didn't attend, and
+ reactivating anyone later marked attended. Preview every change before applying it.
+ pro_tips:
+ - "Job affiliations are never touched, and any row can be left as-is."
+
- name: "Edit an affiliation's details and comments"
area: people
display_status: admin_facing
diff --git a/spec/requests/events/reconcile_affiliations_spec.rb b/spec/requests/events/reconcile_affiliations_spec.rb
index 2b4d53286..a5f30db21 100644
--- a/spec/requests/events/reconcile_affiliations_spec.rb
+++ b/spec/requests/events/reconcile_affiliations_spec.rb
@@ -86,6 +86,17 @@ def registrant_with_affiliation(status:)
end
describe "toggling attendance from the reconcile page" do
+ it "opts the attendance form out of Turbo, so the redirect runs and the row's actions re-render" do
+ person, _affiliation = registrant_with_affiliation(status: "no_show")
+ registration = person.event_registrations.first
+
+ get reconcile_affiliations_event_path(event)
+
+ form = Nokogiri::HTML(response.body).at_css("form[action*='/event_registrations/#{registration.id}']")
+ expect(form["data-turbo"]).to eq("false")
+ expect(form["action"]).to include("return_to=reconcile_affiliations")
+ end
+
it "stays on the reconcile page with a flash instead of leaving for the roster" do
person, _affiliation = registrant_with_affiliation(status: "no_show")
registration = person.event_registrations.first
@@ -130,6 +141,20 @@ def registrant_with_affiliation(status:)
expect(event.reload.affiliations_reconciled_at).to be_present
end
+ it "deactivates a no-show whose one-day training started and ended today" do
+ same_day = create(:event, facilitator_training: true, start_date: 3.hours.ago,
+ end_date: 1.hour.ago, registration_close_date: 4.hours.ago)
+ person = create(:person)
+ reg = create(:event_registration, event: same_day, registrant: person, status: "no_show")
+ create(:event_registration_organization, event_registration: reg, organization: organization)
+ affiliation = create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: Date.current, event_registration: reg)
+
+ post perform_reconcile_affiliations_event_path(same_day), params: { outcome: { "aff:#{affiliation.id}" => "deactivate" } }
+
+ expect(affiliation.reload).not_to be_active
+ end
+
it "spares a row set to keep" do
_person, affiliation = registrant_with_affiliation(status: "no_show")
From 6890bbc497d1603e3b7671e30e3d347009c22bfa Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Tue, 18 Aug 2026 10:00:18 -0400
Subject: [PATCH 26/37] Put the Inactive control on the standalone affiliation
editor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Main moved per-affiliation editing to the gear editor, so that — not the dense
inline row — is where the flag belongs. Trims the comments added across this
branch down to the ones carrying a why.
Co-Authored-By: Claude Opus 5 (1M context)
---
app/controllers/affiliations_controller.rb | 2 +-
.../reconcile_affiliations_controller.rb | 13 ++---
app/decorators/affiliation_decorator.rb | 3 +-
app/models/affiliation.rb | 5 +-
app/models/event.rb | 4 +-
.../affiliation_services/reconcile_event.rb | 30 ++++-------
.../affiliation_services/reconcile_person.rb | 51 ++++++-------------
app/views/affiliations/edit.html.erb | 7 +++
.../_attendance_status_badge.html.erb | 5 +-
.../reconcile_affiliations/_tooltip.html.erb | 1 -
.../reconcile_affiliations/index.html.erb | 6 +--
spec/requests/affiliations_spec.rb | 9 ++++
12 files changed, 54 insertions(+), 82 deletions(-)
diff --git a/app/controllers/affiliations_controller.rb b/app/controllers/affiliations_controller.rb
index da1278bf2..f3694fe90 100644
--- a/app/controllers/affiliations_controller.rb
+++ b/app/controllers/affiliations_controller.rb
@@ -64,7 +64,7 @@ def set_affiliation
def affiliation_params
params.require(:affiliation).permit(
- :person_id, :organization_id, :title, :start_date, :end_date, :primary_contact, :organization_address_id,
+ :person_id, :organization_id, :title, :start_date, :end_date, :inactive, :primary_contact, :organization_address_id,
comments_attributes: [ :id, :topic, :body, :flagged, :_destroy ]
)
end
diff --git a/app/controllers/events/reconcile_affiliations_controller.rb b/app/controllers/events/reconcile_affiliations_controller.rb
index 997ff3c81..80d976991 100644
--- a/app/controllers/events/reconcile_affiliations_controller.rb
+++ b/app/controllers/events/reconcile_affiliations_controller.rb
@@ -1,10 +1,6 @@
module Events
- # The "Reconcile affiliations" bulk action: a preview-and-confirm page that
- # brings each registrant's owned facilitator affiliation in line with reality.
- # For a facilitator training it creates missing affiliations, same-days
- # non-completers, and reactivates late attendees; for a non-training event it
- # removes facilitator affiliations that were auto-created off it. The admin can
- # opt individual rows out (and, for same-day rows, delete instead) before applying.
+ # The "Reconcile affiliations" bulk action: index (edit) → confirm (preview, no
+ # writes) → create (perform). `AffiliationServices::ReconcilePerson` holds the rules.
class ReconcileAffiliationsController < ApplicationController
include AhoyTracking
before_action :set_event
@@ -22,7 +18,6 @@ def index
@event = @event.decorate
end
- # Step 2: show exactly what "Perform changes" will do (no writes yet).
def confirm
authorize! @event, to: :reconcile_affiliations?
@@ -46,8 +41,8 @@ def set_event
@event = Event.find(params[:id])
end
- # `outcome` is a { row.key => choice } map with dynamic keys, read as a plain
- # string hash (never mass-assigned); the service only acts on known choices.
+ # Dynamic keys, so read as a plain string hash (never mass-assigned); the service
+ # only acts on known choices.
def outcome_params
raw = params[:outcome]
return {} unless raw.respond_to?(:each_pair)
diff --git a/app/decorators/affiliation_decorator.rb b/app/decorators/affiliation_decorator.rb
index 3fac6cece..46028193e 100644
--- a/app/decorators/affiliation_decorator.rb
+++ b/app/decorators/affiliation_decorator.rb
@@ -3,8 +3,7 @@ def detail(length: nil)
"#{person.full_name}: #{title.presence || position} - #{organization.name}"
end
- # Compact "started – ended" range for the affiliation, e.g. "Oct 13, 2026 – present".
- # Reads "no start date" when unset so a blank date isn't silently omitted.
+ # e.g. "Oct 13, 2026 – present"
def date_range
start = start_date ? start_date.strftime("%b %-d, %Y") : "no start date"
finish = end_date ? end_date.strftime("%b %-d, %Y") : "present"
diff --git a/app/models/affiliation.rb b/app/models/affiliation.rb
index 04afb9e25..321215825 100644
--- a/app/models/affiliation.rb
+++ b/app/models/affiliation.rb
@@ -163,9 +163,8 @@ def sole_address_id_for_new_organization
addresses.first.id if addresses&.one?
end
- # Derives `inactive` from the dates, unless this save sets it explicitly — an
- # admin's tick, or a same-day deactivation whose end_date (today, or the future
- # start of an upcoming affiliation) the date rule alone would still call active.
+ # An explicit assignment wins: the date rule alone still reads a row ending today
+ # or later as active.
def set_inactive_from_dates
return if inactive_changed?
return unless end_date_changed? || start_date_changed?
diff --git a/app/models/event.rb b/app/models/event.rb
index 28270b6ff..e72e1fd0f 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -168,9 +168,7 @@ def ended?
end_date < Time.current
end
- # A registrant's status changed after affiliations were last reconciled, so the
- # reconciliation may be out of date and worth re-running. False when never
- # reconciled (nothing to be stale against).
+ # A registrant changed since the last reconciliation, so it's worth re-running.
def affiliations_reconciliation_stale?
return false unless affiliations_reconciled_at
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index b95187408..a2cf5ffa1 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -1,15 +1,8 @@
module AffiliationServices
- # Event-level orchestration for the "Reconcile affiliations" bulk action. Walks
- # the event's registrants and, for each organization they linked, asks
- # `ReconcilePerson` what should happen to their facilitator affiliations there —
- # that class holds every rule; this one turns its decisions into reviewable,
- # individually-selectable rows. Job affiliations are never touched.
- #
- # `actionable_person_groups` groups the actionable rows by person (with their
- # attendance registration and other-org facilitator affiliations for context);
- # `skipped_reason_sections` groups the no-action rows by reason. `apply` performs
- # the kept actionable rows and stamps the event. Every facilitator affiliation for
- # a linked org is reconciled — hand-entered rows included, not just app-created ones.
+ # Event-level orchestration for the "Reconcile affiliations" bulk action: asks
+ # `ReconcilePerson` about each registrant's linked orgs and turns its decisions
+ # into individually-selectable rows. Every rule lives there; keys, grouping and
+ # the timestamp live here. Job affiliations are never touched.
class ReconcileEvent
Row = Struct.new(:person, :registration, :organization, :affiliation, :action, :reason, :key, keyword_init: true) do
def actionable?
@@ -21,17 +14,16 @@ def initialize(event)
@event = event
end
- # Actionable rows grouped by person: [{ person:, registration:, rows:,
- # other_facilitators: }]. `other_facilitators` are the person's active
- # facilitator affiliations with orgs they did NOT link on this event.
+ # `other_facilitators` are the person's active facilitator affiliations with orgs
+ # they did NOT link on this event.
def actionable_person_groups
all_rows.select(&:actionable?).group_by(&:person).map do |person, rows|
{ person:, registration: rows.first.registration, rows:, other_facilitators: other_facilitators(person) }
end
end
- # No-action rows grouped by reason: [[reason, [rows]]]. "Active — attended" sorts
- # second-to-last and the trivial "no affiliation" bucket last; the rest alphabetical.
+ # "Active — attended" sorts second-to-last and the trivial "no affiliation" bucket
+ # last; the rest alphabetical.
def skipped_reason_sections
grouped = all_rows.reject(&:actionable?).group_by(&:reason)
grouped.keys.sort_by { |reason| [ reason_rank(reason), reason ] }.map { |reason| [ reason, grouped[reason] ] }
@@ -51,12 +43,10 @@ def any_rows?
Change = Struct.new(:person, :organization, :affiliation, :action, keyword_init: true)
- # Each row's outcome is one radio choice keyed by row.key: the action itself
- # (deactivate/delete/reactivate/create) or "keep" (do nothing).
+ # One radio choice per row: the action itself, or "keep".
ACTION_FOR_CHOICE = { "deactivate" => :deactivate, "delete" => :delete, "reactivate" => :reactivate, "create" => :create }.freeze
- # The concrete changes the given `outcome` map will make, for the confirmation
- # screen. `outcome` is `{ row.key => choice }`.
+ # What the `{ row.key => choice }` map will change, for the confirmation screen.
def planned_changes(outcome:)
outcome = outcome.to_h
diff --git a/app/services/affiliation_services/reconcile_person.rb b/app/services/affiliation_services/reconcile_person.rb
index 2db0bacfe..be1bb02af 100644
--- a/app/services/affiliation_services/reconcile_person.rb
+++ b/app/services/affiliation_services/reconcile_person.rb
@@ -1,31 +1,15 @@
module AffiliationServices
- # Decides what should happen to one person's facilitator affiliations with one
- # organization, in the context of one event. This is the single classifier —
- # `ReconcileEvent` iterates it across an event's registrants, and it can be
- # called on its own for a single person (e.g. after an attendance change).
+ # The single classifier for one person's facilitator affiliations with one
+ # organization, in the context of one event. `ReconcileEvent` iterates it across
+ # an event's registrants; it also stands alone for a single person.
#
# A person is a facilitator of an org iff they have at least one `attended`
- # registration to that org from a facilitator-training event. The decision spans
- # ALL their training registrations for the org, so no-showing one training but
- # attending another keeps them active.
+ # registration to that org from a facilitator training — across ALL their
+ # training registrations, so no-showing one but attending another keeps them
+ # active.
#
- # Actions:
- # :create — facilitator training, none exists yet but one should (pre-event
- # for anyone, post-event only for attendees).
- # :deactivate — facilitator training, its (ended) training wasn't completed.
- # Same-days the row: `end_date := start_date` plus an explicit
- # `inactive: true`, since the model's date rule alone still reads
- # a row ending today or later as active. Preserves `start_date`
- # and is reversible.
- # :reactivate — facilitator training, same-dayed earlier, now attended.
- # :delete — NOT a facilitator training: an affiliation auto-created off
- # this event that shouldn't exist.
- # :noop — nothing to do; the decision carries a `reason`.
- #
- # `include_unowned:` is the auto-vs-manual gate. False (the default) touches only
- # rows the registration flow minted (`event_registration_id` present), leaving
- # hand-created / historical rows alone. The bulk page passes true — an admin
- # reviewing every row is expected to reconcile hand-entered ones too.
+ # `include_unowned: false` (the default) touches only rows the registration flow
+ # minted, leaving hand-created ones alone; the bulk page passes true.
class ReconcilePerson
Decision = Struct.new(:affiliation, :action, :reason, keyword_init: true) do
def actionable?
@@ -50,14 +34,13 @@ def initialize(person:, organization:, event:, registration: nil, include_unowne
@include_unowned = include_unowned
end
- # One Decision per facilitator affiliation in scope, plus a create/no-create
- # decision when the person has none. No writes.
+ # One Decision per affiliation in scope, plus a create/no-create decision when
+ # the person has none. No writes.
def plan
@plan ||= @event.facilitator_training? ? training_plan : non_training_plan
end
- # Perform one planned action. Returns whether anything changed, so callers can
- # count real changes rather than attempted ones.
+ # Returns whether anything changed, so callers can count real changes.
def perform(action, affiliation: nil)
return false if affiliation.nil? && action != :create
@@ -71,7 +54,6 @@ def perform(action, affiliation: nil)
true
end
- # Apply every actionable decision. Returns the actions taken.
def call
plan.select(&:actionable?).filter_map do |decision|
decision.action if perform(decision.action, affiliation: decision.affiliation)
@@ -80,8 +62,6 @@ def call
private
- # Whether the person has any `attended` registration to this org from a
- # facilitator-training event — i.e. actually became a facilitator there.
def completed_training?
return @completed_training if defined?(@completed_training)
@@ -92,9 +72,8 @@ def completed_training?
.exists?
end
- # A non-training event confers no facilitation, so it only removes facilitator
- # affiliations that were auto-created off this event. Rows minted elsewhere —
- # and hand-entered ones, which carry no link — are none of its business.
+ # A non-training event confers no facilitation, so it only removes what was
+ # auto-created off it.
def non_training_plan
facilitator_affiliations.filter_map do |affiliation|
next unless affiliation.event_registration&.event_id == @event.id
@@ -123,8 +102,8 @@ def classify(affiliation)
end
end
- # Deactivation waits for the governing training to end, so a pre-event run never
- # deactivates. A hand-entered row has no source training, so it waits on this event.
+ # Waits for the governing training to end, so a pre-event run never deactivates.
+ # A hand-entered row has no source training, so it waits on this event.
def deactivation_ready?(affiliation)
affiliation.event_registration_id ? affiliation.event_registration&.event&.ended? : @event.ended?
end
diff --git a/app/views/affiliations/edit.html.erb b/app/views/affiliations/edit.html.erb
index 6671c45a5..f08348bed 100644
--- a/app/views/affiliations/edit.html.erb
+++ b/app/views/affiliations/edit.html.erb
@@ -107,6 +107,13 @@
label_html: { class: "block text-sm font-medium text-gray-700 mb-1" },
input_html: { type: "date", value: @affiliation.end_date&.strftime("%Y-%m-%d") } %>
+
+ <%= f.input :inactive,
+ as: :boolean,
+ label: "Inactive",
+ hint: "Overrides the dates — tick to end an affiliation the dates still call active.",
+ input_html: { class: "mr-2 rounded focus:ring-blue-500 text-blue-600" } %>
+
- <%# Without a `return_to` the update answers with a turbo_stream that swaps just this
- badge. A page whose surrounding content depends on the status (the reconcile
- actions) passes one and opts out of Turbo, so it gets the redirect and re-renders. %>
+ <%# No return_to: the update answers with a turbo_stream that swaps just this badge.
+ With one: opt out of Turbo so the redirect runs and the whole page re-renders. %>
<%= form_with model: registration, url: event_registration_path(registration, return_to: badge_return_to),
method: :patch, data: { turbo_frame: "_top", turbo: (false if badge_return_to) } do |f| %>
diff --git a/app/views/events/reconcile_affiliations/_tooltip.html.erb b/app/views/events/reconcile_affiliations/_tooltip.html.erb
index 478a8014c..662cc5247 100644
--- a/app/views/events/reconcile_affiliations/_tooltip.html.erb
+++ b/app/views/events/reconcile_affiliations/_tooltip.html.erb
@@ -1,4 +1,3 @@
-<%# Hover explanation for a reconcile action. `kind` is the action symbol. %>
<% case kind %>
<% when :deactivate %>
diff --git a/app/views/events/reconcile_affiliations/index.html.erb b/app/views/events/reconcile_affiliations/index.html.erb
index 8fd1e9703..e44e5b779 100644
--- a/app/views/events/reconcile_affiliations/index.html.erb
+++ b/app/views/events/reconcile_affiliations/index.html.erb
@@ -60,9 +60,8 @@
- <%# Standalone form (turbo:false so the POST renders the confirm page). The cards
- live OUTSIDE it — the attendance chip renders its own form and nesting forms is
- invalid — so the radios and submit join this form via the HTML form= attribute. %>
+ <%# The cards sit outside this form — the attendance chip renders its own and forms
+ can't nest — so the radios join it via the HTML form= attribute. %>
<%= form_with url: reconcile_affiliations_event_path(@event), method: :post, id: "reconcile_form", data: { turbo: false } do %><% end %>
@@ -94,7 +93,6 @@
<% else %>
<%= row.organization.name %>
<% end %>
- <%# One radio per outcome (native mutual exclusion — no JS). has-[:checked] colors the chosen button. %>
<% outcome_options[row.action].each do |value, label, color| %>
diff --git a/spec/requests/affiliations_spec.rb b/spec/requests/affiliations_spec.rb
index 1cf65b6dc..7d8e32356 100644
--- a/spec/requests/affiliations_spec.rb
+++ b/spec/requests/affiliations_spec.rb
@@ -80,6 +80,15 @@
expect(affiliation.reload.organization_address_id).to eq(address.id)
end
+
+ it "ends an affiliation whose dates still read as active" do
+ affiliation.update!(start_date: Date.current, end_date: nil, inactive: false)
+
+ patch affiliation_path(affiliation, return_to: "organization", origin_id: organization.id),
+ params: { affiliation: { inactive: "1" } }
+
+ expect(affiliation.reload).not_to be_active
+ end
end
context "as a non-admin" do
From 039d3f5e387fa4e027b0e658d23d63dff15af82f Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Wed, 19 Aug 2026 08:14:19 -0400
Subject: [PATCH 27/37] Seed one person's affiliation history across several
years
The person History card and activity timeline had nothing multi-year to render,
so affiliation edits, trainings, memberships and comments couldn't be seen
interleaved. Two gaps kept the seeded rows invisible: affiliation comments were
missing from PersonCommentAggregator (Affiliation became commentable in #2235
without being added), and payment lifecycle events record the STI subclass
("CashPayment"), which the person's Payment filter never matched.
Co-Authored-By: Claude Opus 5 (1M context)
---
.../analytics/person_activity_events.rb | 5 +-
app/services/person_comment_aggregator.rb | 11 +-
db/seeds/dev/affiliation_history.rb | 184 ++++++++++++++++++
lib/tasks/dev.rake | 6 +
.../analytics/person_activity_events_spec.rb | 12 ++
.../person_comment_aggregator_spec.rb | 9 +-
6 files changed, 220 insertions(+), 7 deletions(-)
create mode 100644 db/seeds/dev/affiliation_history.rb
diff --git a/app/services/analytics/person_activity_events.rb b/app/services/analytics/person_activity_events.rb
index c879e6c2d..894a0ab75 100644
--- a/app/services/analytics/person_activity_events.rb
+++ b/app/services/analytics/person_activity_events.rb
@@ -5,6 +5,8 @@ module Analytics
# "Associated records" panel. Powers the person edit "History" card and the
# `person_id` filter on the admin Ahoy activities index.
class PersonActivityEvents
+ PAYMENT_TYPES = %w[ Payment FilemakerPayment ExternalProcessorPayment CheckPayment CashPayment ].freeze
+
def initialize(person)
@person = person
end
@@ -45,7 +47,8 @@ def resource_ids_by_type
"ContinuingEducationRegistration" => ContinuingEducationRegistration.where(event_registration_id: @person.event_registrations.select(:id)).select(:id),
"FormSubmission" => @person.form_submissions.select(:id),
"Grant" => @person.grants.select(:id),
- "Payment" => Payment.where(person_id: @person.id).select(:id),
+ # Lifecycle events record the STI subclass ("CashPayment", …), not "Payment".
+ PAYMENT_TYPES => Payment.where(person_id: @person.id).select(:id),
"Scholarship" => @person.scholarships.select(:id),
"TopicSubscription" => @person.topic_subscriptions.select(:id),
"CommunityNews" => @person.community_news_as_author.select(:id),
diff --git a/app/services/person_comment_aggregator.rb b/app/services/person_comment_aggregator.rb
index 1bc626d11..9eac6ad08 100644
--- a/app/services/person_comment_aggregator.rb
+++ b/app/services/person_comment_aggregator.rb
@@ -1,13 +1,13 @@
# Gathers every comment connected to a person into a single newest-first feed —
# their own profile comments plus the comments left on the records that hang off
-# them: event registrations, scholarships, CE registrations, the stories and
-# story ideas they're credited on, and their login account. Returns one
+# them: affiliations, event registrations, scholarships, CE registrations, the
+# stories and story ideas they're credited on, and their login account. Returns one
# ActiveRecord::Relation of Comment so callers can filter, paginate, and preload
# uniformly. Payments carry no comments, so they never appear here.
class PersonCommentAggregator
# commentable_type => class, in the order sources are surfaced. Kept as strings
# so the query never has to instantiate the classes.
- SOURCE_TYPES = %w[ Person EventRegistration Scholarship ContinuingEducationRegistration TopicSubscription Story StoryIdea User ].freeze
+ SOURCE_TYPES = %w[ Person Affiliation EventRegistration Scholarship ContinuingEducationRegistration TopicSubscription Story StoryIdea User ].freeze
def initialize(person)
@person = person
@@ -16,6 +16,7 @@ def initialize(person)
def comments
scopes = [
scope_for("Person", [ @person.id ]),
+ scope_for("Affiliation", affiliation_ids),
scope_for("EventRegistration", registration_ids),
scope_for("Scholarship", scholarship_ids),
scope_for("ContinuingEducationRegistration", ce_registration_ids),
@@ -37,6 +38,10 @@ def scope_for(type, ids)
Comment.where(commentable_type: type, commentable_id: ids)
end
+ def affiliation_ids
+ person.affiliations.ids
+ end
+
def registration_ids
@registration_ids ||= person.event_registrations.ids
end
diff --git a/db/seeds/dev/affiliation_history.rb b/db/seeds/dev/affiliation_history.rb
new file mode 100644
index 000000000..c4604f64c
--- /dev/null
+++ b/db/seeds/dev/affiliation_history.rb
@@ -0,0 +1,184 @@
+# Several years of interleaved history for one person — trainings, memberships and
+# affiliation edits — so the person History card and the admin activity timeline have
+# something realistic to render. Targets the owner of the first affiliation.
+#
+# Ahoy lifecycle events are written directly rather than letting AhoyTrackable fire
+# them, because the whole point is timestamps spread over past years.
+
+affiliation = Affiliation.order(:id).first
+
+if affiliation.nil?
+ puts "Skipping affiliation history seed: no affiliations. Run db:seed:dev first."
+elsif Ahoy::Event.where(resource_type: "Affiliation", resource_id: affiliation.id).where("time < ?", 1.year.ago).exists?
+ puts "Skipping affiliation history seed (already seeded)"
+else
+ person = affiliation.person
+ actor = person&.user || User.where(super_user: true).first
+
+ if person.nil? || actor.nil?
+ puts "Skipping affiliation history seed: affiliation ##{affiliation.id} has no person or no admin user."
+ else
+ puts "Building #{person.full_name}'s history around affiliation ##{affiliation.id}…"
+
+ home_org = affiliation.organization
+ second_org = Organization.where.not(id: home_org&.id).order(:id).first || home_org
+
+ visits = Hash.new do |cache, year|
+ cache[year] = Ahoy::Visit.create!(
+ visit_token: SecureRandom.uuid, visitor_token: SecureRandom.uuid, user: actor,
+ started_at: Time.zone.local(year, 6, 1, 9, 0), browser: "Chrome", device_type: "Desktop",
+ city: "Los Angeles", country: "US", landing_page: "/people/#{person.id}/edit"
+ )
+ end
+
+ track = ->(action, record, at, extra = {}) do
+ Ahoy::Event.create!(
+ visit: visits[at.year],
+ user: actor,
+ name: "#{action}.#{record.class.table_name.singularize}",
+ resource_type: record.class.name,
+ resource_id: record.id,
+ properties: {
+ resource_type: record.class.name,
+ resource_id: record.id,
+ resource_title: (record.try(:title).presence || record.try(:name).presence || record.id).to_s
+ }.merge(extra),
+ time: at
+ )
+ end
+
+ changed = ->(pairs) { { changes: pairs.transform_values { |(before, after)| { before: before, after: after } } } }
+
+ # Comments reach the person's History through PersonCommentAggregator, so they
+ # only show when left on the person or a record that hangs off them.
+ note = ->(subject, body, at, topic: nil) do
+ comment = subject.comments.create!(body: body, topic: topic, created_by: actor, updated_by: actor)
+ comment.update_columns(created_at: at, updated_at: at)
+ track.("create", comment, at, { resource_title: body.truncate(60) })
+ comment
+ end
+
+ training = ->(title, starts_on, status, organization) do
+ event = Event.create!(
+ title: title,
+ description: "Two-day facilitator training.",
+ start_date: starts_on.to_time(:utc) + 9.hours,
+ end_date: starts_on.to_time(:utc) + 1.day + 16.hours,
+ registration_close_date: starts_on.to_time(:utc) - 1.week,
+ facilitator_training: true,
+ published: true,
+ created_by: actor,
+ cost_cents: 25_000
+ )
+ registration = EventRegistration.create!(event: event, registrant: person, status: status)
+ EventRegistrationOrganization.create!(event_registration: registration, organization: organization)
+ registration.update_columns(created_at: starts_on - 6.weeks, updated_at: starts_on + 3.days)
+
+ track.("create", registration, starts_on - 6.weeks, { resource_title: title })
+ track.("update", registration, starts_on + 3.days,
+ { resource_title: title }.merge(changed.({ "status" => [ "registered", status ] })))
+ registration
+ end
+
+ email = ->(subject, body, at, kind: "manual_log") do
+ Notification.create!(
+ kind: kind, notification_type: 0,
+ channel: "email", direction: "outgoing", recipient_role: "person",
+ recipient_email: person.communications_email, email_subject: subject, email_body_text: body,
+ sender: actor, delivered_at: at
+ ).update_columns(created_at: at, updated_at: at)
+ end
+
+ year = ->(n) { Date.current - n.years }
+
+ # ── 7 years ago: first training, becomes a facilitator ───────────────────
+ first_registration = training.("Facilitator Training: Foundations", year.(7), "attended", second_org)
+ note.(first_registration, "Travelled in from out of state; covered by a partial scholarship.",
+ year.(7) + 1.day, topic: "Registration")
+ email.("Welcome to the AWBW facilitator community",
+ "Congratulations on completing your facilitator training.", year.(7) + 3.days)
+
+ first_facilitator = Affiliation.create!(
+ person: person, organization: second_org, title: "Facilitator", start_date: year.(7) + 2.days
+ )
+ track.("create", first_facilitator, year.(7) + 2.days)
+ note.(first_facilitator, "Minted from the Foundations training roster.", year.(7) + 2.days)
+
+ # ── 6 years ago: first membership year, paid ─────────────────────────────
+ subscription = person.memberships.create!
+ subscription.update_columns(created_at: year.(6), updated_at: year.(6))
+ track.("create", subscription, year.(6), { resource_title: "Membership" })
+
+ [ 6, 4, 3, 0 ].each_with_index do |years_ago, index|
+ invoice = subscription.membership_invoices.create!(
+ start_date: year.(years_ago), cost_cents: Membership::ANNUAL_COST_CENTS
+ )
+ invoice.update_columns(created_at: year.(years_ago), updated_at: year.(years_ago))
+
+ # MembershipInvoice isn't one of the person's tracked resources, so the renewal
+ # shows as an update to the membership itself.
+ unless index.zero?
+ track.("update", subscription, year.(years_ago),
+ { resource_title: "Membership" }.merge(changed.({ "membership_invoices" => [ index, index + 1 ] })))
+ end
+
+ next if index == 3 # current year left unpaid so the badge shows something owing
+
+ paid_at = year.(years_ago) + (index == 2 ? 70 : 9).days
+ payment = CashPayment.create!(
+ person: person, amount_cents: Membership::ANNUAL_COST_CENTS,
+ amount_cents_remaining: Membership::ANNUAL_COST_CENTS, currency: "usd"
+ )
+ payment.update_columns(created_at: paid_at, updated_at: paid_at)
+ Allocation.create!(source: payment, allocatable: invoice, amount: Membership::ANNUAL_COST_CENTS)
+ track.("create", payment, paid_at, { resource_title: "Membership dues #{year.(years_ago).year}" })
+ end
+
+ # ── 5 years ago: takes on a job title alongside the facilitator row ──────
+ job = Affiliation.create!(person: person, organization: second_org, title: "Program Coordinator")
+ track.("create", job, year.(5))
+ note.(job, "Took on the Program Coordinator role alongside facilitating.", year.(5))
+ note.(person, "Promoted internally — worth checking which affiliation should be primary.",
+ year.(5) + 2.days, topic: "Profile")
+
+ # ── 4 years ago: signs up for a refresher and doesn't show ───────────────
+ no_show_registration = training.("Facilitator Training: Refresher", year.(4), "no_show", second_org)
+ note.(no_show_registration, "Called the morning of to say they couldn't make it.",
+ year.(4) + 1.day, topic: "Attendance")
+ first_facilitator.update_columns(end_date: first_facilitator.start_date, inactive: true)
+ track.("update", first_facilitator, year.(4) + 5.days,
+ changed.({ "end_date" => [ nil, first_facilitator.start_date.to_s ], "inactive" => [ false, true ] }))
+ note.(first_facilitator, "Ended after the refresher no-show; reinstate if they complete a later training.",
+ year.(4) + 5.days)
+
+ # ── 2 years ago: completes a training again, affiliation comes back ──────
+ return_registration = training.("Facilitator Training: Trauma-Informed Practice", year.(2), "attended", second_org)
+ note.(return_registration, "Back after two years away; asked about co-facilitating.",
+ year.(2) + 1.day, topic: "Attendance")
+ first_facilitator.update_columns(end_date: nil, inactive: false)
+ track.("update", first_facilitator, year.(2) + 4.days,
+ changed.({ "end_date" => [ first_facilitator.start_date.to_s, nil ], "inactive" => [ true, false ] }))
+ note.(first_facilitator, "Reactivated after the Trauma-Informed Practice training.", year.(2) + 4.days)
+ email.("Your facilitator affiliation is active again",
+ "We've reactivated your facilitator affiliation following the training.", year.(2) + 4.days)
+
+ # ── 1 year ago onward: edits to the affiliation this seed hangs off ──────
+ track.("create", affiliation, affiliation.start_date.to_time + 10.hours)
+ note.(affiliation, "Joined the #{home_org&.name} roster.", affiliation.start_date.to_time + 10.hours)
+
+ track.("update", affiliation, 8.months.ago,
+ changed.({ "title" => [ "Facilitator", affiliation.title ] }))
+ track.("update", affiliation, 5.months.ago,
+ changed.({ "primary_contact" => [ false, true ] }))
+ note.(affiliation, "Now the primary contact for the organization.", 5.months.ago)
+ track.("update", affiliation, 2.months.ago,
+ changed.({ "start_date" => [ (affiliation.start_date + 1.month).to_s, affiliation.start_date.to_s ] }))
+ note.(affiliation, "Corrected the start date against the training roster.", 2.months.ago)
+ note.(person, "Confirmed the corrected dates by phone.", 6.weeks.ago, topic: "Profile")
+
+ puts " #{person.full_name}: #{person.event_registrations.count} registrations, " \
+ "#{person.affiliations.count} affiliations, #{subscription.membership_invoices.count} membership years, " \
+ "#{PersonCommentAggregator.new(person).comments.count} comments, " \
+ "#{Analytics::PersonActivityEvents.new(person).count} activity events"
+ end
+end
diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake
index 476d7fa35..78c7e1fe2 100644
--- a/lib/tasks/dev.rake
+++ b/lib/tasks/dev.rake
@@ -22,6 +22,7 @@ namespace :db do
payments
scholarships
membership
+ affiliation_history
bulk_payments
legacy_form_identifiers
public_forms
@@ -120,6 +121,11 @@ namespace :db do
load Rails.root.join("db/seeds/dev/membership.rb")
end
+ desc "Seed several years of trainings, memberships, comments and affiliation edits for one person (dev only)"
+ task affiliation_history: :environment do
+ load Rails.root.join("db/seeds/dev/affiliation_history.rb")
+ end
+
desc "Seed bulk payment demo submissions, payments, and allocations (dev only)"
task bulk_payments: :environment do
load Rails.root.join("db/seeds/dev/bulk_payments.rb")
diff --git a/spec/services/analytics/person_activity_events_spec.rb b/spec/services/analytics/person_activity_events_spec.rb
index 686648374..139eb60b7 100644
--- a/spec/services/analytics/person_activity_events_spec.rb
+++ b/spec/services/analytics/person_activity_events_spec.rb
@@ -44,6 +44,18 @@ def event(resource_type:, resource_id:, name: "update.record", properties: {})
expect(described_class.new(person).relation).to include(target)
end
+ it "includes payment events recorded under the STI subclass the tracker writes" do
+ payment = create(:payment, person: person, type: "CashPayment")
+ target = event(resource_type: "CashPayment", resource_id: payment.id, name: "create.payment")
+ expect(described_class.new(person).relation).to include(target)
+ end
+
+ it "includes events about comments on the person's affiliations" do
+ comment = create(:comment, commentable: create(:affiliation, person: person))
+ target = event(resource_type: "Comment", resource_id: comment.id, name: "create.comment")
+ expect(described_class.new(person).relation).to include(target)
+ end
+
it "includes events about the person's continuing education registrations" do
registration = create(:event_registration, registrant: person)
ce = create(:continuing_education_registration, event_registration: registration)
diff --git a/spec/services/person_comment_aggregator_spec.rb b/spec/services/person_comment_aggregator_spec.rb
index ae9a804ab..964157613 100644
--- a/spec/services/person_comment_aggregator_spec.rb
+++ b/spec/services/person_comment_aggregator_spec.rb
@@ -6,9 +6,12 @@
let(:person) { create(:person) }
describe "#comments" do
- it "gathers comments from the person, their registrations, scholarships, CE registrations, stories, story ideas, and user account" do
+ it "gathers comments from the person, their affiliations, registrations, scholarships, CE registrations, stories, story ideas, and user account" do
profile_comment = create(:comment, commentable: person)
+ affiliation = create(:affiliation, person: person)
+ affiliation_comment = create(:comment, commentable: affiliation)
+
registration = create(:event_registration, registrant: person)
registration_comment = create(:comment, commentable: registration)
@@ -30,8 +33,8 @@
user_comment = create(:comment, commentable: person.user)
expect(aggregator.comments).to contain_exactly(
- profile_comment, registration_comment, scholarship_comment, ce_comment, subscription_comment,
- story_comment, story_idea_comment, user_comment
+ profile_comment, affiliation_comment, registration_comment, scholarship_comment, ce_comment,
+ subscription_comment, story_comment, story_idea_comment, user_comment
)
end
From 4d94b667bf4274d3be96971e0486d696da45f0b4 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Wed, 19 Aug 2026 22:08:54 -0400
Subject: [PATCH 28/37] Let an explicitly supplied inactive flag survive a
later date edit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The Inactive checkbox only held until the next time anyone touched a date. The
guard tested `inactive_changed?`, which is false when a form re-submits the value
the record already holds, so the date rule ran and derived the flag away — an
unrelated start-date edit silently reactivated a row an admin had ended.
`inactive_supplied` records that a caller set the value on purpose. The standalone
editor always posts the checkbox, so the controller sets it from the params; the
nested rows set it when their end date changes. It is a cast writer because forms
send "0", which is truthy in Ruby and would otherwise suppress the date rule on
every nested row.
An end date of today or earlier now ticks the box for you. The date rule compares
strictly, so today alone still reads as active — the flag is what carries it.
Co-Authored-By: Claude Opus 5 (1M context)
---
app/controllers/affiliations_controller.rb | 4 +-
app/controllers/organizations_controller.rb | 4 +-
app/controllers/people_controller.rb | 4 +-
app/controllers/users_controller.rb | 2 +-
.../controllers/inactive_toggle_controller.js | 37 ++++++++++++++++++-
app/models/affiliation.rb | 15 +++++++-
app/views/affiliations/_fields.html.erb | 23 +++++++++---
spec/models/affiliation_spec.rb | 35 +++++++++++++++---
spec/requests/affiliations_spec.rb | 18 +++++++++
9 files changed, 122 insertions(+), 20 deletions(-)
diff --git a/app/controllers/affiliations_controller.rb b/app/controllers/affiliations_controller.rb
index f3694fe90..7f7f4875a 100644
--- a/app/controllers/affiliations_controller.rb
+++ b/app/controllers/affiliations_controller.rb
@@ -7,6 +7,8 @@ def edit
def update
authorize! @affiliation
+ # This form always posts the Inactive checkbox, so whatever it sends is deliberate.
+ @affiliation.inactive_supplied = affiliation_params.key?(:inactive)
@affiliation.assign_attributes(affiliation_params)
@affiliation.comments.select(&:new_record?).each { |c| c.created_by = current_user; c.updated_by = current_user }
@affiliation.comments.select { |c| c.persisted? && c.body_changed? }.each { |c| c.updated_by = current_user }
@@ -71,7 +73,7 @@ def affiliation_params
# Return to whichever edit page the gear was clicked from, scrolled to the row
# (or the affiliations section after a delete removes the row).
- def affiliation_return_path(anchor: helpers.dom_id(@affiliation))
+ def affiliation_return_path(anchor: @affiliation.decorate.return_anchor)
case params[:return_to]
when "person"
edit_person_path(params[:origin_id], anchor: anchor)
diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb
index bd3869339..4e73558f0 100644
--- a/app/controllers/organizations_controller.rb
+++ b/app/controllers/organizations_controller.rb
@@ -170,8 +170,7 @@ def set_form_variables
affiliations = affiliations.includes(:person) unless affiliations.loaded?
sorted = affiliations.to_a
.sort_by { |affiliation|
- expired = affiliation.inactive? || (affiliation.end_date.present? && affiliation.end_date < Date.current)
- [ expired ? 1 : 0,
+ [ affiliation.active? ? 0 : 1,
affiliation.person&.first_name.to_s.downcase,
affiliation.person&.last_name.to_s.downcase ]
}
@@ -252,6 +251,7 @@ def organization_params
:id,
:person_id,
:inactive,
+ :inactive_supplied,
:primary_contact,
:title,
:start_date,
diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb
index 4314137b9..7b779aba8 100644
--- a/app/controllers/people_controller.rb
+++ b/app/controllers/people_controller.rb
@@ -339,8 +339,7 @@ def set_form_variables
affiliations = affiliations.includes(:organization) unless affiliations.loaded?
sorted = affiliations.to_a
.sort_by { |affiliation|
- expired = affiliation.inactive? || (affiliation.end_date.present? && affiliation.end_date < Date.current)
- [ expired ? 1 : 0,
+ [ affiliation.active? ? 0 : 1,
affiliation.organization&.name.to_s.downcase ]
}
@person.affiliations.proxy_association.target.replace(sorted)
@@ -632,6 +631,7 @@ def person_params
:organization_id,
:title,
:inactive,
+ :inactive_supplied,
:primary_contact,
:start_date,
:end_date,
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index beb31a217..adf1af5c7 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -429,7 +429,7 @@ def user_params
#####
comments_attributes: [ :id, :topic, :body, :flagged, :_destroy ],
- affiliations_attributes: [ :id, :organization_id, :position, :title, :inactive, :primary_contact, :start_date, :end_date, :_destroy ],
+ affiliations_attributes: [ :id, :organization_id, :position, :title, :inactive, :inactive_supplied, :primary_contact, :start_date, :end_date, :_destroy ],
)
end
end
diff --git a/app/frontend/javascript/controllers/inactive_toggle_controller.js b/app/frontend/javascript/controllers/inactive_toggle_controller.js
index 5dad1705e..8be0de170 100644
--- a/app/frontend/javascript/controllers/inactive_toggle_controller.js
+++ b/app/frontend/javascript/controllers/inactive_toggle_controller.js
@@ -5,18 +5,47 @@ import { Controller } from "@hotwired/stimulus";
// is the saturation (active = full, inactive = super-light). Inactive rows also
// strike their fields (.aff-ended).
export default class extends Controller {
- static targets = ["endDate", "title", "row", "accentBar", "valueField"]
+ static targets = ["endDate", "title", "row", "accentBar", "valueField", "inactiveField", "suppliedField", "inactiveCheckbox"]
static values = { expired: Boolean }
connect() {
+ // A row flagged inactive whose dates still read as current is one where the
+ // flag is doing real work, so mark it authoritative up front — otherwise an
+ // unrelated date edit would let the server re-derive it away.
+ if (this.expiredValue && !this.endsOnOrBeforeToday()) this.markSupplied();
if (this.hasTitleTarget) this.updateBorder();
else this.apply();
}
+ // Entering an end date of today or earlier ticks Inactive for you, so the flag
+ // travels with the form — the date rule alone compares strictly and would still
+ // call today "active". Clearing the date (or a future one) unticks it again.
+ //
+ // Only the end date drives this. Ticking the box by hand has to stick, which it
+ // would not if the checkbox's own action recomputed it from the dates.
+ endDateChanged() {
+ const ended = this.endsOnOrBeforeToday();
+ if (this.hasInactiveCheckboxTarget) this.inactiveCheckboxTarget.checked = ended;
+ if (this.hasInactiveFieldTarget) this.inactiveFieldTarget.value = ended ? "1" : "0";
+ this.markSupplied();
+ this.apply();
+ }
+
toggle() {
this.apply();
}
+ markSupplied() {
+ if (this.hasSuppliedFieldTarget) this.suppliedFieldTarget.value = "1";
+ }
+
+ endsOnOrBeforeToday() {
+ const value = this.hasEndDateTarget ? this.endDateTarget.value : "";
+ if (!value) return false;
+
+ return new Date(value) <= new Date(new Date().toDateString());
+ }
+
updateBorder() {
if (!this.hasTitleTarget) return;
if (this.hasAccentBarTarget) {
@@ -87,6 +116,12 @@ export default class extends Controller {
// With an end date, compute from it (live); without one, the JS can't see the
// server's inactive flag, so trust the server-rendered `expired` value.
isPast() {
+ // The standalone editor has an explicit Inactive checkbox, and on that form it
+ // is the whole truth: ticked, or ended on/before today.
+ if (this.hasInactiveCheckboxTarget) {
+ return this.inactiveCheckboxTarget.checked || this.endsOnOrBeforeToday();
+ }
+
const value = this.hasEndDateTarget ? this.endDateTarget.value : "";
if (value) return new Date(value) < new Date(new Date().toDateString());
return this.expiredValue;
diff --git a/app/models/affiliation.rb b/app/models/affiliation.rb
index 321215825..050027d1d 100644
--- a/app/models/affiliation.rb
+++ b/app/models/affiliation.rb
@@ -21,6 +21,17 @@ class Affiliation < ApplicationRecord
# have this link.
belongs_to :event_registration, optional: true, inverse_of: :affiliations
+ # Set by a caller that supplied `inactive` deliberately (the standalone editor's
+ # checkbox, or a nested row whose end date the admin just changed). Re-submitting
+ # the value it already holds isn't a change, so without this the date rule below
+ # would quietly undo a hand-set flag on the next date edit. Cast because it
+ # arrives from a form as "0"/"1", and "0" is truthy in Ruby.
+ attr_reader :inactive_supplied
+
+ def inactive_supplied=(value)
+ @inactive_supplied = ActiveModel::Type::Boolean.new.cast(value)
+ end
+
has_many :comments, -> { newest_first }, as: :commentable, dependent: :destroy
accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attrs| attrs["body"].blank? }
@@ -44,7 +55,7 @@ class Affiliation < ApplicationRecord
# when a view must reflect a fixed point in time — e.g. the event dashboard
# reporting organizations as they stood at the time of the event, so the
# numbers don't drift as affiliations end after the fact.
- scope :active_on, ->(date) {
+ scope :active_by_date_on, ->(date) {
where("affiliations.start_date IS NULL OR affiliations.start_date <= ?", date)
.where("affiliations.end_date IS NULL OR affiliations.end_date >= ?", date)
}
@@ -166,7 +177,7 @@ def sole_address_id_for_new_organization
# An explicit assignment wins: the date rule alone still reads a row ending today
# or later as active.
def set_inactive_from_dates
- return if inactive_changed?
+ return if inactive_changed? || inactive_supplied
return unless end_date_changed? || start_date_changed?
self.inactive = end_date.present? && end_date < Date.current
diff --git a/app/views/affiliations/_fields.html.erb b/app/views/affiliations/_fields.html.erb
index 9e51b1442..976fcccb1 100644
--- a/app/views/affiliations/_fields.html.erb
+++ b/app/views/affiliations/_fields.html.erb
@@ -5,7 +5,7 @@
<% person_side = counterpart == :person %>
<% manage_subject = person_side ? Organization : Person %>
<% if allowed_to?(:manage?, manage_subject) %>
- <% expired = f.object.inactive? || (f.object.end_date.present? && f.object.end_date < Date.current) %>
+ <% expired = !f.object.active? %>
<%# A blank title displays (and saves) as the "Facilitator" default, so treat it
as a facilitator for the row styling too — otherwise the JS (which reads the
shown title) tints the row purple while the server-rendered pill stays neutral. %>
@@ -48,6 +48,10 @@
id="<%= dom_id(f.object) %>"<% end %>
data-inactive-toggle-target="row">
+ <%# Carries the inactive flag the row's end date implies. `inactive_supplied`
+ tells the model this value is deliberate, so it isn't re-derived away. %>
+ <%= f.hidden_field :inactive, data: { inactive_toggle_target: "inactiveField" } %>
+ <%= f.hidden_field :inactive_supplied, value: "0", data: { inactive_toggle_target: "suppliedField" } %>
@@ -138,13 +142,22 @@
<%= render "affiliations/primary_contact_toggle", f: f %>
<% affiliation_comments = f.object.comments.to_a %>
<% if affiliation_comments.any? %>
-
-
+ <%# Opens in a new tab like the gear above it — this row sits in an
+ unsaved form, so navigating away in place would drop the edits. %>
+ <%= link_to edit_affiliation_path(f.object,
+ return_to: person_side ? "organization" : "person",
+ origin_id: person_side ? f.object.organization_id : f.object.person_id,
+ anchor: "comments-section"),
+ target: "_blank", rel: "noopener",
+ title: "Read and edit these comments (opens in a new tab)",
+ class: "group relative inline-flex items-center" do %>
+
<%= pluralize(affiliation_comments.size, "comment") %><%= truncate(affiliation_comments.first.body.to_s, length: 140) %>
+ Open to read them all →
-
+ <% end %>
<% end %>
diff --git a/spec/models/affiliation_spec.rb b/spec/models/affiliation_spec.rb
index 5bb50b6da..e7c730197 100644
--- a/spec/models/affiliation_spec.rb
+++ b/spec/models/affiliation_spec.rb
@@ -192,7 +192,7 @@
end
end
- describe '.active_on' do
+ describe '.active_by_date_on' do
let(:date) { Date.new(2024, 6, 1) }
let!(:spanning) { create(:affiliation, start_date: Date.new(2023, 1, 1), end_date: Date.new(2025, 1, 1)) }
let!(:open_ended) { create(:affiliation, start_date: Date.new(2023, 1, 1), end_date: nil) }
@@ -201,24 +201,24 @@
let!(:no_dates) { create(:affiliation, start_date: nil, end_date: nil) }
it 'includes affiliations whose span covers the date' do
- expect(described_class.active_on(date)).to include(spanning, open_ended)
+ expect(described_class.active_by_date_on(date)).to include(spanning, open_ended)
end
it 'excludes affiliations that ended before the date' do
- expect(described_class.active_on(date)).not_to include(ended_before)
+ expect(described_class.active_by_date_on(date)).not_to include(ended_before)
end
it 'excludes affiliations that start after the date' do
- expect(described_class.active_on(date)).not_to include(starts_after)
+ expect(described_class.active_by_date_on(date)).not_to include(starts_after)
end
it 'includes affiliations with no dates on record' do
- expect(described_class.active_on(date)).to include(no_dates)
+ expect(described_class.active_by_date_on(date)).to include(no_dates)
end
it 'ignores the cached inactive flag, judging purely by dates' do
flagged = create(:affiliation, start_date: Date.new(2023, 1, 1), end_date: nil, inactive: true)
- expect(described_class.active_on(date)).to include(flagged)
+ expect(described_class.active_by_date_on(date)).to include(flagged)
end
end
@@ -298,6 +298,29 @@
expect(op.reload.inactive).to be true
expect(op).not_to be_active
end
+
+ it 'treats a form\'s "0" as not supplied, so the dates still derive' do
+ op.inactive_supplied = "0"
+ op.update!(end_date: 1.day.ago.to_date)
+
+ expect(op.reload.inactive).to be true
+ end
+
+ it 'honours an end date of today when the form supplies the flag' do
+ op.inactive_supplied = "1"
+ op.update!(end_date: Date.current, inactive: "1")
+
+ expect(op.reload).not_to be_active
+ end
+
+ it 'keeps a hand-set flag when a later edit resubmits it alongside a new date' do
+ op.update!(start_date: Date.current, inactive: true)
+
+ op.inactive_supplied = true
+ op.update!(start_date: 1.month.ago.to_date, inactive: true)
+
+ expect(op.reload.inactive).to be true
+ end
end
describe "reassigning the organization" do
diff --git a/spec/requests/affiliations_spec.rb b/spec/requests/affiliations_spec.rb
index 7d8e32356..3dc2296bb 100644
--- a/spec/requests/affiliations_spec.rb
+++ b/spec/requests/affiliations_spec.rb
@@ -89,6 +89,24 @@
expect(affiliation.reload).not_to be_active
end
+
+ it "keeps the ended state when a later edit changes a date with the box still ticked" do
+ affiliation.update!(start_date: Date.current, end_date: nil, inactive: true)
+
+ patch affiliation_path(affiliation, return_to: "organization", origin_id: organization.id),
+ params: { affiliation: { start_date: 1.month.ago.to_date.to_s, inactive: "1" } }
+
+ expect(affiliation.reload).not_to be_active
+ end
+
+ it "still derives the flag from the dates when the form omits it" do
+ affiliation.update!(start_date: 1.year.ago.to_date, end_date: nil, inactive: false)
+
+ patch affiliation_path(affiliation, return_to: "organization", origin_id: organization.id),
+ params: { affiliation: { end_date: 1.day.ago.to_date.to_s } }
+
+ expect(affiliation.reload).not_to be_active
+ end
end
context "as a non-admin" do
From 540f3471df22f57889a8d08d5e0f69ddb0c8671d Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Wed, 19 Aug 2026 22:09:10 -0400
Subject: [PATCH 29/37] Stop reconciliation rewriting history it did not create
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Deactivating same-dayed every row, including ones minted years earlier by a
different training. That erased the whole period a person actually facilitated,
and the anchored program status moved with it: an organization that read Ongoing
at its 2026 training read Reinstated afterwards, changing figures that back grant
applications.
Only the row this training minted collapses to its start date — it recorded an
assumption that never came true, and a strict `<` already excludes it from its own
anchor. Anything older ends on this training's date instead, so the years before
it survive.
Reactivation had the mirror problem: clearing an end date swallowed the gap, so
"Art program since" collapsed `Jan 2023 – Jan 2024, Aug 2026` into `Jan 2023`. A
return is now a new row, which is what CreateFromRegistration has always done, so
`:reactivate` is gone entirely.
Why a row changed is recorded as a comment on the affiliation rather than a new
column — the edit page already surfaces them, and the comment topic is enough to
stop labelling an admin-ended row "didn't attend".
Co-Authored-By: Claude Opus 5 (1M context)
---
.../affiliation_services/reconcile_event.rb | 2 +-
.../affiliation_services/reconcile_person.rb | 70 +++++++++--
.../reconcile_affiliations/_tooltip.html.erb | 11 +-
.../reconcile_affiliations/confirm.html.erb | 11 +-
.../reconcile_affiliations/index.html.erb | 17 ++-
.../reconcile_person_spec.rb | 111 ++++++++++++++++--
6 files changed, 190 insertions(+), 32 deletions(-)
diff --git a/app/services/affiliation_services/reconcile_event.rb b/app/services/affiliation_services/reconcile_event.rb
index a2cf5ffa1..6d3b52418 100644
--- a/app/services/affiliation_services/reconcile_event.rb
+++ b/app/services/affiliation_services/reconcile_event.rb
@@ -44,7 +44,7 @@ def any_rows?
Change = Struct.new(:person, :organization, :affiliation, :action, keyword_init: true)
# One radio choice per row: the action itself, or "keep".
- ACTION_FOR_CHOICE = { "deactivate" => :deactivate, "delete" => :delete, "reactivate" => :reactivate, "create" => :create }.freeze
+ ACTION_FOR_CHOICE = { "deactivate" => :deactivate, "delete" => :delete, "create" => :create }.freeze
# What the `{ row.key => choice }` map will change, for the confirmation screen.
def planned_changes(outcome:)
diff --git a/app/services/affiliation_services/reconcile_person.rb b/app/services/affiliation_services/reconcile_person.rb
index be1bb02af..1ca4ccb62 100644
--- a/app/services/affiliation_services/reconcile_person.rb
+++ b/app/services/affiliation_services/reconcile_person.rb
@@ -20,6 +20,11 @@ def actionable?
ACTIVE_ATTENDED = "Active — attended".freeze
TRAINING_PENDING = "Training hasn't ended yet".freeze
ALREADY_DEACTIVATED = "Already deactivated — didn't attend".freeze
+ ALREADY_ENDED = "Already ended — not by reconciliation".freeze
+ LAPSED = "Ended — a return is recorded as a new affiliation".freeze
+ # Topic on the comment reconciliation leaves behind, so a row can say why it
+ # ended without a dedicated column (ADR-0002 D6b).
+ COMMENT_TOPIC = "Reconciliation".freeze
NOT_ATTENDED = "Didn't attend — no affiliation created".freeze
def self.call(person:, organization:, event:, registration: nil, include_unowned: false)
@@ -45,10 +50,9 @@ def perform(action, affiliation: nil)
return false if affiliation.nil? && action != :create
case action
- when :create then create_affiliation
+ when :create then create_and_note
when :delete then affiliation.destroy!
- when :deactivate then affiliation.update!(end_date: affiliation.start_date || Date.current, inactive: true)
- when :reactivate then affiliation.update!(end_date: nil, inactive: false)
+ when :deactivate then deactivate(affiliation)
else return false
end
true
@@ -72,11 +76,50 @@ def completed_training?
.exists?
end
+ def deactivate(affiliation)
+ ends_on = deactivation_end_date(affiliation)
+ affiliation.update!(end_date: ends_on, inactive: true)
+ note(affiliation, "Ended #{ends_on.strftime('%b %-d, %Y')} and marked inactive by reconciliation " \
+ "for #{@event.title} — no attended facilitator training for #{@organization.name} on record.")
+ end
+
+ def create_and_note
+ created = @person.affiliations.facilitators.where(organization: @organization).pluck(:id)
+ create_affiliation
+ fresh = @person.affiliations.facilitators.where(organization: @organization).where.not(id: created)
+ fresh.each { |affiliation| note(affiliation, "Created by reconciliation for #{@event.title}.") }
+ end
+
+ # Why a row changed, on the affiliation's own comments rather than a dedicated
+ # column — the edit page and its history already surface them (ADR-0002 D6b).
+ def note(affiliation, body)
+ affiliation.comments.create!(topic: COMMENT_TOPIC, body: body,
+ created_by: Current.user, updated_by: Current.user)
+ end
+
+ def ended_by_reconciliation?(affiliation)
+ affiliation.comments.any? { |comment| comment.topic == COMMENT_TOPIC }
+ end
+
+ def minted_here?(affiliation)
+ affiliation.event_registration&.event_id == @event.id
+ end
+
+ # Where a deactivation ends the row (ADR-0002 D6). The row this training minted
+ # is an assumption that never came true, so it collapses to nothing. Any older
+ # row records facilitation that really happened: it ends at this training, so
+ # the years before it survive and anchored program status doesn't move.
+ def deactivation_end_date(affiliation)
+ return affiliation.start_date if minted_here?(affiliation) && affiliation.start_date
+
+ [ @event.start_date&.to_date || Date.current, affiliation.start_date ].compact.max
+ end
+
# A non-training event confers no facilitation, so it only removes what was
# auto-created off it.
def non_training_plan
facilitator_affiliations.filter_map do |affiliation|
- next unless affiliation.event_registration&.event_id == @event.id
+ next unless minted_here?(affiliation)
Decision.new(affiliation:, action: :delete)
end
@@ -84,17 +127,28 @@ def non_training_plan
def training_plan
decisions = reconcilable_affiliations.map { |affiliation| classify(affiliation) }
- decisions << creation_decision if facilitator_affiliations.empty? && @registration
+ decisions << creation_decision if needs_affiliation?
decisions
end
+ # Someone with no active facilitator affiliation needs one when they have never
+ # had one, or when they completed a training here and are returning after a
+ # lapse — the return is a NEW row, never a resurrected one (ADR-0002 D6a).
+ def needs_affiliation?
+ return false unless @registration
+ return false if facilitator_affiliations.any?(&:active?)
+
+ facilitator_affiliations.empty? || completed_training?
+ end
+
def classify(affiliation)
if completed_training?
return Decision.new(affiliation:, action: :noop, reason: ACTIVE_ATTENDED) if affiliation.active?
- Decision.new(affiliation:, action: :reactivate)
+ Decision.new(affiliation:, action: :noop, reason: LAPSED)
elsif !affiliation.active?
- Decision.new(affiliation:, action: :noop, reason: ALREADY_DEACTIVATED)
+ reason = ended_by_reconciliation?(affiliation) ? ALREADY_DEACTIVATED : ALREADY_ENDED
+ Decision.new(affiliation:, action: :noop, reason:)
elsif deactivation_ready?(affiliation)
Decision.new(affiliation:, action: :deactivate)
else
@@ -130,7 +184,7 @@ def reconcilable_affiliations
def facilitator_affiliations
@facilitator_affiliations ||= @person.affiliations.facilitators
.where(organization: @organization)
- .includes(event_registration: :event)
+ .includes(:comments, event_registration: :event)
.to_a
end
end
diff --git a/app/views/events/reconcile_affiliations/_tooltip.html.erb b/app/views/events/reconcile_affiliations/_tooltip.html.erb
index 662cc5247..08d09c2a8 100644
--- a/app/views/events/reconcile_affiliations/_tooltip.html.erb
+++ b/app/views/events/reconcile_affiliations/_tooltip.html.erb
@@ -1,7 +1,12 @@
-
+
<% case kind %>
<% when :deactivate %>
- Ends this facilitator affiliation — its end date is set to its own start date and it's marked inactive — so it no longer counts as active. The record is kept and reactivates if the person is later marked attended.
+
Ends this facilitator affiliation and marks it inactive, so it no longer counts as active. The record is kept — if the person is later marked attended, the return is recorded as a new affiliation rather than by reopening this one.
+
The end date depends on where the affiliation came from:
+
+
Created by this training — set to its own start date, since the person never became a facilitator
+
Any older affiliation — set to this training's date, so the years they did facilitate stay on the record
+
<% when :delete %>
Permanently deletes this facilitator affiliation. Everything else stays as-is:
@@ -10,8 +15,6 @@
<% when :create %>
Creates the facilitator affiliation for this organization.
- <% when :reactivate %>
- Clears the end date so this facilitator affiliation counts as active again.
<% when :keep %>
Leaves this facilitator affiliation exactly as it is — no change.
<% end %>
diff --git a/app/views/events/reconcile_affiliations/confirm.html.erb b/app/views/events/reconcile_affiliations/confirm.html.erb
index dc2785ea5..ea676ecec 100644
--- a/app/views/events/reconcile_affiliations/confirm.html.erb
+++ b/app/views/events/reconcile_affiliations/confirm.html.erb
@@ -1,19 +1,18 @@
<% content_for(:page_title, "Confirm affiliation changes — #{@event.title}") %>
<% content_for(:page_bg_class, "admin-or-owner bg-blue-100") %>
-
+
<%= link_to "← Go back to edit", reconcile_affiliations_event_path(@event, outcome: @outcome), class: "text-sm text-gray-500 hover:text-gray-700" %>
-
Confirm affiliation changes
-
+
Confirm affiliation changes
+
Performing will make the <%= @changes.size %> <%= "change".pluralize(@changes.size) %> below. Nothing else is affected.
<% sections = {
create: [ "Create", "bg-blue-50 text-blue-800 border-blue-200", "A new facilitator affiliation is created for this organization." ],
- reactivate: [ "Reactivate", "bg-green-50 text-green-800 border-green-200", "The end date is cleared so the facilitator affiliation is active again." ],
- deactivate: [ "Deactivate", "bg-red-50 text-red-800 border-red-200", "The facilitator affiliation is same-dayed (end date set to its start date) and marked inactive. Reversible." ],
+ deactivate: [ "Deactivate", "bg-red-50 text-red-800 border-red-200", "The facilitator affiliation is marked inactive and given an end date — its own start date if this training created it, otherwise this training's date, so earlier facilitating stays on the record. Reversible." ],
delete: [ "Delete", "bg-red-100 text-red-900 border-red-300", "The facilitator affiliation is permanently deleted. Job and other-org affiliations are untouched." ]
} %>
@@ -21,7 +20,7 @@
<% sections.each do |action, (label, header_class, description)| %>
<% action_changes = @changes.select { |change| change.action == action } %>
<% next if action_changes.empty? %>
-
+
This step brings facilitator affiliations in line with who registered and attended. Before the training it
creates any missing facilitator affiliations for linked organizations. After the training it
- same-days the affiliation of anyone who didn't attend, and reactivates anyone later marked
- attended. Job affiliations are never touched.
+ ends the affiliation of anyone who didn't attend and marks it inactive. Someone later marked
+ attended gets a new affiliation dated to this training rather than having the old one reopened,
+ so a lapse stays visible. Job affiliations are never touched.
+
+
+ Ending never erases history: an affiliation this training created is same-dayed, while an older one ends on this
+ training's date, so the period the person really facilitated — and this organization's program status at every
+ earlier training — stays as it was.
<% else %>
@@ -33,7 +39,11 @@
<% unless @has_rows %>
- No registrants have linked an organization, so there's nothing to reconcile.
+ <% if @event.facilitator_training? %>
+ No registrants have linked an organization, so there's nothing to reconcile.
+ <% else %>
+ No facilitator affiliations were created from this event, so there's nothing to remove.
+ <% end %>
<% end %>
@@ -47,7 +57,6 @@
} %>
<% outcome_options = {
create: [ [ "create", "Will be created", :blue ], [ "keep", "Skip", :gray ] ],
- reactivate: [ [ "reactivate", "Will be reactivated", :green ], [ "keep", "Leave inactive", :gray ] ],
deactivate: [ [ "keep", "Keep active", :green ], [ "delete", "Delete affiliation", :red ], [ "deactivate", "Deactivate affiliation", :red ] ],
delete: [ [ "delete", "Will be deleted", :red ], [ "keep", "Keep", :gray ] ]
} %>
diff --git a/spec/services/affiliation_services/reconcile_person_spec.rb b/spec/services/affiliation_services/reconcile_person_spec.rb
index c6b404a4a..0b3fd0022 100644
--- a/spec/services/affiliation_services/reconcile_person_spec.rb
+++ b/spec/services/affiliation_services/reconcile_person_spec.rb
@@ -13,12 +13,14 @@ def training_registration(status:, ended: true)
end
# A "Facilitator" affiliation for (person, organization) owned by `registration`.
- def owned_facilitator(registration:, start_date: 1.month.ago.to_date)
+ # Defaults to the training's own date, which is what the registration flow sets
+ # (ADR-0001 D8) and what makes it "the row this training minted" (ADR-0002 D6).
+ def owned_facilitator(registration:, start_date: nil)
create(:affiliation,
person: person,
organization: organization,
title: "Facilitator",
- start_date: start_date,
+ start_date: start_date || registration.event.start_date.to_date,
event_registration: registration)
end
@@ -92,6 +94,72 @@ def reconcile(registration, **options)
expect(hand_created.reload).not_to be_active
end
+
+ it "ends an older affiliation at the training, keeping the years it really facilitated" do
+ reg = training_registration(status: "no_show")
+ started_on = 2.years.ago.to_date
+ hand_created = create(:affiliation, person: person, organization: organization,
+ title: "Facilitator", start_date: started_on)
+
+ reconcile(reg, include_unowned: true)
+
+ expect(hand_created.reload.end_date).to eq(reg.event.start_date.to_date)
+ expect(hand_created.start_date).to eq(started_on)
+ end
+
+ it "same-days an older affiliation that starts after the training rather than ending it before it began" do
+ reg = training_registration(status: "no_show")
+ later = create(:affiliation, person: person, organization: organization,
+ title: "Facilitator", start_date: Date.current)
+
+ reconcile(reg, include_unowned: true)
+
+ expect(later.reload.end_date).to eq(later.start_date)
+ end
+ end
+
+ describe "the comment reconciliation leaves behind" do
+ it "records why a row was ended, and who did it" do
+ user = create(:user, :admin)
+ Current.user = user
+ reg = training_registration(status: "no_show")
+ affiliation = owned_facilitator(registration: reg)
+
+ reconcile(reg)
+
+ comment = affiliation.reload.comments.last
+ expect(comment.topic).to eq(described_class::COMMENT_TOPIC)
+ expect(comment.body).to include("marked inactive by reconciliation")
+ expect(comment.body).to include(reg.event.title)
+ expect(comment.created_by).to eq(user)
+ ensure
+ Current.user = nil
+ end
+
+ it "records why a returning facilitator's new row appeared" do
+ create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: Date.new(2023, 1, 1), end_date: Date.new(2024, 1, 1))
+ reg = training_registration(status: "attended")
+
+ described_class.call(person: person, organization: organization, event: reg.event,
+ registration: reg, include_unowned: true)
+
+ fresh = person.affiliations.facilitators.active.where(organization: organization).last
+ expect(fresh.comments.last.body).to include("Created by reconciliation")
+ end
+
+ it "distinguishes a row it ended from one an admin ended" do
+ reg = training_registration(status: "no_show")
+ admin_ended = create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: 3.years.ago.to_date, end_date: 2.years.ago.to_date)
+
+ plan = described_class.new(person: person, organization: organization, event: reg.event,
+ registration: reg, include_unowned: true).plan
+
+ expect(plan.map(&:reason)).to include(described_class::ALREADY_ENDED)
+ expect(plan.map(&:reason)).not_to include(described_class::ALREADY_DEACTIVATED)
+ expect(admin_ended.reload.end_date).to eq(2.years.ago.to_date)
+ end
end
describe "keeping / activating" do
@@ -115,16 +183,41 @@ def reconcile(registration, **options)
expect(affiliation.reload).to be_active
end
- it "reactivates a previously same-day'd affiliation once the person is marked attended" do
+ it "records a return as a NEW affiliation, leaving the ended one ended" do
reg = training_registration(status: "attended")
- affiliation = owned_facilitator(registration: reg, start_date: 1.month.ago.to_date)
- affiliation.update!(end_date: affiliation.start_date)
- expect(affiliation.reload).not_to be_active
+ ended = owned_facilitator(registration: reg, start_date: 1.month.ago.to_date)
+ ended.update!(end_date: ended.start_date)
+ expect(ended.reload).not_to be_active
- reconcile(reg)
+ expect { described_class.call(person: person, organization: organization, event: reg.event, registration: reg) }
+ .to change { person.affiliations.facilitators.where(organization: organization).count }.by(1)
- expect(affiliation.reload).to be_active
- expect(affiliation.end_date).to be_nil
+ expect(ended.reload.end_date).to eq(ended.start_date)
+ expect(person.affiliations.facilitators.active.where(organization: organization).count).to eq(1)
+ end
+
+ it "keeps the lapse visible instead of swallowing it into one unbroken stretch" do
+ lapsed = create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: Date.new(2023, 1, 1), end_date: Date.new(2024, 1, 1))
+ reg = training_registration(status: "attended")
+
+ described_class.call(person: person, organization: organization, event: reg.event,
+ registration: reg, include_unowned: true)
+
+ expect(lapsed.reload.end_date).to eq(Date.new(2024, 1, 1))
+ expect(organization.reload.facilitator_status_on(Date.new(2025, 1, 1))).to eq(:reinstated)
+ end
+
+ it "plans no action on a lapsed row, explaining why" do
+ create(:affiliation, person: person, organization: organization, title: "Facilitator",
+ start_date: Date.new(2023, 1, 1), end_date: Date.new(2024, 1, 1))
+ reg = training_registration(status: "attended")
+
+ plan = described_class.new(person: person, organization: organization, event: reg.event,
+ registration: reg, include_unowned: true).plan
+
+ expect(plan.map(&:action)).to contain_exactly(:noop, :create)
+ expect(plan.map(&:reason)).to include(described_class::LAPSED)
end
end
From 5b95df9f48b8b6c63a34e6cd98e21129bbfd41da Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Wed, 19 Aug 2026 22:09:10 -0400
Subject: [PATCH 30/37] Judge an organization active by its affiliations, never
the legacy status column
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
`Organization.active` counted a stored "Active" status as enough on its own, and
`#published?` short-circuited on it before ever looking at affiliations — so an
org whose column had drifted read as active with nobody facilitating there. ADR-0001
D3 says the column plays no part; these two were the exceptions.
Also replaces nine open-coded copies of `!inactive? && (end_date.nil? || end_date
>= today)` with `active?`. The rule now lives in one place, which matters more now
that the flag can disagree with the dates.
Expect orgs with a stale "Active" column and no active affiliation to start
rendering as unpublished. That is the drift ADR-0001 D3a warns about, surfaced
rather than introduced.
Co-Authored-By: Claude Opus 5 (1M context)
---
app/controllers/events_controller.rb | 2 +-
app/models/organization.rb | 17 ++++++------
app/views/event_registrations/_form.html.erb | 2 +-
.../_org_affiliation_pills.html.erb | 3 +--
.../organizations_results.html.erb | 3 ++-
app/views/people/people_results.html.erb | 26 +++++++++----------
..._affiliation_organization_buttons.html.erb | 2 +-
.../_affiliation_person_buttons.html.erb | 2 +-
8 files changed, 28 insertions(+), 29 deletions(-)
diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb
index 3cd7e9e08..8866f7f80 100644
--- a/app/controllers/events_controller.rb
+++ b/app/controllers/events_controller.rb
@@ -1098,7 +1098,7 @@ def event_registrations_csv_string
def event_registration_csv_row(registration, cost_required, include_ce = false)
person = registration.registrant
orgs = person.affiliations
- .select { |a| !a.inactive? && (a.end_date.nil? || a.end_date >= Date.current) }
+ .select(&:active?)
.map(&:organization).compact.uniq
org_names = orgs.map(&:name).join("; ")
total_cents = registration.allocations_sum
diff --git a/app/models/organization.rb b/app/models/organization.rb
index 0302c9bf9..c9dfc1df0 100644
--- a/app/models/organization.rb
+++ b/app/models/organization.rb
@@ -79,11 +79,9 @@ def self.awbw
# Scopes
# See TagFilterable, Trendable, WindowsTypeFilterable
- scope :active, -> {
- status_active = joins(:organization_status).where(organization_statuses: { name: "Active" })
- affiliation_active = where(id: Affiliation.active.select(:organization_id))
- status_active.or(affiliation_active)
- }
+ # An org is active because someone is affiliated there, not because the legacy
+ # status column says so (ADR-0001 D3, ADR-0002 D4).
+ scope :active, -> { where(id: Affiliation.active.select(:organization_id)) }
scope :address, ->(address) do
return all if address.blank?
terms = address.to_s.strip.split(/[\s,]+/).reject(&:blank?)
@@ -249,10 +247,11 @@ def organization_locality
end
end
- def published? # needed for my_bookmarks
- return true if organization_status&.name == "Active"
- # #active? is the in-memory twin of the `active` scope, so a list page that
- # preloaded affiliations doesn't query once per row.
+ # Needed for my_bookmarks. Keys off affiliations only — the stored
+ # organization_status has drifted and is never consulted (ADR-0002 D4).
+ # The loaded branch is the in-memory twin of the `active` scope, so a list page
+ # that preloaded affiliations doesn't query once per row.
+ def published?
return affiliations.any?(&:active?) if affiliations.loaded?
affiliations.active.exists?
diff --git a/app/views/event_registrations/_form.html.erb b/app/views/event_registrations/_form.html.erb
index 1701f03f8..b4d63c237 100644
--- a/app/views/event_registrations/_form.html.erb
+++ b/app/views/event_registrations/_form.html.erb
@@ -243,7 +243,7 @@
<% show_ce = f.object.event&.ce_eligible? %>
<% org_span = 1 + (show_scholarship ? 0 : 1) + (show_ce ? 0 : 1) %>
<% org_span_class = { 1 => "sm:col-span-1", 2 => "sm:col-span-2", 3 => "sm:col-span-3" }.fetch(org_span) %>
- <% active_orgs = f.object.registrant.affiliations.select { |a| !a.inactive? && (a.end_date.nil? || a.end_date >= Date.current) }.map(&:organization).compact.uniq.sort_by(&:name) %>
+ <% active_orgs = f.object.registrant.affiliations.select(&:active?).map(&:organization).compact.uniq.sort_by(&:name) %>
<% connected_org_ids = f.object.organizations.map(&:id) %>
<% addable_orgs = active_orgs.reject { |org| connected_org_ids.include?(org.id) } %>
<%= locked_fieldset(locked) do %>
diff --git a/app/views/event_registrations/_org_affiliation_pills.html.erb b/app/views/event_registrations/_org_affiliation_pills.html.erb
index fd6c5d78c..79b3a1484 100644
--- a/app/views/event_registrations/_org_affiliation_pills.html.erb
+++ b/app/views/event_registrations/_org_affiliation_pills.html.erb
@@ -2,13 +2,12 @@
when the person has no affiliation for the org.
Locals: org, affiliations, submitted_org_name, submitted_position, neutral (optional) %>
<% if affiliations.any? %>
- <% active = ->(a) { !a.inactive? && (a.end_date.nil? || a.end_date >= Date.current) } %>
<% position = submitted_position.to_s.strip %>
<% is_submitted_org = submitted_org_name.to_s.strip.present? && org.name.to_s.strip.casecmp?(submitted_org_name.to_s.strip) %>
No <%= Person.model_name.human.pluralize.downcase %> found.
<% end %>
diff --git a/app/views/shared/_affiliation_organization_buttons.html.erb b/app/views/shared/_affiliation_organization_buttons.html.erb
index c6f79a829..3c59fb0dc 100644
--- a/app/views/shared/_affiliation_organization_buttons.html.erb
+++ b/app/views/shared/_affiliation_organization_buttons.html.erb
@@ -5,7 +5,7 @@
<% include_inactive = local_assigns.fetch(:include_inactive, false) %>
<% all_affiliations = affiliations.select { |a| a.organization.present? } %>
<% active_affiliations = all_affiliations
- .select { |a| !a.inactive? && (a.end_date.nil? || a.end_date >= Date.current) }
+ .select(&:active?)
.sort_by { |a| a.organization.name.to_s.downcase } %>
<% inactive_affiliations = include_inactive ?
(all_affiliations - active_affiliations).sort_by { |a| a.organization.name.to_s.downcase } : [] %>
diff --git a/app/views/shared/_affiliation_person_buttons.html.erb b/app/views/shared/_affiliation_person_buttons.html.erb
index 2942b1a72..8b985aa07 100644
--- a/app/views/shared/_affiliation_person_buttons.html.erb
+++ b/app/views/shared/_affiliation_person_buttons.html.erb
@@ -5,7 +5,7 @@
<% include_inactive = local_assigns.fetch(:include_inactive, false) %>
<% all_affiliations = affiliations.select { |a| a.person.present? } %>
<% active_affiliations = all_affiliations
- .select { |a| !a.inactive? && (a.end_date.nil? || a.end_date >= Date.current) }
+ .select(&:active?)
.sort_by { |a| [a.person.first_name.to_s.downcase, a.person.last_name.to_s.downcase] } %>
<% inactive_affiliations = include_inactive ?
(all_affiliations - active_affiliations).sort_by { |a| [a.person.first_name.to_s.downcase, a.person.last_name.to_s.downcase] } : [] %>
From 6596c03db5f94dd206260003ce66ef31d834dd2e Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Wed, 19 Aug 2026 22:09:25 -0400
Subject: [PATCH 31/37] Name the dates-only readers active_by_date_on, and pin
the arithmetic
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
`active?` and `active_on` sat two characters apart while answering different
questions with different inputs — one reads the dates and the inactive flag to say
what is true now, the other reads dates alone to say what was true on a date. The
new name says which input it uses.
ADR-0002 writes down what ADR-0001 left implicit: the two relationships the one
table carries, that `inactive` is now an override rather than a cache, what
`event_registration_id` does and does not mean, and the two rules above about not
erasing history.
The arithmetic behind the grant figures is covered directly rather than inferred
from single-affiliation cases — several people at one anchor, Jan 1 vs Dec 31 in
both directions, a full new → ongoing → reinstated → ongoing walk, and that
reconciling a no-show leaves an anchored verdict where it was.
Co-Authored-By: Claude Opus 5 (1M context)
---
app/services/event_dashboard.rb | 4 +-
app/services/facilitator_program_status.rb | 8 +-
...nization-affiliation-and-program-status.md | 12 +-
...ions-as-the-record-of-two-relationships.md | 256 ++++++++++++++++++
.../facilitator_program_status_math_spec.rb | 205 ++++++++++++++
5 files changed, 477 insertions(+), 8 deletions(-)
create mode 100644 docs/adr/0002-affiliations-as-the-record-of-two-relationships.md
create mode 100644 spec/services/facilitator_program_status_math_spec.rb
diff --git a/app/services/event_dashboard.rb b/app/services/event_dashboard.rb
index 1d3159856..f19b80622 100644
--- a/app/services/event_dashboard.rb
+++ b/app/services/event_dashboard.rb
@@ -577,7 +577,7 @@ def organization_registrant_ids_by_org
.joins(:event_registration)
.where(event_registration_id: active_registration_ids)
.pluck(:organization_id, "event_registrations.registrant_id")
- affiliated = Affiliation.active_on(reference_date)
+ affiliated = Affiliation.active_by_date_on(reference_date)
.where(person_id: registrant_ids)
.pluck(:organization_id, :person_id)
(snapshot + affiliated).each_with_object(Hash.new { |hash, key| hash[key] = Set.new }) do |(organization_id, person_id), map|
@@ -1389,7 +1389,7 @@ def organization_ids
snapshot_ids = EventRegistrationOrganization
.where(event_registration_id: active_registration_ids)
.pluck(:organization_id)
- affiliated_ids = Affiliation.active_on(reference_date)
+ affiliated_ids = Affiliation.active_by_date_on(reference_date)
.where(person_id: registrant_ids)
.pluck(:organization_id)
(snapshot_ids + affiliated_ids).compact.uniq
diff --git a/app/services/facilitator_program_status.rb b/app/services/facilitator_program_status.rb
index d90855ea0..83161bc0c 100644
--- a/app/services/facilitator_program_status.rb
+++ b/app/services/facilitator_program_status.rb
@@ -25,7 +25,7 @@ def year_anchored? = @year_anchored
def status
@status ||= if earlier.empty?
:new
- elsif active_on_anchor.any?
+ elsif active_by_date_on_anchor.any?
:ongoing
else
:reinstated
@@ -37,7 +37,7 @@ def label = status.to_s.titleize
# For :ongoing the most recent start still running on the anchor; for
# :reinstated the most recent start of the lapsed history. Nil for :new.
def active_since
- @active_since ||= (active_on_anchor.presence || earlier).filter_map(&:start_date).max
+ @active_since ||= (active_by_date_on_anchor.presence || earlier).filter_map(&:start_date).max
end
# When a :reinstated program's history ran out. Nil for the other statuses.
@@ -89,7 +89,7 @@ def earlier
@earlier ||= @facilitators.select { |affiliation| affiliation.start_date < as_of }
end
- def active_on_anchor
- @active_on_anchor ||= earlier.select { |affiliation| affiliation.end_date.nil? || affiliation.end_date >= as_of }
+ def active_by_date_on_anchor
+ @active_by_date_on_anchor ||= earlier.select { |affiliation| affiliation.end_date.nil? || affiliation.end_date >= as_of }
end
end
diff --git a/docs/adr/0001-organization-affiliation-and-program-status.md b/docs/adr/0001-organization-affiliation-and-program-status.md
index 8fc97cb27..a822f0635 100644
--- a/docs/adr/0001-organization-affiliation-and-program-status.md
+++ b/docs/adr/0001-organization-affiliation-and-program-status.md
@@ -23,7 +23,10 @@ decisions that resolve the ambiguities so they're written down once.
- **Affiliation** — an Org ↔ Person link (`affiliations` table) with `title`,
`start_date`, `end_date`, and a cached `inactive` flag. **Not tied to any
- event** (there is no `event_id` on an affiliation).
+ event** (there is no `event_id` on an affiliation). **Refined by
+ [ADR-0002](0002-affiliations-as-the-record-of-two-relationships.md) D2a:** still no
+ `event_id`, but there is now an `event_registration_id` recording which
+ registration minted the row.
- **Facilitator affiliation** — an affiliation whose `title` is **exactly
`"Facilitator"`** (trimmed, case-sensitive). No fuzzy/`LIKE` matching; "Lead
Facilitator" and "facilitator" do **not** count. See `Affiliation#facilitator?`
@@ -32,6 +35,9 @@ decisions that resolve the ambiguities so they're written down once.
`>= today`). `inactive` is a cached column derived from the dates on save
(`set_inactive_from_dates`: `inactive = end_date.present? && end_date < today`),
so in practice "active" reduces to **no end date, or end date ≥ today**.
+ **Superseded by [ADR-0002](0002-affiliations-as-the-record-of-two-relationships.md)
+ D2:** `inactive` is now an override that can end a row the dates still call
+ active, so "active" no longer reduces to the dates.
- **Facilitator-training event** — `events.facilitator_training == true`. The
only events for which per-event program status is meaningful.
@@ -208,7 +214,9 @@ coincide when no organization attended twice.
- **Strict `<`** for "earlier": `start_date == anchor` is **not** earlier (so the
affiliation a training mints is **New**, not Ongoing).
-- **Active-at-date** uses `end_date IS NULL OR end_date >= anchor`.
+- **Active-at-date** uses `end_date IS NULL OR end_date >= anchor`. Spelled
+ `Affiliation.active_by_date_on(date)` since ADR-0002 D3 — the `historical` in the
+ name marks it as the dates-only reader.
## Notes / open items
diff --git a/docs/adr/0002-affiliations-as-the-record-of-two-relationships.md b/docs/adr/0002-affiliations-as-the-record-of-two-relationships.md
new file mode 100644
index 000000000..0e961440a
--- /dev/null
+++ b/docs/adr/0002-affiliations-as-the-record-of-two-relationships.md
@@ -0,0 +1,256 @@
+# ADR-0002 — Affiliations record two relationships, and only one of them is the art program
+
+- **Status:** Accepted
+- **Date:** 2026-08-19
+- **Extends:** [ADR-0001](0001-organization-affiliation-and-program-status.md) (supersedes its
+ "Active affiliation" vocabulary entry — see D2 below)
+
+## Context
+
+ADR-0001 pinned how program status is computed. It left two things implicit that
+have since caused real bugs:
+
+1. **`affiliations` carries two different relationships in one table**, and only
+ one of them says anything about the art program. Code that reads "the person's
+ affiliations with this org" without saying which kind it means has been wrong
+ more than once.
+2. **Two different questions get asked of the same rows** — "what was true on
+ date X" and "what is true now" — and they need different inputs. ADR-0001
+ described `inactive` as a cache of the dates, which made the two look
+ interchangeable. They aren't, and reconciliation broke the distinction: ending
+ a no-show's affiliation retroactively changed an organization's program status
+ at trainings years earlier.
+
+This ADR names the two relationships, splits the two questions, and writes down
+what has to be true for the annual grant figures to be trustworthy.
+
+## Decisions
+
+### D1 — One table, two relationships
+
+An `Affiliation` is a Person ↔ Organization link. Its `title` decides which of two
+relationships it records, and they are not interchangeable:
+
+- **Job affiliation** — the role the person holds at the org ("Counselor",
+ "Program Director", "Lead Facilitator"). It answers *who this person is to this
+ organization*. It carries **no** start date by default: we rarely know when they
+ took the job, and dating it to a registration would misrepresent that.
+- **Facilitator affiliation** — `title` exactly `"Facilitator"` (trimmed,
+ case-sensitive; see `Affiliation#facilitator?` and the `.facilitators` scope). It
+ answers *this organization was running an art program, staffed by this person,
+ over this period.* It is dated to the training that conferred it (ADR-0001 D8).
+
+**Only the facilitator affiliation feeds program status.** A job affiliation never
+makes an org active, never makes it Ongoing, and is never touched by
+reconciliation. One person can hold both at the same org at the same time, and
+normally does — a "Lead Facilitator" job affiliation plus a standing "Facilitator"
+one (`AffiliationServices::CreateFromRegistration`).
+
+**Being a facilitator is conferred by a training, not by attending an event.** Only
+a `facilitator_training` registration mints a facilitator affiliation; other
+org-linked registrations mint the job affiliation alone.
+
+### D2 — `inactive` is an override, not a cache
+
+ADR-0001 called `inactive` "a cached column derived from the dates on save," so
+that "active" reduced to the dates. **That is no longer true.** `inactive` is now
+an independent flag that can end an affiliation the dates still read as current:
+
+- It is still **derived** from the dates when no one says otherwise
+ (`set_inactive_from_dates`).
+- An **explicit** assignment wins — `Affiliation#inactive_supplied` marks that a
+ caller supplied the value deliberately, so a later edit to an unrelated date
+ can't quietly undo it.
+
+Why it has to exist: a one-day training that starts and ends today produces an
+affiliation whose end date is today, and `end_date >= today` reads as active. Without
+the flag a no-show would keep facilitator status for the rest of the day. The
+standalone affiliation editor exposes the same flag so an admin can end a row
+effective now without inventing a false end date.
+
+### D2a — Provenance is `event_registration_id`, and there is no `event_id`
+
+An affiliation links to the **registration** that minted it
+(`affiliations.event_registration_id`, nullable, `on_delete: :nullify`). There is
+deliberately **no `event_id`** — the event is reachable only through the
+registration.
+
+What the FK does and does not mean:
+
+- **It is the auto-vs-manual gate.** `NULL` means hand-entered or historical;
+ present means the registration flow created this row. `ReconcilePerson`'s
+ `include_unowned:` switches on exactly this, and D6's "the row this training
+ minted" is `affiliation.event_registration&.event_id == event.id`.
+- **It is NOT the completion signal.** Creation dedupes, so one affiliation can be
+ backed by several training registrations while the FK records only the *creating*
+ one. Reading completion off `affiliation.event_registration.attended?` would end a
+ returning facilitator whose first training was a no-show but who attended a later
+ one. Completion is a query across **all** of the person's facilitator-training
+ registrations for that org (`ReconcilePerson#completed_training?`).
+- **It is scoped to the current org.** Repointing an affiliation at a different
+ organization nulls it (`reset_org_scoped_links_on_org_change`), because the minting
+ registration no longer applies. Invariant: **FK present ⟺ this row was auto-minted
+ for its current organization.**
+- **It says nothing about the kind of relationship.** Both kinds of row (D1) carry
+ it — a job affiliation minted by a non-training registration has a registration
+ whose event is not a facilitator training. `event_registration.event.facilitator_training?`
+ must be checked, never assumed.
+
+Two consequences worth knowing:
+
+- **Provenance is lossy by design.** `EventRegistration has_many :affiliations,
+ dependent: :nullify` and the FK is `on_delete: :nullify`, so deleting a
+ registration leaves its affiliations standing with a `NULL` link. An auto-minted
+ row silently becomes indistinguishable from a hand-entered one, and the default
+ `include_unowned: false` gate will then spare it. That is the safe direction to
+ fail, but it means the gate is a floor, not a guarantee.
+- **The reverse lookup is cheap.** `index_affiliations_on_event_registration_id`
+ means "which affiliations did this registration mint" is an indexed read, which is
+ what lets the affiliation edit page show its minting event inline
+ (`Analytics::AffiliationTimeline`).
+
+### D3 — Two questions, two inputs
+
+| Question | Anchored on | Reads |
+|---|---|---|
+| **Historical** — "what was true on date X" | an explicit date | **dates only** |
+| **Current** — "what is true now" | now | **dates *and* the `inactive` flag** |
+
+- Historical: `FacilitatorProgramStatus` (New / Ongoing / Reinstated) and the
+ `Affiliation.active_by_date_on(date)` scope. They deliberately ignore `inactive`,
+ because the flag describes *now* and a historical answer must not move when
+ someone's status changes later.
+- Current: `Affiliation#active?`, the `.active` / `.active_or_pending` scopes, and
+ `OrganizationDecorator#organization_status_bucket`.
+
+**The corollary that cost us a bug:** because historical readers ignore the flag,
+they can only be kept honest by writing **truthful dates**. See D6.
+
+### D4 — The organization's current status: Active / Formerly active / Never active
+
+Derived purely from facilitator affiliations
+(`OrganizationDecorator#organization_status_bucket`, ADR-0001 D3):
+
+- any **active** facilitator affiliation → **Active**
+- facilitator affiliation(s) but **all ended** → **Formerly active**
+- **no** facilitator affiliation → **Never active**
+
+**Formerly active is a subset of "not active."** The index filter treats it that
+way (`Organization.program_status(:formerly_or_never)`), and any UI offering an
+active/inactive choice must fold Formerly active and Never active under inactive
+while still showing them apart — "used to run a program" and "never ran one" are
+different facts about an org and only one of them is a lapse worth chasing.
+
+The in-memory bucket and the SQL scope must agree; they are two spellings of one
+rule and are tested against each other.
+
+**The stored `organization_status` column is not an independent input.** It is
+maintained *from* the affiliations (`sync_organization_status_with_affiliations`)
+and is not consulted when computing the bucket (ADR-0001 D3/D3a). An org is active
+because someone is facilitating there, not because a column says so.
+
+### D5 — The anchor date, and what it's for
+
+Program status is one value per **(organization, anchor date)**. In event context
+the anchor is the event's `start_date`; with no event in view it falls back to
+January 1 of the current year (ADR-0001 D7).
+
+These figures back grant applications, so the property that matters is
+**stability**: asking the same question about the same past date must give the same
+answer forever, no matter what has happened to the people involved since. Two
+consequences:
+
+- Any anchor is legitimate, not just event dates. Comparing **Jan 1 vs Dec 31** of
+ a year is a supported use — it's how "what moved this year" gets answered.
+- Any write that changes an affiliation's dates is a write to the historical
+ record. It must be justified against D6.
+
+### D6 — Ending an affiliation must not erase the period it records
+
+When reconciliation ends a facilitator affiliation for someone who didn't complete
+a training, where the end date lands depends on what the row represents:
+
+- **The row this training minted** (owned by an `event_registration` for this
+ event) — same-day it: `end_date = start_date`. It recorded an *assumption* that
+ the person would become a facilitator on the training date. They didn't, so it
+ collapses to nothing. It never counted as prior history anyway (ADR-0001 D5/D8
+ use a strict `<`), so no anchored verdict moves.
+- **Any older row** — hand-entered, or minted by an earlier training — ends on
+ **this training's start date**. It records facilitation that really happened.
+ Same-daying it would delete years of history and retroactively flip the org from
+ Ongoing to Reinstated at every training in between.
+
+If an older row somehow starts *after* this training, it same-days instead; an end
+date before its own start is never written.
+
+`inactive: true` is set in both cases (D2), which is what makes the row read as
+ended today even when the end date is today.
+
+The org's *current* bucket is expected to change — that's the point. Its *anchored*
+verdicts are not.
+
+### D6a — A return after a lapse is a new row, never a reopened one
+
+When someone whose facilitator affiliation has ended completes a training for that
+organization again, reconciliation **creates a second affiliation** dated to the new
+training. It does not clear the old row's end date.
+
+Reopening it would swallow the gap: `Jan 2023 – Jan 2024, Aug 2026` collapses to
+`Jan 2023`, and the organization retroactively reads Ongoing across years it was not
+running a program. The lapse is the fact the two rows exist to record — ADR-0001 D2
+renders exactly that shape, and `CreateFromRegistration` has always minted a second
+row rather than extending an ended one (an ended facilitator affiliation does not
+block a new one).
+
+So there is no `:reactivate` action. An ended row is left alone with the reason
+"Ended — a return is recorded as a new affiliation", and the return shows up as an
+ordinary `:create`. The rule for proposing that create: the person has **no active**
+facilitator affiliation for the org, and either never had one or has completed a
+training here.
+
+This is the mirror of D6. D6 stops an ending from reaching too far back; D6a stops a
+reactivation from reaching too far forward. Both exist because the historical readers
+(D3) trust the dates.
+
+### D7 — What has to be tested
+
+The arithmetic is what the grant figures rest on, so it is covered directly rather
+than inferred from the single-affiliation cases
+(`spec/services/facilitator_program_status_math_spec.rb`):
+
+1. **Several people at one anchor** — one person still facilitating keeps the org
+ Ongoing however many others have left; Reinstated requires *every* earlier
+ person to have ended; people arriving *at* the training don't rescue a lapsed
+ program; non-facilitator titles never count.
+2. **One organization at several anchors** — Jan 1 vs Dec 31 of the same year in
+ both directions (a program starting mid-year, a program lapsing mid-year), and
+ a full new → ongoing → reinstated → ongoing walk across a lapse and a return.
+3. **Stability** — a past anchor keeps its verdict after the program later ends.
+4. **Both questions on the same org** — Ongoing at a past training while Formerly
+ active today, and vice versa.
+5. **The bucket agrees with the SQL scope** the index filter uses.
+6. **Reconciliation doesn't move an anchored verdict** — D6, both branches.
+7. **A return after a lapse adds a row and leaves the lapse intact** — D6a, asserted
+ on both the row count and the mid-gap verdict.
+
+Adding a rule here means adding a case there.
+
+## Notes / open items
+
+- **`inactive_reason` is not yet modelled.** Nothing records *why* an affiliation
+ ended — an admin's manual end date, a reconciliation after a no-show, or a
+ derivation from the dates. Worth adding as a plain string column constrained by a
+ constant if the distinction ever needs to be surfaced or filtered; deliberately
+ deferred until there's a reader for it.
+- **Naming now advertises D3.** The historical readers say "by date" —
+ `Affiliation.active_by_date_on(date)` and
+ `FacilitatorProgramStatus#active_by_date_on_anchor` — naming the input that
+ separates them from the current-state `active?` / `.active` — `Affiliation.active_by_date_on(date)` and
+ `FacilitatorProgramStatus#active_by_date_on_anchor` — so a call site can't mistake them
+ for the current-state `active?` / `.active`. Note the subject: these ask whether
+ **one affiliation's own period** covered a date. The organization-level questions
+ are built on top (D4 for now, `FacilitatorProgramStatus` for a date). Anything new
+ that answers "as of a date" should follow the same convention.
+- **ADR-0001's vocabulary entry for "Active affiliation" is superseded by D2**, and
+ its note that an affiliation is "not tied to any event" is superseded by D2a — it
+ is tied to a *registration*, which is not the same thing.
diff --git a/spec/services/facilitator_program_status_math_spec.rb b/spec/services/facilitator_program_status_math_spec.rb
new file mode 100644
index 000000000..0e9d38b5b
--- /dev/null
+++ b/spec/services/facilitator_program_status_math_spec.rb
@@ -0,0 +1,205 @@
+require "rails_helper"
+
+# How several people's facilitator affiliations add up to ONE verdict for the
+# organization — at an anchor date (New / Ongoing / Reinstated) and right now
+# (Active / Formerly active / Never active). ADR-0002 D3–D5.
+#
+# The single-affiliation boundary cases live in facilitator_program_status_spec.rb;
+# this file is about the arithmetic across people, across anchors, and the
+# relationship between the two questions.
+RSpec.describe "facilitator affiliation math" do
+ let(:organization) { create(:organization) }
+
+ def facilitator(start_date:, end_date: nil, title: "Facilitator")
+ create(:affiliation, organization: organization, person: create(:person),
+ title: title, start_date: start_date, end_date: end_date)
+ end
+
+ def status_on(date)
+ organization.reload.facilitator_status_on(date)
+ end
+
+ def bucket
+ organization.reload.decorate.organization_status_bucket
+ end
+
+ describe "several people at one anchor" do
+ let(:anchor) { Date.new(2026, 6, 15) }
+
+ it "is :ongoing when any one person is still facilitating, even if others have left" do
+ facilitator(start_date: Date.new(2015, 1, 1), end_date: Date.new(2018, 1, 1))
+ facilitator(start_date: Date.new(2019, 1, 1), end_date: Date.new(2020, 1, 1))
+ facilitator(start_date: Date.new(2021, 1, 1))
+
+ expect(status_on(anchor)).to eq(:ongoing)
+ end
+
+ it "is :reinstated only when EVERY earlier person has ended" do
+ facilitator(start_date: Date.new(2015, 1, 1), end_date: Date.new(2018, 1, 1))
+ facilitator(start_date: Date.new(2019, 1, 1), end_date: Date.new(2020, 1, 1))
+
+ expect(status_on(anchor)).to eq(:reinstated)
+ end
+
+ it "is :new when every person starts on or after the anchor" do
+ facilitator(start_date: anchor)
+ facilitator(start_date: anchor)
+ facilitator(start_date: anchor + 1.day)
+
+ expect(status_on(anchor)).to eq(:new)
+ end
+
+ it "does not let people arriving at the training rescue a lapsed program" do
+ facilitator(start_date: Date.new(2015, 1, 1), end_date: Date.new(2018, 1, 1))
+ facilitator(start_date: anchor)
+ facilitator(start_date: anchor)
+
+ expect(status_on(anchor)).to eq(:reinstated)
+ end
+
+ it "counts only facilitators — a roomful of other titles is still :new" do
+ facilitator(start_date: Date.new(2010, 1, 1), title: "Volunteer")
+ facilitator(start_date: Date.new(2011, 1, 1), title: "Counselor")
+ facilitator(start_date: Date.new(2012, 1, 1), title: "Lead Facilitator")
+
+ expect(status_on(anchor)).to eq(:new)
+ end
+ end
+
+ describe "the same organization read at different anchors" do
+ it "reads :new on Jan 1 and :ongoing on Dec 31 when the program starts mid-year" do
+ facilitator(start_date: Date.new(2026, 5, 4))
+
+ expect(status_on(Date.new(2026, 1, 1))).to eq(:new)
+ expect(status_on(Date.new(2026, 12, 31))).to eq(:ongoing)
+ end
+
+ it "reads :ongoing on Jan 1 and :reinstated on Dec 31 when the program lapses mid-year" do
+ facilitator(start_date: Date.new(2022, 3, 1), end_date: Date.new(2026, 5, 4))
+
+ expect(status_on(Date.new(2026, 1, 1))).to eq(:ongoing)
+ expect(status_on(Date.new(2026, 12, 31))).to eq(:reinstated)
+ end
+
+ it "walks new → ongoing → reinstated → ongoing across a lapse and a return" do
+ facilitator(start_date: Date.new(2020, 2, 1), end_date: Date.new(2022, 8, 1))
+ facilitator(start_date: Date.new(2025, 9, 1))
+
+ expect(status_on(Date.new(2019, 1, 1))).to eq(:new)
+ expect(status_on(Date.new(2021, 1, 1))).to eq(:ongoing)
+ expect(status_on(Date.new(2024, 1, 1))).to eq(:reinstated)
+ expect(status_on(Date.new(2026, 1, 1))).to eq(:ongoing)
+ end
+
+ it "still reports what was true then after the program later ends" do
+ affiliation = facilitator(start_date: Date.new(2020, 1, 1))
+ expect(status_on(Date.new(2023, 1, 1))).to eq(:ongoing)
+
+ affiliation.update!(end_date: Date.new(2024, 6, 1))
+
+ expect(status_on(Date.new(2023, 1, 1))).to eq(:ongoing)
+ expect(status_on(Date.new(2026, 1, 1))).to eq(:reinstated)
+ end
+ end
+
+ describe "now (Active / Formerly active / Never active)" do
+ it "is :never_active with no facilitator affiliation, whatever else the org has" do
+ facilitator(start_date: 5.years.ago.to_date, title: "Volunteer")
+
+ expect(bucket).to eq(:never_active)
+ end
+
+ it "is :active while any one person is still facilitating" do
+ facilitator(start_date: 5.years.ago.to_date, end_date: 3.years.ago.to_date)
+ facilitator(start_date: 2.years.ago.to_date)
+
+ expect(bucket).to eq(:active)
+ end
+
+ it "is :formerly_active once every facilitator has ended — a subset of not-active" do
+ facilitator(start_date: 5.years.ago.to_date, end_date: 3.years.ago.to_date)
+ facilitator(start_date: 2.years.ago.to_date, end_date: 1.year.ago.to_date)
+
+ expect(bucket).to eq(:formerly_active)
+ expect(organization.reload.affiliations.facilitators.active).to be_empty
+ end
+
+ it "is :formerly_active when the flag ends a row the dates still call active" do
+ affiliation = facilitator(start_date: 2.years.ago.to_date)
+ expect(bucket).to eq(:active)
+
+ affiliation.inactive_supplied = true
+ affiliation.update!(inactive: true)
+
+ expect(bucket).to eq(:formerly_active)
+ end
+
+ it "agrees with the SQL scope the index filter uses" do
+ facilitator(start_date: 5.years.ago.to_date, end_date: 3.years.ago.to_date)
+
+ expect(bucket).to eq(:formerly_active)
+ expect(Organization.program_status(:formerly_active)).to include(organization)
+ expect(Organization.program_status(:active)).not_to include(organization)
+ end
+ end
+
+ describe "the two questions are independent" do
+ it "reads Ongoing at a past training while reading Formerly active today" do
+ facilitator(start_date: 4.years.ago.to_date, end_date: 1.year.ago.to_date)
+
+ expect(status_on(2.years.ago.to_date)).to eq(:ongoing)
+ expect(bucket).to eq(:formerly_active)
+ end
+
+ it "reads New at a past date while reading Active today" do
+ facilitator(start_date: 1.year.ago.to_date)
+
+ expect(status_on(3.years.ago.to_date)).to eq(:new)
+ expect(bucket).to eq(:active)
+ end
+ end
+
+ describe "reconciliation does not move an anchored verdict" do
+ it "keeps the training-date status when a no-show's older affiliation is ended" do
+ person = create(:person)
+ event = create(:event, :ended, facilitator_training: true)
+ anchor = event.start_date.to_date
+ older = create(:affiliation, organization: organization, person: person,
+ title: "Facilitator", start_date: 3.years.ago.to_date)
+ registration = create(:event_registration, event: event, registrant: person, status: "no_show")
+ create(:event_registration_organization, event_registration: registration, organization: organization)
+
+ expect(status_on(anchor)).to eq(:ongoing)
+
+ AffiliationServices::ReconcilePerson.new(
+ person: person, organization: organization, event: event,
+ registration: registration, include_unowned: true
+ ).perform(:deactivate, affiliation: older)
+
+ expect(status_on(anchor)).to eq(:ongoing)
+ expect(status_on(anchor + 1.year)).to eq(:reinstated)
+ expect(bucket).to eq(:formerly_active)
+ end
+
+ it "leaves the verdict alone when the row the training minted is same-dayed" do
+ person = create(:person)
+ event = create(:event, :ended, facilitator_training: true)
+ anchor = event.start_date.to_date
+ registration = create(:event_registration, event: event, registrant: person, status: "no_show")
+ create(:event_registration_organization, event_registration: registration, organization: organization)
+ minted = create(:affiliation, organization: organization, person: person, title: "Facilitator",
+ start_date: anchor, event_registration: registration)
+
+ expect(status_on(anchor)).to eq(:new)
+
+ AffiliationServices::ReconcilePerson.new(
+ person: person, organization: organization, event: event,
+ registration: registration, include_unowned: true
+ ).perform(:deactivate, affiliation: minted)
+
+ expect(status_on(anchor)).to eq(:new)
+ expect(minted.reload.end_date).to eq(anchor)
+ expect(bucket).to eq(:formerly_active)
+ end
+ end
+end
From 6ea1586ea752018d457278d54a1ac75d69b44ae5 Mon Sep 17 00:00:00 2001
From: Mae Beale
Date: Wed, 19 Aug 2026 22:09:25 -0400
Subject: [PATCH 32/37] Split the affiliation editor into Active and Inactive
tabs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Long affiliation lists mix people who facilitate now with rows that ended years
ago. The server already knows which is which, so it renders both groups and two
radios drive the visibility through :has() — no JavaScript, and the person and
organization forms share one partial instead of two copies.
A row you end while editing stays exactly where it is and just restyles; its
bucket only moves once you save. That is why this is not the registrants page's
server-round-trip filter: switching tabs must not discard unsaved edits.
Two Tailwind traps shape the markup. Radio ids cannot contain underscores —
Tailwind reads `_` as a space inside an arbitrary value, so the selector matches
nothing. And the group is named, because `group-hover:` matches any `.group`
ancestor and an unnamed one made hovering pop every row's comment tooltip at once.
The standalone editor now uses the same live styling, its comment icon opens the
comments it is previewing, and a back link to an ended row lands on the section
rather than a row hidden on the other tab.
Co-Authored-By: Claude Opus 5 (1M context)
---
app/decorators/affiliation_decorator.rb | 7 ++
.../affiliations/_address_picker.html.erb | 2 +-
app/views/affiliations/_editor.html.erb | 94 +++++++++++++++
app/views/affiliations/edit.html.erb | 49 +++++---
app/views/organizations/_form.html.erb | 19 +--
app/views/people/_form.html.erb | 19 +--
spec/decorators/affiliation_decorator_spec.rb | 22 ++++
.../stimulus_controller_registration_spec.rb | 28 +++++
.../requests/affiliation_comment_icon_spec.rb | 66 +++++++++++
spec/requests/affiliation_filter_tabs_spec.rb | 96 +++++++++++++++
.../affiliation_return_anchor_spec.rb | 36 ++++++
.../affiliation_edit_live_styling_spec.rb | 112 ++++++++++++++++++
spec/system/affiliation_filter_tabs_spec.rb | 88 ++++++++++++++
13 files changed, 586 insertions(+), 52 deletions(-)
create mode 100644 app/views/affiliations/_editor.html.erb
create mode 100644 spec/frontend/stimulus_controller_registration_spec.rb
create mode 100644 spec/requests/affiliation_comment_icon_spec.rb
create mode 100644 spec/requests/affiliation_filter_tabs_spec.rb
create mode 100644 spec/requests/affiliation_return_anchor_spec.rb
create mode 100644 spec/system/affiliation_edit_live_styling_spec.rb
create mode 100644 spec/system/affiliation_filter_tabs_spec.rb
diff --git a/app/decorators/affiliation_decorator.rb b/app/decorators/affiliation_decorator.rb
index 46028193e..2c16e8b20 100644
--- a/app/decorators/affiliation_decorator.rb
+++ b/app/decorators/affiliation_decorator.rb
@@ -3,6 +3,13 @@ def detail(length: nil)
"#{person.full_name}: #{title.presence || position} - #{organization.name}"
end
+ # Where a back link should land on the person/organization editor. An inactive
+ # row sits on the Inactive tab, so jumping to the row itself would scroll to
+ # something the page isn't showing — land on the section instead.
+ def return_anchor
+ active? ? h.dom_id(object) : "affiliations"
+ end
+
# e.g. "Oct 13, 2026 – present"
def date_range
start = start_date ? start_date.strftime("%b %-d, %Y") : "no start date"
diff --git a/app/views/affiliations/_address_picker.html.erb b/app/views/affiliations/_address_picker.html.erb
index 99e8fe08b..41e3ff139 100644
--- a/app/views/affiliations/_address_picker.html.erb
+++ b/app/views/affiliations/_address_picker.html.erb
@@ -13,7 +13,7 @@
<% inline = local_assigns.fetch(:hide_label, false) %>
<% if options.any? %>