Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ def update
@person.comments.select(&:new_record?).each { |c| c.created_by = current_user; c.updated_by = current_user }
@person.comments.select { |c| c.persisted? && c.body_changed? }.each { |c| c.updated_by = current_user }

# Inline-logged notifications are addressed to the person.
recipient_email = @person.preferred_email.presence || "n/a"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 From Claude: Falls back to "n/a" when the person has no email, mirroring the registration controller — recipient_email is presence-validated on Notification, so a manual log against an email-less person still saves.

@person.notifications.select(&:new_record?).each { |n| n.recipient_email = recipient_email }

if @person.save
assign_associations(@person) if params.dig(:person, :category_ids)
redirect_to person_update_return_path, notice: "Person was successfully updated."
Expand Down Expand Up @@ -488,6 +492,7 @@ def person_params
:_destroy
],
comments_attributes: [ :id, :topic, :body, :flagged, :_destroy ],
notifications_attributes: [ :channel, :sender_id, :email_subject, :email_body_text, :noticeable_type, :noticeable_id ]
)
end
end
1 change: 1 addition & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Notification < ApplicationRecord
NOTICEABLE_TYPES = %w[
EventRegistration
FormSubmission
Person
Report
StoryIdea
User
Expand Down
2 changes: 2 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Person < ApplicationRecord
has_many :comments, -> { newest_first }, as: :commentable, dependent: :destroy
has_many :contact_methods, as: :contactable, dependent: :destroy
has_many :categorizable_items, inverse_of: :categorizable, as: :categorizable, dependent: :destroy
has_many :notifications, as: :noticeable, dependent: :destroy
has_many :sectorable_items, as: :sectorable, dependent: :destroy
has_many :stories_as_spotlighted_facilitator, inverse_of: :spotlighted_facilitator, class_name: "Story",
dependent: :restrict_with_error
Expand Down Expand Up @@ -69,6 +70,7 @@ class Person < ApplicationRecord
accepts_nested_attributes_for :affiliations, allow_destroy: true,
reject_if: proc { |attrs| attrs["organization_id"].blank? }
accepts_nested_attributes_for :comments, allow_destroy: true, reject_if: proc { |attrs| attrs["body"].blank? }
accepts_nested_attributes_for :notifications, reject_if: proc { |attrs| attrs["email_subject"].blank? }

# Search Cop
include SearchCop
Expand Down
4 changes: 2 additions & 2 deletions app/views/event_registrations/_notifications.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
<%# Inline add — manual log entries, saved with the registration form. %>
<div class="space-y-2 border-t border-gray-100 pt-3">
<%= f.simple_fields_for :notifications, registration.notifications.select(&:new_record?) do |nf| %>
<%= render "notifications/notification_fields", f: nf, event_registration: registration %>
<%= render "notifications/notification_fields", f: nf, record: registration %>
<% end %>
<%= link_to_add_association f, :notifications,
partial: "notifications/notification_fields",
render_options: { locals: { event_registration: registration } },
render_options: { locals: { record: registration } },
class: "inline-flex items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-700 cursor-pointer" do %>
<i class="fa-solid fa-plus text-[0.6rem]"></i>
Add notification
Expand Down
6 changes: 3 additions & 3 deletions app/views/notifications/_notification_fields.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<% sender_users |= [ current_user ] if current_user %>
<% sender_options = sender_users.sort_by { |u| u.full_name.to_s.downcase }.map { |u| [ u.full_name, u.id ] } %>
<% channel_default = Notification::MANUAL_CHANNELS.include?(f.object.channel) ? f.object.channel : "email" %>
<% record = local_assigns[:event_registration] %>
<% record = local_assigns[:record] %>

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 From Claude: Renamed the event_registration: local to a neutral record: so this partial can be shared by both the registration and person notification sections. Both callers updated.

<div class="nested-fields rounded-md border border-amber-200 bg-amber-50 p-3">
<%# Record this communication against the event registration (the "Record"
column in the notifications index). %>
<%# Record this communication against its parent (the "Record" column in the
notifications index) — e.g. an event registration or a person. %>
<% if record %>
<%= f.hidden_field :noticeable_type, value: record.class.name %>
<%= f.hidden_field :noticeable_id, value: record.id %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/people/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@
</div>

<% if f.object.persisted? %>
<!-- Notifications -->
<% if allowed_to?(:index?, with: NotificationPolicy) %>
<%= render "notifications", f: f %>
<% end %>

