bh 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad17bacb7ffb22fb404857bfce7a63c26b7e182d
4
- data.tar.gz: 844812324781c1cd1af6b0fc2baf7d4f7f2dd354
3
+ metadata.gz: 2efc7f83e6175dbda16f557b14d18d6889748c35
4
+ data.tar.gz: d8f958fb28c28731a4fe32587b00f2767f1fc441
5
5
  SHA512:
6
- metadata.gz: 1eaa5367a5d7ba4796cb7711a3940f22e1ef1001ce6b430ac1c10cea804edd5c6a19c63a4d0ef4b69cd2f86b9b26ac0460f4dab64ec681199dc01495caa9d8b1
7
- data.tar.gz: f4ddda3cd0aa3e7ccd51b4d04dbd1b9ec7450615024927ad7d5af1e0d5ad3c2db3a572ec5243b02a2c048537591224ab748b89afe6bbc1cbdc91409e8961ffab
6
+ metadata.gz: 16df29192db7253cda1592a8bdb3d0cad6272fe0eb6c5b6393f44dbd61b70fc1bba2117ee571b65d8283107fe4f9a91a546c41bb0894c950f94d95a03c9092dc
7
+ data.tar.gz: 43106b14b0e3741cb56b2165d0babe1241056e2e23189efd2ed42dead693b3283e5c6d2afc013af415c36eece5c1acb072fd7895b9ef8ada27a6a7159c3e8355
@@ -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.6 - 2014-08-22
10
+
11
+ * [FEATURE] Add `:prefix`, `:suffix` options to form field helpers
12
+
9
13
  ## 0.0.5 - 2014-08-22
10
14
 
11
15
  * [FEATURE] Add `form_for` and form helpers for every type of field
data/README.md CHANGED
@@ -46,7 +46,7 @@ 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.5'
49
+ gem 'bh', '~> 0.0.6'
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*)
@@ -11,7 +11,7 @@ module Bh
11
11
  errors = (object.errors.get method if object) || {}
12
12
  label = label_for method, errors, options
13
13
  field = field_container(options) do
14
- field_tags errors, field_type, &block
14
+ field_tags errors, field_type, options, &block
15
15
  end
16
16
  label_and_field = safe_join [label, field].compact
17
17
  label_and_field_container label_and_field, field_type, errors
@@ -27,29 +27,47 @@ module Bh
27
27
  end
28
28
  end
29
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)
30
+ def field_tags(errors, field_type, options = {}, &block)
31
+ prefix, suffix = options.delete(:prefix), options.delete(:suffix)
32
+ field = @template.capture(&block)
33
+ tags = [field_in_group(field, prefix, suffix)]
34
+ tags << error_icon_tag if show_error_icon?(field_type, errors, suffix)
33
35
  tags << error_help_tag(errors) if errors.any? && show_error_help?
34
36
  safe_join tags
35
37
  end
36
38
 
39
+ def field_in_group(field, prefix, suffix)
40
+ input_group_container(prefix || suffix) do
41
+ safe_join [input_addon(prefix), field, input_addon(suffix)].compact
42
+ end
43
+ end
44
+
45
+ def input_addon(addon)
46
+ content_tag :span, addon, class: 'input-group-addon' if addon
47
+ end
48
+
49
+ def input_group_container(has_addons, &block)
50
+ if has_addons
51
+ content_tag :div, class: 'input-group', &block
52
+ else
53
+ yield
54
+ end
55
+ end
56
+
37
57
  def label_and_field_container(label_and_field, field_type, errors = {})
38
58
  klass = ['form-group']
39
- if errors.any?
40
- klass << 'has-error'
41
- klass << 'has-feedback' if show_error_icon?(field_type)
42
- end
59
+ klass << 'has-error' if errors.any?
60
+ klass << 'has-feedback' if show_error_icon?(field_type, errors)
43
61
  content_tag :div, label_and_field, class: klass.join(' ')
44
62
  end
45
63
 
46
64
 
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
65
+ def show_error_icon?(field_type, errors, suffix = nil)
66
+ no_icon = %w(checkbox number_field radio_button select legend)
67
+ hide = no_icon.include?(field_type.to_s)
68
+ errors.any? && @options.fetch(:errors, {}).fetch(:icons, true) && !hide && suffix.nil?
50
69
  end
51
70
 
52
-
53
71
  def error_icon_tag
54
72
  glyphicon :remove, class: 'form-control-feedback'
55
73
  end
@@ -1,3 +1,3 @@
1
1
  module Bh
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -42,6 +42,20 @@ field_helpers_to_test.each do |form_field|
42
42
  it { expect(form).to include 'Given name</label>' }
43
43
  end
44
44
 
45
+ specify 'not given a prefix or suffix option, does not use an input group' do
46
+ expect(form).not_to include 'input-group'
47
+ end
48
+
49
+ context 'given a prefix option, prints the prefix before the field' do
50
+ let(:options) { {prefix: 'Mr.'} }
51
+ it { expect(form).to include '<div class="input-group"><span class="input-group-addon">Mr.</span><' }
52
+ end
53
+
54
+ context 'given a suffix option, prints the prefix after the field' do
55
+ let(:options) { {suffix: 'Jr'} }
56
+ it { expect(form).to match %r{<div class="input-group"><.+?><span class="input-group-addon">Jr</span></div>}m }
57
+ end
58
+
45
59
  specify 'not given an error, does not apply has-error to the form group' do
46
60
  expect(form).not_to include 'has-error'
47
61
  end
@@ -63,6 +77,11 @@ field_helpers_to_test.each do |form_field|
63
77
  it{ expect(form).not_to include 'has-feedback' }
64
78
  end
65
79
 
80
+ context 'given the suffix option, hides error icons (or they would overlap)' do
81
+ let(:options) { {suffix: 'Jr'} }
82
+ it{ expect(form).not_to include 'form-control-feedback' }
83
+ end
84
+
66
85
  context 'given an option to hide error messages, hides error messages' do
67
86
  let(:errors) { {messages: false} }
68
87
  it{ expect(form).not_to include 'help-block' }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo