Module: RailsConnector::CmsTagHelper

Included in:
DefaultCmsHelper
Defined in:
app/helpers/rails_connector/cms_tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#cms_tag(tag_name, obj_or_widget, field_name, options = {}, &block) ⇒ String

Returns an HTML block tag containing content from an Obj. Add HTML attributes by passing an attributes hash to options. The helper method is somewhat similar to (and internally uses) api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag.

This helper method also renders additional data attributes, which are needed for inplace editing. These attributes are only rendered when appropriate, i.e. not for a regular visitor.

Examples:

Renders an <h2> tag containing the text of the headline attribute of @obj and assigns the tag a css class called very_important.

<%= cms_tag :h2, @obj, :headline, class: "very_important" %>

Renders an <h2> tag containing an escaped headline.

<%= cms_tag :h2, @obj, :headline do %>
  <%= strip_tags @obj.headline %>
<% end %>

Parameters:

  • tag_name (String, Symbol)

    Name of the html tag (e.g. :h1 or :div).

  • obj (Obj)

    A Obj from which attribute is read.

  • field_name (String, Symbol)

    Which field of the Obj should be rendered.

  • options (Hash) (defaults to: {})

    Additional options, which are passed to content_tag. Use them to add HTML attributes to the tag.

  • block (Proc)

    Optional block to render inner HTML. If none given the value of attribute will be rendered instead.

Returns:

  • (String)

    The rendered html tag


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/rails_connector/cms_tag_helper.rb', line 41

def cms_tag(tag_name, obj_or_widget, field_name, options = {}, &block)
  begin
    field_type = obj_or_widget.type_of_attribute(field_name.to_s)
  rescue RailsConnectorError => e
    return (tag_name, '', options)
  end

  if inplace_editing_allowed?
    options = options.merge({
      'data-ip-field-name'      => field_name,
      'data-ip-field-obj-class' => obj_or_widget.obj_class,
      'data-ip-field-type'      => field_type,
    })

    if obj_or_widget.kind_of?(BasicWidget)
      options['data-ip-private-field-id'] = obj_or_widget.obj.id
      options['data-ip-private-field-widget-id'] = obj_or_widget.id
    else
      options['data-ip-private-field-id'] = obj_or_widget.id
    end
  end

  if field_type == 'widget'
    rendered_widgets = obj_or_widget[field_name].map do |widget|
      render_widget(widget, obj_or_widget, field_name, obj_or_widget)
    end
    inner_html = safe_join(rendered_widgets)
  else
    if inplace_editing_allowed? && FIELD_TYPES_WITH_ORIGINAL_CONTENT.include?(field_type)
      original_value = display_original_value(obj_or_widget[field_name])
      original_content = cms_tag_original_content(field_type, original_value)
      options['data-ip-private-field-original-content'] = MultiJson.encode(original_content)
    end

    inner_html = block_given? ? capture { yield } : display_value(obj_or_widget[field_name])
  end

  if VOID_TAGS.include?(tag_name.to_s)
    tag(tag_name, options)
  else
    (tag_name, inner_html, options)
  end
end