Sha256: 0a468bafab8d7477b9a0c1d1b98183fe4769fbb3756203fc8be04a19c5418c59

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

class ComfyBlog::FormBuilder < ActionView::Helpers::FormBuilder
  
  helpers = field_helpers -
    %w(hidden_field fields_for) +
    %w(select)
    
  helpers.each do |name|
    class_eval %Q^
      def #{name}(field, *args)
        options = args.extract_options!
        args << options
        return super if options.delete(:disable_builder)
        default_field('#{name}', field, options){ super }
      end
    ^
  end
  
  def default_field(type, field, options = {}, &block)
    errors = if object.respond_to?(:errors) && object.errors[field].present?
      "<div class='errors'>#{[object.errors[field]].flatten.first}</div>"
    end
    if desc = options.delete(:desc)
      desc = "<div class='desc'>#{desc}</div>"
    end
    %(
      <div class='form_element #{type}_element #{'errors' if errors}'>
        <div class='label'>#{label_for(field, options)}</div>
        <div class='value'>#{yield}</div>
        #{desc}
        #{errors}
      </div>
    ).html_safe
  end
  
  def label_for(field, options)
    label = options.delete(:label) || field.to_s.titleize.capitalize
    "<label for=\"#{object_name}_#{field}\">#{label}</label>".html_safe
  end
  
  def simple_field(label = nil, content = nil, options = {}, &block)
    content ||= @template.capture(&block) if block_given?
    %(
      <div class='form_element #{options.delete(:class)}'>
        <div class='label'>#{label}</div>
        <div class='value'>#{content}</div>
      </div>
    ).html_safe
  end
  
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
comfy_blog-0.1.8 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.7 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.6 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.5 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.4 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.3 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.2 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.1 lib/comfy_blog/form_builder.rb
comfy_blog-0.1.0 lib/comfy_blog/form_builder.rb
comfy_blog-0.0.0 lib/comfy_blog/form_builder.rb