<% # headmin/forms/repeater # # ==== Options # * form - Form object # * attribute - Name of the attribute of the form model # * header - Name of the template to use as header # * label - Text to show as label. Label will be hidden if value is false # * templates - List of all views that can be used as a template for a new block # * flush - Set to true if you want the list items to sit flush with its parent. # # ==== Examples # # Basic version # <% render 'headmin/forms/repeater', form: form, attribute: :questions do |question| %#> # <% render 'admin/questions/fields, form: :question' %#> # <% end %#> # # # Let list group sit flush within its container # <% render 'headmin/forms/repeater', form: form, attribute: :questions, flush: true do |question| %#> # <% render 'admin/questions/fields, form: :question' %#> # <% end %#> # # # With fixed header row. A header row can be used to show the labels, so you can omit them in the repeated fields # <% render 'headmin/forms/repeater', form: form, attribute: :questions, header: 'admin/questions/header' do |question| %#> # <% render 'admin/questions/fields, form: :question' %#> # <% end %#> # # # Allow more than one type of fields to be inserted. You must specify the templates as an array of view paths # <% templates = ['admin/questions/fields/_type_1.html.erb', 'admin/questions/fields/_type_2.html.erb'] %#> # <% render 'headmin/forms/repeater', form: form, attribute: :questions, templates: templates do |question| %#> # <% render 'admin/questions/fields, form: :question' %#> # <% end %#> label = local_assigns.has_key?(:label) ? label : nil required = local_assigns.has_key?(:required) ? required : false header = local_assigns.has_key?(:header) ? header : nil templates = local_assigns.has_key?(:templates) ? templates : [] flush = local_assigns.has_key?(:flush) ? flush : true template_names = templates.map { |template| File.basename(template, '.html.erb') } template_names = template_names.any? ? template_names : ['new'] object_model = form.object.class association_model = object_model.reflect_on_association(attribute).class_name.constantize with_positions = association_model.new.attributes.keys.include?('position') associations = form.object.send(attribute) associations = with_positions ? associations.order(:position) : associations repeater_id = form.object_id pass_thru = template_names.count == 1 ? "[data-template-name=\"#{template_names.first}\"]" : nil show_label = label != false %>
<% if show_label %> <%= render 'headmin/forms/label', form: form, attribute: attribute, name: label, required: required %> <% end %>