Module: Alchemy::ElementsBlockHelper
- Included in:
- Admin::ElementsHelper, 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, ElementEditorHelper, ElementViewHelper
Instance Method Summary collapse
-
#element_editor_for(element, options = {}) ⇒ Object
Block-level helper for element editors.
-
#element_view_for(element, options = {}) ⇒ Object
Block-level helper for element views.
Instance Method Details
#element_editor_for(element, options = {}) ⇒ Object
Block-level helper for element editors. Provides a block helper object you can use for concise access to Alchemy’s various helpers.
Example:
<%= element_editor_for(element) do |el| %>
<%= el.edit :title %>
<%= el.edit :body %>
<%= el.edit :target_url %>
<% end %>
152 153 154 155 156 157 158 159 160 |
# File 'app/helpers/alchemy/elements_block_helper.rb', line 152 def element_editor_for(element, = {}) = { # nothing here yet. }.merge() capture do yield ElementEditorHelper.new(self, element: element) if block_given? end end |
#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 %>
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 |
# File 'app/helpers/alchemy/elements_block_helper.rb', line 109 def element_view_for(element, = {}) = { tag: :div, id: element_dom_id(element), class: element.name, tags_formatter: ->() { .join(" ") } }.merge() # capture inner template block output = capture do yield ElementViewHelper.new(self, element: element) if block_given? end # wrap output in a useful DOM element if tag = .delete(:tag) # add preview attributes .merge!(element_preview_code_attributes(element)) # add tags if = .delete(:tags_formatter) .merge!((element, formatter: )) end output = content_tag(tag, output, ) end # that's it! output end |