Module: Alchemy::ElementsHelper

Includes:
ElementsBlockHelper, EssencesHelper, UrlHelper
Included in:
Admin::ElementsHelper, PagesHelper
Defined in:
app/helpers/alchemy/elements_helper.rb

Overview

This helpers are useful to render elements from pages.

The most important helper for frontend developers is the #render_elements helper.

Instance Method Summary collapse

Methods included from ElementsBlockHelper

#element_editor_for, #element_view_for

Methods included from UrlHelper

#download_alchemy_attachment_path, #download_alchemy_attachment_url, #full_url_for_element, #show_alchemy_page_path, #show_alchemy_page_url, #show_alchemy_picture_path, #show_alchemy_picture_url, #show_page_path_params, #show_picture_path_params

Methods included from EssencesHelper

#render_essence, #render_essence_view, #render_essence_view_by_name

Instance Method Details

#element_dom_id(element) ⇒ Object

Returns a string for the id attribute of a html element for the given element



181
182
183
184
# File 'app/helpers/alchemy/elements_helper.rb', line 181

def element_dom_id(element)
  return "" if element.nil?
  "#{element.name}_#{element.id}".html_safe
end

#element_preview_code(element) ⇒ Object

Renders the HTML tag attributes required for preview mode.



187
188
189
# File 'app/helpers/alchemy/elements_helper.rb', line 187

def element_preview_code(element)
  tag_options(element_preview_code_attributes(element))
end

#element_preview_code_attributes(element) ⇒ Object

Returns a hash containing the HTML tag attributes required for preview mode.



192
193
194
195
# File 'app/helpers/alchemy/elements_helper.rb', line 192

def element_preview_code_attributes(element)
  return {} unless element.present? && @preview_mode && element.page == @page
  { :'data-alchemy-element' => element.id }
end

#element_tags(element, options = {}) ⇒ String

Returns the element’s tags information as a string. Parameters and options are equivalent to #element_tags_attributes.

Returns:

  • (String)

    HTML tag attributes containing the element’s tag information.

See Also:



205
206
207
# File 'app/helpers/alchemy/elements_helper.rb', line 205

def element_tags(element, options = {})
  tag_options(element_tags_attributes(element, options))
end

#element_tags_attributes(element, options = {}) ⇒ Hash

Returns the element’s tags information as an attribute hash.

Parameters:

Options Hash (options):

  • :formatter (Proc) — default: 'lambda { |tags| tags.join(' ') }'

    Lambda converting array of tags to a string.

Returns:

  • (Hash)

    HTML tag attributes containing the element’s tag information.



220
221
222
223
224
225
226
227
# File 'app/helpers/alchemy/elements_helper.rb', line 220

def element_tags_attributes(element, options = {})
  options = {
    formatter: lambda { |tags| tags.join(' ') }
  }.merge(options)

  return {} if !element.taggable? || element.tag_list.blank?
  { :'data-element-tags' => options[:formatter].call(element.tag_list) }
end

#render_element(element, part = :view, options = {}, counter = 1) ⇒ Object

Note:

If the view partial is not found alchemy/elements/_view_not_found.html.erb or alchemy/elements/_editor_not_found.html.erb gets rendered.

This helper renders a Alchemy::Element partial.

A element has always two partials:

  1. A view partial (This is the view presented to the website visitor)

  2. A editor partial (This is the form presented to the website editor while in page edit mode)

The partials are located in app/views/alchemy/elements.

View partial naming

The partials have to be named after the name of the element as defined in the elements.yml file and has to be suffixed with the partial part.

Example

Given a headline element

# elements.yml
- name: headline
  contents:
  - name: text
    type: EssenceText

Then your element view partials has to be named like:

app/views/alchemy/elements/_headline_editor.html.erb
app/views/alchemy/elements/_headline_view.html.erb

Element partials generator

You can use this handy generator to let Alchemy generate the partials for you:

$ rails generate alchemy:elements --skip

Usage

<%= render_element(Alchemy::Element.published.named(:headline).first) %>

