Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AO3-6774 Create invite_increase_notification mailer preview #5025

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ Rails/OutputSafety:

Rails/Output:
Exclude:
# Allow patches to print warnings to console:
- 'config/initializers/monkeypatches/*.rb'
# Allow migrations to print pt-osc comments to console:
- 'db/migrate/*.rb'

Expand Down
10 changes: 4 additions & 6 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@ def batch_subscription_notification(subscription_id, entries)
def invite_increase_notification(user_id, total)
@user = User.find(user_id)
@total = total
I18n.with_locale(@user.preference.locale.iso) do
mail(
to: @user.email,
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
)
end
mail(
to: @user.email,
subject: default_i18n_subject(app_name: ArchiveConfig.APP_SHORT_NAME)
)
end

# Emails a user to say that their request for invitation codes has been declined
Expand Down
8 changes: 6 additions & 2 deletions app/models/invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def self.grant_all(total)
total.times do
user.invitations.create
end
UserMailer.invite_increase_notification(user.id, total).deliver_later
I18n.with_locale(user.preference.locale.iso) do
UserMailer.invite_increase_notification(user.id, total).deliver_later
end
end
User.out_of_invites.update_all('out_of_invites = 0')
end
Expand All @@ -43,7 +45,9 @@ def self.grant_empty(total)
total.times do
user.invitations.create
end
UserMailer.invite_increase_notification(user.id, total).deliver_later
I18n.with_locale(user.preference.locale.iso) do
UserMailer.invite_increase_notification(user.id, total).deliver_later
end
end
User.out_of_invites.update_all('out_of_invites = 0')
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/user_invite_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def grant_request
self.quantity.times do
self.user.invitations.create
end
UserMailer.invite_increase_notification(self.user.id, self.quantity).deliver_after_commit
I18n.with_locale(self.user.preference.locale.iso) do
UserMailer.invite_increase_notification(self.user.id, self.quantity).deliver_after_commit
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :message do %>
<p><%= t("mailer.general.greeting.informal.addressed_html", name: style_bold(@user.login)) %></p>

<p><%= t(".html.body", count: @total, invitation_page_link: style_link(t(".invitation_page_link_text"), user_invitations_url(@user))).html_safe %></p>
<p><%= t(".body.html", count: @total, invitation_page_link: style_link(t(".invitation_page_link_text"), user_invitations_url(@user))) %></p>

<p>
<%= t("mailer.general.closing.informal") %><br />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :message do %>
<%= t("mailer.general.greeting.informal.addressed_html", name: @user.login) %>

<%= t(".text.body", count: @total, invitation_page_url: user_invitations_url(@user)) %>
<%= t(".body.text", count: @total, invitation_page_url: user_invitations_url(@user)) %>

<%= t("mailer.general.closing.informal") %>
<%= t("mailer.general.signature.app_short_name") %>
Expand Down
26 changes: 26 additions & 0 deletions config/initializers/monkeypatches/deliver_after_commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,29 @@ def deliver_after_commit
end
end
end

module AfterCommitEverywhereWithLocale
def initialize(connection: ActiveRecord::Base.connection, **handlers)
@connection = connection
@handlers = handlers
@locale = I18n.locale
end

def before_committed!(*)
I18n.with_locale(@locale) { @handlers[:before_commit]&.call }
end

def committed!(*)
I18n.with_locale(@locale) { @handlers[:after_commit]&.call }
end

def rolledback!(*)
I18n.with_locale(@locale) { @handlers[:after_rollback]&.call }
end
end

if AfterCommitEverywhere::VERSION == "1.4.0"
Copy link
Member

Choose a reason for hiding this comment

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

There are some Rubocop warnings on this file. You should fix the Layout/TrailingEmptyLines one.

The Rails/Output I think is actually fine. You can update Rubocop's rules from:

otwarchive/.rubocop.yml

Lines 155 to 158 in 8467810

Rails/Output:
Exclude:
# Allow migrations to print pt-osc comments to console:
- 'db/migrate/*.rb'

to:

Rails/Output:
  Exclude:
    # Allow patches to print warnings to console:
    - 'config/initializers/monkeypatches/*.rb'
    # Allow migrations to print pt-osc comments to console:
    - 'db/migrate/*.rb'

AfterCommitEverywhere::Wrap.prepend(AfterCommitEverywhereWithLocale)
else
puts "WARNING: The monkeypatch #{__FILE__} was written for version 1.4.0 of the after_commit_everywhere gem, but you are running #{AfterCommitEverywhere::VERSION}. Please update or remove the monkeypatch."
end
11 changes: 5 additions & 6 deletions config/locales/mailers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,15 @@ en:
text: If you would like Open Doors to update the redirect to point to your pre-existing work, please delete the imported copy, and contact Open Doors at %{open_doors_link} with your AO3 account name, your account name on the imported archive, and the title and URL of the fanwork you would like the redirect to point to. (If you have multiple works you would like to change the redirects for, you can list these in one email.)
uploaded_list: 'The works uploaded include:'
invite_increase_notification:
html:
body:
body:
html:
one: We just wanted to let you know that you have %{count} new invitation, which can be used to create a new account at the Archive. You can invite a friend at %{invitation_page_link}.
other: We just wanted to let you know that you have %{count} new invitations, which can be used to create new accounts at the Archive. You can invite a friend at %{invitation_page_link}.
invitation_page_link_text: your invitations page
subject: "[%{app_name}] New invitations"
text:
body:
text:
one: We just wanted to let you know that you have %{count} new invitation, which can be used to create a new account at the Archive. You can invite a friend at your invitations page (%{invitation_page_url}).
other: We just wanted to let you know that you have %{count} new invitations, which can be used to create new accounts at the Archive. You can invite a friend at your invitations page (%{invitation_page_url}).
invitation_page_link_text: your invitations page
subject: "[%{app_name}] New invitations"
invite_request_declined:
main:
one: We regret to inform you that your request for a new invitation cannot be fulfilled at this time.
Expand Down
12 changes: 12 additions & 0 deletions features/other_a/invite_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,15 @@ Feature: Invite requests
Then "notuser1" should be emailed
And the email should have "Additional invitation request declined" in the subject
And the email to "notuser1" should be non-translated

Scenario: Translated email is sent when new invitation is given to registered user
Given a locale with translated emails
And invitations are required
And the user "user1" exists and is activated
And the user "user1" enables translated emails
And all emails have been delivered
When as "user1" I request some invites
And an admin grants the request
Then "user1" should be emailed
And the email should have "New invitations" in the subject
And the email to "user1" should be translated
6 changes: 6 additions & 0 deletions test/mailers/previews/user_mailer_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def change_email
UserMailer.change_email(user.id, old_email, new_email)
end

def invite_increase_notification
user = create(:user, :for_mailer_preview)
total = params[:total] || 1
UserMailer.invite_increase_notification(user.id, total.to_i)
end

private

def creatorship_notification_data(creation_type)
Expand Down
Loading