Sha256: 9d3d8b05b9e88299262b0ad01417d5c9f12775123a43bd4b16187f600bfd727e

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Eac
  module CommonFormHelper
    class FormBuilder
      module FieldsFor
        def fields_for(association, &block)
          fieldset(association) do
            nested_fields(association, &block) << add_link(association)
          end
        end

        private

        def nested_fields(association, &block)
          form.nested_fields_for(association) do |nested_form|
            helper.content_tag(:div, class: 'nested_form_row') do
              helper.capture(FormBuilder.new(nested_form, helper), &block) <<
                remove_link(nested_form)
            end
          end
        end

        def add_link(association)
          form.add_nested_fields_link(association, 'Adicionar', class: 'btn btn-primary',
                                                                role: 'button')
        end

        def remove_link(nested_form)
          nested_form.remove_nested_fields_link('Remover', class: 'btn btn-danger',
                                                           role: 'button')
        end

        def fieldset(association, &block)
          helper.content_tag(:fieldset, class: 'nested_form') do
            helper.content_tag(:legend, fieldset_legend(association)) << block.call
          end
        end

        def fieldset_legend(association)
          I18n.t("helpers.label.#{form.object.class.name.underscore}.#{association}")
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
eac_rails_utils-0.10.1 lib/eac/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.10.0 lib/eac/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.9.1 lib/eac/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.9.0 lib/eac/common_form_helper/form_builder/fields_for.rb