Skip to content

Commit b0753a2

Browse files
authored
Notification refresh (#626)
* Generate Notifications/mailers and run a job via a service every time StoryIdea, WorkshopIdea, WorkshopVariation, EventRegistration is created * Add some includes to bookmark endpoints to try and remove n+1's * Update action_mailer and mail-related settings (and delete mail.rb initializer) * Add class for SectorImpact bc it's needed for rake tasks * Rubocop and test fixes * Prune workshop_log_decorator
1 parent 90aa018 commit b0753a2

35 files changed

+733
-46
lines changed

app/controllers/bookmarks_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class BookmarksController < ApplicationController
33

44
def index
55
per_page = params[:number_of_items_per_page] || 25
6-
unfiltered = Bookmark.all
6+
unfiltered = Bookmark.includes(:primary_asset, :gallery_assets, :windows_type)
77
filtered = unfiltered.search(params)
88
filtered = filtered.sorted(params[:sort])
99

@@ -24,7 +24,8 @@ def personal
2424
@user_name = user.full_name if user
2525
@viewing_self = user == current_user
2626

27-
bookmarks = Bookmark.search(params, user: user)
27+
bookmarks = Bookmark.includes(bookmarkable: [ :primary_asset, :gallery_assets, :windows_type ])
28+
bookmarks = bookmarks.search(params, user: user)
2829
bookmarks = bookmarks.sorted(params[:sort])
2930

3031
@bookmarks_count = bookmarks.length

app/controllers/event_registrations_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def create
2424
@event_registration = EventRegistration.new(event_registration_params)
2525

2626
if @event_registration.save
27+
NotificationServices::CreateNotification.call(
28+
noticeable: @event_registration,
29+
kind: :record_created,
30+
recipient_role: (current_user.super_user? ? :admin : :facilitator),
31+
recipient_email: current_user.email,
32+
notification_type: 0)
33+
2734
respond_to do |format|
2835
format.html {
2936
redirect_to event_registrations_path,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class NotificationsController < ApplicationController
2+
before_action :set_notification, only: [ :show ]
3+
4+
def index
5+
per_page = params[:number_of_items_per_page].presence || 25
6+
@notifications =
7+
if current_user.super_user?
8+
Notification.all
9+
else
10+
Notification.where(recipient_email: current_user.email)
11+
end
12+
13+
@notifications = @notifications
14+
.includes(:noticeable)
15+
.order(created_at: :desc)
16+
.paginate(page: params[:page], per_page: per_page)
17+
end
18+
19+
def show
20+
end
21+
22+
private
23+
24+
def set_notification
25+
@notification = Notification.find(params[:id])
26+
end
27+
end

app/controllers/story_ideas_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def create
2828
@story_idea = StoryIdea.new(story_idea_params)
2929

3030
if @story_idea.save
31+
NotificationServices::CreateNotification.call(
32+
noticeable: @story_idea,
33+
kind: :record_created,
34+
recipient_role: (current_user.super_user? ? :admin : :facilitator),
35+
recipient_email: current_user.email,
36+
notification_type: 0)
3137
redirect_to story_ideas_path, notice: "StoryIdea was successfully created."
3238
else
3339
set_form_variables

app/controllers/workshop_ideas_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ def create
2424
@workshop_idea = WorkshopIdea.new(workshop_idea_params)
2525

2626
if @workshop_idea.save
27+
NotificationServices::CreateNotification.call(
28+
noticeable: @story_idea,
29+
kind: :record_created,
30+
recipient_role: (current_user.super_user? ? :admin : :facilitator),
31+
recipient_email: current_user.email,
32+
notification_type: 0)
2733
redirect_to workshop_ideas_path, notice: "Workshop idea was successfully created."
2834
else
2935
set_form_variables

app/controllers/workshop_logs_controller.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def create
6161
@workshop_log = WorkshopLog.new(workshop_log_params)
6262

6363
if @workshop_log.save
64+
NotificationServices::CreateNotification.call(
65+
noticeable: @workshop_log,
66+
kind: :record_created,
67+
recipient_role: (current_user.super_user? ? :admin : :facilitator),
68+
recipient_email: current_user.email,
69+
notification_type: 0)
70+
6471
flash[:notice] = "Thank you for submitting a workshop log. To see all of your completed logs, please view your Profile."
6572
redirect_to authenticated_root_path
6673
else
@@ -139,12 +146,11 @@ def set_form_variables
139146
@workshop = Workshop.new
140147
end
141148

142-
workshops = if current_user.super_user?
143-
Workshop.all
144-
else
145-
Workshop.published
149+
workshops = Workshop.includes(:windows_type)
150+
unless current_user.super_user?
151+
workshops = workshops.published
146152
end
147-
@workshops = workshops.or(Workshop.where(id: @workshop_log.workshop_id))
153+
@workshops = workshops.or(Workshop.where(id: @workshop_log.workshop_id).includes(:windows_type))
148154
.distinct
149155
.order(title: :asc)
150156

@@ -165,7 +171,7 @@ def set_form_variables
165171
# @files = MediaFile.where(["workshop_log_id = ?", @workshop_log.id])
166172

167173
@windows_type_id = params[:windows_type_id].presence || @workshop.windows_type_id ||
168-
WindowsType.where(short_name: "COMBINED")
174+
WindowsType.where(short_name: "COMBINED").last.id
169175
form = FormBuilder.where(windows_type_id: @windows_type_id)
170176
.first&.forms.first # because there's only one form per form_builder
171177
if form

app/controllers/workshop_variations_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def new
2727
def create
2828
@workshop_variation = WorkshopVariation.new(workshop_variation_params)
2929
if @workshop_variation.save
30+
NotificationServices::CreateNotification.call(
31+
noticeable: @workshop_variation,
32+
kind: :record_created,
33+
recipient_role: (current_user.super_user? ? :admin : :facilitator),
34+
recipient_email: current_user.email,
35+
notification_type: 0)
36+
3037
flash[:notice] = "Workshop Variation has been created."
3138
if params[:from] == "workshop_show"
3239
redirect_to workshop_path(@workshop_variation.workshop, anchor: "workshop-variations")
Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,71 @@
11
class WorkshopLogDecorator < ApplicationDecorator
22
def detail(length: nil)
3-
length ? description&.truncate(length) : description
3+
description = length ? object.description&.truncate(length) : object.description
4+
"#{description}<br>#{participants_table}".html_safe
5+
end
6+
7+
private
8+
9+
def participants_table
10+
children_first = object.children_first_time.to_i
11+
children_ongoing = object.children_ongoing.to_i
12+
13+
teens_first = object.teens_first_time.to_i
14+
teens_ongoing = object.teens_ongoing.to_i
15+
16+
adults_first = object.adults_first_time.to_i
17+
adults_ongoing = object.adults_ongoing.to_i
18+
19+
totals_first = children_first + teens_first + adults_first
20+
totals_ongoing = children_ongoing + teens_ongoing + adults_ongoing
21+
22+
<<~HTML.html_safe
23+
<table width="100%" cellpadding="0" cellspacing="0"
24+
style="border-collapse:collapse; margin-top:12px;">
25+
#{header_row}
26+
#{data_row("Children", children_first, children_ongoing)}
27+
#{data_row("Teens", teens_first, teens_ongoing)}
28+
#{data_row("Adults", adults_first, adults_ongoing)}
29+
#{total_row(totals_first, totals_ongoing)}
30+
</table>
31+
HTML
32+
end
33+
34+
def header_row
35+
%w[Group First\ Time Ongoing Total].map do |label|
36+
"<td style=\"#{cell_style('font-weight:bold;background:#f5f5f5')}\">#{label}</td>"
37+
end.then { |cells| "<tr>#{cells.join}</tr>" }
38+
end
39+
40+
def data_row(label, first, ongoing)
41+
total = first + ongoing
42+
43+
"<tr>" \
44+
"<td style=\"#{cell_style}\">#{label}</td>" \
45+
"<td style=\"#{cell_style}\">#{first}</td>" \
46+
"<td style=\"#{cell_style}\">#{ongoing}</td>" \
47+
"<td style=\"#{cell_style}\">#{total}</td>" \
48+
"</tr>"
49+
end
50+
51+
def total_row(first, ongoing)
52+
total = first + ongoing
53+
54+
"<tr>" \
55+
"<td style=\"#{cell_style('font-weight:bold')}\">Total</td>" \
56+
"<td style=\"#{cell_style('font-weight:bold')}\">#{first}</td>" \
57+
"<td style=\"#{cell_style('font-weight:bold')}\">#{ongoing}</td>" \
58+
"<td style=\"#{cell_style('font-weight:bold')}\">#{total}</td>" \
59+
"</tr>"
60+
end
61+
62+
def cell_style(extra = "")
63+
[
64+
"border:1px solid #ddd",
65+
"padding:6px",
66+
"display:table-cell",
67+
"vertical-align:top",
68+
extra
69+
].reject(&:blank?).join("; ")
470
end
571
end

app/jobs/application_job.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class ApplicationJob < ActiveJob::Base
2+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class NotificationMailerJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform(notification_id)
5+
notification = Notification.find(notification_id)
6+
7+
case notification.kind
8+
when "record_created"
9+
mailer = NotificationMailer.created_notification(notification)
10+
when "record_submitted"
11+
mailer = NotificationMailer.submitted_notification(notification)
12+
end
13+
14+
return if notification.delivered_at.present? # Idempotency guard
15+
16+
mailer.deliver_now
17+
18+
persist_email(notification, mailer)
19+
end
20+
end
21+
22+
private
23+
24+
def persist_email(notification, mail)
25+
notification.update!(
26+
email_subject: mail.subject,
27+
email_body_html: mail.html_part&.body&.decoded,
28+
email_body_text: mail.text_part&.body&.decoded,
29+
delivered_at: Time.current
30+
)
31+
end

0 commit comments

Comments
 (0)