Skip to content

Commit 13812ce

Browse files
committed
add mailer for collection invitations
1 parent 5f486c5 commit 13812ce

File tree

11 files changed

+122
-0
lines changed

11 files changed

+122
-0
lines changed

app/controllers/collections_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def download_all
9494

9595
def share
9696
if share_message.save
97+
recipient = User.find_by(email: params[:user])
98+
CollectionInvitationMailer.with(collection: @collection, recipient:).send_invitation.deliver_later
9799
redirect_to @collection, notice: t('.success_notice'), status: :see_other
98100
else
99101
redirect_to @collection, alert: share_message.errors.full_messages.join(', '), status: :see_other
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class CollectionInvitationMailer < ApplicationMailer
4+
def send_invitation
5+
@collection = params.fetch(:collection)
6+
@recipient = params.fetch(:recipient)
7+
8+
I18n.with_locale(@recipient.preferred_locale || I18n.default_locale) do
9+
mail(to: @recipient.email, subject: t('collections.invitation_mailer.subject', collection: @collection.title))
10+
end
11+
end
12+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
h3 = t('collections.invitation_mailer.greeting', user: @recipient.name)
3+
p = t('collections.invitation_mailer.invitation_message', collection: @collection.title)
4+
p = t('collections.invitation_mailer.collaboration_details')
5+
p = t('collections.invitation_mailer.view_collection_instructions')
6+
p = link_to t('collections.invitation_mailer.view_collection'), collection_url(@collection)
7+
p = t('collections.invitation_mailer.contact_info')
8+
p = t('collections.invitation_mailer.thank_you')
9+
p = t('collections.invitation_mailer.team_signature', app_name: t('application.name'))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
= t('collections.invitation_mailer.greeting', user: @recipient.name)
2+
3+
= t('collections.invitation_mailer.invitation_message', collection: @collection.title)
4+
5+
= t('collections.invitation_mailer.collaboration_details')
6+
7+
= t('collections.invitation_mailer.view_collection_instructions')
8+
= collection_url(@collection)
9+
10+
= t('collections.invitation_mailer.contact_info')
11+
12+
= t('collections.invitation_mailer.thank_you')
13+
= t('collections.invitation_mailer.team_signature', app_name: t('application.name'))

config/locales/de/views/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ de:
1616
markdown_editor:
1717
collapse: Editor zuklappen
1818
expand: Editor aufklappen
19+
name: CodeHarbor
1920
navigation:
2021
rails_admin: Rails Admin
2122
session:

config/locales/de/views/collections.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ de:
1818
mine: Meine Sammlungen
1919
public: Öffentliche Sammlungen
2020
view_shared: Geteilte Sammlung anzeigen
21+
invitation_mailer:
22+
collaboration_details: Diese Zusammenarbeit ermöglicht es Ihnen, zum Inhalt dieser Sammlung beizutragen und bei der Verwaltung zu helfen.
23+
contact_info: Wenn Sie Fragen zu dieser Einladung oder Ihrer Rolle als Mitarbeiter haben, wenden Sie sich bitte direkt an den Besitzer der Sammlung.
24+
greeting: Hallo %{user},
25+
invitation_message: Sie wurden eingeladen, an der Sammlung "%{collection}" mitzuarbeiten.
26+
subject: 'Einladung zur Sammlung: %{collection}'
27+
team_signature: "%{app_name} Team"
28+
thank_you: Vielen Dank,
29+
view_collection: Sammlung anzeigen
30+
view_collection_instructions: 'Um die Sammlung anzuzeigen und mit Ihrer Zusammenarbeit zu beginnen, klicken Sie auf den Link unten:'
2131
new:
2232
header: Neue Sammlung
2333
shared:

config/locales/en/views/application.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ en:
1616
markdown_editor:
1717
collapse: Collapse editor
1818
expand: Expand editor
19+
name: CodeHarbor
1920
navigation:
2021
rails_admin: Rails admin
2122
session:

config/locales/en/views/collections.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ en:
1818
mine: My Collections
1919
public: Public Collections
2020
view_shared: View Shared Collection
21+
invitation_mailer:
22+
collaboration_details: This collaboration will allow you to contribute to and help manage this collection's content.
23+
contact_info: If you have any questions about this invitation or your role as a collaborator, please contact the collection owner directly.
24+
greeting: Hi %{user},
25+
invitation_message: You've been invited to collaborate on the collection "%{collection}".
26+
subject: 'Invitation to collection: %{collection}'
27+
team_signature: "%{app_name} Team"
28+
thank_you: Thank you,
29+
view_collection: View collection
30+
view_collection_instructions: 'To view the collection and get started with your collaboration, click the link below:'
2131
new:
2232
header: New Collection
2333
shared:

spec/controllers/collections_controller_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@
445445
expect { post_request }.to change(Message, :count).by(1)
446446
end
447447

448+
it 'sends an invitation email' do
449+
expect { post_request }
450+
.to have_enqueued_mail(CollectionInvitationMailer, :send_invitation)
451+
.with(params: {collection:, recipient:}, args: [])
452+
end
453+
448454
it 'redirects to collection' do
449455
post_request
450456
expect(response).to redirect_to collection
@@ -460,6 +466,12 @@
460466
expect { post_request }.not_to change(Message, :count)
461467
end
462468

469+
it 'does not send an invitation email' do
470+
expect { post_request }
471+
.not_to have_enqueued_mail(CollectionInvitationMailer, :send_invitation)
472+
.with(params: {collection:, recipient:}, args: [])
473+
end
474+
463475
it 'redirects to collection' do
464476
post_request
465477
expect(response).to redirect_to collection
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
require 'cgi'
5+
6+
RSpec.describe CollectionInvitationMailer do
7+
describe '#send_invitation' do
8+
subject(:invitation_email) { described_class.with(collection:, recipient: user).send_invitation }
9+
10+
let(:collection) { create(:collection) }
11+
let(:user) { create(:user) }
12+
13+
it 'sends an email to the correct recipient' do
14+
expect(invitation_email.to).to include(user.email)
15+
end
16+
17+
it 'has the correct subject' do
18+
expect(invitation_email.subject).to include(collection.title)
19+
end
20+
21+
it 'contains the correct content' do
22+
body = CGI.unescapeHTML(invitation_email.body.encoded)
23+
expect(body).to include(user.name)
24+
expect(body).to include(collection.title)
25+
expect(body).to include(I18n.t('collections.invitation_mailer.invitation_message', collection:))
26+
end
27+
28+
context 'with different locales' do
29+
let(:locale) { 'de' }
30+
31+
before do
32+
user.update(preferred_locale: locale)
33+
end
34+
35+
it 'uses the user\'s preferred locale' do
36+
expect(invitation_email.body.encoded).to include('Sammlung')
37+
expect(invitation_email.subject).to include(I18n.t('collections.invitation_mailer.subject', collection:, locale:))
38+
end
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)