bh 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +4 -0
  4. data/README.md +77 -31
  5. data/bh.gemspec +1 -0
  6. data/gemfiles/Gemfile.rails-3.x +1 -0
  7. data/gemfiles/Gemfile.rails-4.x +1 -0
  8. data/lib/bh/form_builders/form_builder.rb +41 -0
  9. data/lib/bh/helpers/alert_helper.rb +2 -5
  10. data/lib/bh/helpers/base_helper.rb +17 -0
  11. data/lib/bh/helpers/form/base_helper.rb +112 -0
  12. data/lib/bh/helpers/form/check_box_helper.rb +35 -0
  13. data/lib/bh/helpers/form/field_helper.rb +15 -0
  14. data/lib/bh/helpers/form/fields_for_helper.rb +17 -0
  15. data/lib/bh/helpers/form/fieldset_helper.rb +16 -0
  16. data/lib/bh/helpers/form/legend_helper.rb +17 -0
  17. data/lib/bh/helpers/form/radio_button_helper.rb +19 -0
  18. data/lib/bh/helpers/form/select_helper.rb +16 -0
  19. data/lib/bh/helpers/form/static_control_helper.rb +43 -0
  20. data/lib/bh/helpers/form/submit_helper.rb +23 -0
  21. data/lib/bh/helpers/form_for_helper.rb +30 -0
  22. data/lib/bh/helpers/glyphicon_helper.rb +5 -5
  23. data/lib/bh/helpers/modal_helper.rb +2 -3
  24. data/lib/bh/helpers/panel_helper.rb +5 -5
  25. data/lib/bh/helpers/panel_row_helper.rb +1 -5
  26. data/lib/bh/railtie.rb +2 -0
  27. data/lib/bh/version.rb +1 -1
  28. data/spec/helpers/form/check_box_helper_spec.rb +95 -0
  29. data/spec/helpers/form/field_helper_spec.rb +102 -0
  30. data/spec/helpers/form/fields_for_helper_spec.rb +35 -0
  31. data/spec/helpers/form/fieldset_helper_spec.rb +32 -0
  32. data/spec/helpers/form/legend_helper_spec.rb +35 -0
  33. data/spec/helpers/form/radio_button_helper_spec.rb +67 -0
  34. data/spec/helpers/form/select_helper_spec.rb +71 -0
  35. data/spec/helpers/form/static_control_helper_spec.rb +67 -0
  36. data/spec/helpers/form/submit_helper_spec.rb +30 -0
  37. data/spec/helpers/form_for_helper_spec.rb +43 -0
  38. data/spec/helpers/panel_helper_spec.rb +6 -0
  39. data/spec/spec_helper.rb +29 -1
  40. metadata +49 -2
