a | b | |
---|
| 0 | + | module SexyForms |
---|
| 0 | + | def field(control, attribute, options = {}) |
---|
| 0 | + | field_class = options.delete(:field_class) |
---|
| 0 | + | note = options.delete(:note) |
---|
| 0 | + | required = options.delete(:required) |
---|
| 0 | + | errors_for = options.delete(:errors_for) || attribute |
---|
| 0 | + | options[:label] = Inflector.humanize(attribute) if options[:label].blank? |
---|
| 0 | + | |
---|
| 0 | + | errors = errors_on_attribute(errors_for) |
---|
| 0 | + | |
---|
| 0 | + | field_classes = ["field"] |
---|
| 0 | + | field_classes << "error" if errors.any? |
---|
| 0 | + | field_classes << options[:field_class] if options[:field_class] |
---|
| 0 | + | field_classes << control.to_s |
---|
| 0 | + | field_classes << "required" if required |
---|
| 0 | + | |
---|
| 0 | + | |
---|
| 0 | + | if options[:name] |
---|
| 0 | + | content = send("#{control}_field", options) |
---|
| 0 | + | else |
---|
| 0 | + | content = send("#{control}_control", attribute, options) |
---|
| 0 | + | end |
---|
| 0 | + | content += %(<p class="note">#{note}</p>) if note |
---|
| 0 | + | content += %(<p class="error">#{errors}</p>) if errors.any? |
---|
| 0 | + | |
---|
| 0 | + | tag :div, content, :class => field_classes.join(' ') |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def errors_on_attribute(attribute) |
---|
| 0 | + | # @_obj comes automagically from merb |
---|
| 0 | + | [@_obj.errors[attribute.to_s]].flatten.join(' and ') rescue [] |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | # Pass more_html for cancel link (or whatever) |
---|
| 0 | + | def form_submit(string, options = {}) |
---|
| 0 | + | %(<div class="field controls">)+ |
---|
| 0 | + | (submit_button string, :class => "positive") + |
---|
| 0 | + | (options[:cancel] ? link_to("Cancel", options[:cancel], :class => "cancel") : '')+ |
---|
| 0 | + | %(</div>) |
---|
| 0 | + | end |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
... | |
---|