F4j3Q3VM / Sexy_Forms_AR.rb

Revision 95149780e2e1 (3 weeks, 2 days ago) - Diff

Link to this snippet : http://friendpaste.com/F4j3Q3VM

  • Wrap long lines
  • Show line numbers
1module 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] = Inflector.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 errors_on_attribute(attribute)
30 # @_obj comes automagically from merb
31 [@_obj.errors[attribute.to_s]].flatten.join(' and ') rescue []
32 end
33
34 # Pass more_html for cancel link (or whatever)
35 def form_submit(string, options = {})
36 %(<div class="field controls">)+
37 (submit_button string, :class => "positive") +
38 (options[:cancel] ? link_to("Cancel", options[:cancel], :class => "cancel") : '')+
39 %(</div>)
40 end
41end

View in other formats:
raw format | download source

  1.   

F4j3Q3VM / Sexy_Forms_AR.rb

last change Tue Aug 5 09:35:19 2008 (UTC)

Older revisions