Class: MerbFormFields::FormFieldBuilder

Inherits:
Merb::Helpers::Form::Builder::ResourcefulForm
  • Object
show all
Defined in:
lib/merb_form_fields/form_field_builder.rb

Instance Method Summary collapse

Instance Method Details

#add_field_type_class?Boolean

whether or not to add the field_type class

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/merb_form_fields/form_field_builder.rb', line 35

def add_field_type_class?
  if Merb::Plugins.config[:merb_form_fields][:add_field_type_class?].nil?
    true
  else
    Merb::Plugins.config[:merb_form_fields][:add_field_type_class?]
  end
end

#bound_check_box(method, attrs = {}) ⇒ Object



131
132
133
134
135
136
# File 'lib/merb_form_fields/form_field_builder.rb', line 131

def bound_check_box(method, attrs = {})
  name = control_name(method)
  field_wrapper(:check_box, attrs.merge(:method => method)) do |new_attrs|
    super(method, new_attrs)
  end
end

#bound_radio_button(method, attrs = {}) ⇒ Object



138
139
140
141
142
143
# File 'lib/merb_form_fields/form_field_builder.rb', line 138

def bound_radio_button(method, attrs = {})
  name = control_name(method)
  field_wrapper(:radio_button, attrs.merge(:method => method)) do |new_attrs|
    super(method, new_attrs)
  end
end

#bound_radio_group(method, arr) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/merb_form_fields/form_field_builder.rb', line 145

def bound_radio_group(method, arr)
  val = control_value(method)

  field_wrapper(:radio_group, attrs.merge(:method => method, :arr => arr)) do |new_attrs|
    super(method, new_attrs)
  end
end

#bound_select(method, attrs = {}) ⇒ Object



153
154
155
156
157
158
# File 'lib/merb_form_fields/form_field_builder.rb', line 153

def bound_select(method, attrs = {})
  name = control_name(method)
  field_wrapper(:select, attrs.merge(:method => method)) do |new_attrs|
    super(method, new_attrs)
  end
end

#bound_text_area(method, attrs = {}) ⇒ Object



160
161
162
163
164
165
# File 'lib/merb_form_fields/form_field_builder.rb', line 160

def bound_text_area(method, attrs = {})
  name = "#{@name}[#{method}]"
  field_wrapper(:text_area, attrs.merge(:method => method)) do |new_attrs|
    super(method, new_attrs)
  end
end

#error_message_tagObject

This tag wraps inner error messages



9
10
11
# File 'lib/merb_form_fields/form_field_builder.rb', line 9

def error_message_tag
  Merb::Plugins.config[:merb_form_fields][:error_message_tag] || :em
end

#error_messages_for(obj, error_class, build_li, header, before) ⇒ Object

we’ll make our own errors, thank you very much



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/merb_form_fields/form_field_builder.rb', line 101

def error_messages_for(obj, error_class, build_li, header, before)
  obj ||= @obj
  return "" unless obj.respond_to?(:errors)

  sequel = !obj.errors.respond_to?(:each)
  errors = sequel ? obj.errors.full_messages : obj.errors

  return "" if errors.empty?

  header_message = header % [errors.size, errors.size == 1 ? "" : "s"]
  markup = %Q{<div class='#{error_class}'>#{header_message}<ul>}
  errors.each {|err| markup << (build_li % (sequel ? err : err.join(" ")))}
  markup << %Q{</ul></div>}
end

#field_classObject

This is the default field class



19
20
21
# File 'lib/merb_form_fields/form_field_builder.rb', line 19

def field_class
  Merb::Plugins.config[:merb_form_fields][:field_class] || "field"
end

#field_error_class(obj, method) ⇒ Object

returns the class added to the field element if there was an error



82
83
84
85
86
87
88
# File 'lib/merb_form_fields/form_field_builder.rb', line 82

def field_error_class(obj, method)
  if obj && method && !obj.errors.on(method.to_sym).blank?
    Merb::Plugins.config[:merb_form_fields][:field_error_class] || "error"
  else
    ""
  end
end

#field_error_message(obj, method, error_override = nil) ⇒ Object

displays the inner error message element



91
92
93
94
95
96
97
98
# File 'lib/merb_form_fields/form_field_builder.rb', line 91

def field_error_message(obj, method, error_override = nil)
  if obj && method
    error = obj.errors.on(method.to_sym)
    unless error.blank?
      return "\n<#{error_message_tag} class='#{Merb::Plugins.config[:merb_form_fields][:error_message_class] || "error"}'>#{error_override || error}</#{error_message_tag}>"
    end
  end
end

#field_tagObject

This tag wraps the field



14
15
16
# File 'lib/merb_form_fields/form_field_builder.rb', line 14

def field_tag
  Merb::Plugins.config[:merb_form_fields][:field_tag] || :div
end

#field_wrapper(field_type, attrs = {}) ⇒ Object


wrapper method implementation




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
77
78
79
# File 'lib/merb_form_fields/form_field_builder.rb', line 51

def field_wrapper(field_type, attrs = {})
  field_options = attrs.delete(:field) || {}
  note = attrs.delete(:note)
  error_override = attrs.delete(:error)

  # build inner field html (using the passed in block) 
  inner_html = yield(attrs) if block_given?

  # skip certain field types 
  # :fields => {:force => true} will force the field to show
  # :fields => {:skip => true} will prevent the field from showing
  if (field_options[:force] != true and skipped_field_types.include?(field_type)) or field_options[:skip] == true
    return inner_html
  end

  # build class string
  css_class = field_options[:class] || "#{field_class} #{field_type if add_field_type_class?}"
  if error_class = field_error_class(@obj, attrs[:method])
    css_class << " #{error_class}"
  end

  # build note
  note_html = tag(note_tag, note, :class => note_class) unless note.blank?

  # build field
  tag field_tag,
      "#{inner_html}#{field_error_message(@obj, attrs[:method], error_override)}#{note_html}",
      field_options.merge(:class => css_class)
end

#note_classObject

This is the default note class



29
30
31
# File 'lib/merb_form_fields/form_field_builder.rb', line 29

def note_class
  Merb::Plugins.config[:merb_form_fields][:note_class] || "note"
end

#note_tagObject

This tag wraps the note



24
25
26
# File 'lib/merb_form_fields/form_field_builder.rb', line 24

def note_tag
  Merb::Plugins.config[:merb_form_fields][:note_tag] || :span
end

#skipped_field_typesObject

These field types are skipped (not wrapped in a field div)



44
45
46
# File 'lib/merb_form_fields/form_field_builder.rb', line 44

def skipped_field_types
  Merb::Plugins.config[:merb_form_fields][:skipped_field_types] || [:hidden_field, :check_box, :radio_button]
end