Sha256: 4700c6f256c7726332fd53acd5c1f6a2b8b65d6d40915676800ebb52d370fbba

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

module Redde::FormHelper
  # Override the default ActiveRecordHelper behaviour of wrapping the input.
  # This gets taken care of semantically by adding an error class to the wrapper tag
  # containing the input.
  #
  FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
    html_tag
  end

  def redde_form_for(object, options = {}, &block)
    options = make_options(options)
    options[:builder] ||= ReddeFormBuilder

    with_clean_form_field_error_proc do
      form_for(object, options) do |f|
        concat f.error_messages
        concat content_tag(:table, capture(f, &block), class: 'redde-form__tbl')
        concat f.redde_submits
      end
    end
  end

  def redde_fields_for(record_name, record_object = nil, options = {}, &block)
    options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
    options[:builder] ||= ReddeFormBuilder

    with_clean_form_field_error_proc do
      fields_for(record_name, record_object, options, &block)
    end
  end

  private

  def make_options(options)
    return options.merge(html: { class: 'redde-form' }) unless options.key?(:html)
    unless options[:html].key?(:class)
      options[:html][:class] = 'redde-form'
      return options
    end
    if options[:html][:class].is_a? String
      options[:html][:class] += ' redde-form'
    elsif options[:html][:class].is_a? Array
      options[:html][:class] << 'redde-form'
    end
    options
  end

  def with_clean_form_field_error_proc
    default_field_error_proc = ::ActionView::Base.field_error_proc
    begin
      ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
      yield
    ensure
      ::ActionView::Base.field_error_proc = default_field_error_proc
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
redde-0.3.23 app/helpers/redde/form_helper.rb
redde-0.3.22 app/helpers/redde/form_helper.rb
redde-0.3.21 app/helpers/redde/form_helper.rb
redde-0.3.20 app/helpers/redde/form_helper.rb
redde-0.3.19 app/helpers/redde/form_helper.rb
redde-0.3.18 app/helpers/redde/form_helper.rb
redde-0.3.17 app/helpers/redde/form_helper.rb
redde-0.3.16 app/helpers/redde/form_helper.rb
redde-0.3.15 app/helpers/redde/form_helper.rb
redde-0.3.14 app/helpers/redde/form_helper.rb
redde-0.3.13 app/helpers/redde/form_helper.rb
redde-0.3.12 app/helpers/redde/form_helper.rb
redde-0.3.11 app/helpers/redde/form_helper.rb
redde-0.3.10 app/helpers/redde/form_helper.rb
redde-0.3.9 app/helpers/redde/form_helper.rb
redde-0.3.8 app/helpers/redde/form_helper.rb