<!-- Comments -->
<% if allowed_to?(:manage?, Comment) %>
<div class="admin-only bg-blue-100 form-group space-y-4" id="comments-section">
Expand Down
62 changes: 62 additions & 0 deletions app/views/people/_notifications.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<%# ---- Notifications — communications addressed to this person, newest first.
Persisted entries are read-only and paginated like the comments box; new
manual log entries (email/phone/text/video) can be added inline and are
saved with the person form. Links open in a new tab so unsaved edits aren't
lost. Mirrors the registration edit notifications section. ---- %>
<% person = f.object %>
<% email = person.preferred_email %>
<% notifications = email.present? ? Notification.email(email).order(created_at: :desc) : Notification.none %>
<section class="overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm">
<div class="flex items-center gap-3 border-b border-gray-100 px-4 py-3">
<span class="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg <%= DomainTheme.bg_class_for(:users, intensity: 100) %> <%= DomainTheme.text_class_for(:users, intensity: 600) %>">
<i class="fa-solid fa-bell"></i>
</span>
<h2 class="text-sm font-semibold text-gray-700">Communications</h2>
<% if email.present? %>
<%= link_to notifications_path(email: email),
class: "ml-auto inline-flex items-center gap-1.5 text-xs font-medium text-gray-500 hover:text-gray-700 hover:underline",
target: "_blank", rel: "noopener" do %>
View all
<i class="fa-solid fa-arrow-up-right-from-square text-[0.6rem]"></i>
<% end %>
<% end %>
</div>

<div class="space-y-3 p-4">
<% if notifications.any? %>
<div data-controller="paginated-fields" data-paginated-fields-per-page-value="5" class="space-y-2">
<% notifications.each do |notification| %>
<% subject = notification.email_subject.presence || notification.kind.to_s.humanize %>
<% body = notification.email_body_text.to_s %>
<div data-paginated-fields-target="item" class="flex items-baseline gap-x-2">
<span class="shrink-0 whitespace-nowrap text-xs text-gray-400"><%= notification.created_at.strftime("%-m/%-d/%Y") %></span>
<span class="shrink-0 whitespace-nowrap text-xs font-medium text-gray-500"><%= notification.sender&.full_name.presence || "AWBW Portal" %></span>
<span class="min-w-0 flex-1 truncate text-sm" title="<%= [ subject, body.presence ].compact.join(" — ") %>">
<span class="font-semibold text-gray-900"><%= subject %></span>
<% if body.present? %>
<span class="text-gray-500"><%= body %></span>
<% end %>
</span>
</div>
<% end %>
<div data-paginated-fields-target="nav"></div>
</div>
<% else %>
<p class="text-sm text-gray-400">No notifications for this person yet.</p>
<% end %>

<%# Inline add — manual log entries, saved with the person form. %>
<div class="space-y-2 border-t border-gray-100 pt-3">
<%= f.simple_fields_for :notifications, person.notifications.select(&:new_record?) do |nf| %>
<%= render "notifications/notification_fields", f: nf, record: person %>
<% end %>
<%= link_to_add_association f, :notifications,
partial: "notifications/notification_fields",
render_options: { locals: { record: person } },
class: "inline-flex items-center gap-1.5 rounded-md px-2 py-1 text-xs font-medium text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-700 cursor-pointer" do %>
<i class="fa-solid fa-plus text-[0.6rem]"></i>
Add notification
<% end %>
</div>
</div>
</section>
50 changes: 50 additions & 0 deletions spec/requests/people_notifications_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "rails_helper"

RSpec.describe "Person notifications", type: :request do
let(:admin) { create(:user, :admin) }
let(:person) { create(:person) }

before { sign_in admin }

describe "PATCH /people/:id logging a notification" do
it "creates a manual notification log from nested attributes" do
expect {
patch person_path(person), params: {
person: {
notifications_attributes: { "0" => { channel: "phone", email_subject: "Left a voicemail" } }
}
}
}.to change { person.notifications.count }.by(1)

notification = person.notifications.order(:created_at).last
expect(notification.channel).to eq("phone")
expect(notification.kind).to eq("manual_log")
expect(notification.email_subject).to eq("Left a voicemail")
expect(notification.recipient_email).to eq(person.preferred_email)
expect(notification.noticeable).to eq(person)
end

it "ignores a blank notification with no note" do
expect {
patch person_path(person), params: {
person: {
notifications_attributes: { "0" => { channel: "email", email_subject: "" } }
}
}
}.not_to change(Notification, :count)
end
end

describe "GET /people/:id/edit" do
it "lists past communications addressed to the person" do
person = create(:person, user: nil, email: "comms@example.com")
create(:notification, recipient_email: "comms@example.com", email_subject: "Welcome aboard",
kind: "manual_log", recipient_role: "person", notification_type: 0)

get edit_person_path(person)

expect(response.body).to include("Communications")
expect(response.body).to include("Welcome aboard")
end
end
end