7cWIvEfFDQcNTR4kAHIzgy changeset

Changeset644ab7ba1654 (b)
ParentNone (a)
ab
0+# @_obj isn't working anymore
0+module Merb::Helpers::Form::Builder
0+  class Base
0+    def current_obj
0+      @obj
0+    end
0+  end
0+end
0+
0+
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] = 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+    begin
0+      content = send("#{control}_field", attribute, options)
0+    rescue
0+      content = send(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 humanize(sym)
0+    sym.to_s.gsub(/_id$/, "").gsub(/_/, " ").gsub( /\S+/ ){|s| s.capitalize}
0+  end
0+ 
0+  def errors_on_attribute(attribute)
0+    [current_form_context.current_obj.errors.on(attribute)].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+
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
--- Revision None
+++ Revision 644ab7ba1654
@@ -0,0 +1,55 @@
+# @_obj isn't working anymore
+module Merb::Helpers::Form::Builder
+ class Base
+ def current_obj
+ @obj
+ end
+ end
+end
+
+
+module 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>)
+ end
+end
+