@@ -0,0 +1,35 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module CheckBoxHelper
6
+ include BaseHelper
7
+
8
+ def check_box(method, options = {}, checked_value = '1', unchecked_value = '0')
9
+ block = -> { super method, options, checked_value, unchecked_value }
10
+ if options.delete(:inline_label) { true }
11
+ check_box_with_inline_label method, options, &block
12
+ else
13
+ check_box_with_block_label method, options, &block
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def check_box_with_block_label(method, options = {}, &block)
20
+ append_class! options, 'form-control' unless inline_form?
21
+ append_class! options, 'checkbox' if horizontal_form?
22
+ options[:label] ||= method.to_s.humanize
23
+ base_field method, :checkbox, options, &block
24
+ end
25
+
26
+ def check_box_with_inline_label(method, options = {}, &block)
27
+ options.merge! offset: true, use_label: false
28
+ options[:label] ||= method.to_s.humanize
29
+ base_field method, :checkbox, options do
30
+ label_and_field 'checkbox', method, options, &block
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module FieldHelper
6
+ include BaseHelper
7
+
8
+ def field(method, field_type, options = {}, &block)
9
+ options[:placeholder] ||= method.to_s.humanize
10
+ append_class! options, 'form-control'
11
+ base_field method, field_type, options, &block
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require 'bh/helpers/form/fieldset_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module FieldsForHelper
6
+ include BaseHelper
7
+ include FieldsetHelper # for fieldset
8
+
9
+ def fields_for(record_object = nil, fields_options = {}, &block)
10
+ fields_options[:layout] ||= @options[:layout]
11
+ fields_options[:errors] ||= @options[:errors]
12
+ title = fields_options.delete(:title) { record_object.to_s.humanize }
13
+ fieldset(title) { super record_object, fields_options, &block }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ require 'bh/helpers/panel_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module FieldsetHelper
6
+ include BaseHelper
7
+ include PanelHelper # for panel
8
+
9
+ def fieldset(title = nil, &block)
10
+ options = {tag: :fieldset, body: @template.capture(&block)}
11
+ options[:heading] = title if title.present? && !inline_form?
12
+ panel options
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module LegendHelper
6
+ include BaseHelper
7
+
8
+ def legend(value, options = {})
9
+ append_class! options, 'sr-only' if inline_form?
10
+ text = content_tag :legend, value, options
11
+ text = content_tag :div, text, class: 'col-sm-12' if horizontal_form?
12
+ text = label_and_field_container(text, :legend) unless inline_form?
13
+ text
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module RadioButtonHelper
6
+ include BaseHelper
7
+
8
+ def radio_button(method, tag_value, options = {})
9
+ options.merge! offset: true, use_label: false
10
+ options[:label] ||= tag_value
11
+ base_field method, :radio_button, options do
12
+ label_and_field 'radio', method, options do
13
+ super method, tag_value, options
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module SelectHelper
6
+ include BaseHelper
7
+
8
+ def select(method, choices = nil, options = {}, html_options = {}, &block)
9
+ append_class! html_options, 'form-control'
10
+ base_field method, :select, options do
11
+ super method, choices, options, html_options, &block
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module StaticControlHelper
6
+ include BaseHelper
7
+
8
+ def static_control(text_or_options_with_block = nil, options = {}, &block)
9
+ text, options = if block_given?
10
+ [@template.capture(&block), text_or_options_with_block]
11
+ else
12
+ [text_or_options_with_block, options]
13
+ end
14
+ static_control_string text, options || {}
15
+ end
16
+
17
+ private
18
+
19
+ def static_control_string(text, options = {})
20
+ content_tag :div, class: 'form-group' do
21
+ if inline_form?
22
+ label = content_tag :label, options[:label], class: 'sr-only' if options[:label]
23
+ field = content_tag :p, text, class: 'form-control-static'
24
+ safe_join [label, field].compact
25
+ elsif horizontal_form?
26
+ label = content_tag :label, options[:label], class: 'col-sm-3 control-label' if options[:label]
27
+ field = content_tag :p, text, class: 'form-control-static'
28
+ if options[:label]
29
+ field = content_tag :div, field, class: 'col-sm-9'
30
+ else
31
+ field = content_tag :div, field, class: 'col-sm-9 col-sm-offset-3'
32
+ end
33
+ safe_join [label, field].compact
34
+ else
35
+ label = content_tag :label, options[:label] if options[:label]
36
+ field = content_tag :p, text, class: 'form-control-static'
37
+ safe_join [label, field].compact
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ require 'bh/helpers/form/base_helper'
2
+
3
+ module Bh
4
+ module Form
5
+ module SubmitHelper
6
+ include BaseHelper
7
+
8
+ def submit(value=nil, options={})
9
+ context = options.delete(:context) || 'primary'
10
+ append_class! options, "btn btn-#{context}"
11
+ if horizontal_form?
12
+ content_tag :div, class: 'form-group' do
13
+ content_tag :div, class: 'col-sm-offset-3 col-sm-9' do
14
+ super value, options
15
+ end
16
+ end
17
+ else
18
+ super value, options
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ require 'bh/helpers/base_helper'
2
+ require 'bh/form_builders/form_builder'
3
+
4
+ module Bh
5
+ module FormForHelper
6
+ include BaseHelper
7
+ include ActionView::Helpers::FormHelper # for form_for
8
+
9
+ def form_for(record, options = {}, &block)
10
+ add_form_options!(options) if options[:layout]
11
+ super record, options, &block
12
+ end
13
+
14
+ private
15
+
16
+ def add_form_options!(options)
17
+ options[:html] ||= {}
18
+ options[:html].merge! role: 'form'
19
+ append_class! options[:html], class_for(options[:layout])
20
+ options.merge! builder: FormBuilder
21
+ end
22
+
23
+ def class_for(layout)
24
+ case layout.to_s
25
+ when 'inline' then 'form-inline'
26
+ when 'horizontal' then 'form-horizontal'
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,10 +1,10 @@
1
- require 'action_view'
1
+ require 'bh/helpers/base_helper'
2
2
 
