Skip to content

Commit 63f4c0e

Browse files
committed
add mailer for collection invitations
1 parent 5dc4094 commit 63f4c0e

File tree

11 files changed

+123
-0
lines changed

11 files changed

+123
-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')
98100
else
99101
redirect_to @collection, alert: share_message.errors.full_messages.join(', ')
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,24 @@
439439
let(:post_request) { post :share, params: }
440440
let(:params) { {id: collection.id, user: recipient.email} }
441441
let(:recipient) { create(:user) }
442+
let(:mailer) { instance_double(ActionMailer::MessageDelivery) }
443+
444+
before do
445+
allow(CollectionInvitationMailer).to receive(:send_invitation).and_return(mailer)
446+
allow(mailer).to receive(:deliver_later)
447+
end
442448

443449
shared_examples 'success' do
444450
it 'creates a message' do
445451
expect { post_request }.to change(Message, :count).by(1)
446452
end
447453

454+
it 'sends an invitation email' do
455+
post_request
456+
expect(CollectionInvitationMailer).to have_received(:send_invitation).with(collection, recipient)
457+
expect(mailer).to have_received(:deliver_later)
458+
end
459+
448460
it 'redirects to collection' do
449461
post_request
450462
expect(response).to redirect_to collection
@@ -460,6 +472,11 @@
460472
expect { post_request }.not_to change(Message, :count)
461473
end
462474

475+
it 'does not send an invitation email' do
476+
post_request
477+
expect(CollectionInvitationMailer).not_to have_received(:send_invitation)
478+
end
479+
463480
it 'redirects to collection' do
464481
post_request
465482
expect(response).to redirect_to collection
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe CollectionInvitationMailer do
6+
describe '#send_invitation' do
7+
subject(:invitation_email) { described_class.with(collection:, recipient: user).send_invitation }
8+
9+
let(:collection) { create(:collection) }
10+
let(:user) { create(:user) }
11+
12+
it 'sends an email to the correct recipient' do
13+
expect(invitation_email.to).to include(user.email)
14+
end
15+
16+
it 'has the correct subject' do
17+
expect(invitation_email.subject).to include(collection.title)
18+
end
19+
20+
it 'contains the correct content' do
21+
expect(invitation_email.body.encoded).to include(user.name)
22+
expect(invitation_email.body.encoded).to include(collection.title)
23+
expect(invitation_email.body.encoded).to include('collaborate')
24+
end
25+
26+
context 'with different locales' do
27+
before do
28+
user.update(preferred_locale: 'de')
29+
end
30+
31+
it 'uses the user\'s preferred locale' do
32+
expect(invitation_email.body.encoded).to include('Sammlung')
33+
expect(invitation_email.subject).to include('Einladung zur Sammlung')
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)