Commit eeb3ce4a authored by Kaspar Vollenweider's avatar Kaspar Vollenweider

email template variables and methods can now be defined independently

by reminder mailing kind
different reminder mailing kind can have the same variable name pointing to two different methods
parent d4e1950e
......@@ -31,10 +31,10 @@ class EmailTemplate < ApplicationRecord
{
signup: [],
assignment: [:Anrede, :Name, :EinsatzTitel, :FeedbackLink],
trial: ReminderMailing::TEMPLATE_VARNAMES,
termination: ReminderMailing::TEMPLATE_VARNAMES,
half_year_process_email: SemesterProcessMail.template_varnames[:mail],
half_year_process_overdue: SemesterProcessMail.template_varnames[:reminder]
trial: ReminderMailing.template_varnames(:trial_period).values,
termination: ReminderMailing.template_varnames(:termination).values,
half_year_process_email: ReminderMailing.template_varnames(:half_year_process_email).values,
half_year_process_overdue: ReminderMailing.template_varnames(:half_year_process_overdue).values
}
end
......
class ReminderMailing < ApplicationRecord
before_update :remove_untoggled_volunteers
TEMPLATE_VARNAMES = [
:Anrede,
:Name,
:Einsatz,
:EinsatzStart,
:FeedbackLink,
:EmailAbsender,
:OnlinePlattformUrl
].freeze
# Key is method name, value is users variable %{FeedbackLink}
TEMPLATE_VARNAMES_GENERAL = {
anrede: :Anrede,
name: :Name,
einsatz: :Einsatz,
email_absender: :EmailAbsender,
online_plattform_url: :OnlinePlattformUrl
}.freeze
# method var_name pairs per reminder_mailing kind
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'
......@@ -26,7 +41,8 @@ class ReminderMailing < ApplicationRecord
source_type: 'GroupAssignment'
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] }
validates :subject, presence: true
......@@ -35,6 +51,16 @@ class ReminderMailing < ApplicationRecord
validate :no_reminder_volunteer_present, unless: :reminder_volunteer_mailings_any?
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
def reminder_mailing_volunteers=(reminder_mailable)
if [Assignment, GroupAssignment].include? reminder_mailable.first&.class
......
......@@ -71,6 +71,20 @@ class ReminderMailingVolunteer < ApplicationRecord
template % template_variables
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
I18n.t("salutation.#{volunteer.salutation}")
end
......@@ -95,30 +109,30 @@ class ReminderMailingVolunteer < ApplicationRecord
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
"[#{reminder_mailing_creator_name}](mailto:"\
"#{reminder_mailing.creator.email})"
end
def reminder_mailing_creator_name
reminder_mailing.creator.profile&.contact&.natural_name ||
reminder_mailing.creator.email
def feedback_link_trial
"[Probezeit-Feedback erstellen](#{feedback_url})"
end
def feedback_link_termination
"[Abschlussevaluations-Feedback erstellen](#{feedback_url})"
end
def feedback_link
"[Halbjahres-Rapport erstellen](#{feedback_url})"
def feedback_link_semester
"[Halbjahres-Feedback erstellen](#{feedback_url})"
end
def online_plattform_url
"[Online-Plattform Url](#{Rails.application.routes.url_helpers.root_path})"
end
# template variable assisting methods
def reminder_mailing_creator_name
reminder_mailing.creator.profile&.contact&.natural_name ||
reminder_mailing.creator.email
end
end
......@@ -12,42 +12,60 @@ class SemesterProcessMail < ApplicationRecord
delegate :volunteer, 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 = [
:Anrede,
:Name,
:Einsatz,
:FeedbackLink,
:EmailAbsender,
:OnlinePlattformUrl
].freeze
def self.template_varnames
{
mail: TEMPLATE_VARNAMES,
reminder: TEMPLATE_VARNAMES
}
def template_varnames
TEMPLATE_VARNAMES[kind.to_sym]
end
def process_template
{
subject: replace_ruby_template(self.subject),
body: replace_ruby_template(self.body)
subject: replace_ruby_template(subject),
body: replace_ruby_template(body)
}
end
private
def send_email
VolunteerMailer.half_year_process_email(self).deliver
update!(sent_at: Time.zone.now)
end
def replace_ruby_template(template)
template % template_variables
end
def template_variables
template_variables = SemesterProcessMail::template_varnames[self.kind.to_sym].map do |varname|
[varname, send(varname.to_s.underscore)]
template_variables = 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 SemesterProcessMail::TEMPLATE_VARNAMES
# Otherwise there will be a method missing error if that template variable is used
def anrede
I18n.t("salutation.#{volunteer.salutation}")
end
......@@ -56,10 +74,6 @@ class SemesterProcessMail < ApplicationRecord
volunteer.contact.natural_name
end
def einsatz
''
end
def email_absender
"[#{reminder_mailing_creator_name}](mailto:"\
"#{semester_process.creator.email})"
......@@ -67,14 +81,14 @@ class SemesterProcessMail < ApplicationRecord
def reminder_mailing_creator_name
semester_process.creator.profile&.contact&.natural_name ||
semester_process.creator.email
semester_process.creator.email
end
def feedback_link
"[Halbjahres-Rapport erstellen](#{feedback_url})"
end
def feedback_url(options = {})
def feedback_url
Rails.application.routes.url_helpers.review_semester_review_semester_url(
semester_process_volunteer,
ActionMailer::Base.default_url_options
......@@ -85,10 +99,8 @@ class SemesterProcessMail < ApplicationRecord
"[Online-Plattform Url](#{Rails.application.routes.url_helpers.root_path})"
end
private
def send_email
VolunteerMailer.half_year_process_email(self).deliver
self.update(sent_at: Time.zone.now)
def semester_from_to_date
semester = semester_process_volunteer.semester
"#{I18n.l(semester.first)} bis #{I18n.l(semester.last)}}"
end
end
......@@ -3,7 +3,7 @@
= f.input_field :kind, value: @reminder_mailing.kind, as: :hidden
.row
.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
= render 'email_templates/subject_body_inputs', f: f
......
......@@ -8,7 +8,7 @@
.row
.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
= render 'email_templates/subject_body_inputs', f: f
......
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