Add system_notification sending email feature

This commit is contained in:
Urban Rotnik 2019-02-14 12:51:30 +01:00
parent 8e905b67b2
commit 29d77ca81d
8 changed files with 65 additions and 3 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AppMailer < Devise::Mailer
helper :application, :mailer, :input_sanitize
include Devise::Controllers::UrlHelpers
@ -20,4 +22,17 @@ class AppMailer < Devise::Mailer
}.merge(opts)
mail(headers)
end
def system_notification(user, system_notification)
@user = user
@system_notification = system_notification
headers = {
to: @user.email,
subject: "[#{t('system_notifications.emails.title')}] "\
"#{@system_notification.title}"
}
mail(headers)
end
end

View file

@ -6,8 +6,15 @@ class UserSystemNotification < ApplicationRecord
scope :unseen, -> { where(seen_at: nil) }
def self.mark_as_seen(notifications_id)
where(system_notification_id: notifications_id)
def send_email
if user.system_message_email_notification
AppMailer
.system_notification(user, system_notification)
end
end
def self.mark_as_seen(notifications)
where(system_notification_id: notifications)
.update_all(seen_at: Time.now)
end

View file

@ -0,0 +1,10 @@
<p>
<%= I18n.t("system_notifications.emails.intro_paragraph", user_name: @user.name) %>
</p>
<p>
<%= link_to @system_notification.title.html_safe, system_notifications_url %>
</p>
<p>
<%= @system_notification.description.html_safe %>
</p>

View file

@ -57,6 +57,8 @@ Rails.application.configure do
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
config.action_mailer.preview_path = "#{Rails.root}/test/mailers/previews"
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load

View file

@ -56,7 +56,7 @@ Rails.application.configure do
config.action_mailer.default_url_options = { host: Rails.application.secrets.mail_server_url }
config.action_mailer.default_options = { from: Rails.application.secrets.mail_from }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: Rails.application.secrets.mailer_address,

View file

@ -1292,6 +1292,9 @@ en:
create:
success_flash: "Successfully added sample group <strong>%{sample_group}</strong> to team <strong>%{team}</strong>."
system_notifications:
emails:
title: "What's new notification"
intro_paragraph: "Hello %{user_name}, you received new What's new notification!"
index:
whats_new: "What's New"
more_notifications: "More system notifications"

View file

@ -15,6 +15,25 @@ describe UserSystemNotification do
it { is_expected.to belong_to(:system_notification) }
end
describe '.send_email' do
context 'when user has enabled notifications' do
it 'delivers new email' do
allow(user_system_notification.user)
.to receive(:system_message_email_notification).and_return(true)
expect { user_system_notification.send_email&.deliver_now }
.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
context 'when user has disabled notifications' do
it 'doesn\'t deliver new email' do
expect { user_system_notification.send_email&.deliver_now }
.not_to(change { ActionMailer::Base.deliveries.count })
end
end
end
describe 'Methods' do
let(:notifcation_one) { create :system_notification }
let(:notifcation_two) { create :system_notification }

View file

@ -82,6 +82,12 @@ class AppMailerPreview < ActionMailer::Preview
)
end
def system_notification
sn = FactoryBot.build(:system_notification)
user = FactoryBot.build(:user)
AppMailer.system_notification(user, sn)
end
private
def fake_user