form-bootstrap 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -28,57 +28,78 @@ Install the gem
28
28
 
29
29
  bundle
30
30
 
31
- Add the following to application.rb
32
31
 
33
- ActionView::Base.field_error_proc = proc { |input, instance| input }
32
+ Example Form
33
+ ------------
34
34
 
35
+ <%= form_bootstrap_for(@user, help: :block) do |f| %>
36
+ <fieldset>
37
+ <legend>Sign Up Today!</legend>
38
+ <%= f.alert_message "Please fix the errors below." %>
35
39
 
36
- Example (HAML)
37
- --------------
40
+ <%= f.text_field :email, autofocus: :true %>
41
+ <%= f.password_field :password, help: 'Must be at least 6 characters long' %>
42
+ <%= f.password_field :password_confirmation, label: 'Confirm Password' %>
43
+ <%= f.actions 'Sign Up' %>
44
+ </fieldset>
45
+ <% end %>
38
46
 
39
- = form_bootstrap_for @user do |f|
40
- %fieldset
41
- %legend
42
- Sign Up
43
-
44
- = f.error_message "Please fix the errors below."
45
-
46
- = f.text_field :email, label: 'Email Address'
47
- = f.password_field :password, label: 'Password', help: 'Minimum 6 characters'
48
- = f.actions 'Sign Up'
47
+ ![Example Form](https://github.com/potenza/form-bootstrap/raw/master/assets/example_form.png)
49
48
 
50
49
 
51
- Example Output
52
- --------------
50
+ Example HTML
51
+ ------------
53
52
 
54
53
  <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
55
54
  ...
56
55
  <fieldset>
57
- <legend>
58
- Sign Up
59
- </legend>
60
-
61
- <div class="clearfix ">
56
+ <legend>Sign Up Today!</legend>
57
+
58
+ <div class="clearfix">
62
59
  <label for="user_email">Email</label>
63
60
  <div class="input">
64
61
  <input id="user_email" name="user[email]" size="30" type="text" />
65
62
  </div>
66
63
  </div>
67
-
68
- <div class="clearfix ">
64
+ <div class="clearfix">
69
65
  <label for="user_password">Password</label>
70
66
  <div class="input">
71
67
  <input id="user_password" name="user[password]" size="30" type="password" />
72
- <span class="help-inline">Minimum 6 characters</span>
68
+ <span class="help-block">Must be at least 6 characters long</span>
69
+ </div>
70
+ </div>
71
+ <div class="clearfix">
72
+ <label for="user_password_confirmation">Confirm Password</label>
73
+ <div class="input">
74
+ <input id="user_password_confirmation" name="user[password_confirmation]" size="30" type="password" />
73
75
  </div>
74
76
  </div>
75
-
76
77
  <div class="actions">
77
78
  <input class="btn primary" name="commit" type="submit" value="Sign Up" />
78
79
  </div>
79
80
  </fieldset>
80
81
  </form>
81
-
82
+
83
+
84
+ Errors
85
+ ------
86
+
87
+ When a validation error is triggered, the field will be outlined and the
88
+ error will be displayed next to the field. Rails normally wraps fields
89
+ in a div (field_with_errors), but this behavior is suppressed when `form_bootstrap_for` is called.
90
+
91
+ ![Example form with errors](https://github.com/potenza/form-bootstrap/raw/master/assets/example_form_error.png)
92
+
93
+
94
+ Options
95
+ -------
96
+
97
+ By default, help (and error) messages will be placed to the right of the
98
+ input field. If you want to place them under the input field, pass the option
99
+ `help: :block`:
100
+
101
+ <%= form_bootstrap_for @user, help: :block do |f| %>
102
+
82
103
 
83
104
  Credits
84
105
  -------
@@ -2,17 +2,20 @@ module FormBootstrap
2
2
  class Builder < ActionView::Helpers::FormBuilder
3
3
  delegate :content_tag, to: :@template
4
4
 
5
+ def initialize(object_name, object, template, options, proc)
6
+ super
7
+ @help_css = (options[:help].try(:to_sym) == :block) ? 'help-block' : 'help-inline'
8
+ end
9
+
5
10
  %w{text_field text_area password_field collection_select}.each do |method_name|
6
11
  define_method(method_name) do |name, *args|
7
- options = args.extract_options!
12
+ options = args.extract_options!.stringify_keys
8
13
  content_tag :div, class: "clearfix#{(' error' if object.errors[name].any?)}" do
9
- field_label(name, *args) +
14
+ label(name, options['label']) +
10
15
  content_tag(:div, class: 'input') do
11
- help = (object.errors[name].any?) ? object.errors[name].join(', ') : options[:help]
12
- super(name, *args) +
13
- content_tag(:span, class: 'help-inline') do
14
- help
15
- end
16
+ help = object.errors[name].any? ? object.errors[name].join(', ') : options['help']
17
+ help = content_tag(:span, class: @help_css) { help } if help
18
+ super(name, *args, options.except('label', 'help')) + help
16
19
  end
17
20
  end
18
21
  end
@@ -24,21 +27,15 @@ module FormBootstrap
24
27
  end
25
28
  end
26
29
 
27
- def error_message(title)
30
+ def alert_message(title, *args)
31
+ options = args.extract_options!
32
+ css = options[:class] || "alert-message error"
33
+
28
34
  if object.errors.full_messages.any?
29
- content_tag :div, class: "alert-message error" do
30
- content_tag :p do
31
- title
32
- end
35
+ content_tag :div, class: css do
36
+ title
33
37
  end
34
38
  end
35
39
  end
36
-
37
- private
38
-
39
- def field_label(name, *args)
40
- options = args.extract_options!
41
- label(name, options[:label])
42
- end
43
40
  end
44
41
  end
@@ -2,7 +2,17 @@ module FormBootstrap
2
2
  module Helper
3
3
  def form_bootstrap_for(object, options = {}, &block)
4
4
  options[:builder] = FormBootstrap::Builder
5
- form_for(object, options, &block)
5
+ temporarily_disable_field_error_proc do
6
+ form_for(object, options, &block)
7
+ end
8
+ end
9
+
10
+ def temporarily_disable_field_error_proc
11
+ original_proc = ActionView::Base.field_error_proc
12
+ ActionView::Base.field_error_proc = proc { |input, instance| input }
13
+ yield
14
+ ensure
15
+ ActionView::Base.field_error_proc = original_proc
6
16
  end
7
17
  end
8
18
  end
@@ -1,3 +1,3 @@
1
1
  module FormBootstrap
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -42,4 +42,3 @@ module Dummy
42
42
  config.assets.version = '1.0'
43
43
  end
44
44
  end
45
-
@@ -6,48 +6,96 @@ class FormBootstrapTest < ActionView::TestCase
6
6
  @builder = FormBootstrap::Builder.new :user, @user, self, {}, nil
7
7
  end
8
8
 
9
+ test "helper method to wrap form bootstrap builder" do
10
+ expected = %{<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div></form>}
11
+ assert_equal expected, form_bootstrap_for(@user) { |f| nil }
12
+ end
13
+
14
+ test "alert message is wrapped correctly" do
15
+ @user.email = nil
16
+ @user.valid?
17
+ expected = %{<div class="alert-message error">Please fix the following errors:</div>}
18
+ assert_equal expected, @builder.alert_message('Please fix the following errors:')
19
+ end
20
+
21
+ test "changing the class name for the alert message" do
22
+ @user.email = nil
23
+ @user.valid?
24
+ expected = %{<div class="my-css-class">Please fix the following errors:</div>}
25
+ assert_equal expected, @builder.alert_message('Please fix the following errors:', class: 'my-css-class')
26
+ end
27
+
9
28
  test "text fields are wrapped correctly" do
10
- expected = %{<div class="clearfix"><label for="user_email">Email</label><div class="input"><input id="user_email" name="user[email]" size="30" type="text" value="steve@example.com" /><span class="help-inline"></span></div></div>}
29
+ expected = %{<div class="clearfix"><label for="user_email">Email</label><div class="input"><input id="user_email" name="user[email]" size="30" type="text" value="steve@example.com" /></div></div>}
11
30
  assert_equal expected, @builder.text_field(:email)
12
31
  end
13
32
 
14
33
  test "password fields are wrapped correctly" do
15
- expected = %{<div class="clearfix"><label for="user_password">Password</label><div class="input"><input id="user_password" name="user[password]" size="30" type="text" value="secret" /><span class="help-inline"></span></div></div>}
34
+ expected = %{<div class="clearfix"><label for="user_password">Password</label><div class="input"><input id="user_password" name="user[password]" size="30" type="text" value="secret" /></div></div>}
16
35
  assert_equal expected, @builder.text_field(:password)
17
36
  end
18
37
 
19
38
  test "text areas are wrapped correctly" do
20
- expected = %{<div class="clearfix"><label for="user_comments">Comments</label><div class="input"><textarea cols="40" id="user_comments" name="user[comments]" rows="20">my comment</textarea><span class="help-inline"></span></div></div>}
39
+ expected = %{<div class="clearfix"><label for="user_comments">Comments</label><div class="input"><textarea cols="40" id="user_comments" name="user[comments]" rows="20">my comment</textarea></div></div>}
21
40
  assert_equal expected, @builder.text_area(:comments)
22
41
  end
23
42
 
24
43
  test "selects are wrapped correctly" do
25
- expected = %{<div class="clearfix"><label for="user_status">Status</label><div class="input"><select id="user_status" name="user[status]"></select><span class="help-inline"></span></div></div>}
44
+ expected = %{<div class="clearfix"><label for="user_status">Status</label><div class="input"><select id="user_status" name="user[status]"></select></div></div>}
26
45
  assert_equal expected, @builder.collection_select(:status, [], :id, :name)
27
46
  end
28
47
 
48
+ test "changing the label text" do
49
+ expected = %{<div class="clearfix"><label for="user_email">Email Address</label><div class="input"><input id="user_email" name="user[email]" size="30" type="text" value="steve@example.com" /></div></div>}
50
+ assert_equal expected, @builder.text_field(:email, label: 'Email Address')
51
+ end
52
+
53
+ test "passing :help to a field displays it inline" do
54
+ expected = %{<div class="clearfix"><label for="user_email">Email</label><div class="input"><input id="user_email" name="user[email]" size="30" type="text" value="steve@example.com" /><span class="help-inline">This is required</span></div></div>}
55
+ assert_equal expected, @builder.text_field(:email, help: 'This is required')
56
+ end
57
+
58
+ test "passing other options to a field get passed through" do
59
+ expected = %{<div class="clearfix"><label for="user_email">Email</label><div class="input"><input autofocus="autofocus" id="user_email" name="user[email]" size="30" type="text" value="steve@example.com" /></div></div>}
60
+ assert_equal expected, @builder.text_field(:email, autofocus: true)
61
+ end
62
+
29
63
  test "actions are wrapped correctly" do
30
64
  expected = %{<div class="actions"><input class="btn primary" name="commit" type="submit" value="Submit" /></div>}
31
65
  assert_equal expected, @builder.actions('Submit')
32
66
  end
33
67
 
34
- test "error messages are wrapped correctly" do
68
+ test "passing :help :block to the form builder" do
69
+ output = form_bootstrap_for(@user, help: :block) do |f|
70
+ f.text_field(:email, help: 'This is required')
71
+ end
72
+
73
+ expected = %{<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div><div class="clearfix"><label for="user_email">Email</label><div class="input"><input id="user_email" name="user[email]" size="30" type="text" value="steve@example.com" /><span class="help-block">This is required</span></div></div></form>}
74
+ assert_equal expected, output
75
+ end
76
+
77
+ test "the field contains the error and is not wrapped in div.field_with_errors when form_bootstrap_for is used" do
35
78
  @user.email = nil
36
79
  @user.valid?
37
- expected = %{<div class="alert-message error"><p>Please fix the following errors:</p></div>}
38
- assert_equal expected, @builder.error_message('Please fix the following errors:')
80
+
81
+ output = form_bootstrap_for(@user, help: :block) do |f|
82
+ f.text_field(:email, help: 'This is required')
83
+ end
84
+
85
+ expected = %{<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div><div class="clearfix error"><label for="user_email">Email</label><div class="input"><input id="user_email" name="user[email]" size="30" type="text" /><span class="help-block">can't be blank, is too short (minimum is 5 characters)</span></div></div></form>}
86
+ assert_equal expected, output
39
87
  end
40
88
 
41
- test "help message contains the field error when invalid" do
89
+ test "the field is wrapped with div.field_with_errors when form_for is used" do
42
90
  @user.email = nil
43
91
  @user.valid?
44
- expected = %{<div class="clearfix error"><div class="field_with_errors"><label for="user_email">Email</label></div><div class="input"><div class="field_with_errors"><input id="user_email" name="user[email]" size="30" type="text" /></div><span class="help-inline">can't be blank, is too short (minimum is 5 characters)</span></div></div>}
45
- assert_equal expected, @builder.text_field(:email)
46
- end
47
92
 
48
- test "helper method to wrap form bootstrap builder" do
49
- expected = %{<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div></form>}
50
- assert_equal expected, form_bootstrap_for(@user) { |f| nil }
93
+ output = form_for(@user, builder: FormBootstrap::Builder, help: :block) do |f|
94
+ f.text_field(:email, help: 'This is required')
95
+ end
96
+
97
+ expected = %{<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div><div class="clearfix error"><div class="field_with_errors"><label for="user_email">Email</label></div><div class="input"><div class="field_with_errors"><input id="user_email" name="user[email]" size="30" type="text" /></div><span class="help-block">can't be blank, is too short (minimum is 5 characters)</span></div></div></form>}
98
+ assert_equal expected, output
51
99
  end
52
100
  end
53
101
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: form-bootstrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70322401781560 !ruby/object:Gem::Requirement
16
+ requirement: &70259696646240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70322401781560
24
+ version_requirements: *70259696646240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &70322401719540 !ruby/object:Gem::Requirement
27
+ requirement: &70259696643700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70322401719540
35
+ version_requirements: *70259696643700
36
36
  description: Provides a builder that makes it super easy to take advantage of Twitter
37
37
  Bootstrap forms styles.
38
38
  email: