Commit d0add18a authored by Kaspar Vollenweider's avatar Kaspar Vollenweider

Merge branch 'feat/email_template_variables_per_kind_with_specific_methods' into 'develop'

Feat/email template variables per kind with specific methods

See merge request !941
parents d4e1950e 5e791665
Pipeline #41121 passed with stage
in 37 minutes and 56 seconds
...@@ -31,10 +31,10 @@ class EmailTemplate < ApplicationRecord ...@@ -31,10 +31,10 @@ class EmailTemplate < ApplicationRecord
{ {
signup: [], signup: [],
assignment: [:Anrede, :Name, :EinsatzTitel, :FeedbackLink], assignment: [:Anrede, :Name, :EinsatzTitel, :FeedbackLink],
trial: ReminderMailing::TEMPLATE_VARNAMES, trial: ReminderMailing.template_varnames(:trial_period).values,
termination: ReminderMailing::TEMPLATE_VARNAMES, termination: ReminderMailing.template_varnames(:termination).values,
half_year_process_email: SemesterProcessMail.template_varnames[:mail], half_year_process_email: ReminderMailing.template_varnames(:half_year_process_email).values,
half_year_process_overdue: SemesterProcessMail.template_varnames[:reminder] half_year_process_overdue: ReminderMailing.template_varnames(:half_year_process_overdue).values
} }
end end
......
class ReminderMailing < ApplicationRecord class ReminderMailing < ApplicationRecord
before_update :remove_untoggled_volunteers # Key is method name, value is users variable %{FeedbackLink}
TEMPLATE_VARNAMES_GENERAL = {
TEMPLATE_VARNAMES = [ anrede: :Anrede,
:Anrede, name: :Name,
:Name, einsatz: :Einsatz,
:Einsatz, email_absender: :EmailAbsender,
:EinsatzStart, online_plattform_url: :OnlinePlattformUrl
:FeedbackLink, }.freeze
:EmailAbsender,
:OnlinePlattformUrl # method var_name pairs per reminder_mailing kind
].freeze TEMPLATE_VARNAMES = {
trial_period: {
einsatz_start: :EinsatzStart,
feedback_link_trial: :FeedbackLink
}.merge(TEMPLATE_VARNAMES_GENERAL),
termination: {
einsatz_start: :EinsatzStart,
feedback_link_termination: :FeedbackLink
}.merge(TEMPLATE_VARNAMES_GENERAL),
half_year_process_email: {
feedback_link_semester: :FeedbackLink
}.merge(TEMPLATE_VARNAMES_GENERAL),
half_year_process_overdue: {
feedback_link_semester: :FeedbackLink
}.merge(TEMPLATE_VARNAMES_GENERAL)
}.freeze
belongs_to :creator, -> { with_deleted }, class_name: 'User', inverse_of: 'reminder_mailings' belongs_to :creator, -> { with_deleted }, class_name: 'User', inverse_of: 'reminder_mailings'
...@@ -26,7 +41,8 @@ class ReminderMailing < ApplicationRecord ...@@ -26,7 +41,8 @@ class ReminderMailing < ApplicationRecord
source_type: 'GroupAssignment' source_type: 'GroupAssignment'
has_many :process_submitters, through: :reminder_mailing_volunteers, source: :process_submitted_by has_many :process_submitters, through: :reminder_mailing_volunteers, source: :process_submitted_by
enum kind: { trial_period: 1, termination: 2, half_year_process_email: 3, half_year_process_overdue: 4 } enum kind: { trial_period: 1, termination: 2, half_year_process_email: 3, half_year_process_overdue: 4 }
ransacker :kind, formatter: ->(value) { kinds[value] } ransacker :kind, formatter: ->(value) { kinds[value] }
validates :subject, presence: true validates :subject, presence: true
...@@ -35,6 +51,16 @@ class ReminderMailing < ApplicationRecord ...@@ -35,6 +51,16 @@ class ReminderMailing < ApplicationRecord
validate :no_reminder_volunteer_present, unless: :reminder_volunteer_mailings_any? validate :no_reminder_volunteer_present, unless: :reminder_volunteer_mailings_any?
validate :mailing_not_to_change_after_sent validate :mailing_not_to_change_after_sent
before_update :remove_untoggled_volunteers
def self.template_varnames(kind_requested)
TEMPLATE_VARNAMES[kind_requested.to_sym]
end
def template_varnames
TEMPLATE_VARNAMES[kind.to_sym]
end
# setter generates relation to assignment/group_assignment and volunteer in one go # setter generates relation to assignment/group_assignment and volunteer in one go
def reminder_mailing_volunteers=(reminder_mailable) def reminder_mailing_volunteers=(reminder_mailable)
if [Assignment, GroupAssignment].include? reminder_mailable.first&.class if [Assignment, GroupAssignment].include? reminder_mailable.first&.class
......
...@@ -71,6 +71,20 @@ class ReminderMailingVolunteer < ApplicationRecord ...@@ -71,6 +71,20 @@ class ReminderMailingVolunteer < ApplicationRecord
template % template_variables template % template_variables
end end
def template_variables
template_variables = reminder_mailing.template_varnames.map do |method_name, var_name|
[var_name, send(method_name)]
end.to_h
template_variables.default = ''
template_variables
end
## template variable converters
#
# there needs to be a method with the name matching each value
# from ReminderMailing::TEMPLATE_VARNAMES
# Otherwise there will be a method missing error if that template variable is used
def anrede def anrede
I18n.t("salutation.#{volunteer.salutation}") I18n.t("salutation.#{volunteer.salutation}")
end end
...@@ -95,30 +109,30 @@ class ReminderMailingVolunteer < ApplicationRecord ...@@ -95,30 +109,30 @@ class ReminderMailingVolunteer < ApplicationRecord
end end
end end
def template_variables
template_variables = ReminderMailing::TEMPLATE_VARNAMES.map do |varname|
[varname, send(varname.to_s.underscore)]
end.to_h
template_variables.default = ''
template_variables
end
def email_absender def email_absender
"[#{reminder_mailing_creator_name}](mailto:"\ "[#{reminder_mailing_creator_name}](mailto:"\
"#{reminder_mailing.creator.email})" "#{reminder_mailing.creator.email})"
end end
def reminder_mailing_creator_name def feedback_link_trial
reminder_mailing.creator.profile&.contact&.natural_name || "[Probezeit-Feedback erstellen](#{feedback_url})"
reminder_mailing.creator.email end
def feedback_link_termination
"[Abschlussevaluations-Feedback erstellen](#{feedback_url})"
end end
def feedback_link def feedback_link_semester
"[Halbjahres-Rapport erstellen](#{feedback_url})" "[Halbjahres-Feedback erstellen](#{feedback_url})"
end end
def online_plattform_url def online_plattform_url
"[Online-Plattform Url](#{Rails.application.routes.url_helpers.root_path})" "[Online-Plattform Url](#{Rails.application.routes.url_helpers.root_path})"
end end
# template variable assisting methods
def reminder_mailing_creator_name
reminder_mailing.creator.profile&.contact&.natural_name ||
reminder_mailing.creator.email
end
end end
...@@ -12,42 +12,60 @@ class SemesterProcessMail < ApplicationRecord ...@@ -12,42 +12,60 @@ class SemesterProcessMail < ApplicationRecord
delegate :volunteer, to: :semester_process_volunteer delegate :volunteer, to: :semester_process_volunteer
delegate :semester_process, to: :semester_process_volunteer delegate :semester_process, to: :semester_process_volunteer
# Key is method name, value is users variable %{FeedbackLink}
TEMPLATE_VARNAMES_GENERAL = {
anrede: :Anrede,
name: :Name,
feedback_link: :FeedbackLink,
email_absender: :EmailAbsender,
online_plattform_url: :OnlinePlattformUrl,
semester_from_to_date: :SemesterAnfangUndEndDatum
}.freeze
TEMPLATE_VARNAMES = {
mail: TEMPLATE_VARNAMES_GENERAL,
reminder: TEMPLATE_VARNAMES_GENERAL
}.freeze
def self.template_varnames(kind = :mail)
TEMPLATE_VARNAMES[kind]
end
TEMPLATE_VARNAMES = [ def template_varnames
:Anrede, TEMPLATE_VARNAMES[kind.to_sym]
:Name,
:Einsatz,
:FeedbackLink,
:EmailAbsender,
:OnlinePlattformUrl
].freeze
def self.template_varnames
{
mail: TEMPLATE_VARNAMES,
reminder: TEMPLATE_VARNAMES
}
end end
def process_template def process_template
{ {
subject: replace_ruby_template(self.subject), subject: replace_ruby_template(subject),
body: replace_ruby_template(self.body) body: replace_ruby_template(body)
} }
end end
private
def send_email
VolunteerMailer.half_year_process_email(self).deliver
update!(sent_at: Time.zone.now)
end
def replace_ruby_template(template) def replace_ruby_template(template)
template % template_variables template % template_variables
end end
def template_variables def template_variables
template_variables = SemesterProcessMail::template_varnames[self.kind.to_sym].map do |varname| template_variables = template_varnames.map do |method_name, var_name|
[varname, send(varname.to_s.underscore)] [var_name, send(method_name)]
end.to_h end.to_h
template_variables.default = '' template_variables.default = ''
template_variables template_variables
end end
## template variable converters
#
# there needs to be a method with the name matching each value
# from SemesterProcessMail::TEMPLATE_VARNAMES
# Otherwise there will be a method missing error if that template variable is used
def anrede def anrede
I18n.t("salutation.#{volunteer.salutation}") I18n.t("salutation.#{volunteer.salutation}")
end end
...@@ -56,10 +74,6 @@ class SemesterProcessMail < ApplicationRecord ...@@ -56,10 +74,6 @@ class SemesterProcessMail < ApplicationRecord
volunteer.contact.natural_name volunteer.contact.natural_name
end end
def einsatz
''
end
def email_absender def email_absender
"[#{reminder_mailing_creator_name}](mailto:"\ "[#{reminder_mailing_creator_name}](mailto:"\
"#{semester_process.creator.email})" "#{semester_process.creator.email})"
...@@ -67,14 +81,14 @@ class SemesterProcessMail < ApplicationRecord ...@@ -67,14 +81,14 @@ class SemesterProcessMail < ApplicationRecord
def reminder_mailing_creator_name def reminder_mailing_creator_name
semester_process.creator.profile&.contact&.natural_name || semester_process.creator.profile&.contact&.natural_name ||
semester_process.creator.email semester_process.creator.email
end end
def feedback_link def feedback_link
"[Halbjahres-Rapport erstellen](#{feedback_url})" "[Halbjahres-Rapport erstellen](#{feedback_url})"
end end
def feedback_url(options = {}) def feedback_url
Rails.application.routes.url_helpers.review_semester_review_semester_url( Rails.application.routes.url_helpers.review_semester_review_semester_url(
semester_process_volunteer, semester_process_volunteer,
ActionMailer::Base.default_url_options ActionMailer::Base.default_url_options
...@@ -85,10 +99,8 @@ class SemesterProcessMail < ApplicationRecord ...@@ -85,10 +99,8 @@ class SemesterProcessMail < ApplicationRecord
"[Online-Plattform Url](#{Rails.application.routes.url_helpers.root_path})" "[Online-Plattform Url](#{Rails.application.routes.url_helpers.root_path})"
end end
private def semester_from_to_date
semester = semester_process_volunteer.semester
def send_email "#{I18n.l(semester.first)} bis #{I18n.l(semester.last)}}"
VolunteerMailer.half_year_process_email(self).deliver
self.update(sent_at: Time.zone.now)
end end
end end
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
= f.input_field :kind, value: @reminder_mailing.kind, as: :hidden = f.input_field :kind, value: @reminder_mailing.kind, as: :hidden
.row .row
.col-xs-12.col-md-4 .col-xs-12.col-md-4
= render 'email_templates/explain_panel', template_varnames: ReminderMailing::TEMPLATE_VARNAMES = render 'email_templates/explain_panel', template_varnames: @reminder_mailing.template_varnames.values
.col-xs-12.col-md-8 .col-xs-12.col-md-8
= render 'email_templates/subject_body_inputs', f: f = render 'email_templates/subject_body_inputs', f: f
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
.row .row
.col-xs-12.col-md-4 .col-xs-12.col-md-4
= render 'email_templates/explain_panel', template_varnames: SemesterProcessMail::template_varnames[:mail] = render 'email_templates/explain_panel', template_varnames: SemesterProcessMail.template_varnames.values
.col-xs-12.col-md-8 .col-xs-12.col-md-8
= render 'email_templates/subject_body_inputs', f: f = render 'email_templates/subject_body_inputs', f: f
......
...@@ -157,7 +157,8 @@ class ReminderMailingsTest < ApplicationSystemTestCase ...@@ -157,7 +157,8 @@ class ReminderMailingsTest < ApplicationSystemTestCase
assert_equal "Beendigung Gruppenangebot #{group_offer.title} (#{group_offer.department})", assert_equal "Beendigung Gruppenangebot #{group_offer.title} (#{group_offer.department})",
mailer.subject mailer.subject
assert_includes mail_body, "#{group_assignment.volunteer.contact.natural_name} Halbjahres-Rapport erstellen"
assert_includes mail_body, "#{group_assignment.volunteer.contact.natural_name} Abschlussevaluations-Feedback erstellen"
assert_includes mail_body, "#{I18n.l group_assignment.period_start} Gruss, AOZ" assert_includes mail_body, "#{I18n.l group_assignment.period_start} Gruss, AOZ"
refute_includes mailer.subject, '%{' refute_includes mailer.subject, '%{'
refute_includes mail_body, '%{' refute_includes mail_body, '%{'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment