Class: MerbFormFields::FormFieldBuilder
- Inherits:
-
Merb::Helpers::Form::Builder::ResourcefulForm
- Object
- Merb::Helpers::Form::Builder::ResourcefulForm
- MerbFormFields::FormFieldBuilder
- Defined in:
- lib/merb_form_fields/form_field_builder.rb
Instance Method Summary collapse
-
#add_field_type_class? ⇒ Boolean
whether or not to add the field_type class.
- #bound_check_box(method, attrs = {}) ⇒ Object
- #bound_radio_button(method, attrs = {}) ⇒ Object
- #bound_radio_group(method, arr) ⇒ Object
- #bound_select(method, attrs = {}) ⇒ Object
- #bound_text_area(method, attrs = {}) ⇒ Object
-
#error_message_tag ⇒ Object
This tag wraps inner error messages.
-
#error_messages_for(obj, error_class, build_li, header, before) ⇒ Object
we’ll make our own errors, thank you very much.
-
#field_class ⇒ Object
This is the default field class.
-
#field_error_class(obj, method) ⇒ Object
returns the class added to the field element if there was an error.
-
#field_error_message(obj, method, error_override = nil) ⇒ Object
displays the inner error message element.
-
#field_tag ⇒ Object
This tag wraps the field.
-
#field_wrapper(field_type, attrs = {}) ⇒ Object
——————————————— wrapper method implementation ———————————————.
-
#note_class ⇒ Object
This is the default note class.
-
#note_tag ⇒ Object
This tag wraps the note.
-
#skipped_field_types ⇒ Object
These field types are skipped (not wrapped in a field div).
Instance Method Details
#add_field_type_class? ⇒ Boolean
whether or not to add the field_type class
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 (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_tag ⇒ Object
This tag wraps inner error messages
9 10 11 |
# File 'lib/merb_form_fields/form_field_builder.rb', line 9 def 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 (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. : obj.errors return "" if errors.empty? = header % [errors.size, errors.size == 1 ? "" : "s"] markup = %Q{<div class='#{error_class}'>#{}<ul>} errors.each {|err| markup << (build_li % (sequel ? err : err.join(" ")))} markup << %Q{</ul></div>} end |
#field_class ⇒ Object
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 (obj, method, error_override = nil) if obj && method error = obj.errors.on(method.to_sym) unless error.blank? return "\n<#{} class='#{Merb::Plugins.config[:merb_form_fields][:error_message_class] || "error"}'>#{error_override || error}</#{}>" end end end |
#field_tag ⇒ Object
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 = {}) = 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 ([:force] != true and skipped_field_types.include?(field_type)) or [:skip] == true return inner_html end # build class string css_class = [: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}#{(@obj, attrs[:method], error_override)}#{note_html}", .merge(:class => css_class) end |
#note_class ⇒ Object
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_tag ⇒ Object
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_types ⇒ Object
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 |