class MailTemplate < ApplicationRecord include Auditable include HtmlBlankable belongs_to :project, optional: true has_many :mailings, dependent: :nullify has_many :mailings_as_b, class_name: "Mailing", foreign_key: :mail_template_b_id, dependent: :nullify clears_blank_html :body_html validates :name, :subject, :body_html, presence: true scope :for_project, ->(project) { where(project_id: [nil, project&.id].compact).order(:name) } def preview_html(organization: nil, contact: nil, project: nil, opportunity: nil) MailMerge.render(body_html, organization:, contact:, project:, opportunity:) end end