Sha256: 22b618999707441f1d162d7d44e717363edd1806c53fd03435807f3feb1db3d6

Contents?: true

Size: 1.43 KB

Versions: 41

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module EacRailsUtils
  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

41 entries across 41 versions & 1 rubygems

Version Path
eac_rails_utils-0.25.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.24.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.23.4 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.23.3 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.23.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.23.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.23.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.22.3 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.22.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.22.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.22.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.21.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.20.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.19.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.18.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.17.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.17.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.17.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.16.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb
eac_rails_utils-0.15.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/fields_for.rb