Skip to content

Edit Emails with Comfortable Mexican Sofa

Gleb Mazovetskiy edited this page Jul 27, 2013 · 42 revisions

Comfortable mexican sofa provides editable snippets on a per-site basis (sites can be mirrored, e.g. per locale). REP comes with CMS integration.

Simply add this to an initializer:

require 'rails_email_preview/integrations/comfortable_mexica_sofa'

This will make cms_email_subject and cms_email_snippet methods available to your mailers; when rendered in preview, this will add an Edit link to the content pointing to a customized snippet edit page.

CMS integration screenshot

Now in your email you can use subject and body from the automatically associated CMS snippet:

# account_mailer.rb
def confirmation_email(user_id)
  # you can provide values to be interpolated with %{}
  # e.g. "Hello %{user}"
  mail subject: cms_email_subject(user: find_user(user_id).name)
end

# confirmation_email.html.slim
= cms_email_snippet 

Note that you can use {{cms:helper:}} tag to call methods with string arguments from the rendering context, e.g.: - need link to cms helper docs here!

# app/helpers/mailer_helper.rb:
# usage: {{cms:helper:confirmation_link:Confirm your account}}
def confirmation_link(text = '')
  link_to text, confirmation_url(@resource, confirmation_token: @resource.confirmation_token)
end

You can also put multiple snippets in the email body, and each will get its own edit link:

# confirmation_email.html.slim
= cms_email_snippet cms_email_id + '-first'
= cms_email_snippet cms_email_id + '-second'

You can add a filter around cms_email_snippet, e.g. to render in Markdown using kramdown:

def cms_email_snippet(*args)
  Kramdown::Document.new(super).to_html.html_safe
end

Rich text / markup editor

While CMS does not provide a configuration option for changing the editor from default CodeMirror html source editor, you can still override the file like with any Rails engine:

mkdir -p app/views/cms_admin/snippets
cp $(bundle show comfortable_mexican_sofa)/app/views/cms_admin/snippets/_form.html.haml app/views/cms_admin/snippets/_form.html.haml

Now find the line with = form.text_area :content and replace it with:

= form.text_area :content, data: {rich_text: true}

Or, for a CodeMirror editor other than html set the appropriate data-cm_mode attribute. See full list of modes on codemirror.net. E.g for a Markdown editor:

= form.text_area :content, data: {cm_mode: 'text/x-markdown'}

You need to have gem 'haml' in your Gemfile or reformat the view to your templating language of choice.

NB: If customized like this, to receive any updates for _form.html.haml from upstream you will need to repeat the procedure each time.

Clone this wiki locally