3
3
  module Bh
4
4
  # Provides methods to include Glyphicons.
5
5
  # @see http://getbootstrap.com/components/#glyphicons
6
6
  module GlyphiconHelper
7
- include ActionView::Helpers::TagHelper # for content_tag
7
+ include BaseHelper
8
8
 
9
9
  # Returns an HTML block tag that follows the Bootstrap documentation
10
10
  # on how to display *glyphicons*.
@@ -16,9 +16,9 @@ module Bh
16
16
  # @example Display the "zoom out" glyphicon
17
17
  # glyphicon 'zoom-out'
18
18
  def glyphicon(name = nil, options = {})
19
- glyphicon_class = "glyphicon-#{name.to_s.gsub '_', '-'}" if name
20
- klass = ['glyphicon', glyphicon_class].compact.join ' '
21
- content_tag :span, nil, options.merge(class: klass)
19
+ append_class! options, 'glyphicon'
20
+ append_class! options, "glyphicon-#{name.to_s.gsub '_', '-'}" if name
21
+ content_tag :span, nil, options
22
22
  end
23
23
  end
24
24
  end
@@ -1,11 +1,10 @@
1
- require 'action_view'
1
+ require 'bh/helpers/base_helper'
2
2
 
3
3
  module Bh
4
4
  # Provides methods to include modals.
5
5
  # @see http://getbootstrap.com/javascript/#modals
6
6
  module ModalHelper
7
- include ActionView::Helpers, ActionView::Context
8
-
7
+ include BaseHelper
9
8
 
10
9
  # Returns an HTML block tag that follows the Bootstrap documentation
11
10
  # on how to display *modals*.
@@ -1,12 +1,10 @@
1
- require 'action_view'
1
+ require 'bh/helpers/base_helper'
2
2
 
3
3
  module Bh
4
4
  # Provides methods to include panels.
5
5
  # @see http://getbootstrap.com/components/#panels
6
6
  module PanelHelper
7
- include ActionView::Helpers::TagHelper # for content_tag
8
- include ActionView::Context # for capture
9
- include ActionView::Helpers::OutputSafetyHelper # for safe_join
7
+ include BaseHelper
10
8
 
11
9
  # Returns an HTML block tag that follows the Bootstrap documentation
12
10
  # on how to display *panels*.
@@ -36,6 +34,7 @@ module Bh
36
34
  # @option options [#to_s] :title if present, the panel will include a
37
35
  # heading with the provided text wrapped in a 'panel-title' block, for
38
36
  # proper title styling and link coloring.
37
+ # @option options [#to_s] :tag (:div) the HTML tag to wrap the panel in.
39
38
  # @see http://getbootstrap.com/components/#panels-heading
40
39
  def panel(content_or_options_with_block = nil, options = nil, &block)
41
40
  if block_given?
@@ -52,7 +51,8 @@ module Bh
52
51
  def panel_string(content = nil, options = {})
53
52
  content = prepend_optional_body_to content, options
54
53
  content = prepend_optional_heading_to content, options
55
- content_tag :div, content, class: panel_class(options[:context])
54
+ tag = options.fetch :tag, :div
55
+ content_tag tag, content, class: panel_class(options[:context])
56
56
  end
57
57
 
58
58
  def panel_class(context = nil)
@@ -1,4 +1,3 @@
1
- require 'action_view'
2
1
  require 'bh/helpers/panel_helper'
3
2
 
4
3
  module Bh
@@ -6,10 +5,7 @@ module Bh
6
5
  # @see http://getbootstrap.com/css/#grid
7
6
  # @see http://getbootstrap.com/components/#panels
8
7
  module PanelRowHelper
9
- include ActionView::Helpers::TagHelper # for content_tag
10
- include ActionView::Context # for capture
11
- include ActionView::Helpers::OutputSafetyHelper # for safe_join
12
- include Bh::PanelHelper # for panel
8
+ include PanelHelper # for panel
13
9
 
14
10
  # Returns an HTML block tag that follows the Bootstrap documentation
15
11
  # on how to display a *row*, passing column options to each panel in
@@ -1,5 +1,6 @@
1
1
  require 'bh/helpers/alert_helper'
2
2
  require 'bh/helpers/cdn_helper'
3
+ require 'bh/helpers/form_for_helper'
3
4
  require 'bh/helpers/glyphicon_helper'
4
5
  require 'bh/helpers/modal_helper'
