Module: Alchemy::ElementsBlockHelper

Included in:
ElementsHelper
Defined in:
app/helpers/alchemy/elements_block_helper.rb

Overview

Provides a collection of block-level helpers, allowing for a much more concise way of writing element view/editor partials.

Defined Under Namespace

Classes: BlockHelper, ElementViewHelper

Instance Method Summary collapse

Instance Method Details

#element_view_for(element, options = {}) ⇒ Object

Block-level helper for element views. Constructs a DOM element wrapping your content element and provides a block helper object you can use for concise access to Alchemy’s various helpers.

Example:

<%= element_view_for(element) do |el| %>
  <%= el.render :title %>
  <%= el.render :body %>
  <%= link_to "Go!", el.ingredient(:target_url) %>
<% end %>

You can override the tag, ID and class used for the generated DOM element:

<%= element_view_for(element, tag: 'span', id: 'my_id', class: 'thing') do |el| %>
   <%- ... %>
<% end %>

If you don’t want your view to be wrapped into an extra element, simply set tag to false:

<%= element_view_for(element, tag: false) do |el| %>
   <%- ... %>
<% end %>

Parameters:

  • element (Alchemy::Element)

    The element to display.

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

    Additional options.

Options Hash (options):

  • :tag (Object) — default: :div

    The HTML tag to be used for the wrapping element.

  • :id (Object) — default: the element's dom_id

    The wrapper tag’s DOM ID.

  • :class (Object) — default: the element's name

    The wrapper tag’s DOM class.

  • :tags_formatter (Object)

    A lambda used for formatting the element’s tags (see Alchemy::ElementsHelper::element_tags_attributes). Set to false to not include tags in the wrapper element.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/alchemy/elements_block_helper.rb', line 102

def element_view_for(element, options = {})
  if options[:id].nil?
    Alchemy::Deprecation.warn "      Relying on an implicit DOM id in `element_view_for` is deprecated. Please provide an explicit `id` if you actually want to render an `id` attribute on the \#{element.name} element wrapper tag.\n    WARN\n  end\n\n  if options[:class].nil?\n    Alchemy::Deprecation.warn <<~WARN\n      Relying on an implicit CSS class in `element_view_for` is deprecated. Please provide an explicit `class` for the \#{element.name} element wrapper tag.\n    WARN\n  end\n\n  options = {\n    tag: :div,\n    id: (!!options[:id]) ? options[:id] : element.dom_id,\n    class: element.name,\n    tags_formatter: ->(tags) { tags.join(\" \") }\n  }.merge(options)\n\n  # capture inner template block\n  output = capture do\n    yield ElementViewHelper.new(self, element: element) if block_given?\n  end\n\n  # wrap output in a useful DOM element\n  if (tag = options.delete(:tag))\n    # add preview attributes\n    options.merge!(element_preview_code_attributes(element))\n\n    # add tags\n    if (tags_formatter = options.delete(:tags_formatter))\n      options.merge!(element_tags_attributes(element, formatter: tags_formatter))\n    end\n\n    output = content_tag(tag, output, options)\n  end\n\n  # that's it!\n  output\nend\n"