%
# headmin/forms/blocks
#
# ==== Options
# * form - Form object
# * path - Directory where to look for block templates
# * allow - Names of block templates that can be added
#
# ==== Examples
# Basic version. This will only look for blocks in 'admin/blocks'
# <%= render "headmin/forms/blocks", form: form %#>
#
# Define the path to look for block templates like this:
# <%= render "headmin/forms/blocks", form: form, path: 'admin/pages/blocks' %#>
#
# Limit the types of blocks
# <%= render "headmin/forms/blocks", form: form, allow: ['text', 'text-image'] %#>
path = local_assigns.has_key?(:path) ? path : nil
allowed_block_names = local_assigns.has_key?(:allow) ? allow.map(&:to_s) : BlockService.block_names(path: path)
templates = allowed_block_names.map { |name| BlockService.form_view(name, path: path) }
%>
<%= render 'headmin/forms/repeater', form: form, attribute: :blocks, templates: templates, label: false do |block_form, template| %>
<% name = template ? BlockService.view_name(template) : block_form.object.name %>
<%= block_form.hidden_field :name, value: name %>
<% view_path = BlockService.form_view(name, path: path).gsub('/_', '/').split('.').first %>
<%= render view_path, form: block_form %>
<%= name.titleize %>
<% end %>