Sha256: 4992e68c844e9014345061829c3ae47221e140ad0d5f57ea081b886538eaeef1

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 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)

    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|
        if object.errors.any?
          @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

2 entries across 2 versions & 1 rubygems

Version Path
formal-0.0.2 lib/formal.rb
formal-0.0.1 lib/formal.rb