Merge pull request #4301 from consul/unsubscribe
Include link to unsubscribe in email notifications
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
@import "sdg/**/*";
|
||||
@import "sdg_management/*";
|
||||
@import "sdg_management/**/*";
|
||||
@import "subscriptions";
|
||||
@import "widgets/**/*";
|
||||
|
||||
@import "custom";
|
||||
|
||||
@@ -74,7 +74,8 @@ main {
|
||||
&.sdg-goals-index,
|
||||
&.sdg-goal-show,
|
||||
&.topic-edit,
|
||||
&.topic-new {
|
||||
&.topic-new,
|
||||
&.subscriptions-edit {
|
||||
@include grid-column-gutter;
|
||||
}
|
||||
}
|
||||
|
||||
5
app/assets/stylesheets/subscriptions.scss
Normal file
5
app/assets/stylesheets/subscriptions.scss
Normal file
@@ -0,0 +1,5 @@
|
||||
.subscriptions-edit {
|
||||
form {
|
||||
max-width: $global-width * 7 / 12;
|
||||
}
|
||||
}
|
||||
13
app/components/subscriptions/edit_component.html.erb
Normal file
13
app/components/subscriptions/edit_component.html.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<main class="subscriptions-edit">
|
||||
<%= form_for user, url: subscriptions_path(token: user.subscriptions_token) do |f| %>
|
||||
<h2><%= t("account.show.notifications") %></h2>
|
||||
|
||||
<div><%= f.check_box :email_on_comment %></div>
|
||||
<div><%= f.check_box :email_on_comment_reply %></div>
|
||||
<div><%= f.check_box :newsletter %></div>
|
||||
<div><%= f.check_box :email_digest %></div>
|
||||
<div><%= f.check_box :email_on_direct_message %></div>
|
||||
|
||||
<%= f.submit t("account.show.save_changes_submit"), class: "button margin-top" %>
|
||||
<% end %>
|
||||
</main>
|
||||
7
app/components/subscriptions/edit_component.rb
Normal file
7
app/components/subscriptions/edit_component.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class Subscriptions::EditComponent < ApplicationComponent
|
||||
attr_reader :user
|
||||
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
end
|
||||
38
app/controllers/subscriptions_controller.rb
Normal file
38
app/controllers/subscriptions_controller.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
class SubscriptionsController < ApplicationController
|
||||
before_action :set_user, :set_user_locale
|
||||
skip_authorization_check
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def update
|
||||
@user.update!(subscriptions_params)
|
||||
redirect_to edit_subscriptions_path(token: @user.subscriptions_token),
|
||||
notice: t("flash.actions.save_changes.notice")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_user
|
||||
@user = if params[:token].present?
|
||||
User.find_by!(subscriptions_token: params[:token])
|
||||
else
|
||||
current_user || raise(CanCan::AccessDenied)
|
||||
end
|
||||
end
|
||||
|
||||
def subscriptions_params
|
||||
params.require(:user).permit(allowed_params)
|
||||
end
|
||||
|
||||
def allowed_params
|
||||
[:email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter]
|
||||
end
|
||||
|
||||
def set_user_locale
|
||||
if params[:locale].blank?
|
||||
I18n.locale = I18n.available_locales.find { |locale| locale == @user.locale&.to_sym }
|
||||
session[:locale] = I18n.locale
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,6 +9,7 @@ class Mailer < ApplicationMailer
|
||||
@comment = comment
|
||||
@commentable = comment.commentable
|
||||
@email_to = @commentable.author.email
|
||||
manage_subscriptions_token(@commentable.author)
|
||||
|
||||
with_user(@commentable.author) do
|
||||
subject = t("mailers.comment.subject", commentable: t("activerecord.models.#{@commentable.class.name.underscore}", count: 1).downcase)
|
||||
@@ -19,6 +20,7 @@ class Mailer < ApplicationMailer
|
||||
def reply(reply)
|
||||
@email = ReplyEmail.new(reply)
|
||||
@email_to = @email.to
|
||||
manage_subscriptions_token(@email.recipient)
|
||||
|
||||
with_user(@email.recipient) do
|
||||
mail(to: @email_to, subject: @email.subject) if @email.can_be_sent?
|
||||
@@ -41,6 +43,7 @@ class Mailer < ApplicationMailer
|
||||
@direct_message = direct_message
|
||||
@receiver = @direct_message.receiver
|
||||
@email_to = @receiver.email
|
||||
manage_subscriptions_token(@receiver)
|
||||
|
||||
with_user(@receiver) do
|
||||
mail(to: @email_to, subject: t("mailers.direct_message_for_receiver.subject"))
|
||||
@@ -60,6 +63,7 @@ class Mailer < ApplicationMailer
|
||||
def proposal_notification_digest(user, notifications)
|
||||
@notifications = notifications
|
||||
@email_to = user.email
|
||||
manage_subscriptions_token(user)
|
||||
|
||||
with_user(user) do
|
||||
mail(to: @email_to, subject: t("mailers.proposal_notification_digest.title", org_name: Setting["org_name"]))
|
||||
@@ -116,6 +120,7 @@ class Mailer < ApplicationMailer
|
||||
def newsletter(newsletter, recipient_email)
|
||||
@newsletter = newsletter
|
||||
@email_to = recipient_email
|
||||
manage_subscriptions_token(User.find_by(email: @email_to))
|
||||
|
||||
mail(to: @email_to, from: @newsletter.from, subject: @newsletter.subject)
|
||||
end
|
||||
@@ -150,4 +155,9 @@ class Mailer < ApplicationMailer
|
||||
mail.perform_deliveries = false
|
||||
end
|
||||
end
|
||||
|
||||
def manage_subscriptions_token(user)
|
||||
user.add_subscriptions_token
|
||||
@token = user.subscriptions_token
|
||||
end
|
||||
end
|
||||
|
||||
@@ -415,6 +415,10 @@ class User < ApplicationRecord
|
||||
devise_mailer.send(notification, self, *args).deliver_later
|
||||
end
|
||||
|
||||
def add_subscriptions_token
|
||||
update!(subscriptions_token: SecureRandom.base58(32)) if subscriptions_token.blank?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clean_document_number
|
||||
|
||||
@@ -18,6 +18,13 @@
|
||||
</p>
|
||||
|
||||
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 12px;font-weight: normal;line-height: 20px;">
|
||||
<%= t("mailers.config.manage_email_subscriptions") %> <%= link_to t("account.show.title"), account_url, style: "color: #2895F1; text-decoration:none;" %>
|
||||
<%= sanitize(t("mailers.config.unsubscribe_text",
|
||||
notifications: link_to(
|
||||
t("mailers.config.notifications_link"),
|
||||
edit_subscriptions_url(token: @token),
|
||||
style: "color: #2895F1; text-decoration: none;"
|
||||
),
|
||||
notification: User.human_attribute_name(:email_on_comment)
|
||||
)) %>
|
||||
</p>
|
||||
</td>
|
||||
|
||||
@@ -26,9 +26,10 @@
|
||||
<tr>
|
||||
<td style="padding-left: 10px;">
|
||||
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px; margin: 0; font-style: italic; padding-bottom: 20px;">
|
||||
<%= sanitize(t("mailers.direct_message_for_receiver.unsubscribe",
|
||||
account: link_to(t("mailers.direct_message_for_receiver.unsubscribe_account"),
|
||||
account_url, style: "color: #2895F1; text-decoration: none;"))) %>
|
||||
<%= sanitize(t("mailers.direct_message_for_receiver.unsubscribe_text",
|
||||
notifications: link_to(t("mailers.config.notifications_link"),
|
||||
edit_subscriptions_url(token: @token),
|
||||
style: "color: #2895F1; text-decoration: none;"))) %>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -2,4 +2,15 @@
|
||||
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;line-height: 24px;">
|
||||
<%= auto_link_already_sanitized_html wysiwyg(@newsletter.body) %>
|
||||
</p>
|
||||
|
||||
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 12px;font-weight: normal;line-height: 20px;">
|
||||
<%= sanitize(t("mailers.config.unsubscribe_text",
|
||||
notifications: link_to(
|
||||
t("mailers.config.notifications_link"),
|
||||
edit_subscriptions_url(token: @token),
|
||||
style: "color: #2895F1; text-decoration: none;"
|
||||
),
|
||||
notification: User.human_attribute_name(:newsletter)
|
||||
)) %>
|
||||
</p>
|
||||
</td>
|
||||
|
||||
@@ -70,9 +70,10 @@
|
||||
<tr>
|
||||
<td style="padding-left: 10px;">
|
||||
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px; margin: 0; font-style: italic; padding-bottom: 20px;">
|
||||
<%= sanitize(t("mailers.proposal_notification_digest.unsubscribe",
|
||||
account: link_to(t("mailers.proposal_notification_digest.unsubscribe_account"),
|
||||
account_url, style: "color: #2895F1; text-decoration: none;"))) %>
|
||||
<%= sanitize(t("mailers.proposal_notification_digest.unsubscribe_text",
|
||||
notifications: link_to(t("mailers.config.notifications_link"),
|
||||
edit_subscriptions_url(token: @token),
|
||||
style: "color: #2895F1; text-decoration: none;"))) %>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -18,6 +18,13 @@
|
||||
</div>
|
||||
|
||||
<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 12px;font-weight: normal;line-height: 20px;">
|
||||
<%= t("mailers.config.manage_email_subscriptions") %> <%= link_to t("account.show.title"), account_url, style: "color: #2895F1; text-decoration:none;" %>
|
||||
<%= sanitize(t("mailers.config.unsubscribe_text",
|
||||
notifications: link_to(
|
||||
t("mailers.config.notifications_link"),
|
||||
edit_subscriptions_url(token: @token),
|
||||
style: "color: #2895F1; text-decoration: none;"
|
||||
),
|
||||
notification: User.human_attribute_name(:email_on_comment_reply)
|
||||
)) %>
|
||||
</p>
|
||||
</td>
|
||||
|
||||
1
app/views/subscriptions/edit.html.erb
Normal file
1
app/views/subscriptions/edit.html.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= render Subscriptions::EditComponent.new(@user) %>
|
||||
Reference in New Issue
Block a user