simple-form-builder 0.0.2.rails3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1 @@
1
+ Not ready for prime time.
data/ROADMAP.md ADDED
File without changes
@@ -0,0 +1,2 @@
1
+ require 'simple_form_builder/form_helper'
2
+ ActionView::Base.send :include, SimpleFormBuilder::FormHelper
@@ -0,0 +1,249 @@
1
+ module SimpleFormBuilder
2
+ class FormBuilder < ActionView::Helpers::FormBuilder
3
+
4
+ SFBOptions = %w(wrapper_el no_colon label_text label_class label img input_val hint).collect { |o| o.to_sym }
5
+ InputOptions = %w(required checked rows type).collect { |o| o.to_sym }
6
+
7
+ def initialize(*args)
8
+ @tab_index = 0
9
+ super
10
+ end
11
+
12
+ def error_messages
13
+ if @object.errors.any?
14
+ @template.content_tag(:div,
15
+ @template.content_tag(:ul,
16
+ @object.errors.full_messages.collect do |msg|
17
+ @template.content_tag(:li, msg)
18
+ end.join("\n").html_safe
19
+ ),
20
+ :class => "errors"
21
+ )
22
+ end
23
+ end
24
+
25
+ def date_select(method, options={})
26
+ sfb_options = extract_sfb_options(options)
27
+ list_item(label(method, sfb_options, options) + super, sfb_options, options.merge(:class => "dates"))
28
+ end
29
+
30
+ def time_select(method, options={})
31
+ sfb_options = extract_sfb_options(options)
32
+ list_item(label(method, sfb_options, options) + super, sfb_options, options.merge(:class => "dates"))
33
+ end
34
+
35
+ def text_field(method, options={})
36
+ sfb_options = extract_sfb_options(options)
37
+ labelled_field(method, sfb_options, options, super)
38
+ end
39
+
40
+ def password_field(method, options={})
41
+ sfb_options = extract_sfb_options(options)
42
+ labelled_field(method, sfb_options, options, super)
43
+ end
44
+
45
+ def text_area(method, options={})
46
+ sfb_options = extract_sfb_options(options)
47
+ labelled_field(method, sfb_options, options, super)
48
+ end
49
+
50
+ def check_box(method, options={}, checked_value="1", unchecked_value="0")
51
+ sfb_options = extract_sfb_options(options)
52
+
53
+ options[:class] = "checkbox"
54
+
55
+ list_item(super + label(method, sfb_options, options.merge(:no_colon => true)), sfb_options, options)
56
+ end
57
+
58
+ def radio_button(method, tag_value, options={})
59
+ sfb_options = extract_sfb_options(options)
60
+ label_text = sfb_options.delete(:label_text) || tag_value
61
+ sfb_options[:input_val] = tag_value
62
+ super + radio_label(method, label_text, sfb_options, options)
63
+ end
64
+
65
+ def select(method, choices, options={}, html_options={})
66
+ sfb_options = extract_sfb_options(options)
67
+ labelled_field(method, sfb_options, options, super)
68
+ end
69
+
70
+ def collection_select(method, collection, value_method=:id, text_method=:name, options={}, html_options={})
71
+ sfb_options = extract_sfb_options(options)
72
+ list_item(label(method, sfb_options, options) + super, sfb_options, options)
73
+ end
74
+
75
+ def country_select(method, priority_countries=nil, options={}, html_options={})
76
+ sfb_options = extract_sfb_options(options)
77
+ labelled_field(method, sfb_options, options, super)
78
+ end
79
+
80
+ def time_zone_select(method, priority_zones=nil, options={}, html_options={})
81
+ sfb_options = extract_sfb_options(options)
82
+ labelled_field(method, sfb_options, options, super)
83
+ end
84
+
85
+ def file_field(method, options={})
86
+ sfb_options = extract_sfb_options(options)
87
+ labelled_field(method, sfb_options, options, super)
88
+ end
89
+
90
+ def title(text)
91
+ @template.content_tag(:h3, text)
92
+ end
93
+
94
+ def hint(text)
95
+ @template.content_tag(:p, text)
96
+ end
97
+
98
+ def fieldset(options={}, &block)
99
+ sfb_options = extract_sfb_options(options)
100
+ contents = capture(&block)
101
+ @template.content_tag(
102
+ :fieldset,
103
+ @template.content_tag(:ol, contents),
104
+ options
105
+ )
106
+ end
107
+
108
+ def buttons(&block)
109
+ contents = capture(&block)
110
+ @template.content_tag(
111
+ :fieldset,
112
+ @template.content_tag(:ol, contents)
113
+ )
114
+ end
115
+
116
+ def button(options={})
117
+ sfb_options = extract_sfb_options(options)
118
+ prefix = @object.respond_to?(:new_record?) && @object.new_record? ? "Create" : "Save"
119
+ label = sfb_options.delete(:label) || "#{prefix @object_name.humanize}"
120
+ button_content = sfb_options.delete(:img) ? @template.content_tag(:img, nil, :src => button_content, :alt => label) + label : label
121
+ list_item(@template.content_tag(:button, button_content, options.merge(:type => "submit")), sfb_options, options.merge(:class => "button"))
122
+ end
123
+
124
+ def inner_fieldset(legend=nil, &block)
125
+ contents = capture(&block)
126
+ @template.content_tag(
127
+ :li,
128
+ @template.content_tag(
129
+ :fieldset,
130
+ @template.content_tag(:h3, "#{legend}:") +
131
+ @template.content_tag(:ol, contents)
132
+ )
133
+ )
134
+ end
135
+
136
+ def submit(value="Save Changes", options={})
137
+ sfb_options = extract_sfb_options(options)
138
+ @template.content_tag(:fieldset, super, :class => "button")
139
+ end
140
+
141
+ def fieldgroup(options={}, &block)
142
+ sfb_options = extract_sfb_options(options)
143
+ contents = capture(&block)
144
+ @already_grouping = true
145
+ @template.content_tag(
146
+ :li,
147
+ contents,
148
+ options
149
+ )
150
+ @already_grouping = false
151
+ end
152
+
153
+ def labelled_field(method, sfb_options, options, markup)
154
+ if @already_grouping
155
+ markup + label(method, sfb_options, options)
156
+ else
157
+ list_item(label(method, sfb_options, options) + markup, sfb_options, options)
158
+ end
159
+ end
160
+
161
+ def reversed_labelled_field(method, sfb_options, options, markup)
162
+ if @already_grouping
163
+ markup + label(method, sfb_options, options)
164
+ else
165
+ list_item(markup + label(method, sfb_options, options), sfb_options, options)
166
+ end
167
+ end
168
+
169
+ def simple_fields_for(record_or_name_or_array, *args, &proc)
170
+ options = args.extract_options!
171
+ fields_for(record_or_name_or_array, *(args << options.merge(:builder => self.class)), &proc)
172
+ end
173
+
174
+ private
175
+ def extract_sfb_options(options={})
176
+ SFBOptions.inject({}) do |h, k|
177
+ h[k] = options.delete(k)
178
+ h
179
+ end
180
+ end
181
+
182
+ def strip_input_options(options={})
183
+ options.tap do |o|
184
+ InputOptions.each do |k|
185
+ o.delete(k)
186
+ end
187
+ end
188
+ end
189
+
190
+ def capture(&block)
191
+ if block_given?
192
+ contents = if @template.respond_to?(:is_haml?) && @template.is_haml?
193
+ @template.capture_haml(&block)
194
+ else
195
+ @template.capture(&block)
196
+ end
197
+ end
198
+ end
199
+
200
+ def list_item(markup, sfb_options, options={})
201
+ container_element = sfb_options[:wrapper_el] || :li
202
+ @template.content_tag(container_element, markup, strip_input_options(options))
203
+ end
204
+
205
+ def label(method, sfb_options, options={})
206
+ text = sfb_options[:label] || (@already_grouping ? method.to_s.titleize.upcase : method.to_s.titleize)
207
+ text += ":" unless sfb_options[:no_colon]
208
+
209
+ hint = sfb_options[:hint]
210
+
211
+ text = text + " <span class='hint'>#{hint}</span>" if hint
212
+
213
+ dom_class = sfb_options[:label_class] || method
214
+
215
+ ActionView::Helpers::InstanceTag.new(
216
+ object_name, method, self, object
217
+ ).to_label_tag(text.html_safe, :class => dom_class)
218
+ end
219
+
220
+ def radio_label(method, value, sfb_options, options={})
221
+ for_val = sfb_options[:input_val] || value
222
+ @template.content_tag :label,
223
+ value.humanize,
224
+ options.merge(:for => radio_button_id(method, for_val, strip_input_options(options)))
225
+ end
226
+
227
+ def radio_button_id(method, value, options)
228
+ pretty_value = value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
229
+
230
+ options[:id] || defined?(@auto_index) ?
231
+ "#{object_name}_#{@auto_index}_#{method}_#{pretty_value}" :
232
+ "#{object_name}_#{method}_#{pretty_value}"
233
+ end
234
+
235
+ def cancel_link(options_or_url)
236
+ if options_or_url.is_a? Hash
237
+ cancel_url = options_or_url.delete(:url)
238
+ dom_class = options_or_url.delete(:class)
239
+ cancel_label = options_or_url.delete(:label)
240
+ else
241
+ cancel_url = options_or_url
242
+ dom_class = nil
243
+ cancel_label = nil
244
+ end
245
+ @template.link_to cancel_label || "Cancel", cancel_url || :back, :class => "cancel #{dom_class}"
246
+ end
247
+ end
248
+
249
+ end
@@ -0,0 +1,20 @@
1
+ require 'simple_form_builder/form_builder'
2
+
3
+ module SimpleFormBuilder
4
+ module FormHelper
5
+ def simple_form_for(record_or_name_or_array, *args, &proc)
6
+ options = args.extract_options!
7
+ # Remove any nils if we received an array, this allows for common forms to be built
8
+ # that can be used for both edit and new actions but that don't require the edit action
9
+ # to be nested within a parent resource
10
+ record_or_name_or_array.reject! {|a| a.nil? } if record_or_name_or_array.is_a? Array
11
+ # Now build the form
12
+ form_for(record_or_name_or_array, *(args << options.merge(:builder => SimpleFormBuilder::FormBuilder)), &proc)
13
+ end
14
+
15
+ def simple_fields_for(record_or_name_or_array, *args, &proc)
16
+ options = args.extract_options!
17
+ fields_for(record_or_name_or_array, *(args << options.merge(:builder => SimpleFormBuilder::FormBuilder)), &proc)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleFormBuilder
2
+ VERSION = '0.0.2.rails3'
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-form-builder
3
+ version: !ruby/object:Gem::Version
4
+ hash: 708262389
5
+ prerelease: 6
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ - rails
11
+ - 3
12
+ version: 0.0.2.rails3
13
+ platform: ruby
14
+ authors:
15
+ - Ben Askins
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-01-13 00:00:00 +11:00
21
+ default_executable:
22
+ dependencies: []
23
+
24
+ description: Extends ActionView::Helpers::FormBuilder with simple, best practices markup.
25
+ email:
26
+ - ben.askins@plus2.com.au
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/simple_form_builder/form_builder.rb
35
+ - lib/simple_form_builder/form_helper.rb
36
+ - lib/simple_form_builder/version.rb
37
+ - lib/simple_form_builder.rb
38
+ - CHANGELOG.md
39
+ - README.md
40
+ - ROADMAP.md
41
+ has_rdoc: true
42
+ homepage: http://github.com/plustwo/simple-form-builder
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 23
65
+ segments:
66
+ - 1
67
+ - 3
68
+ - 6
69
+ version: 1.3.6
70
+ requirements: []
71
+
72
+ rubyforge_project: simple-form-builder
73
+ rubygems_version: 1.4.2
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A best-practice FormBuilder
77
+ test_files: []
78
+