foundation-formbuilder-rails 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 827b18acfa92f945be3a48f81574b80a8a6edb17
4
+ data.tar.gz: 02dd3ff317f269155bfbfdf59c752c3bbb5f1006
5
+ SHA512:
6
+ metadata.gz: d053d31a056d78fba3f0a209b37eace519bd1adeb3b236b30a2f9e1f0cb688dfd0b4d88fa52cbaf34173ccda225e7ce6b56f5d8d0101b589d767f75ef4931b68
7
+ data.tar.gz: 3882052b6a98e18fc41b91cfa3d3bac3a933552e79dadbd007b148bda4f1e2a2adbf1511817bd5c53f5944a4e130bc9123b716b1446426311edeb0c0968f9056
@@ -0,0 +1,75 @@
1
+ # foundation-formbuilder-rails v0.2.1
2
+
3
+ This is a custom FormBuilder used to display form elements generated
4
+ by the form_for helpers in the correct style for
5
+ the [*Foundation 5 Framework*](http://foundation.zurb.com/).
6
+
7
+
8
+ See the Foundation [docs](http://foundation.zurb.com/docs/components/forms.html)
9
+ for form specifics.
10
+
11
+ # Getting Started
12
+
13
+ 1. Add the foundation-formbuilder-rails Gem to your project's `Gemfile`:
14
+
15
+ gem 'foundation-formbuilder-rails', '0.2.1'
16
+
17
+ 2. Ensure you are using the FormBuilder in your form:
18
+
19
+ form_for @test, builder: Foundation::FormBuilder::Rails::FormBuilder do |f|
20
+ ...
21
+ end
22
+
23
+ 3. For usage, see comments in the [source code](https://github.com/ashleybye/foundation-formbuilder-rails/blob/master/lib/foundation/form_builder/rails/form_builder.rb). I will get around to adding proper use examples to the README in the future.
24
+
25
+ # Complete
26
+
27
+ I have completed the builder for all of the Rails FormBuilder and FormOptionsHelper
28
+ methods. I have also included some Foundation specific methods, but I may remove these
29
+ before the final release.
30
+
31
+ ## FormHelper
32
+
33
+ `check_box`
34
+ `color_field`
35
+ `date_field`
36
+ `datetime_field`
37
+ `datetime_local_field`
38
+ `email_field`
39
+ `file_field`
40
+ `month_field`
41
+ `number_field`
42
+ `password_field`
43
+ `phone_field`
44
+ `radio_button`
45
+ `range_field`
46
+ `search_field`
47
+ `telephone_field`
48
+ `time_field`
49
+ `text_field`
50
+ `text_area`
51
+ `url_field`
52
+ `week_field`
53
+
54
+ ## FormOptionsHelper
55
+
56
+ `collection_check_boxes`
57
+ `collection_radio_buttons`
58
+ `collection_select`
59
+ `grouped_collection_select`
60
+ `select`
61
+ `time_zone_select`
62
+
63
+ ## Foundation Specific
64
+
65
+ (May be removed at a later stage)
66
+ `check_box_group`
67
+ `radio_button_group`
68
+
69
+ # TODO
70
+
71
+ I am still deciding whether it is worth writing builder methods to provide the following:
72
+
73
+ `Pre/Postfix labels (Foundation)`
74
+ `Switches (Foundation)`
75
+ `Abide Validation (Foundaation)`
@@ -0,0 +1 @@
1
+ require 'foundation/form_builder'
@@ -0,0 +1 @@
1
+ require 'foundation/form_builder/rails'
@@ -0,0 +1,3 @@
1
+ require 'foundation/form_builder/rails/engine'
2
+ require 'foundation/form_builder/rails/version'
3
+ require 'foundation/form_builder/rails/form_builder'
@@ -0,0 +1,9 @@
1
+ module Foundation
2
+ module FormBuilder
3
+ module Rails
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace Foundation::FormBuilder::Rails
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,663 @@
1
+ module Foundation
2
+ module FormBuilder
3
+ module Rails
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+
6
+ =begin
7
+
8
+ =================================================
9
+ TODO
10
+ =================================================
11
+
12
+ ????? Pre/Postfix labels (Foundation) ?????
13
+ ????? Switches (Foundation) ?????
14
+ ????? Abide Validation (Foundaation) ?????
15
+
16
+ =end
17
+
18
+ ERROR_CSS_CLASS = :error
19
+
20
+ # A custom FormBuilder to provide forms marked up to work with the Foundation 5 CSS framework.
21
+ # The default ActionView::Helpers::FormHelper methods are still available and will work exactly
22
+ # as expected with RoR. Clearly, they will still have the Foundation 5 styling applied to them.
23
+ #
24
+ # For example of HTML markup for the forms, see:
25
+ # http://foundation.zurb.com/docs/components/forms.html
26
+ #
27
+ # Each form needs to specifically reference this builder.
28
+
29
+ # All examples shown are in the context of:
30
+ #
31
+ # form_for @test, builder: ZurbFormBuilder do |f|
32
+ # ...
33
+ # end
34
+
35
+ # Returns a single check_box with a label wrapped around it and an error label if an error in validation.
36
+ # The options hash can be used to further customise the label and field, but specific options must be
37
+ # provided in a hash for that element.
38
+ #
39
+ # f.zurb_check_box :check_box
40
+ # # => <input name="test[check_box]" value="0" type="hidden">
41
+ # <input id="test_check_box" name="test[check_box]" value="1" type="checkbox">
42
+ # <label for="test">Check box</label>
43
+ #
44
+ # f.zurb_check_box :check_box, field: { checked: true }
45
+ # # => <input name="test[check_box]" value="0" type="hidden">
46
+ # <input checked="checked" id="test_check_box" name="test[check_box]" value="1" type="checkbox">
47
+ # <label for="test">Check box</label>
48
+
49
+ def zurb_check_box(method, options = { label: {}, field: {} }, checked_value = "1", unchecked_value = "0")
50
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
51
+ errors = get_field_errors(method)
52
+ add_error_class_to(options) if errors.any?
53
+
54
+ field = @template.check_box(@object_name, method, options[:field], checked_value, unchecked_value) +
55
+ @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}".html_safe,
56
+ options[:label])
57
+
58
+ errors.any? ? add_error_message(field, errors) : field
59
+ end
60
+
61
+ # Used to group a collection of check boxes together under one labelled heading. The overall label
62
+ # name is determined by the method name or by specifying it explicitly in the :label options hash.
63
+ # Each check box and its corresponding label is specified in a hash passed to tag_values, as
64
+ # label: [checked_value, unchecked_value], with the any default checked values being specified in the
65
+ # default options array. The options hash will be applied to radio button fields and labels only.
66
+ # Full usage is similar to that of radio_button_group.
67
+ #
68
+ # f.zurb_check_box_group :check_box_group, { yes: [true, false], no: [false, true] }, [:yes]
69
+ # # => <label for="test">Check box group</label>
70
+ # <input checked="checked" id="test_check_box_group" name="test[check_box_group]" value="true" type="checkbox">
71
+ # <label for="test">Yes</label>
72
+ # <input name="test[check_box_group]" value="true" type="hidden">
73
+ # <input id="test_check_box_group" name="test[check_box_group]" value="false" type="checkbox">
74
+ # <label for="test">No</label>
75
+ def zurb_check_box_group(method, tag_values, checked = [], options = { label: {}, field: {} })
76
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
77
+ errors = get_field_errors(method)
78
+ add_error_class_to(options) if errors.any?
79
+
80
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
81
+ options[:label])
82
+
83
+ # Iterate through check boxes hash and check to see what values should be set as checked in
84
+ # the checked array and adding each check box to @field
85
+ tag_values.each do |tag_name, checked_unchecked_values|
86
+ options[:field][:checked] = true if checked.include?(tag_name)
87
+ field << @template.check_box(@object_name, method, options[:field], checked_unchecked_values[0],
88
+ checked_unchecked_values[1]) +
89
+ @template.label_tag(@object_name, "#{tag_name.to_s.humanize}".html_safe,
90
+ options[:label])
91
+ options[:field][:checked] = nil # Reset checked as options is directly modified for all values
92
+ end
93
+
94
+ errors.any? ? add_error_message(field, errors) : field
95
+ end
96
+
97
+ # Works in exactly the same way as the standard collection_check_boxes method
98
+ def zurb_collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
99
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
100
+ errors = get_field_errors(method)
101
+ add_error_class_to(options) if errors.any?
102
+
103
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
104
+ options[:label])
105
+ field << @template.collection_check_boxes(@object_name, method, collection, value_method, text_method,
106
+ objectify_options(options[:field]), @default_options.merge(html_options), &block)
107
+
108
+ errors.any? ? add_error_message(field, errors) : field
109
+ end
110
+
111
+ # Works in exactly the same way as the standard collection_radio_buttons method
112
+ def zurb_collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
113
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
114
+ errors = get_field_errors(method)
115
+ add_error_class_to(options) if errors.any?
116
+
117
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
118
+ options[:label])
119
+ field << @template.collection_radio_buttons(@object_name, method, collection, value_method, text_method,
120
+ objectify_options(options[:field]), @default_options.merge(html_options), &block)
121
+
122
+ errors.any? ? add_error_message(field, errors) : field
123
+ end
124
+
125
+ # Works in exactly the same way as the standard collection_radio_buttons method
126
+ def zurb_collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
127
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
128
+ errors = get_field_errors(method)
129
+ add_error_class_to(options) if errors.any?
130
+
131
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
132
+ options[:label])
133
+ field << @template.collection_select(@object_name, method, collection, value_method, text_method,
134
+ objectify_options(options[:field]), @default_options.merge(html_options))
135
+
136
+ errors.any? ? add_error_message(field, errors) : field
137
+ end
138
+
139
+ # Returns a color_field with a label wrapped around it and an error label if an error in validation. The
140
+ # options hash can be used to further customise the label and field, but specific options must be
141
+ # provided in a hash for that element.
142
+ #
143
+ # f.zurb_color_field :color_field
144
+ # # => <label for="test">Color field<input id="test_color_field" name="test[color_field]" type="color"></label>
145
+ def zurb_color_field(method, options = { label: {}, field: {} })
146
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
147
+ errors = get_field_errors(method)
148
+ add_error_class_to(options) if errors.any?
149
+
150
+ field = @template.label_tag(@object_name,
151
+ "#{options[:label][:label] || method.to_s.humanize}
152
+ #{@template.color_field(@object_name, method, options[:field])}".html_safe,
153
+ options[:label]
154
+ )
155
+
156
+ errors.any? ? add_error_message(field, errors) : field
157
+ end
158
+
159
+ # Returns a date_field with a label wrapped around it and an error label if an error in validation. The
160
+ # options hash can be used to further customise the label and field, but specific options must be
161
+ # provided in a hash for that element.
162
+ #
163
+ # f.zurb_date_field :date_field
164
+ # # => <label for="test">Date field<input id="test_date_field" name="test[date_field]" type="date"></label>
165
+ def zurb_date_field(method, options = { label: {}, field: {} })
166
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
167
+ errors = get_field_errors(method)
168
+ add_error_class_to(options) if errors.any?
169
+
170
+ field = @template.label_tag(@object_name,
171
+ "#{options[:label][:label] || method.to_s.humanize}
172
+ #{@template.date_field(@object_name, method, options[:field])}".html_safe,
173
+ options[:label]
174
+ )
175
+
176
+ errors.any? ? add_error_message(field, errors) : field
177
+ end
178
+
179
+ # Returns a datetime_field with a label wrapped around it and an error label if an error in validation. The
180
+ # options hash can be used to further customise the label and field, but specific options must be
181
+ # provided in a hash for that element.
182
+ #
183
+ # f.zurb_datetime_field :datetime_field
184
+ # # => <label for="test">Datetime field<input id="test_datetime_field" name="test[datetime_field]"
185
+ # type="datetime"></label>
186
+ def zurb_datetime_field(method, options = { label: {}, field: {} })
187
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
188
+ errors = get_field_errors(method)
189
+ add_error_class_to(options) if errors.any?
190
+
191
+ field = @template.label_tag(@object_name,
192
+ "#{options[:label][:label] || method.to_s.humanize}
193
+ #{@template.datetime_field(@object_name, method, options[:field])}".html_safe,
194
+ options[:label]
195
+ )
196
+
197
+ errors.any? ? add_error_message(field, errors) : field
198
+ end
199
+
200
+ # Returns a datetime_local_field with a label wrapped around it and an error label if an error in validation.
201
+ # The options hash can be used to further customise the label and field, but specific options must be
202
+ # provided in a hash for that element.
203
+ #
204
+ # f.zurb_datetime_local_field :datetime_local_field
205
+ # # => <label for="test">Datetime local field<input id="test_datetime_local_field"
206
+ # name="test[datetime_local_field]" type="datetime-local"></label>
207
+ def zurb_datetime_local_field(method, options = { label: {}, field: {} })
208
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
209
+ errors = get_field_errors(method)
210
+ add_error_class_to(options) if errors.any?
211
+
212
+ field = @template.label_tag(@object_name,
213
+ "#{options[:label][:label] || method.to_s.humanize}
214
+ #{@template.datetime_local_field(@object_name, method, options[:field])}".html_safe,
215
+ options[:label]
216
+ )
217
+
218
+ errors.any? ? add_error_message(field, errors) : field
219
+ end
220
+
221
+ # Returns a email_field with a label wrapped around it and an error label if an error in validation. The
222
+ # options hash can be used to further customise the label and field, but specific options must be
223
+ # provided in a hash for that element.
224
+ #
225
+ # f.zurb_email_field :email_field
226
+ # # => <label for="test">Email field<input id="test_email_field" name="test[email_field]"
227
+ # type="email"></label>
228
+ def zurb_email_field(method, options = { label: {}, field: {} })
229
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
230
+ errors = get_field_errors(method)
231
+ add_error_class_to(options) if errors.any?
232
+
233
+ field = @template.label_tag(@object_name,
234
+ "#{options[:label][:label] || method.to_s.humanize}
235
+ #{@template.email_field(@object_name, method, options[:field])}".html_safe,
236
+ options[:label]
237
+ )
238
+
239
+ errors.any? ? add_error_message(field, errors) : field
240
+ end
241
+
242
+ # Returns a file_field with a label wrapped around it and an error label if an error in validation. The
243
+ # options hash can be used to further customise the label and field, but specific options must be
244
+ # provided in a hash for that element.
245
+ #
246
+ # f.zurb_file_field :file_field
247
+ # # => <label for="test">File field<input id="test_file_field" name="test[file_field]" type="file"></label>
248
+ def zurb_file_field(method, options = { label: {}, field: {} })
249
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
250
+ errors = get_field_errors(method)
251
+ add_error_class_to(options) if errors.any?
252
+
253
+ field = @template.label_tag(@object_name,
254
+ "#{options[:label][:label] || method.to_s.humanize}
255
+ #{@template.file_field(@object_name, method, options[:field])}".html_safe,
256
+ options[:label]
257
+ )
258
+
259
+ errors.any? ? add_error_message(field, errors) : field
260
+ end
261
+
262
+ # Works in exactly the same way as the standard grouped_collection_select method
263
+ def zurb_grouped_collection_select(method, collection, group_method, group_label_method,
264
+ option_key_method, option_value_method, options = {}, html_options = {})
265
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
266
+ errors = get_field_errors(method)
267
+ add_error_class_to(options) if errors.any?
268
+
269
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
270
+ options[:label])
271
+ field << @template.grouped_collection_select(@object_name, method, collection, group_method,
272
+ group_label_method, option_key_method, option_value_method, objectify_options(options),
273
+ @default_options.merge(html_options))
274
+
275
+ errors.any? ? add_error_message(field, errors) : field
276
+ end
277
+
278
+
279
+
280
+
281
+ # Returns a month_field with a label wrapped around it and an error label if an error in validation. The
282
+ # options hash can be used to further customise the label and field, but specific options must be
283
+ # provided in a hash for that element.
284
+ #
285
+ # f.zurb_month_field :month_field
286
+ # # => <label for="test">Month field<input id="test_month_field" name="test[month_field]" type="month"></label>
287
+ def zurb_month_field(method, options = { label: {}, field: {} })
288
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
289
+ errors = get_field_errors(method)
290
+ add_error_class_to(options) if errors.any?
291
+
292
+ field = @template.label_tag(@object_name,
293
+ "#{options[:label][:label] || method.to_s.humanize}
294
+ #{@template.month_field(@object_name, method, options[:field])}".html_safe,
295
+ options[:label]
296
+ )
297
+
298
+ errors.any? ? add_error_message(field, errors) : field
299
+ end
300
+
301
+ # Returns a number_field with a label wrapped around it and an error label if an error in validation. The
302
+ # options hash can be used to further customise the label and field, but specific options must be
303
+ # provided in a hash for that element.
304
+ #
305
+ # f.zurb_number_field :number_field
306
+ # # => <label for="test">Number field<input id="test_number_field" name="test[number_field]" type="number"></label>
307
+ def zurb_number_field(method, options = { label: {}, field: {} })
308
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
309
+ errors = get_field_errors(method)
310
+ add_error_class_to(options) if errors.any?
311
+
312
+ field = @template.label_tag(@object_name,
313
+ "#{options[:label][:label] || method.to_s.humanize}
314
+ #{@template.number_field(@object_name, method, options[:field])}".html_safe,
315
+ options[:label]
316
+ )
317
+
318
+ errors.any? ? add_error_message(field, errors) : field
319
+ end
320
+
321
+ # Returns a text_field with a label wrapped around it and an error label if an error in validation. The
322
+ # options hash can be used to further customise the label and field, but specific options must be
323
+ # provided in a hash for that element.
324
+ #
325
+ # f.zurb_password_field :password_field
326
+ # # => <label for="test">Password field
327
+ # <input id="test_password_field" name="test[password_field]" type="password"></label>
328
+ def zurb_password_field(method, options = { label: {}, field: {} })
329
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
330
+ errors = get_field_errors(method)
331
+ add_error_class_to(options) if errors.any?
332
+
333
+ field = @template.label_tag(@object_name,
334
+ "#{options[:label][:label] || method.to_s.humanize}
335
+ #{@template.password_field(@object_name, method, options[:field])}".html_safe,
336
+ options[:label]
337
+ )
338
+
339
+ errors.any? ? add_error_message(field, errors) : field
340
+ end
341
+
342
+ # Returns a phone_field with a label wrapped around it and an error label if an error in validation. The
343
+ # options hash can be used to further customise the label and field, but specific options must be
344
+ # provided in a hash for that element.
345
+ #
346
+ # f.zurb_phone_field :phone_field
347
+ # # => <label for="test">Phone field<input id="test_phone_field" name="test[phone_field]" type="phone"></label>
348
+ def zurb_phone_field(method, options = { label: {}, field: {} })
349
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
350
+ errors = get_field_errors(method)
351
+ add_error_class_to(options) if errors.any?
352
+
353
+ field = @template.label_tag(@object_name,
354
+ "#{options[:label][:label] || method.to_s.humanize}
355
+ #{@template.phone_field(@object_name, method, options[:field])}".html_safe,
356
+ options[:label]
357
+ )
358
+
359
+ errors.any? ? add_error_message(field, errors) : field
360
+ end
361
+
362
+ # Returns a single radio_button with a label wrapped around it and an error label if an error in validation.
363
+ # The options hash can be used to further customise the label and field, but specific options must be
364
+ # provided in a hash for that element.
365
+ #
366
+ # It is very unlikely that this will be used on its own. Ever! Possibly remove?
367
+ #
368
+ # f.zurb_radio_button :radio_button, :yes
369
+ # # => <input id="test_radio_button_yes" name="test[radio_button]" value="yes" type="radio">
370
+ # <label for="test">Radio button</label>
371
+
372
+ def zurb_radio_button(method, tag_value, options = { label: {}, field: {} })
373
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
374
+ errors = get_field_errors(method)
375
+ add_error_class_to(options) if errors.any?
376
+
377
+ field = @template.radio_button(@object_name, method, tag_value, options[:field]) +
378
+ @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}".html_safe,
379
+ options[:label])
380
+
381
+ errors.any? ? add_error_message(field, errors) : field
382
+ end
383
+
384
+ # Used to group a collection of radio buttons together under one labelled heading. The overall label
385
+ # name is determined by the method name or by specifying it explicitly in the :label options hash.
386
+ # Each radio button and its corresponding label is specified in a hash passed to tag_values, as
387
+ # label: :value, with the default radio button value being specified in the default option.
388
+ # The options hash will be applied to radio button fields and labels only.
389
+ #
390
+ # f.zurb_radio_button_group :radio_button_group, {yes: true, no: false
391
+ # # => <label for="test">Radio button group</label>
392
+ # <input id="test_radio_button_group_true" name="test[radio_button_group]" value="true" type="radio">
393
+ # <label for="test">Yes</label>
394
+ # <input id="test_radio_button_group_false" name="test[radio_button_group]" value="false" type="radio">
395
+ # <label for="test">No</label>
396
+ #
397
+ # f.zurb_radio_button_group :radio_button_group, {yes: true, no: false}, false
398
+ # # => <label for="test">Radio button group</label>
399
+ # <input id="test_radio_button_group_true" name="test[radio_button_group]" value="true" type="radio">
400
+ # <label for="test">Yes</label>
401
+ # <input checked="checked" id="test_radio_button_group_false" name="test[radio_button_group]" value="false" type="radio">
402
+ # <label for="test">No</label>
403
+ #
404
+ # f.zurb_radio_button_group :text_field, {yes: true, no: false}, false,
405
+ # label: { label: "Giant chicken?" }
406
+ # # => <label for="test" label="Giant chicken?">Giant chicken?</label>
407
+ # <input id="test_text_field_true" name="test[text_field]" value="true" type="radio">
408
+ # <label for="test" label="Giant chicken?">Yes</label>
409
+ # <input checked="checked" id="test_text_field_false" name="test[text_field]" value="false" type="radio">
410
+ # <label for="test" label="Giant chicken?">No</label>
411
+ def zurb_radio_button_group(method, tag_values, default = nil, options = { label: {}, field: {} })
412
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
413
+ errors = get_field_errors(method)
414
+ add_error_class_to(options) if errors.any?
415
+
416
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
417
+ options[:label])
418
+
419
+ # Iterate through radio buttons hash and check to see what value should be set as the default
420
+ # adding each radio button to @field
421
+ tag_values.each do |tag_name, tag_value|
422
+ options[:field][:checked] = true if tag_value == default
423
+ field << @template.radio_button(@object_name, method, tag_value, options[:field]) +
424
+ @template.label_tag(@object_name, tag_name.to_s.capitalize, options[:label])
425
+ end
426
+
427
+ errors.any? ? add_error_message(field, errors) : field
428
+ end
429
+
430
+ # Returns a range_field with a label wrapped around it and an error label if an error in validation. The
431
+ # options hash can be used to further customise the label and field, but specific options must be
432
+ # provided in a hash for that element.
433
+ #
434
+ # f.zurb_range_field :range_field
435
+ # # => <label for="test">Range field<input id="test_range_field" name="test[range_field]" type="range"></label>
436
+ def zurb_range_field(method, options = { label: {}, field: {} })
437
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
438
+ errors = get_field_errors(method)
439
+ add_error_class_to(options) if errors.any?
440
+
441
+ field = @template.label_tag(@object_name,
442
+ "#{options[:label][:label] || method.to_s.humanize}
443
+ #{@template.range_field(@object_name, method, options[:field])}".html_safe,
444
+ options[:label]
445
+ )
446
+
447
+ errors.any? ? add_error_message(field, errors) : field
448
+ end
449
+
450
+ # Returns a search_field with a label wrapped around it and an error label if an error in validation. The
451
+ # options hash can be used to further customise the label and field, but specific options must be
452
+ # provided in a hash for that element.
453
+ #
454
+ # f.zurb_search_field :search_field
455
+ # # => <label for="test">Search field<input id="test_search_field" name="test[search_field]" type="search"></label>
456
+ def zurb_search_field(method, options = { label: {}, field: {} })
457
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
458
+ errors = get_field_errors(method)
459
+ add_error_class_to(options) if errors.any?
460
+
461
+ field = @template.label_tag(@object_name,
462
+ "#{options[:label][:label] || method.to_s.humanize}
463
+ #{@template.search_field(@object_name, method, options[:field])}".html_safe,
464
+ options[:label]
465
+ )
466
+
467
+ errors.any? ? add_error_message(field, errors) : field
468
+ end
469
+
470
+ # Works in exactly the same way as the standard select method
471
+ #
472
+ # f.zurb_select :id, Continent.all.collect { |c| [c.name, c.id] }, {include_blank: true, label: {label:"Test"}}
473
+ # # => <label for="continent" label="Test">Test</label>
474
+ # <select id="continent_id" name="continent[id]">
475
+ # <option value=""></option>
476
+ # <option value="1">Europe</option>
477
+ # <option value="2">Asia</option></select>
478
+ def zurb_select(method, choices = nil, options = { label: {}, field: {} }, html_options = {}, &block)
479
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
480
+ errors = get_field_errors(method)
481
+ add_error_class_to(options) if errors.any?
482
+
483
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
484
+ options[:label])
485
+ field << @template.select(@object_name, method, choices, options, html_options, &block)
486
+
487
+ errors.any? ? add_error_message(field, errors) : field
488
+ end
489
+
490
+ # Returns a telephone_field with a label wrapped around it and an error label if an error in validation. The
491
+ # options hash can be used to further customise the label and field, but specific options must be
492
+ # provided in a hash for that element.
493
+ #
494
+ # f.zurb_telephone_field :telephone_field
495
+ # # => <label for="test">Telephone field<input id="test_telephone_field" name="test[telephone_field]" type="telephone"></label>
496
+ def zurb_telephone_field(method, options = { label: {}, field: {} })
497
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
498
+ errors = get_field_errors(method)
499
+ add_error_class_to(options) if errors.any?
500
+
501
+ field = @template.label_tag(@object_name,
502
+ "#{options[:label][:label] || method.to_s.humanize}
503
+ #{@template.telephone_field(@object_name, method, options[:field])}".html_safe,
504
+ options[:label]
505
+ )
506
+
507
+ errors.any? ? add_error_message(field, errors) : field
508
+ end
509
+
510
+ # Returns a text_area with a label wrapped around it and an error label if an error in validation. The
511
+ # options hash can be used to further customise the label and field, but specific options must be
512
+ # provided in a hash for that element.
513
+ #
514
+ # f.zurb_text_area :text_area, field: { rows: 5 }
515
+ # # => <label for="test">Text area
516
+ # <textarea id="test_text_area" name="test[text_area]" rows="5"></textarea></label>
517
+ def zurb_text_area(method, options = { label: {}, field: {} })
518
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
519
+ errors = get_field_errors(method)
520
+ add_error_class_to(options) if errors.any?
521
+
522
+ field = @template.label_tag(@object_name,
523
+ "#{options[:label][:label] || method.to_s.humanize}
524
+ #{@template.text_area(@object_name, method, options[:field])}".html_safe,
525
+ options[:label]
526
+ )
527
+
528
+ errors.any? ? add_error_message(field, errors) : field
529
+ end
530
+
531
+ # Returns a text_field with a label wrapped around it and an error label if an error in validation. The
532
+ # options hash can be used to further customise the label and field, but specific options must be
533
+ # provided in a hash for that element.
534
+ #
535
+ # f.zurb_text_field :text_field
536
+ # # => <label for="test">Text field<input id="test_text_field" name="test[text_field]" type="text" /></label>
537
+ #
538
+ # f.zurb_text_field :text_field, label: { label: "Custom label text", class: "some-class" }
539
+ # # => <label class="some-class" for="test" label="Custom label text">Custom label text
540
+ # <input id="test_text_field" name="test[text_field]" type="text"></label>
541
+ #
542
+ # f.zurb_text_field :text_field, field: { placeholder: "Text here" }
543
+ # # => <label for="test">Text field
544
+ # <input id="test_text_field" name="test[text_field]" placeholder="Text here" type="text"></label>
545
+ #
546
+ # f.zurb_text_field :text_field, label: { class: "some-class" }, field: { placeholder: "Text here" }
547
+ # # => <label class="some-class" for="test">Text field
548
+ # <input id="test_text_field" name="test[text_field]" placeholder="Text here" type="text"></label>
549
+ def zurb_text_field(method, options = { label: {}, field: {} })
550
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
551
+ errors = get_field_errors(method)
552
+ add_error_class_to(options) if errors.any?
553
+
554
+ field = @template.label_tag(@object_name,
555
+ "#{options[:label][:label] || method.to_s.humanize}
556
+ #{@template.text_field(@object_name, method, options[:field])}".html_safe,
557
+ options[:label]
558
+ )
559
+
560
+ errors.any? ? add_error_message(field, errors) : field
561
+ end
562
+
563
+ # Returns a time_field with a label wrapped around it and an error label if an error in validation. The
564
+ # options hash can be used to further customise the label and field, but specific options must be
565
+ # provided in a hash for that element.
566
+ #
567
+ # f.zurb_time_field :time_field
568
+ # # => <label for="test">Time field<input id="test_time_field" name="test[time_field]" type="time"></label>
569
+ def zurb_time_field(method, options = { label: {}, field: {} })
570
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
571
+ errors = get_field_errors(method)
572
+ add_error_class_to(options) if errors.any?
573
+
574
+ field = @template.label_tag(@object_name,
575
+ "#{options[:label][:label] || method.to_s.humanize}
576
+ #{@template.time_field(@object_name, method, options[:field])}".html_safe,
577
+ options[:label]
578
+ )
579
+
580
+ errors.any? ? add_error_message(field, errors) : field
581
+ end
582
+
583
+ # Works in exactly the same way as the standard time_zone_select method
584
+ def zurb_time_zone_select(method, priority_zones = nil, options = { label: {}, field: {} }, html_options = {})
585
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
586
+ errors = get_field_errors(method)
587
+ add_error_class_to(options) if errors.any?
588
+
589
+ field = @template.label_tag(@object_name, "#{options[:label][:label] || method.to_s.humanize}",
590
+ options[:label])
591
+ field << @template.time_zone_select(@object_name, method, priority_zones, options[:field], html_options)
592
+
593
+ errors.any? ? add_error_message(field, errors) : field
594
+ end
595
+
596
+ # Returns a url_field with a label wrapped around it and an error label if an error in validation. The
597
+ # options hash can be used to further customise the label and field, but specific options must be
598
+ # provided in a hash for that element.
599
+ #
600
+ # f.zurb_url_field :url_field
601
+ # # => <label for="test">Url field<input id="test_url_field" name="test[url_field]" type="url"></label>
602
+ def zurb_url_field(method, options = { label: {}, field: {} })
603
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
604
+ errors = get_field_errors(method)
605
+ add_error_class_to(options) if errors.any?
606
+
607
+ field = @template.label_tag(@object_name,
608
+ "#{options[:label][:label] || method.to_s.humanize}
609
+ #{@template.url_field(@object_name, method, options[:field])}".html_safe,
610
+ options[:label]
611
+ )
612
+
613
+ errors.any? ? add_error_message(field, errors) : field
614
+ end
615
+
616
+ # Returns a week_field with a label wrapped around it and an error label if an error in validation. The
617
+ # options hash can be used to further customise the label and field, but specific options must be
618
+ # provided in a hash for that element.
619
+ #
620
+ # f.zurb_week_field :week_field
621
+ # # => <label for="test">Week field<input id="test_week_field" name="test[week_field]" type="week"></label>
622
+ def zurb_week_field(method, options = { label: {}, field: {} })
623
+ set_options(options) # If only :field set throws error when accessing :label, and vice versa.
624
+ errors = get_field_errors(method)
625
+ add_error_class_to(options) if errors.any?
626
+
627
+ field = @template.label_tag(@object_name,
628
+ "#{options[:label][:label] || method.to_s.humanize}
629
+ #{@template.week_field(@object_name, method, options[:field])}".html_safe,
630
+ options[:label]
631
+ )
632
+
633
+ errors.any? ? add_error_message(field, errors) : field
634
+ end
635
+
636
+ private
637
+
638
+ # Ensure the options hash contains a hash for :field and :label, otherwise we get an error
639
+ def set_options(options)
640
+ options[:label] ||= {}
641
+ options[:field] ||= {}
642
+ end
643
+
644
+ # Return any errors for the field we are working with
645
+ def get_field_errors(method)
646
+ object.errors[method]
647
+ end
648
+
649
+ # Return a class attribute containing the css error class for the label
650
+ # Could also put in option to return an error class to the field tag too.
651
+ def add_error_class_to(options)
652
+ options[:label][:class].blank? ? options[:label][:class] = ERROR_CSS_CLASS : options[:label][:class] += ERROR_CSS_CLASS
653
+ end
654
+
655
+ # Return an error notice concatenated to the end of the field and label
656
+ def add_error_message(field, errors)
657
+ field += @template.content_tag(:small, errors.join(', ').humanize, class: ERROR_CSS_CLASS)
658
+ end
659
+
660
+ end
661
+ end
662
+ end
663
+ end
@@ -0,0 +1,7 @@
1
+ module Foundation
2
+ module FormBuilder
3
+ module Rails
4
+ VERSION = '0.2.2'
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foundation-formbuilder-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Ashley Bye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: foundation-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.3'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 5.3.0.1
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '5.3'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 5.3.0.1
47
+ description: FormBuilder for the ZURB Foundation 5 CSS Framework
48
+ email:
49
+ - ashley.bye85@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - README.md
55
+ - lib/foundation-formbuilder-rails.rb
56
+ - lib/foundation/form_builder.rb
57
+ - lib/foundation/form_builder/rails.rb
58
+ - lib/foundation/form_builder/rails/engine.rb
59
+ - lib/foundation/form_builder/rails/form_builder.rb
60
+ - lib/foundation/form_builder/rails/version.rb
61
+ homepage: https://github.com/ashleybye/foundation-formbuilder-rails
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: FormBuilder for the ZURB Foundation 5 CSS Framework
85
+ test_files: []