Sha256: 68b612fc59a2b8f54c148021cdf16944805ff8f7440ac67e4a28a981263e4876

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require "formal/version"

module Formal
  class Builder < ActionView::Helpers::FormBuilder

    ActionView::Base.field_error_proc = proc { |input, instance| input }

    @@field_helper_methods = %w(label_tag text_field password_field text_area select email_field search_field telephone_field number_field file_field range_field)

    def label(method, text = nil, options = {}, &block)
      text = text || method.to_s.humanize

      if object.errors.any?
        error_message = object.errors[method]
        errors = error_message.is_a?(Array) ? error_message.first : error_message
        error_span = @template.content_tag(:span, errors, class: 'error')
        text << " #{error_span}"
        @template.content_tag(:dt, super(method, text.html_safe, options, &block), class: 'error')
      else
        @template.content_tag(:dt, super(method, text.html_safe, options, &block))
      end
    end

    @@field_helper_methods.each do |method_name|
      define_method method_name do |*args|
        method_name = args.first
        if object.errors[method_name].present?
          @template.content_tag(:dd, super(*args), class: 'error')
        else
          @template.content_tag(:dd, super(*args))
        end
      end
    end

    def check_box_with_label(method, text = nil, *args)
      text = text || method.to_s
      box = [check_box(method, *args).html_safe, text].join(" ")
      label(method, box, *args)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formal-0.0.3 lib/formal.rb