Sha256: 03fad65db231406c5935ebadc2cf3624ac8f7a767dd18ada953145ae05b07abf

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'action_view/helpers/form_helper'
require 'eac_ruby_utils/core_ext'

module EacRailsUtils
  module CommonFormHelper
    common_concern do
      include ::ActionView::Helpers::FormHelper
    end
    require_sub __FILE__

    def common_form(model_instance, options = {}, &block)
      submit_label = options.delete(:submit_label)
      options[:html] ||= {}
      options[:html][:class] = 'CommonFormHelper'
      form_for(model_instance, options) do |form|
        fb = FormBuilder.new(form, self)
        errors(model_instance) <<
          capture(fb, &block) <<
          errors_not_showed(model_instance, fb.field_errors_showed) <<
          form.submit(submit_label, class: 'btn btn-primary')
      end
    end

    private

    def errors(model_instance)
      if model_instance.errors.any?
        content_tag(:div, class: 'alert alert-danger', id: 'flash_alert') do
          'Há pendências no preenchimento do formulário.'
        end
      else
        ActiveSupport::SafeBuffer.new
      end
    end

    def errors_not_showed(model_instance, field_errors_showed)
      s = ActiveSupport::SafeBuffer.new
      model_instance.errors.each do |error|
        next if field_errors_showed.include?(error.attribute)

        s << content_tag(:div, "#{error.attribute}: #{error.message}", class: 'error')
      end
      s
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
eac_rails_utils-0.25.0 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.24.0 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.23.4 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.23.3 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.23.2 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.23.1 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.23.0 app/helpers/eac_rails_utils/common_form_helper.rb
eac_rails_utils-0.22.3 app/helpers/eac_rails_utils/common_form_helper.rb