bootstrap-forms 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Seth Vargo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
Bootstrap Forms
|
2
|
+
===============
|
3
|
+
Bootstrap Forms is a nice Rails generator that makes working with [Bootstrap (by Twitter)](http://twitter.github.com/bootstrap) even easier on Rails.
|
4
|
+
|
5
|
+
Forms with Bootstrap are crowded with additional layout markup. While it's necessary, you shouldn't have to type it every time you create a form! That's why I created Bootstrap Forms.
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
Add it to your `Gemfile`:
|
10
|
+
|
11
|
+
gem 'bootstrap-forms'
|
12
|
+
|
13
|
+
Don't forget to run the `bundle` command. Run the generator:
|
14
|
+
|
15
|
+
rails g bootstrap_forms:install
|
16
|
+
|
17
|
+
This will create 2 files in your project.
|
18
|
+
|
19
|
+
Restart your Rails server.
|
20
|
+
|
21
|
+
Why?
|
22
|
+
----
|
23
|
+
With Bootstrap, you would need the following code for a form:
|
24
|
+
|
25
|
+
# using HAML
|
26
|
+
= form_for @model do |f|
|
27
|
+
.clearfix
|
28
|
+
%label MyLabel
|
29
|
+
.input
|
30
|
+
= f.text_area :field, :opts => {...}
|
31
|
+
|
32
|
+
# using ERB
|
33
|
+
<%= form_for @model do |f| %>
|
34
|
+
<div class="clearfix">
|
35
|
+
<label>MyLabel</label>
|
36
|
+
<div class="input">
|
37
|
+
<%= f.text_area :field, :opts => {...} %>
|
38
|
+
</div>
|
39
|
+
</div>
|
40
|
+
<% end %>
|
41
|
+
|
42
|
+
Using Bootstrap Forms, this is **much** simpler:
|
43
|
+
|
44
|
+
# using HAML
|
45
|
+
= form_for @model do |f|
|
46
|
+
= f.text_area :field, :opts => {...}
|
47
|
+
|
48
|
+
# using ERB
|
49
|
+
<%= form_for @model do |f| %>
|
50
|
+
<%= f.text_area :field, :opts => {...} %>
|
51
|
+
<% end %>
|
52
|
+
|
53
|
+
The custom form builder will automatically wrap everything for you. This helps clean up your view layer significantly!
|
54
|
+
|
55
|
+
Additional Form Methods
|
56
|
+
-----------------------
|
57
|
+
Just when you thought you were done... Bootstrap Forms includes additional form helpers that make life **a lot** easier! For example, the markup required for a list of checkboxes is quite cumbersome... well, it used to be.
|
58
|
+
|
59
|
+
### collection_check_boxes
|
60
|
+
`collection_check_boxes` behaves very similarly to `collection_select`:
|
61
|
+
|
62
|
+
= f.collection_check_boxes :category_ids, Category.all, :id, :name
|
63
|
+
|
64
|
+
### collection_radio_buttons
|
65
|
+
See description above...
|
66
|
+
|
67
|
+
= f.collection_radio_buttons :primary_category_id, Category.all, :id, :name
|
68
|
+
|
69
|
+
Uneditable Field
|
70
|
+
----------------
|
71
|
+
Bootstrap Forms adds another helper method that generates the necessary markup for uneditable fields:
|
72
|
+
|
73
|
+
= f.uneditable_field :name
|
74
|
+
|
75
|
+
generates:
|
76
|
+
|
77
|
+
<div class="clearfix">
|
78
|
+
<label for="organization_name">Organization Name</label>
|
79
|
+
<div class="input">
|
80
|
+
<span class="uneditable-input">The Variety Hour</span>
|
81
|
+
</div>
|
82
|
+
</div>
|
83
|
+
|
84
|
+
Submit Tag
|
85
|
+
----------
|
86
|
+
Bootstrap Forms also adds a default actions panel when you call `f.submit`:
|
87
|
+
|
88
|
+
= f.submit
|
89
|
+
|
90
|
+
generates:
|
91
|
+
|
92
|
+
<div class="actions">
|
93
|
+
<input type="submit" value="..." class="btn primary" />
|
94
|
+
<a href="..." class="btn">Cancel</a>
|
95
|
+
</div>
|
96
|
+
|
97
|
+
Pretty swell if you ask me.
|
98
|
+
|
99
|
+
Adding More Options
|
100
|
+
-------------------
|
101
|
+
You can add as many options to any form helper tag. If they are interpreted by Bootstrap Forms, they are interpreted and rendered in the output. If not, they are passed along as values to the final HTML form object.
|
102
|
+
|
103
|
+
### Available Options
|
104
|
+
|
105
|
+
<table>
|
106
|
+
<tr>
|
107
|
+
<th>Name</th>
|
108
|
+
<th>Description</th>
|
109
|
+
<th>Usage</th>
|
110
|
+
</tr>
|
111
|
+
<tr>
|
112
|
+
<th>help_inline</th>
|
113
|
+
<td>Add inline help text</td>
|
114
|
+
<td>`= f.text_field :name, :help_inline => 'help me!'`</td>
|
115
|
+
</tr>
|
116
|
+
<tr>
|
117
|
+
<th>help_block</th>
|
118
|
+
<td>Add block help text (below)</td>
|
119
|
+
<td>`= f.text_field :name, :help_block => 'help me!'`</td>
|
120
|
+
</tr>
|
121
|
+
<tr>
|
122
|
+
<th>error</th>
|
123
|
+
<td>Styles the field as error (red)</td>
|
124
|
+
<td>`= f.text_field :name, :error => 'This is an error!'`</td>
|
125
|
+
</tr>
|
126
|
+
<tr>
|
127
|
+
<th>success</th>
|
128
|
+
<td>Styles the field as success (green)</td>
|
129
|
+
<td>`= f.text_field :name, :success => 'This checked out OK'`</td>
|
130
|
+
</tr>
|
131
|
+
<tr>
|
132
|
+
<th>warning</th>
|
133
|
+
<td>Styles the field as warning (yellow)</td>
|
134
|
+
<td>`= f.text_field :name, :warning => 'Take a look at this...'`</td>
|
135
|
+
</tr>
|
136
|
+
<tr>
|
137
|
+
<th>prepend</th>
|
138
|
+
<td>Adds special text to the front of the input</td>
|
139
|
+
<td>`= f.text_field :name, :prepend => '@'`</td>
|
140
|
+
</tr>
|
141
|
+
<tr>
|
142
|
+
<th>append</th>
|
143
|
+
<td>Adds special text at the end of the input</td>
|
144
|
+
<td>`= f.text_field :name, :append => '@'`</td>
|
145
|
+
</tr>
|
146
|
+
</table>
|
147
|
+
|
148
|
+
License
|
149
|
+
-------
|
150
|
+
Copyright (c) 2011 Seth Vargo
|
151
|
+
|
152
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
153
|
+
|
154
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
155
|
+
|
156
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/bootstrap_forms.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "bootstrap-forms"
|
6
|
-
s.version = "0.0.
|
6
|
+
s.version = "0.0.2"
|
7
7
|
s.author = "Seth Vargo"
|
8
8
|
s.email = "sethvargo@gmail.com"
|
9
9
|
s.homepage = "https://github.com/sethvargo/bootstrap_forms"
|
@@ -1,7 +1,23 @@
|
|
1
1
|
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
|
2
2
|
delegate :content_tag, :hidden_field_tag, :check_box_tag, :radio_button_tag, :link_to, :to => :@template
|
3
|
+
|
4
|
+
def error_messages
|
5
|
+
if object.errors.full_messages.any?
|
6
|
+
content_tag(:div, :class => 'alert-message block-message error') do
|
7
|
+
link_to('×'.html_safe, '#', :class => 'close') +
|
8
|
+
content_tag(:p, "<strong>Oh snap! You got an error!</strong> Fix the errors below and try again.".html_safe) +
|
9
|
+
content_tag(:ul) do
|
10
|
+
object.errors.full_messages.map do |message|
|
11
|
+
content_tag(:li, message)
|
12
|
+
end.join('').html_safe
|
13
|
+
end
|
14
|
+
end
|
15
|
+
else
|
16
|
+
'' # return empty string
|
17
|
+
end
|
18
|
+
end
|
3
19
|
|
4
|
-
%w(select check_box email_field file_field number_field password_field phone_field radio_button range_field search_field telephone_field text_area text_field url_field).each do |method_name|
|
20
|
+
%w(collection_select select check_box email_field file_field number_field password_field phone_field radio_button range_field search_field telephone_field text_area text_field url_field).each do |method_name|
|
5
21
|
define_method(method_name) do |name, *args|
|
6
22
|
@name = name
|
7
23
|
@options = args.extract_options!
|
@@ -89,15 +105,15 @@ class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
|
|
89
105
|
@options[:class] = 'btn primary'
|
90
106
|
|
91
107
|
content_tag(:div, :class => 'actions') do
|
92
|
-
super(name, *args << @options) +
|
93
|
-
link_to('Back', :back, :class => 'btn')
|
108
|
+
super(name, *args << @options) + ' ' + link_to('Cancel', :back, :class => 'btn')
|
94
109
|
end
|
95
110
|
end
|
96
111
|
|
97
112
|
private
|
98
113
|
def clearfix_div(&block)
|
114
|
+
@options[:error] = object.errors[@name].collect{|e| "#{@options[:label] || @name} #{e}".humanize}.join(', ') unless object.errors[@name].empty?
|
115
|
+
|
99
116
|
klasses = ['clearfix']
|
100
|
-
klasses << @options[:class] if @options[:class]
|
101
117
|
klasses << 'error' if @options[:error]
|
102
118
|
klasses << 'success' if @options[:success]
|
103
119
|
klasses << 'warning' if @options[:warning]
|
@@ -1,18 +1,24 @@
|
|
1
1
|
module ActionView
|
2
2
|
module Helpers
|
3
3
|
module FormHelper
|
4
|
-
def form_for_with_bootstrap(record, options = {}, &
|
4
|
+
def form_for_with_bootstrap(record, options = {}, &block)
|
5
5
|
options[:builder] = BootstrapFormBuilder
|
6
|
-
form_for_without_bootstrap(record, options
|
6
|
+
form_for_without_bootstrap(record, options) do |f|
|
7
|
+
f.error_messages.html_safe + capture(f, &block).html_safe
|
8
|
+
end
|
7
9
|
end
|
8
|
-
|
10
|
+
|
9
11
|
def fields_for_with_bootstrap(record_name, record_object = nil, options = {}, &block)
|
10
12
|
options[:builder] = BootstrapFormBuilder
|
11
13
|
fields_for_without_bootstrap(record_name, record_object, options, &block)
|
12
14
|
end
|
13
|
-
|
15
|
+
|
14
16
|
alias_method_chain :form_for, :bootstrap
|
15
17
|
alias_method_chain :fields_for, :bootstrap
|
16
18
|
end
|
17
19
|
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
|
23
|
+
html_tag
|
18
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bootstrap-forms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-01 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Bootstrap Forms makes Twitter's Bootstrap on Rails easy to use by creating
|
15
15
|
helpful form builders that minimize markup in your views.
|
@@ -20,6 +20,8 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
22
|
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.markdown
|
23
25
|
- Rakefile
|
24
26
|
- bootstrap_forms.gemspec
|
25
27
|
- lib/bootstrap_forms.rb
|