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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/form_fu/helpers.rb', line 67

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



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/form_fu/helpers.rb', line 53

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)


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

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, &block) ⇒ Object Also known as: build_form_for

Create a form_for block using FormFuBuilder

Raises:

  • (ArgumentError)


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

def formfu_for(record_or_name_or_array, *args, &block)  
  raise ArgumentError, "Missing block" unless block_given?
  
  options = args.extract_options!
  args << options.merge(:builder => FormFu::FormBuilder)
  form_for(record_or_name_or_array, *args, &block)
  
end

#remote_formfu_for(record_or_name_or_array, *args, &block) ⇒ Object Also known as: build_remote_form_for

Create a form_for block using FormFuBuilder

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
# File 'lib/form_fu/helpers.rb', line 18

def remote_formfu_for(record_or_name_or_array, *args, &block)  
  raise ArgumentError, "Missing block" unless block_given?
  
  options = args.extract_options!
  args << options.merge(:builder => FormFu::FormBuilder)
  remote_form_for(record_or_name_or_array, *args, &block)
  
end

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

show validation error is applicable



97
98
99
100
101
102
103
104
105
# File 'lib/form_fu/helpers.rb', line 97

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