Sha256: a44482f1c721935c8442cc76899284f182bccb6d8b8b212ed1584d46dc409273

Contents?: true

Size: 1.1 KB

Versions: 17

Compression:

Stored size: 1.1 KB

Contents

module Eac
  module CommonFormHelper
    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 |k, v|
        next if field_errors_showed.include?(k)
        s << content_tag(:div, "#{k}: #{v}", class: 'error')
      end
      s
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
eac_rails_utils-0.8.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.7.1 lib/eac/common_form_helper.rb
eac_rails_utils-0.7.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.6.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.5.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.4.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.3.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.2.2 lib/eac/common_form_helper.rb
eac_rails_utils-0.2.1 lib/eac/common_form_helper.rb
eac_rails_utils-0.2.0 lib/eac/common_form_helper.rb
eac_rails_utils-0.1.15 lib/eac/common_form_helper.rb
eac_rails_utils-0.1.14 lib/eac/common_form_helper.rb
eac_rails_utils-0.1.13 lib/eac/common_form_helper.rb
eac_rails_utils-0.1.12 lib/eac/common_form_helper.rb
eac_rails_utils-0.1.11 lib/eac/common_form_helper.rb
eac_rails_utils-0.1.10 lib/eac/common_form_helper.rb
eac_rails_utils-0.0.1 lib/eac/common_form_helper.rb