NZrQcCjb / Sexy_Forms_DM.rb
Revision 928841cadb1b (3 weeks, 2 days ago) - Diff
Link to this snippet : http://friendpaste.com/NZrQcCjb
| 1 | module SexyForms |
|---|---|
| 2 | def field(control, attribute, options = {}) |
| 3 | field_class = options.delete(:field_class) |
| 4 | note = options.delete(:note) |
| 5 | required = options.delete(:required) |
| 6 | errors_for = options.delete(:errors_for) || attribute |
| 7 | options[:label] = humanize(attribute) if options[:label].blank? |
| 8 | |
| 9 | errors = errors_on_attribute(errors_for) |
| 10 | |
| 11 | field_classes = ["field"] |
| 12 | field_classes << "error" if errors.any? |
| 13 | field_classes << options[:field_class] if options[:field_class] |
| 14 | field_classes << control.to_s |
| 15 | field_classes << "required" if required |
| 16 | |
| 17 | |
| 18 | if options[:name] |
| 19 | content = send("#{control}_field", options) |
| 20 | else |
| 21 | content = send("#{control}_control", attribute, options) |
| 22 | end |
| 23 | content += %(<p class="note">#{note}</p>) if note |
| 24 | content += %(<p class="error">#{errors}</p>) if errors.any? |
| 25 | |
| 26 | tag :div, content, :class => field_classes.join(' ') |
| 27 | end |
| 28 | |
| 29 | def humanize(sym) |
| 30 | sym.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize |
| 31 | end |
| 32 | |
| 33 | def errors_on_attribute(attribute) |
| 34 | # @_obj comes automagically from merb |
| 35 | [@_obj.errors.on(attribute)].flatten.join(' and ') rescue [] |
| 36 | end |
| 37 | |
| 38 | # Pass more_html for cancel link (or whatever) |
| 39 | def form_submit(string, options = {}) |
| 40 | %(<div class="field controls">)+ |
| 41 | (submit_button string, :class => "positive") + |
| 42 | (options[:cancel] ? link_to("Cancel", options[:cancel], :class => "cancel") : '')+ |
| 43 | %(</div>) |
| 44 | end |
| 45 | end |
View in other formats:
raw format | download source