bh 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +4 -0
- data/README.md +77 -31
- data/bh.gemspec +1 -0
- data/gemfiles/Gemfile.rails-3.x +1 -0
- data/gemfiles/Gemfile.rails-4.x +1 -0
- data/lib/bh/form_builders/form_builder.rb +41 -0
- data/lib/bh/helpers/alert_helper.rb +2 -5
- data/lib/bh/helpers/base_helper.rb +17 -0
- data/lib/bh/helpers/form/base_helper.rb +112 -0
- data/lib/bh/helpers/form/check_box_helper.rb +35 -0
- data/lib/bh/helpers/form/field_helper.rb +15 -0
- data/lib/bh/helpers/form/fields_for_helper.rb +17 -0
- data/lib/bh/helpers/form/fieldset_helper.rb +16 -0
- data/lib/bh/helpers/form/legend_helper.rb +17 -0
- data/lib/bh/helpers/form/radio_button_helper.rb +19 -0
- data/lib/bh/helpers/form/select_helper.rb +16 -0
- data/lib/bh/helpers/form/static_control_helper.rb +43 -0
- data/lib/bh/helpers/form/submit_helper.rb +23 -0
- data/lib/bh/helpers/form_for_helper.rb +30 -0
- data/lib/bh/helpers/glyphicon_helper.rb +5 -5
- data/lib/bh/helpers/modal_helper.rb +2 -3
- data/lib/bh/helpers/panel_helper.rb +5 -5
- data/lib/bh/helpers/panel_row_helper.rb +1 -5
- data/lib/bh/railtie.rb +2 -0
- data/lib/bh/version.rb +1 -1
- data/spec/helpers/form/check_box_helper_spec.rb +95 -0
- data/spec/helpers/form/field_helper_spec.rb +102 -0
- data/spec/helpers/form/fields_for_helper_spec.rb +35 -0
- data/spec/helpers/form/fieldset_helper_spec.rb +32 -0
- data/spec/helpers/form/legend_helper_spec.rb +35 -0
- data/spec/helpers/form/radio_button_helper_spec.rb +67 -0
- data/spec/helpers/form/select_helper_spec.rb +71 -0
- data/spec/helpers/form/static_control_helper_spec.rb +67 -0
- data/spec/helpers/form/submit_helper_spec.rb +30 -0
- data/spec/helpers/form_for_helper_spec.rb +43 -0
- data/spec/helpers/panel_helper_spec.rb +6 -0
- data/spec/spec_helper.rb +29 -1
- metadata +49 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad17bacb7ffb22fb404857bfce7a63c26b7e182d
|
4
|
+
data.tar.gz: 844812324781c1cd1af6b0fc2baf7d4f7f2dd354
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1eaa5367a5d7ba4796cb7711a3940f22e1ef1001ce6b430ac1c10cea804edd5c6a19c63a4d0ef4b69cd2f86b9b26ac0460f4dab64ec681199dc01495caa9d8b1
|
7
|
+
data.tar.gz: f4ddda3cd0aa3e7ccd51b4d04dbd1b9ec7450615024927ad7d5af1e0d5ad3c2db3a572ec5243b02a2c048537591224ab748b89afe6bbc1cbdc91409e8961ffab
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,10 @@ For more information about changelogs, check
|
|
6
6
|
[Keep a Changelog](http://keepachangelog.com) and
|
7
7
|
[Vandamme](http://tech-angels.github.io/vandamme).
|
8
8
|
|
9
|
+
## 0.0.5 - 2014-08-22
|
10
|
+
|
11
|
+
* [FEATURE] Add `form_for` and form helpers for every type of field
|
12
|
+
|
9
13
|
## 0.0.4 - 2014-08-17
|
10
14
|
|
11
15
|
* [FEATURE] Add `modal`
|
data/README.md
CHANGED
@@ -33,7 +33,7 @@ repetitive, and prone to errors.
|
|
33
33
|
Bh offers a solution to this problem by means of a set of helper methods.
|
34
34
|
The example above can be rewritten with just one line of code:
|
35
35
|
|
36
|
-
```
|
36
|
+
```rhtml
|
37
37
|
<%= alert_box 'You accepted the Terms of service.', dismissible: true %>
|
38
38
|
```
|
39
39
|
|
@@ -46,14 +46,14 @@ How to install
|
|
46
46
|
|
47
47
|
Bh is meant to be included in Rails apps by adding this line to the Gemfile:
|
48
48
|
|
49
|
-
gem 'bh', '~> 0.0.
|
49
|
+
gem 'bh', '~> 0.0.5'
|
50
50
|
|
51
51
|
Since the gem follows [Semantic Versioning](http://semver.org),
|
52
52
|
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
|
53
53
|
guarantees that your project won’t occur in any error when you `bundle update`
|
54
54
|
and a new version of Bh is released.
|
55
55
|
|
56
|
-
Adding
|
56
|
+
Adding `bh` to your Gemfile is all you need!
|
57
57
|
From now on, you will be able to use any of the following Bh helpers in your Rails views.
|
58
58
|
|
59
59
|
AlertHelper
|
@@ -66,11 +66,11 @@ Here are some examples.
|
|
66
66
|
Basic alerts
|
67
67
|
------------
|
68
68
|
|
69
|
-
```
|
69
|
+
```rhtml
|
70
70
|
<%= alert_box 'You accepted the Terms of service.' %>
|
71
71
|
```
|
72
72
|
|
73
|
-
will generate the HTML to render an
|
73
|
+
will generate the HTML to render an *info* alert:
|
74
74
|
|
75
75
|
```html
|
76
76
|
<div class="alert alert-info" role="alert">You accepted the Terms of service.</div>
|
@@ -81,11 +81,11 @@ will generate the HTML to render an "info" alert:
|
|
81
81
|
Dismissible alerts
|
82
82
|
------------------
|
83
83
|
|
84
|
-
```
|
84
|
+
```rhtml
|
85
85
|
<%= alert_box 'You accepted the Terms of service.', dismissible: true %>
|
86
86
|
```
|
87
87
|
|
88
|
-
will generate the HTML to render an alert with an
|
88
|
+
will generate the HTML to render an alert with an `×` to close it:
|
89
89
|
|
90
90
|
```html
|
91
91
|
<div class="alert alert-info" role="alert">
|
@@ -101,11 +101,11 @@ will generate the HTML to render an alert with an '×' to close it:
|
|
101
101
|
Contextual alerts
|
102
102
|
-----------------
|
103
103
|
|
104
|
-
```
|
104
|
+
```rhtml
|
105
105
|
<%= alert_box 'You accepted the Terms of service.', context: :success %>
|
106
106
|
```
|
107
107
|
|
108
|
-
will generate the HTML to render a
|
108
|
+
will generate the HTML to render a *success* alert (green background):
|
109
109
|
|
110
110
|
```html
|
111
111
|
<div class="alert alert-success" role="alert">You accepted the Terms of service.</div>
|
@@ -122,11 +122,11 @@ Since a common use of alert boxes in Rails applications is to display
|
|
122
122
|
[flash messages](http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html),
|
123
123
|
the `alert_box` helper accepts the priority of the flash message as an option.
|
124
124
|
|
125
|
-
```
|
125
|
+
```rhtml
|
126
126
|
<%= alert_box 'You accepted the Terms of service.', priority: :notice %>
|
127
127
|
```
|
128
128
|
|
129
|
-
will generate the HTML to render a dismissible
|
129
|
+
will generate the HTML to render a dismissible *success* alert (green background):
|
130
130
|
|
131
131
|
```html
|
132
132
|
<div class="alert alert-success" role="alert">
|
@@ -144,13 +144,13 @@ Available priorities are `:alert`, `:notice`.
|
|
144
144
|
Complex alerts
|
145
145
|
--------------
|
146
146
|
|
147
|
-
```
|
147
|
+
```rhtml
|
148
148
|
<%= alert_box context: :success, dismissible: true do %>
|
149
149
|
<strong>Thanks!</strong> You accepted the <%= link_to 'Terms of service', '/terms' %>.
|
150
150
|
<% end %>
|
151
151
|
```
|
152
152
|
|
153
|
-
will generate the HTML to render a dismissible
|
153
|
+
will generate the HTML to render a dismissible *success* alert that includes
|
154
154
|
highlighted text and appropriately styled links:
|
155
155
|
|
156
156
|
```html
|
@@ -177,7 +177,7 @@ Here are some examples.
|
|
177
177
|
Load the latest Bootstrap assets
|
178
178
|
--------------------------------
|
179
179
|
|
180
|
-
```
|
180
|
+
```rhtml
|
181
181
|
<%= stylesheet_link_tag bootstrap_css, bootstrap_theme_css, :application %>
|
182
182
|
<%= javascript_include_tag bootstrap_js, :application %>
|
183
183
|
```
|
@@ -197,7 +197,7 @@ Theme CSS and Bootstrap JS from MaxCDN, before your application assets:
|
|
197
197
|
Load a specific version of a Bootstrap asset
|
198
198
|
--------------------------------------------
|
199
199
|
|
200
|
-
```
|
200
|
+
```rhtml
|
201
201
|
<%= stylesheet_link_tag bootstrap_css(version: '3.1.0', minified: false, scheme: :http) %>
|
202
202
|
```
|
203
203
|
|
@@ -219,7 +219,7 @@ Here are some examples.
|
|
219
219
|
Display the "zoom in" icon
|
220
220
|
--------------------------
|
221
221
|
|
222
|
-
```
|
222
|
+
```rhtml
|
223
223
|
<%= glyphicon :ok, title: 'Approved' %>
|
224
224
|
```
|
225
225
|
|
@@ -231,6 +231,7 @@ will generate the HTML to render an "ok" icon with the "Approved" title:
|
|
231
231
|
|
232
232
|

|
233
233
|
|
234
|
+
Available glyphicons are listed in [Boostrap documentation](http://getbootstrap.com/components/#glyphicons).
|
234
235
|
|
235
236
|
PanelHelper
|
236
237
|
===========
|
@@ -243,7 +244,7 @@ Here are some examples.
|
|
243
244
|
Basic panel
|
244
245
|
-----------
|
245
246
|
|
246
|
-
```
|
247
|
+
```rhtml
|
247
248
|
<%= panel body: 'You accepted the Terms of service.' %>
|
248
249
|
```
|
249
250
|
|
@@ -259,7 +260,7 @@ will generate the HTML to render a basic panel:
|
|
259
260
|
Panel with heading
|
260
261
|
------------------
|
261
262
|
|
262
|
-
```
|
263
|
+
```rhtml
|
263
264
|
<%= panel body: 'You accepted the Terms of service.', heading: 'Congratulations' %>
|
264
265
|
```
|
265
266
|
|
@@ -277,7 +278,7 @@ will generate the HTML to render a panel with a heading:
|
|
277
278
|
Panel with title
|
278
279
|
------------------
|
279
280
|
|
280
|
-
```
|
281
|
+
```rhtml
|
281
282
|
<%= panel body: 'You accepted the Terms of service.', title: 'Congratulations' %>
|
282
283
|
```
|
283
284
|
|
@@ -297,11 +298,11 @@ will generate the HTML to render a panel with a title:
|
|
297
298
|
Contextual panel
|
298
299
|
-----------------
|
299
300
|
|
300
|
-
```
|
301
|
+
```rhtml
|
301
302
|
<%= panel body: 'You accepted the Terms of service.', title: 'Congratulations', context: :success %>
|
302
303
|
```
|
303
304
|
|
304
|
-
will generate the HTML to render a
|
305
|
+
will generate the HTML to render a *success* panel (green background):
|
305
306
|
|
306
307
|
```html
|
307
308
|
<div class="panel panel-success">
|
@@ -320,23 +321,23 @@ Available contexts are `:default` (default), `:primary`, `:success`, `:info`,
|
|
320
321
|
Complex panels
|
321
322
|
--------------
|
322
323
|
|
323
|
-
```
|
324
|
-
<%= panel do %>
|
324
|
+
```rhtml
|
325
|
+
<%= panel tag: :aside do %>
|
325
326
|
<div class='panel-body'>You accepted the Terms of service. <%= glyphicon :ok %></div>
|
326
327
|
<div class='panel-footer'><h4>Thanks</h4></div>
|
327
328
|
<% end %>
|
328
329
|
```
|
329
330
|
|
330
|
-
will generate the HTML to render
|
331
|
+
will generate the HTML to render an aside panel with HTML body and footer:
|
331
332
|
|
332
333
|
```html
|
333
|
-
<
|
334
|
+
<aside class="panel panel-default">
|
334
335
|
<div class="panel-body">
|
335
336
|
You accepted the Terms of service.
|
336
337
|
<span class="glyphicon glyphicon-ok"></span>
|
337
338
|
</div>
|
338
339
|
<div class="panel-footer"><h4>Thanks</h4></div>
|
339
|
-
</
|
340
|
+
</aside>
|
340
341
|
```
|
341
342
|
|
342
343
|

|
@@ -353,7 +354,7 @@ Here are some examples.
|
|
353
354
|
Basic row of panels
|
354
355
|
-------------------
|
355
356
|
|
356
|
-
```
|
357
|
+
```rhtml
|
357
358
|
<%= panel_row column_class: 'col-sm-4' do %>
|
358
359
|
<%= panel body: 'Panel #1' %>
|
359
360
|
<%= panel body: 'Panel #2' %>
|
@@ -382,7 +383,7 @@ will generate the HTML to render a row of three basic panels:
|
|
382
383
|
Complex row of panels
|
383
384
|
---------------------
|
384
385
|
|
385
|
-
```
|
386
|
+
```rhtml
|
386
387
|
<%= panel_row column_class: 'col-sm-4' do %>
|
387
388
|
<%= panel title: 'User', context: :info do %>
|
388
389
|
<div class='panel-body'><%= glyphicon :user %> John Smith</div>
|
@@ -428,7 +429,7 @@ Here are some examples.
|
|
428
429
|
Basic modal
|
429
430
|
-----------
|
430
431
|
|
431
|
-
```
|
432
|
+
```rhtml
|
432
433
|
<%= modal title: 'Terms of service', body: 'Do what you want!' %>
|
433
434
|
```
|
434
435
|
|
@@ -458,14 +459,14 @@ will generate the HTML to render a button that toggles a model when clicked:
|
|
458
459
|
Complex modal
|
459
460
|
-------------
|
460
461
|
|
461
|
-
```
|
462
|
+
```rhtml
|
462
463
|
<%= modal title: 'Terms of service', size: :small, button: {caption: 'Continue', size: :large, context: :info} do %>
|
463
464
|
Please accept the Terms of service.
|
464
465
|
<div class="modal-footer"><button type="button" class="btn btn-primary">Accept</button></div>
|
465
466
|
<% end %>
|
466
467
|
```
|
467
468
|
|
468
|
-
will generate the HTML to render a large,
|
469
|
+
will generate the HTML to render a large, *info* button (blue background) with
|
469
470
|
the caption "Continue" that toggles a small modal with a title and HTML content:
|
470
471
|
|
471
472
|
```html
|
@@ -490,6 +491,51 @@ the caption "Continue" that toggles a small modal with a title and HTML content:
|
|
490
491
|
|
491
492
|

|
492
493
|
|
494
|
+
FormForHelper
|
495
|
+
=============
|
496
|
+
|
497
|
+
To include [Boostrap forms](http://getbootstrap.com/css/#forms)
|
498
|
+
in your Rails views, you can use the
|
499
|
+
[form_for](http://rubydoc.info/github/Fullscreen/bh/master/Bh/FormForHelper)
|
500
|
+
helper.
|
501
|
+
|
502
|
+
By default, Bh does not override the `form_for` method provided by [ActionView](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for).
|
503
|
+
To apply Bootstrap classes and attributes, you **must** set the `:layout` option to
|
504
|
+
|
505
|
+
* `:basic`, in order to get a [Basic form](http://getbootstrap.com/css/#forms-example)
|
506
|
+
* `:horizontal`, in order to get a [Horizontal form](http://getbootstrap.com/css/#forms-horizontal)
|
507
|
+
* `:inline`, in order to get an [Inline form](http://getbootstrap.com/css/#forms-inline)
|
508
|
+
|
509
|
+
Here is how a form with a text field and a submit button looks like with each layout:
|
510
|
+
|
511
|
+
```rhtml
|
512
|
+
<%= form_for @user, layout: :basic do |f| %>
|
513
|
+
<%= f.text_field :name %>
|
514
|
+
<%= f.submit %>
|
515
|
+
<% end %>
|
516
|
+
```
|
517
|
+
|
518
|
+

|
519
|
+
|
520
|
+
|
521
|
+
```rhtml
|
522
|
+
<%= form_for @user, layout: :horizontal do |f| %>
|
523
|
+
<%= f.text_field :name %>
|
524
|
+
<%= f.submit %>
|
525
|
+
<% end %>
|
526
|
+
```
|
527
|
+
|
528
|
+

|
529
|
+
|
530
|
+
```rhtml
|
531
|
+
<%= form_for @user, layout: :inline do |f| %>
|
532
|
+
<%= f.text_field :name %>
|
533
|
+
<%= f.submit %>
|
534
|
+
<% end %>
|
535
|
+
```
|
536
|
+
|
537
|
+

|
538
|
+
|
493
539
|
|
494
540
|
How to release new versions
|
495
541
|
===========================
|
data/bh.gemspec
CHANGED
data/gemfiles/Gemfile.rails-3.x
CHANGED
data/gemfiles/Gemfile.rails-4.x
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'bh/helpers/form/check_box_helper'
|
2
|
+
require 'bh/helpers/form/field_helper'
|
3
|
+
require 'bh/helpers/form/fieldset_helper'
|
4
|
+
require 'bh/helpers/form/fields_for_helper'
|
5
|
+
require 'bh/helpers/form/legend_helper'
|
6
|
+
require 'bh/helpers/form/radio_button_helper'
|
7
|
+
require 'bh/helpers/form/select_helper'
|
8
|
+
require 'bh/helpers/form/static_control_helper'
|
9
|
+
require 'bh/helpers/form/submit_helper'
|
10
|
+
|
11
|
+
module Bh
|
12
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
13
|
+
include Form::CheckBoxHelper
|
14
|
+
include Form::FieldHelper
|
15
|
+
include Form::FieldsetHelper
|
16
|
+
include Form::FieldsForHelper
|
17
|
+
include Form::LegendHelper
|
18
|
+
include Form::RadioButtonHelper
|
19
|
+
include Form::SelectHelper
|
20
|
+
include Form::StaticControlHelper
|
21
|
+
include Form::SubmitHelper
|
22
|
+
|
23
|
+
# @note: field_helpers are returned as symbols in ActionView 4 and as
|
24
|
+
# strings in ActionView 3
|
25
|
+
def self.textual_field_helpers
|
26
|
+
non_textual_field_helpers = %w(label hidden_field range_field check_box
|
27
|
+
radio_button select submit fields_for label)
|
28
|
+
field_helpers.map(&:to_s) - non_textual_field_helpers
|
29
|
+
end
|
30
|
+
|
31
|
+
# Use the same template for all the textual field helpers such as
|
32
|
+
# email_field, password_field, etc.
|
33
|
+
# Exclude the ones that should not have additional styles.
|
34
|
+
# Do not show error icons on number_field not to cover the sliders.
|
35
|
+
textual_field_helpers.each do |field_type|
|
36
|
+
define_method field_type do |method, options = {}|
|
37
|
+
field(method, field_type, options) { super method, options }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,11 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'bh/helpers/base_helper'
|
2
2
|
|
3
3
|
module Bh
|
4
4
|
module AlertHelper
|
5
|
-
include
|
6
|
-
include ActionView::Context # for capture
|
7
|
-
include ActionView::Helpers::OutputSafetyHelper # for safe_join
|
8
|
-
|
5
|
+
include BaseHelper
|
9
6
|
# Returns an HTML block tag that follows the Bootstrap documentation
|
10
7
|
# on how to display *alert boxes*.
|
11
8
|
# Alert boxes provide contextual feedback messages for typical user
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
module Bh
|
4
|
+
module BaseHelper
|
5
|
+
include ActionView::Helpers::TagHelper # for content_tag
|
6
|
+
include ActionView::Context # for capture
|
7
|
+
include ActionView::Helpers::OutputSafetyHelper # for safe_join
|
8
|
+
include ActionView::Helpers::RenderingHelper # for render
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def append_class!(hash, new_class)
|
13
|
+
existing_class = hash[:class]
|
14
|
+
hash[:class] = [existing_class, new_class].compact.join ' '
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'bh/helpers/glyphicon_helper'
|
2
|
+
|
3
|
+
module Bh
|
4
|
+
module Form
|
5
|
+
module BaseHelper
|
6
|
+
include GlyphiconHelper # for glyphicon
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def base_field(method, field_type, options = {}, &block)
|
11
|
+
errors = (object.errors.get method if object) || {}
|
12
|
+
label = label_for method, errors, options
|
13
|
+
field = field_container(options) do
|
14
|
+
field_tags errors, field_type, &block
|
15
|
+
end
|
16
|
+
label_and_field = safe_join [label, field].compact
|
17
|
+
label_and_field_container label_and_field, field_type, errors
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def field_container(options = {}, &block)
|
22
|
+
if horizontal_form?
|
23
|
+
klass = [('col-sm-offset-3' if options[:offset]), 'col-sm-9']
|
24
|
+
content_tag :div, class: klass.compact.join(' '), &block
|
25
|
+
else
|
26
|
+
yield
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def field_tags(errors, field_type, &block)
|
31
|
+
tags = [@template.capture(&block)]
|
32
|
+
tags << error_icon_tag if errors.any? && show_error_icon?(field_type)
|
33
|
+
tags << error_help_tag(errors) if errors.any? && show_error_help?
|
34
|
+
safe_join tags
|
35
|
+
end
|
36
|
+
|
37
|
+
def label_and_field_container(label_and_field, field_type, errors = {})
|
38
|
+
klass = ['form-group']
|
39
|
+
if errors.any?
|
40
|
+
klass << 'has-error'
|
41
|
+
klass << 'has-feedback' if show_error_icon?(field_type)
|
42
|
+
end
|
43
|
+
content_tag :div, label_and_field, class: klass.join(' ')
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def show_error_icon?(field_type)
|
48
|
+
hide = [:checkbox, :number_field, :radio_button, :select, :legend].include? field_type
|
49
|
+
@options.fetch(:errors, {}).fetch(:icons, true) && !hide
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def error_icon_tag
|
54
|
+
glyphicon :remove, class: 'form-control-feedback'
|
55
|
+
end
|
56
|
+
|
57
|
+
def error_help_tag(errors)
|
58
|
+
klass = ['help-block', 'text-left']
|
59
|
+
klass << 'sr-only' if inline_form?
|
60
|
+
content_tag :span, errors.to_sentence, class: klass.join(' ')
|
61
|
+
end
|
62
|
+
|
63
|
+
def label_for(method, errors, options)
|
64
|
+
if options.delete(:use_label) { true }
|
65
|
+
args = [method, options.delete(:label), label_options(errors)]
|
66
|
+
label *args.compact
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def label_options(errors = {})
|
71
|
+
klass = []
|
72
|
+
klass << 'sr-only' if inline_form?
|
73
|
+
klass << 'col-sm-3' if horizontal_form?
|
74
|
+
klass << 'control-label' if horizontal_form?
|
75
|
+
klass << 'control-label' if basic_form? && errors.any?
|
76
|
+
{class: klass.join(' ')} if klass.any?
|
77
|
+
end
|
78
|
+
|
79
|
+
# Rails adds <div class='field_with_errors'> which messes up
|
80
|
+
# Bootstrap inline form unless the label is inserted within
|
81
|
+
# the div itself.
|
82
|
+
def label_and_field(container_class, method, options = {}, &block)
|
83
|
+
label_and_field = @template.capture(&block)
|
84
|
+
label = options.delete(:label)
|
85
|
+
if index = label_and_field =~ %r{</div>$}
|
86
|
+
label_and_field.insert index, " #{label}"
|
87
|
+
else
|
88
|
+
label_and_field.concat " #{label}"
|
89
|
+
end
|
90
|
+
content_tag :div, class: container_class do
|
91
|
+
content_tag :label, label_and_field
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def show_error_help?
|
96
|
+
@options.fetch(:errors, {}).fetch(:messages, :inline).to_s == 'inline'
|
97
|
+
end
|
98
|
+
|
99
|
+
def inline_form?
|
100
|
+
@options[:layout].to_s == 'inline'
|
101
|
+
end
|
102
|
+
|
103
|
+
def horizontal_form?
|
104
|
+
@options[:layout].to_s == 'horizontal'
|
105
|
+
end
|
106
|
+
|
107
|
+
def basic_form?
|
108
|
+
!inline_form? && !horizontal_form?
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|