public
Description: A handy and flexible table / css form builder
Homepage: http://code.google.com/p/uber-builder/
Clone URL: git://github.com/timcharper/uber-builder.git
converted line endings
timcharper (author)
Sat Sep 27 14:39:19 -0700 2008
commit  e53c27c4219bf8a4a481f7f15edc24b656343a8d
tree    f7041a84612119da6956ca3d28a42375e97c5f91
parent  9ab84c1a89cf90c269f1158656f229566842b6d1
...
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
128
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
0
@@ -1,127 +1,141 @@
0
-module UberBuilder
0
- module TemplatedMethods
0
- include ActionView::Helpers::TextHelper
0
- include ActionView::Helpers::CaptureHelper
0
-
0
- def initialize(*args)
0
- super
0
-
0
- @layout = nil
0
- @bypass_for_block = false
0
- end
0
-
0
- ( ActionView::Helpers::FormBuilder.field_helpers - %w(check_box radio_button radio_button_list hidden_field) + %w(time_select)).each do |selector|
0
- class_eval <<-END_SRC, __FILE__, __LINE__
0
- def #{selector}(field, options = {})
0
- field = field.to_s
0
- tabular_options = extract_tabular_options(field, options)
0
- generic_field(field, super, tabular_options )
0
- end
0
- END_SRC
0
- end
0
-
0
- def hidden_field(field, options={})
0
- return super
0
- end
0
-
0
- %w(check_box radio_button).each do |selector|
0
- class_eval <<-END_SRC, __FILE__, __LINE__
0
- def #{selector}(field, options = {})
0
- field = field.to_s
0
- tabular_options = extract_tabular_options(field, options)
0
- generic_field(field, super, tabular_options.merge(:label => :after))
0
- end
0
- END_SRC
0
- end
0
-
0
- def static(text, options = {})
0
- tabular_options = extract_tabular_options("", options)
0
- generic_field(nil, text, tabular_options)
0
- end
0
-
0
- def submit(text="Submit", options = {})
0
- generic_field(nil, @template.submit_tag(text, options))
0
- end
0
-
0
- def select(field, choices, options = {}, html_options = {})
0
- field = field.to_s
0
- tabular_options = extract_tabular_options(field, options)
0
- generic_field(field, super, tabular_options )
0
- end
0
-
0
- def radio_button_list(field, choices, options = {})
0
- field = field.to_s
0
- tabular_options = extract_tabular_options(field, options)
0
- generic_field(field, super, tabular_options )
0
- end
0
-
0
- def generic_field(fieldname, field_content, options = {})
0
- return field_content if @layout.nil? || @bypass_for_block
0
-
0
- field_content = options[:prefix] + field.to_s if options[:prefix]
0
- field_content = field.to_s + options[:suffix] if options[:suffix]
0
-
0
- label_text = options[:label_text]
0
- label_text = "*#{label_text}" if options[:required]
0
-
0
- @layout.field(fieldname, field_content, label_text, options)
0
- end
0
-
0
- def manual(options = {}, &block)
0
- raise "manual expects a block" unless block_given?
0
-
0
- content = with_layout(nil) {capture(&block)}
0
- tabular_options = extract_tabular_options( "", options )
0
-
0
- concat( generic_field("", content, tabular_options), block.binding)
0
- end
0
- alias :multiple :manual
0
-
0
- # create a new instance of the builder as a different class - helpful when wanting to convert to and from static
0
- def to(klass)
0
- new_builder = klass.allocate
0
- self.instance_variables.each{|instance_variable|
0
- new_builder.instance_variable_set(instance_variable, self.instance_variable_get(instance_variable))
0
- }
0
-
0
- new_builder
0
- end
0
-
0
- def to_static
0
- self.to(UberBuilder::StaticBuilder)
0
- end
0
-
0
- def static?
0
- is_a?(UberBuilder::StaticBuilder)
0
- end
0
-
0
- UberBuilder::Layouts.constants.each do |layout|
0
- class_eval <<-EOF
0
- def #{layout.downcase}(html_options = {}, &block)
0
- raise "expected a block" unless block_given?
0
- layout = UberBuilder::Layouts::#{layout}.new(@template, @object_name, self)
0
- content = with_layout(layout) { capture(&block) }
0
- concat( layout.section(content, html_options), block.binding )
0
- end
0
- EOF
0
- end
0
-
0
- protected
0
- def extract_tabular_options(field, options)
0
- {
0
- :label_text => options.delete(:label) || field.to_s.humanize,
0
- :required => options.delete(:required) || false,
0
- :prefix => options.delete(:prefix),
0
- :suffix => options.delete(:suffix),
0
- :row => options.delete(:row)
0
- }
0
- end
0
-
0
- def with_layout(layout, &block)
0
- last_layout, @layout = @layout, layout
0
- return_value = yield
0
- @layout = last_layout
0
- return_value
0
- end
0
- end
0
+module UberBuilder
0
+ module TemplatedMethods
0
+ include ActionView::Helpers::TextHelper
0
+ include ActionView::Helpers::CaptureHelper
0
+
0
+ def initialize(*args)
0
+ super
0
+
0
+ @layout = nil
0
+ @bypass_for_block = false
0
+ end
0
+
0
+ ( ActionView::Helpers::FormBuilder.field_helpers - %w(check_box radio_button radio_button_list hidden_field) + %w(time_select)).each do |selector|
0
+ class_eval <<-END_SRC, __FILE__, __LINE__
0
+ def #{selector}(field, options = {})
0
+ field = field.to_s
0
+ tabular_options = extract_tabular_options(field, options)
0
+ generic_field(field, super, tabular_options )
0
+ end
0
+ END_SRC
0
+ end
0
+
0
+ def hidden_field(field, options={})
0
+ return super
0
+ end
0
+
0
+ %w(check_box radio_button).each do |selector|
0
+ class_eval <<-END_SRC, __FILE__, __LINE__
0
+ def #{selector}(field, options = {})
0
+ field = field.to_s
0
+ tabular_options = extract_tabular_options(field, options)
0
+ generic_field(field, super, tabular_options.merge(:label => :after))
0
+ end
0
+ END_SRC
0
+ end
0
+
0
+ def static(text, options = {})
0
+ tabular_options = extract_tabular_options("", options)
0
+ generic_field(nil, text, tabular_options)
0
+ end
0
+
0
+ def submit(text="Submit", options = {})
0
+ generic_field(nil, @template.submit_tag(text, options))
0
+ end
0
+
0
+ def select(field, choices, options = {}, html_options = {})
0
+ field = field.to_s
0
+ tabular_options = extract_tabular_options(field, options)
0
+ generic_field(field, super, tabular_options )
0
+ end
0
+
0
+ def radio_button_list(field, choices, options = {})
0
+ field = field.to_s
0
+ tabular_options = extract_tabular_options(field, options)
0
+ generic_field(field, super, tabular_options )
0
+ end
0
+
0
+ def generic_field(fieldname, field_content, options = {})
0
+ return field_content if @layout.nil? || @bypass_for_block
0
+
0
+ field_content = options[:prefix] + field.to_s if options[:prefix]
0
+ field_content = field.to_s + options[:suffix] if options[:suffix]
0
+
0
+ label_text = options[:label_text]
0
+ label_text = "*#{label_text}" if options[:required]
0
+
0
+ @layout.field(fieldname, field_content, label_text, options)
0
+ end
0
+
0
+ def manual(options = {}, &block)
0
+ raise "manual expects a block" unless block_given?
0
+
0
+ content = with_layout(nil) {capture(&block)}
0
+ tabular_options = extract_tabular_options( "", options )
0
+
0
+ concat( generic_field("", content, tabular_options), block.binding)
0
+ end
0
+ alias :multiple :manual
0
+
0
+ # create a new instance of the builder as a different class - helpful when wanting to convert to and from static
0
+ def to(klass)
0
+ new_builder = klass.allocate
0
+ self.instance_variables.each{|instance_variable|
0
+ new_builder.instance_variable_set(instance_variable, self.instance_variable_get(instance_variable))
0
+ }
0
+
0
+ new_builder
0
+ end
0
+
0
+ def to_static
0
+ self.to(UberBuilder::StaticBuilder)
0
+ end
0
+
0
+ def static?
0
+ is_a?(UberBuilder::StaticBuilder)
0
+ end
0
+
0
+ def respond_to?(method, include_private = false)
0
+ super || define_layout_method(method)
0
+ end
0
+
0
+ def method_missing(method, *args, &block)
0
+ respond_to?(method) ? send(method, *args, &block) : super
0
+ end
0
+
0
+ protected
0
+ def define_layout_method(method)
0
+ const_name = method.to_s.classify
0
+ return false unless UberBuilder::Layouts.const_defined?(const_name)
0
+
0
+ self.class.class_eval <<-EOF
0
+ def #{method}(html_options = {}, &block)
0
+ raise "expected a block" unless block_given?
0
+ layout = UberBuilder::Layouts::#{const_name}.new(@template, @object_name, self)
0
+ content = with_layout(layout) { capture(&block) }
0
+ concat( layout.section(content, html_options), block.binding )
0
+ end
0
+
0
+ public :#{method}
0
+ EOF
0
+ true
0
+ end
0
+
0
+ def extract_tabular_options(field, options)
0
+ {
0
+ :label_text => options.delete(:label) || field.to_s.humanize,
0
+ :required => options.delete(:required) || false,
0
+ :prefix => options.delete(:prefix),
0
+ :suffix => options.delete(:suffix),
0
+ :outer => options.delete(:outer)
0
+ }
0
+ end
0
+
0
+ def with_layout(layout, &block)
0
+ last_layout, @layout = @layout, layout
0
+ return_value = yield
0
+ @layout = last_layout
0
+ return_value
0
+ end
0
+ end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.