Sha256: f4bb36ddd3209e0ea0b22167ee96cff2aa6aeafaf164e5d946e41bb704afd13e

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

module Slices
  class CmsFormBuilder < ActionView::Helpers::FormBuilder

    FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
      html_tag
    end

    %w{email_field password_field select text_area text_field}.each do |selector|
      class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
        def cms_#{selector}(attribute, options = {})       # def cms_text_field(attribute, options = {})
          wrap_field(:#{selector}, attribute, options)     #   wrap_field(:text_field, attribute, options)
        end                                                # end
      RUBY_EVAL
    end

    private

    def wrap_field(selector, attribute, options)
      with_custom_field_error_proc do
        content = send(selector, attribute, options)
        options = {}
        errors = @object.errors[attribute]

        if errors.any?
          options[:class] = 'error'
          content << @template.send(:content_tag, :p, errors.join(' and '))
        end

        @template.content_tag(:li, label(attribute) + content, options)
      end
    end

    def with_custom_field_error_proc(&block)
      default_field_error_proc = ::ActionView::Base.field_error_proc
      ::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

9 entries across 9 versions & 1 rubygems

Version Path
slices-2.0.2 lib/slices/cms_form_builder.rb
slices-2.0.1 lib/slices/cms_form_builder.rb
slices-2.0.0 lib/slices/cms_form_builder.rb
slices-1.0.5 lib/slices/cms_form_builder.rb
slices-1.0.4 lib/slices/cms_form_builder.rb
slices-1.0.3 lib/slices/cms_form_builder.rb
slices-1.0.2 lib/slices/cms_form_builder.rb
slices-1.0.1 lib/slices/cms_form_builder.rb
slices-1.0.0 lib/slices/cms_form_builder.rb