demarches-normaliennes/app/models/concerns/mail_template_concern.rb

54 lines
1.3 KiB
Ruby
Raw Normal View History

module MailTemplateConcern
extend ActiveSupport::Concern
include TagsSubstitutionConcern
2019-07-22 15:21:05 +02:00
module Actions
SHOW = :show
ASK_QUESTION = :ask_question
REPLY = :reply
end
def subject_for_dossier(dossier)
replace_tags(subject, dossier).presence || replace_tags(self.class::DEFAULT_SUBJECT, dossier)
end
def body_for_dossier(dossier)
replace_tags(body, dossier)
end
2019-07-22 15:21:05 +02:00
def actions_for_dossier(dossier)
[MailTemplateConcern::Actions::SHOW, MailTemplateConcern::Actions::ASK_QUESTION]
end
def attachment_for_dossier(dossier)
nil
end
2019-04-10 15:21:08 +02:00
def update_rich_body
self.rich_body = self.body
end
2019-04-10 15:21:08 +02:00
included do
has_rich_text :rich_body
2019-04-10 15:21:08 +02:00
before_save :update_rich_body
2019-04-10 15:21:08 +02:00
end
class_methods do
def default_for_procedure(procedure)
template_name = default_template_name_for_procedure(procedure)
2022-04-19 09:39:24 +02:00
rich_body = ActionController::Base.render template: template_name
trix_rich_body = rich_body.gsub(/(?<!^|[.-])(?<!<\/strong>)\n/, '')
new(subject: const_get(:DEFAULT_SUBJECT), body: trix_rich_body, rich_body: trix_rich_body, procedure: procedure)
end
def default_template_name_for_procedure(procedure)
const_get(:DEFAULT_TEMPLATE_NAME)
end
end
def dossier_tags
TagsSubstitutionConcern::DOSSIER_TAGS + TagsSubstitutionConcern::DOSSIER_TAGS_FOR_MAIL
end
end