5
6
  require 'bh/helpers/panel_helper'
@@ -11,6 +12,7 @@ module Bh
11
12
  initializer 'bh.add_helpers' do
12
13
  ActionView::Base.send :include, AlertHelper
13
14
  ActionView::Base.send :include, CdnHelper
15
+ ActionView::Base.send :include, FormForHelper
14
16
  ActionView::Base.send :include, GlyphiconHelper
15
17
  ActionView::Base.send :include, ModalHelper
16
18
  ActionView::Base.send :include, PanelHelper
@@ -1,3 +1,3 @@
1
1
  module Bh
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+ require 'bh/helpers/form_for_helper'
3
+ include Bh::FormForHelper
4
+
5
+ describe 'check_box' do
6
+ attr_accessor :output_buffer
7
+ let(:protect_against_forgery?) { false }
8
+ let(:form) { form_for user, layout: layout, errors: errors, url: '/', &block }
9
+ let(:user) { User.new }
10
+ let(:errors) { {} }
11
+ let(:block) { Proc.new {|f| f.check_box :name, options} }
12
+ let(:options) { {} }
13
+
14
+ context 'given any layout' do
15
+ let(:layout) { :whatever }
16
+
17
+ specify 'not given a label option, automatically generates one' do
18
+ expect(form).to include 'Name</label>'
19
+ end
20
+
21
+ context 'given a label option, uses the provided one' do
22
+ let(:options) { {label: 'Given name'} }
23
+ it { expect(form).to include 'Given name</label>' }
24
+ end
25
+
26
+ specify 'not given an error, does not apply has-error to the form group' do
27
+ expect(form).not_to include 'has-error'
28
+ end
29
+
30
+ context 'given an error' do
31
+ before { user.errors.add :name, 'cannot be nil' }
32
+
33
+ specify 'shows errors and error messages' do
34
+ expect(form).to include 'has-error'
35
+ expect(form).to include '<span class="help-block text-left">cannot be nil</span>'
36
+ end
37
+ end
38
+ end
39
+
40
+ describe 'given a basic layout' do
41
+ let(:layout) { :basic }
42
+ specify 'applies checkbox to the container, and an inline label' do
43
+ expect(form).to match %r{<div class="checkbox"><label><input.+? /> Name</label></div>}
44
+ end
45
+
46
+ context 'given the inline_label: false option' do
47
+ let(:options) { {inline_label: false} }
48
+
49
+ specify 'aligns the label on top of the check-box' do
50
+ expect(form).to match %r{<label for="user_name">Name</label><input}
51
+ expect(form).to include 'input class="form-control"'
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'given a horizontal layout' do
57
+ let(:layout) { :horizontal }
58
+ specify 'applies form-group to the outer container, .col-sm-offset-3.col-sm-9 to the field container, checkbox to the container, and an inline label' do
59
+ expect(form).to match %r{<div class="form-group"><div class="col-sm-offset-3 col-sm-9"><div class="checkbox"><label><input.+? /> Name</label></div></div></div>}
60
+ end
61
+
62
+ context 'given the inline_label: false option' do
63
+ let(:options) { {inline_label: false} }
64
+
65
+ specify 'aligns the label on top of the check-box' do
66
+ expect(form).to match %r{<label class="col-sm-3 control-label" for="user_name">Name</label><div class="col-sm-9"><input}
67
+ expect(form).to include 'input class="form-control checkbox"'
68
+ end
69
+ end
70
+ end
71
+
72
+ describe 'given an inline layout' do
73
+ let(:layout) { :inline }
74
+ specify 'applies checkbox to the container, and an inline label' do
75
+ expect(form).to match %r{<div class="checkbox"><label><input.+? /> Name</label></div>}
76
+ end
77
+
78
+ context 'given an error' do
79
+ before { user.errors.add :name, 'cannot be nil' }
80
+
81
+ specify 'applies sr-only to the error message' do
82
+ expect(form).to include '<span class="help-block text-left sr-only">cannot be nil</span>'
83
+ end
84
+ end
85
+
86
+ context 'given the inline_label: false option' do
87
+ let(:options) { {inline_label: false} }
88
+
89
+ specify 'aligns the label on top of the check-box' do
90
+ expect(form).to match %r{<label class="sr-only" for="user_name">Name</label><input}
91
+ expect(form).not_to include 'form-control'
92
+ end
93
+ end
94
+ end
95
+ end