bs_form_builder 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22d8343821df9a4c861d5591267fedb460c43435
4
- data.tar.gz: 9bc5b0e7811cc3b4465d80e0ec980801490198e0
3
+ metadata.gz: cc56817ddd7ab47fbcabcca6b36c219890895a9d
4
+ data.tar.gz: 441875d1c46389bf24584d8a38070d1542dab08a
5
5
  SHA512:
6
- metadata.gz: d0d4305e91d3da0cccfa55922f2ba970f143537a9ca0143b665ec9bf4403f8639a0f758d0c243679e2af5a44e3ea3f501cf60bdcd1b1f5457b77d77510530d1a
7
- data.tar.gz: 801f8eaa3f398dd32fd282df61c09b08c740b2647c52fbe874f05897ccb9a7710fedf55e81edae03172004ab78714bd8f270b71262579ac7505de720c5811ce8
6
+ metadata.gz: a3af01183a3a2cc3bbaa6626e716668e9341b5e8b3e059c76019630d73ab0ee86a1af79cb1ae02c35aa803004952ac0f85846affb047d006ddfdda49fbd00ea9
7
+ data.tar.gz: 8ea82e7c6ccbaf8b486a6faa9902ed5dca8c21aa963722b9314b714adb9dd430ff807f2b040ed6946eb18714637f1ebb014aa1a6a8d8a4cebc7e08d44e28beb4
@@ -4,14 +4,16 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
4
4
  def email_field(name, opts = {})
5
5
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
6
6
  super(name, opts.reverse_merge(:class => 'form-control',
7
- :placeholder => help(name)))
7
+ :placeholder => help(name)).
8
+ reverse_merge(validation_attributes(name)))
8
9
  end
9
10
  end
10
11
 
11
12
  def text_field(name, opts = {})
12
13
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
13
14
  super(name, opts.reverse_merge(:class => 'form-control',
14
- :placeholder => help(name)))
15
+ :placeholder => help(name)).
16
+ reverse_merge(validation_attributes(name)))
15
17
  end
16
18
  end
17
19
 
@@ -19,7 +21,8 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
19
21
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
20
22
  @template.content_tag(:div,
21
23
  super(name, opts.reverse_merge(:class => 'form-control',
22
- :placeholder => help(name))) +
24
+ :placeholder => help(name)).
25
+ reverse_merge(validation_attributes(name))) +
23
26
  @template.content_tag(:span,
24
27
  @template.content_tag(:span, '', :class => 'glyphicon glyphicon-search'),
25
28
  :class => 'input-group-addon'),
@@ -30,21 +33,24 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
30
33
  def password_field(name, opts = {})
31
34
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
32
35
  super(name, opts.reverse_merge(:class => 'form-control',
33
- :placeholder => help(name)))
36
+ :placeholder => help(name)).
37
+ reverse_merge(validation_attributes(name)))
34
38
  end
35
39
  end
36
40
 
37
41
  def date_field(name, opts = {})
38
42
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
39
43
  super(name, opts.reverse_merge(:class => 'form-control',
40
- :placeholder => help(name)))
44
+ :placeholder => help(name)).
45
+ reverse_merge(validation_attributes(name)))
41
46
  end
42
47
  end
43
48
 
44
49
  def number_field(name, opts = {})
45
50
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
46
51
  super(name, opts.reverse_merge(:class => 'form-control',
47
- :placeholder => help(name)))
52
+ :placeholder => help(name)).
53
+ reverse_merge(validation_attributes(name)))
48
54
  end
49
55
  end
50
56
 
@@ -63,7 +69,7 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
63
69
  checked_value,
64
70
  unchecked_value) +
65
71
  label_description(name).html_safe),
66
- :class => 'checkbox')
72
+ :class => 'checkbox', :data => validation_attributes(name))
67
73
  end
68
74
 
69
75
  # uses bootstrap option to stretch the buttons to the full enclosing width
@@ -100,7 +106,8 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
100
106
  @template.content_tag(:div,
101
107
  buttons,
102
108
  :class => 'btn-group',
103
- :data => { :toggle => 'buttons' })
109
+ :data => { :toggle => 'buttons' }.
110
+ merge(validation_attributes(name)))
104
111
  end
105
112
  end
106
113
 
@@ -114,14 +121,17 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
114
121
 
115
122
  def select(name, choices, options = {}, html_options = {})
116
123
  form_group(name, options.slice(:label_options, :group_options, :tip_options)) do
117
- super(name, choices, options, html_options.reverse_merge(:class => 'form-control'))
124
+ super(name, choices, options,
125
+ html_options.reverse_merge(:class => 'form-control').
126
+ reverse_merge(validation_attributes(name)))
118
127
  end
119
128
  end
120
129
 
121
130
  def text_area(name, opts = {})
122
131
  form_group(name, opts.slice(:label_options, :group_options, :tip_options)) do
123
132
  super(name, opts.reverse_merge(:class => 'form-control',
124
- :placeholder => help(name)))
133
+ :placeholder => help(name)).
134
+ reverse_merge(validation_attributes(name)))
125
135
  end
126
136
  end
127
137
 
@@ -207,4 +217,34 @@ class BootstrapFormBuilder::HorizontalFormBuilder < ActionView::Helpers::FormBui
207
217
  :class => 'button-group'),
208
218
  :class => 'form-group')
209
219
  end
220
+
221
+ def validation_attributes(name)
222
+ return {} unless options[:validations]
223
+ return {} unless object.respond_to?(:_validators)
224
+
225
+ validation_attribute_map = {
226
+ ActiveModel::Validations::PresenceValidator => proc { { :required => true } },
227
+ ActiveModel::Validations::InclusionValidator => proc {|validator|
228
+ # Inclusion is weird, in Rails if you have a radio button
229
+ # where you want one of the options selected, and they map
230
+ # to a boolean then using :presence doesn't work, because
231
+ # the presence check on false fails. Instead you have to use
232
+ # inclusion, so we can handle this here and map it to required
233
+ # validation attribute, this is kinda getting a bit hacky though,
234
+ # and may come back to bite us in the foot, so we limit the use
235
+ # to only those cases where true and false are the only options.
236
+ if validator.options[:in] == [true,false]
237
+ { :required => true }
238
+ else
239
+ { }
240
+ end
241
+ }
242
+ }
243
+
244
+ validators = object._validators.fetch(name, [])
245
+
246
+ validators.reduce({}) do |attributes, validator|
247
+ attributes.merge(validation_attribute_map.fetch(validator.class, proc {}).call(validator))
248
+ end
249
+ end
210
250
  end
@@ -1,3 +1,3 @@
1
1
  module BootstrapFormBuilder
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bs_form_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Geoghegan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-13 00:00:00.000000000 Z
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview