Module: FormFu::Helpers

Defined in:
lib/form_fu/helpers.rb

Instance Method Summary collapse

Instance Method Details

#field_tag(options_or_label = {}, content = nil, &block) ⇒ Object

generate a field div with label



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/form_fu/helpers.rb', line 49

def field_tag(options_or_label = {}, content = nil, &block)
  label, options =  "", {}
  if options_or_label.class == String
    label = options_or_label
    options[:class] = "field"
  else
    options = options_or_label
    # grab the error indicator out of the options
    has_error = options.delete(:has_error) || false

    # grab the field_type out of the options
    field_type = options.delete(:field_type) || nil

    # append field as css class for div options
    options[:class] = "field #{field_type} #{options[:class]} #{'withErrors' if has_error}"
  end
  
  if block_given?
    concat((:div, options) do
      (label.blank? ? "" : (:label, label.strip)) +
      capture(&block)
    end, block.binding)
  else
    (:div, options) do
      (label.blank? ? "" : (:label, label.strip))+content.to_s
    end
  end
end

#fieldset_tag(legend_name, options = {}, &block) ⇒ Object

wrap content with a fieldset tag with a legend



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/form_fu/helpers.rb', line 35

def fieldset_tag(legend_name, options = {}, &block)
  if block_given?
    concat((:fieldset, options) do
      (legend_name ? (:legend, legend_name) : "")+
      capture(&block)
    end, block.binding)
  else
    return (:fieldset, options) do
       :legend, legend_name
    end
  end
end

#formfu_fields_for(object_or_object_name, *args) {|FormFu::FormBuilder.new(object_name, object, self, options, block)| ... } ⇒ Object Also known as: build_fields_for

Create a fields_for block using FormFuBuilder

Yields:

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/form_fu/helpers.rb', line 14

def formfu_fields_for(object_or_object_name, *args, &block)
  raise ArgumentError, "Missing block" unless block_given?
  options = args.extract_options!

  if object_or_object_name.class == Symbol
    # if object_or_object_name is a symbol, use the object from args
    object_name = object_or_object_name
    object  = args.first
  else
    # otherwise retrieve the object_name from ActiveRecord helper
    object = object_or_object_name
    object_name = ActionController::RecordIdentifier.singular_class_name(object)
  end

  yield FormFu::FormBuilder.new(object_name, object, self, options, block)
end

#formfu_for(record_or_name_or_array, *args, &proc) ⇒ Object Also known as: build_form_for

Create a form_for block using FormFuBuilder



4
5
6
7
8
# File 'lib/form_fu/helpers.rb', line 4

def formfu_for(record_or_name_or_array, *args, &proc)  
  options = args.extract_options!
  args << options.merge(:builder => FormFu::FormBuilder)
  form_for(record_or_name_or_array, *args, &proc)
end

#validation_tag(model, attribute, options = {}) ⇒ Object

show validation error is applicable



79
80
81
82
83
84
85
86
87
# File 'lib/form_fu/helpers.rb', line 79

def validation_tag(model, attribute, options = {})
  return if model.blank? or model.errors.blank?    
  unless model.errors[attribute].blank?
    # generate error markup
     :span, :class => "error-message" do
      [model.errors[attribute]].flatten.join(options[:separator] || ", ").to_s
    end
  end
end