Module: MarkupAttributesHelper

Defined in:
app/helpers/markup_attributes_helper.rb

Overview

The top-level Rails helper which enables consistent rendering of markup attributes.

Rails will make this automatically available in your views; you may need to explicitly include it in other places (like serializers).

This helper exposes a single method: render_markup

Instance Method Summary collapse

Instance Method Details

#render_markup(markup_attribute) ⇒ Object

Given an attribute, returned an HTML-safe, rendered version of the content in that attribute, rendered according to the markup options defined in the model.

If the passed attribute has not been configured as a markup attribute, it will be returned without performing any rendering at all. Th ft ft

Examples:

rendering an attribute from a model

# Given this model exists somewhere, with `title` as a markup attribute:
# post = Post.new(title: '"This":http://example.com is _great_')

render_markup(post.title) # => '<a href="http://example.com" rel="nofollow">This</a> is <em>great</em>'

Parameters:

  • markup_attribute (String)

    the attribute to render. If this is an attribute that has not been configured as a markup attribute, it will be returned without any rendering.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/markup_attributes_helper.rb', line 22

def render_markup(markup_attribute)
  return markup_attribute if markup_attribute.blank?

  if markup_attribute.respond_to?(:markup_options)
    options = markup_attribute.markup_options

    html = case options[:markup]
    when :textile
      require 'redcloth'
      RedCloth.new(sanitize_using_app(markup_attribute), [:no_span_caps]).to_html
    else
      raise "unknown markup attribute type: #{options[:markup]}"
    end

    cleaned_html = clean_rendered_markup(html, options)
    cleaned_html.html_safe
  end
end