Sexy_Forms_DM.rb Revision 644ab7ba1654 (Tue Dec 16 2008 at 17:08) - Diff Link to this snippet: https://friendpaste.com/7cWIvEfFDQcNTR4kAHIzgy Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# @_obj isn't working anymoremodule Merb::Helpers::Form::Builder class Base def current_obj @obj end endendmodule SexyForms def field(control, attribute, options = {}) field_class = options.delete(:field_class) note = options.delete(:note) required = options.delete(:required) errors_for = options.delete(:errors_for) || attribute options[:label] = humanize(attribute) if options[:label].blank? errors = errors_on_attribute(errors_for) field_classes = ["field"] field_classes << "error" if errors.any? field_classes << options[:field_class] if options[:field_class] field_classes << control.to_s field_classes << "required" if required begin content = send("#{control}_field", attribute, options) rescue content = send(control, attribute, options) end content += %(<p class="note">#{note}</p>) if note content += %(<p class="error">#{errors}</p>) if errors.any? tag :div, content, :class => field_classes.join(' ') end def humanize(sym) sym.to_s.gsub(/_id$/, "").gsub(/_/, " ").gsub( /\S+/ ){|s| s.capitalize} end def errors_on_attribute(attribute) [current_form_context.current_obj.errors.on(attribute)].flatten.join(' and ') rescue [] end # Pass more_html for cancel link (or whatever) def form_submit(string, options = {}) %(<div class="field controls">)+ (submit_button string, :class => "positive") + (options[:cancel] ? link_to("Cancel", options[:cancel], :class => "cancel") : '')+ %(</div>) endend