Sha256: 5277a99a9d7ec9c49fe8950bcb1b26d2cf2dce4a2a842fef009cb2c5d89085a2

Contents?: true

Size: 1.69 KB

Versions: 16

Compression:

Stored size: 1.69 KB

Contents

module EffectiveFormBuilderHelper
  def effective_form_with(**options, &block)

    # Compute the default ID
    subject = Array(options[:scope] || options[:model]).last
    class_name = subject.class.name.underscore

    html_id = if subject.kind_of?(Symbol)
      subject.to_s
    elsif subject.respond_to?(:persisted?) && subject.persisted?
      "edit_#{class_name}_#{subject.to_param}"
    else
      "new_#{class_name}"
    end

    options[:class] = [options[:class], 'needs-validation', ('form-inline' if options[:layout] == :inline), ('with-errors' if subject.respond_to?(:errors) && subject.errors.present?)].compact.join(' ')
    options[:html] = (options[:html] || {}).merge(novalidate: true, onsubmit: 'return EffectiveForm.validate(this);')

    remote_index = options.except(:model).hash.abs

    if options.delete(:remote) == true
      @_effective_remote_index ||= 0

      if options[:html][:data].kind_of?(Hash)
        options[:html][:data][:remote] = true
        options[:html][:data]['data-remote-index'] = remote_index
      else
        options[:html]['data-remote'] = true
        options[:html]['data-remote-index'] = remote_index
      end

      html_id = "#{html_id}_#{remote_index}"
    end

    # Assign default ID
    options[:id] ||= options[:html].delete(:id) || html_id

    without_error_proc do
      form_with(**options.merge(builder: Effective::FormBuilder), &block)
    end
  end

  private

  # Disables a .fields_with_errors wrapping div when
  def without_error_proc
    original = ActionView::Base.field_error_proc

    begin
      ActionView::Base.field_error_proc = proc { |input, _| input }; yield
    ensure
      ActionView::Base.field_error_proc = original
    end
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
effective_bootstrap-0.4.6 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.4.5 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.4.4 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.4.3 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.4.2 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.4.1 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.4.0 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.22 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.21 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.20 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.19 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.18 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.17 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.16 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.15 app/helpers/effective_form_builder_helper.rb
effective_bootstrap-0.3.14 app/helpers/effective_form_builder_helper.rb