Parameters:

  • element (Alchemy::Element)

    The element you want to render the view for

  • part (Symbol) (defaults to: :view)

    The type of element partial (:editor or :view) you want to render

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

    Additional options

  • counter (Number) (defaults to: 1)

    a counter



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/alchemy/elements_helper.rb', line 153

def render_element(element, part = :view, options = {}, counter = 1)
  if element.nil?
    warning('Element is nil')
    render "alchemy/elements/#{part}_not_found", {name: 'nil'}
    return
  end

  options = {
    element: element,
    counter: counter,
    options: options,
    locals: options.delete(:locals) || {}
  }

  element.store_page(@page) if part.to_sym == :view
  render "alchemy/elements/#{element.name}_#{part}", options
rescue ActionView::MissingTemplate => e
  warning(%(
    Element #{part} partial not found for #{element.name}.\n
    #{e}
  ))
  render "alchemy/elements/#{part}_not_found", {
    name: element.name,
    error: "Element #{part} partial not found.<br>Use <code>rails generate alchemy:elements</code> to generate it."
  }
end

#render_elements(options = {}) ⇒ Object

Renders all elements from current page

Examples:

Render only certain elements:

<header>
  <%= render_elements only: ['header', 'claim'] %>
</header>
<section id="content">
  <%= render_elements except: ['header', 'claim'] %>
</section>

Render elements from global page:

<footer>
  <%= render_elements from_page: 'footer' %>
</footer>

Render elements from cell:

<aside>
  <%= render_elements from_cell: 'sidebar' %>
</aside>

Fallback to elements from global page:

You can use the fallback option as an override for elements that are stored on another page. So you can take elements from a global page and only if the user adds an element on current page the local one gets rendered.

  1. You have to pass the the name of the element the fallback is for as for key.

  2. You have to pass a page_layout name or Page from where the fallback elements is taken from as from key.

  3. You can pass the name of element to fallback with as with key. This is optional (the element name from the for key is taken as default).

<%= render_elements(fallback: {
  for: 'contact_teaser',
  from: 'sidebar',
  with: 'contact_teaser'
}) %>

Parameters:

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

    Additional options.

Options Hash (options):

  • :count (Number)

    The amount of elements to be rendered (begins with first element found)

  • :except (Array or String) — default: []

    A list of element names not to be rendered.

  • :fallback (Hash)

    Define elements that are rendered from another page.

  • :from_cell (Alchemy::Cell or String)

    The cell the elements are rendered from. You can pass a Cell name String or a Cell object.

  • :from_page (Alchemy::Page or String) — default: @page

    The page the elements are rendered from. You can pass a page_layout String or a Page object.

  • :only (Array or String) — default: []

    A list of element names only to be rendered.

  • :random (Boolean)

    Randomize the output of elements

  • :reverse (Boolean)

    Reverse the rendering order

  • :sort_by (String)

    The name of a Content to sort the elements by

  • :separator (String)

    A string that will be used to join the element partials. Default nil



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/alchemy/elements_helper.rb', line 76

def render_elements(options = {})
  options = {
    from_page: @page,
    render_format: 'html',
    reverse: false
  }.update(options)

  pages = pages_holding_elements(options.delete(:from_page))

  if pages.blank?
    warning('No page to get elements from was found')
    return
  end

  elements = collect_elements_from_pages(pages, options)

  if options[:sort_by].present?
    elements = sort_elements_by_content(
      elements,
      options.delete(:sort_by),
      options[:reverse]
    )
  end

  render_element_view_partials(elements, options)
end

#sort_elements_by_content(elements, content_name, reverse = false) ⇒ Array

Sort given elements by content.

Parameters:

  • elements (Array)
    • The elements you want to sort

  • content_name (String)
    • The name of the content you want to sort by

  • reverse (Boolean) (defaults to: false)
    • Reverse the sorted elements order

Returns:

  • (Array)


237
238
239
240
241
242
243
244
# File 'app/helpers/alchemy/elements_helper.rb', line 237

def sort_elements_by_content(elements, content_name, reverse = false)
  sorted_elements = elements.sort_by do |element|
    content = element.content_by_name(content_name)
    content ? content.ingredient.to_s : ''
  end

  reverse ? sorted_elements.reverse : sorted_elements
end