bootstrap_forms 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/LICENSE +7 -0
- data/README.markdown +172 -0
- data/Rakefile +1 -0
- data/bootstrap_forms.gemspec +17 -0
- data/lib/bootstrap_forms.rb +1 -0
- data/lib/bootstrap_forms/form_builder.rb +161 -0
- data/lib/bootstrap_forms/initializer.rb +24 -0
- data/lib/bootstrap_forms/railtie.rb +11 -0
- metadata +55 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2012 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,172 @@
|
|
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. The gem will add 2 methods `bootstrap_form_for` and `bootstrap_fields_for` for use in your project. This is different from `bootstrap_forms < 0.1.0`. In previous versions, the default form builders were overridden by default. With backlash from various community members, this is no longer the default.
|
14
|
+
|
15
|
+
Be sure to restart your Rails server after installing the gem.
|
16
|
+
|
17
|
+
Why?
|
18
|
+
----
|
19
|
+
With Bootstrap, you would need the following code for a form:
|
20
|
+
|
21
|
+
```haml
|
22
|
+
/ using haml
|
23
|
+
= form_for @model do |f|
|
24
|
+
.clearfix
|
25
|
+
%label MyLabel
|
26
|
+
.input
|
27
|
+
= f.text_area :field, :opts => {...}
|
28
|
+
```
|
29
|
+
|
30
|
+
```erb
|
31
|
+
<!-- using ERB -->
|
32
|
+
<%= form_for @model do |f| %>
|
33
|
+
<div class="clearfix">
|
34
|
+
<label>MyLabel</label>
|
35
|
+
<div class="input">
|
36
|
+
<%= f.text_area :field, :opts => {...} %>
|
37
|
+
</div>
|
38
|
+
</div>
|
39
|
+
<% end %>
|
40
|
+
```
|
41
|
+
|
42
|
+
Using Bootstrap Forms, this is **much** simpler:
|
43
|
+
|
44
|
+
```haml
|
45
|
+
/ using HAML
|
46
|
+
= bootstrap_form_for @model do |f|
|
47
|
+
= f.text_area :field, :opts => {...}
|
48
|
+
```
|
49
|
+
|
50
|
+
```erb
|
51
|
+
<!-- using ERB -->
|
52
|
+
<%= bootstrap_form_for @model do |f| %>
|
53
|
+
<%= f.text_area :field, :opts => {...} %>
|
54
|
+
<% end %>
|
55
|
+
```
|
56
|
+
|
57
|
+
The custom form builder will automatically wrap everything for you. This helps clean up your view layer significantly!
|
58
|
+
|
59
|
+
Additional Form Methods
|
60
|
+
-----------------------
|
61
|
+
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.
|
62
|
+
|
63
|
+
### collection_check_boxes
|
64
|
+
`collection_check_boxes` behaves very similarly to `collection_select`:
|
65
|
+
|
66
|
+
```haml
|
67
|
+
= f.collection_check_boxes :category_ids, Category.all, :id, :name
|
68
|
+
```
|
69
|
+
|
70
|
+
### collection_radio_buttons
|
71
|
+
See description above...
|
72
|
+
|
73
|
+
```haml
|
74
|
+
= f.collection_radio_buttons :primary_category_id, Category.all, :id, :name
|
75
|
+
```
|
76
|
+
|
77
|
+
Uneditable Field
|
78
|
+
----------------
|
79
|
+
Bootstrap Forms adds another helper method that generates the necessary markup for uneditable fields:
|
80
|
+
|
81
|
+
```haml
|
82
|
+
= f.uneditable_field :name
|
83
|
+
```
|
84
|
+
|
85
|
+
yields:
|
86
|
+
|
87
|
+
```html
|
88
|
+
<div class="clearfix">
|
89
|
+
<label for="organization_name">Organization Name</label>
|
90
|
+
<div class="input">
|
91
|
+
<span class="uneditable-input">The Variety Hour</span>
|
92
|
+
</div>
|
93
|
+
</div>
|
94
|
+
```
|
95
|
+
|
96
|
+
Submit Tag
|
97
|
+
----------
|
98
|
+
Bootstrap Forms also adds a default actions panel when you call `f.submit`:
|
99
|
+
|
100
|
+
```haml
|
101
|
+
= f.submit
|
102
|
+
```
|
103
|
+
|
104
|
+
generates:
|
105
|
+
|
106
|
+
```html
|
107
|
+
<div class="actions">
|
108
|
+
<input type="submit" value="..." class="btn primary" />
|
109
|
+
<a href="..." class="btn">Cancel</a>
|
110
|
+
</div>
|
111
|
+
```
|
112
|
+
|
113
|
+
Pretty swell if you ask me.
|
114
|
+
|
115
|
+
Adding More Options
|
116
|
+
-------------------
|
117
|
+
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.
|
118
|
+
|
119
|
+
### Available Options
|
120
|
+
|
121
|
+
<table>
|
122
|
+
<tr>
|
123
|
+
<th>Name</th>
|
124
|
+
<th>Description</th>
|
125
|
+
<th>Usage</th>
|
126
|
+
</tr>
|
127
|
+
<tr>
|
128
|
+
<th>help_inline</th>
|
129
|
+
<td>Add inline help text</td>
|
130
|
+
<td>= f.text_field :name, :help_inline => 'help me!'</td>
|
131
|
+
</tr>
|
132
|
+
<tr>
|
133
|
+
<th>help_block</th>
|
134
|
+
<td>Add block help text (below)</td>
|
135
|
+
<td>= f.text_field :name, :help_block => 'help me!'</td>
|
136
|
+
</tr>
|
137
|
+
<tr>
|
138
|
+
<th>error</th>
|
139
|
+
<td>Styles the field as error (red)</td>
|
140
|
+
<td>= f.text_field :name, :error => 'This is an error!'</td>
|
141
|
+
</tr>
|
142
|
+
<tr>
|
143
|
+
<th>success</th>
|
144
|
+
<td>Styles the field as success (green)</td>
|
145
|
+
<td>= f.text_field :name, :success => 'This checked out OK'</td>
|
146
|
+
</tr>
|
147
|
+
<tr>
|
148
|
+
<th>warning</th>
|
149
|
+
<td>Styles the field as warning (yellow)</td>
|
150
|
+
<td>= f.text_field :name, :warning => 'Take a look at this...'</td>
|
151
|
+
</tr>
|
152
|
+
<tr>
|
153
|
+
<th>prepend</th>
|
154
|
+
<td>Adds special text to the front of the input</td>
|
155
|
+
<td>= f.text_field :name, :prepend => '@'</td>
|
156
|
+
</tr>
|
157
|
+
<tr>
|
158
|
+
<th>append</th>
|
159
|
+
<td>Adds special text at the end of the input</td>
|
160
|
+
<td>= f.text_field :name, :append => '@'</td>
|
161
|
+
</tr>
|
162
|
+
</table>
|
163
|
+
|
164
|
+
License
|
165
|
+
-------
|
166
|
+
Copyright (c) 2012 Seth Vargo
|
167
|
+
|
168
|
+
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:
|
169
|
+
|
170
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
171
|
+
|
172
|
+
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/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "bootstrap_forms"
|
6
|
+
s.version = "0.1.0"
|
7
|
+
s.author = "Seth Vargo"
|
8
|
+
s.email = "sethvargo@gmail.com"
|
9
|
+
s.homepage = "https://github.com/sethvargo/bootstrap_forms"
|
10
|
+
s.summary = %q{Bootstrap Forms makes Twitter's Bootstrap on Rails easy!}
|
11
|
+
s.description = %q{Bootstrap Forms makes Twitter's Bootstrap on Rails easy to use by creating helpful form builders that minimize markup in your views.}
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'bootstrap_forms/railtie' if defined?(Rails)
|
@@ -0,0 +1,161 @@
|
|
1
|
+
module BootstrapForms
|
2
|
+
class FormBuilder < ActionView::Helpers::FormBuilder
|
3
|
+
delegate :content_tag, :hidden_field_tag, :check_box_tag, :radio_button_tag, :link_to, :to => :@template
|
4
|
+
|
5
|
+
def error_messages
|
6
|
+
if object.errors.full_messages.any?
|
7
|
+
content_tag(:div, :class => 'alert-message block-message error') do
|
8
|
+
link_to('×'.html_safe, '#', :class => 'close') +
|
9
|
+
content_tag(:p, "<strong>Oh snap! You got an error!</strong> Fix the errors below and try again.".html_safe) +
|
10
|
+
content_tag(:ul) do
|
11
|
+
object.errors.full_messages.map do |message|
|
12
|
+
content_tag(:li, message)
|
13
|
+
end.join('').html_safe
|
14
|
+
end
|
15
|
+
end
|
16
|
+
else
|
17
|
+
'' # return empty string
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
%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|
|
22
|
+
define_method(method_name) do |name, *args|
|
23
|
+
@name = name
|
24
|
+
@options = args.extract_options!
|
25
|
+
@args = args
|
26
|
+
|
27
|
+
clearfix_div do
|
28
|
+
label_field + input_div do
|
29
|
+
extras { super(name, *(@args << @options)) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def collection_check_boxes(attribute, records, record_id, record_name, *args)
|
36
|
+
@name = attribute
|
37
|
+
@options = args.extract_options!
|
38
|
+
@args = args
|
39
|
+
|
40
|
+
clearfix_div do
|
41
|
+
label_field + input_div do
|
42
|
+
extras do
|
43
|
+
content_tag(:ul, :class => 'inputs-list') do
|
44
|
+
records.collect do |record|
|
45
|
+
element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
|
46
|
+
checkbox = check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), :id => element_id)
|
47
|
+
|
48
|
+
content_tag(:li) do
|
49
|
+
content_tag(:label) do
|
50
|
+
checkbox + content_tag(:span, record.send(record_name))
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end.join('').html_safe
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end + hidden_field_tag("#{object_name}[#{attribute}][]")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def collection_radio_buttons(attribute, records, record_id, record_name, *args)
|
61
|
+
@name = attribute
|
62
|
+
@options = args.extract_options!
|
63
|
+
@args = args
|
64
|
+
|
65
|
+
clearfix_div do
|
66
|
+
label_field + input_div do
|
67
|
+
extras do
|
68
|
+
content_tag(:ul, :class => 'inputs-list') do
|
69
|
+
records.collect do |record|
|
70
|
+
element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
|
71
|
+
radiobutton = radio_button_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute) == record.send(record_id), :id => element_id)
|
72
|
+
|
73
|
+
content_tag(:li) do
|
74
|
+
content_tag(:label) do
|
75
|
+
radiobutton + content_tag(:span, record.send(record_name))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end.join('').html_safe
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def uneditable_field(name, *args)
|
86
|
+
@name = name
|
87
|
+
@options = args.extract_options!
|
88
|
+
@args = args
|
89
|
+
|
90
|
+
clearfix_div do
|
91
|
+
label_field + input_div do
|
92
|
+
extras do
|
93
|
+
content_tag(:span, :class => 'uneditable-input') do
|
94
|
+
@options[:value] || object.send(@name.to_sym)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def submit(name = nil, *args)
|
102
|
+
@name = name
|
103
|
+
@options = args.extract_options!
|
104
|
+
@args = args
|
105
|
+
|
106
|
+
@options[:class] = 'btn primary'
|
107
|
+
|
108
|
+
content_tag(:div, :class => 'actions') do
|
109
|
+
super(name, *(args << @options)) + ' ' + link_to('Cancel', :back, :class => 'btn')
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def clearfix_div(&block)
|
115
|
+
@options[:error] = object.errors[@name].collect{|e| "#{@options[:label] || @name} #{e}".humanize}.join(', ') unless object.errors[@name].empty?
|
116
|
+
|
117
|
+
klasses = ['clearfix']
|
118
|
+
klasses << 'error' if @options[:error]
|
119
|
+
klasses << 'success' if @options[:success]
|
120
|
+
klasses << 'warning' if @options[:warning]
|
121
|
+
klass = klasses.join(' ')
|
122
|
+
|
123
|
+
content_tag(:div, :class => klass, &block)
|
124
|
+
end
|
125
|
+
|
126
|
+
def input_div(&block)
|
127
|
+
content_tag(:div, :class => 'input') do
|
128
|
+
if @options[:append] || @options[:prepend]
|
129
|
+
klass = 'input-prepend' if @options[:prepend]
|
130
|
+
klass = 'input-append' if @options[:append]
|
131
|
+
content_tag(:div, :class => klass, &block)
|
132
|
+
else
|
133
|
+
yield if block_given?
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def label_field(&block)
|
139
|
+
required = object.class.validators_on(@name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
|
140
|
+
label(@name, block_given? ? block : @options[:label], :class => ('required' if required))
|
141
|
+
end
|
142
|
+
|
143
|
+
%w(help_inline error success warning help_block append prepend).each do |method_name|
|
144
|
+
define_method(method_name) do |*args|
|
145
|
+
return '' unless value = @options[method_name.to_sym]
|
146
|
+
klass = 'help-inline'
|
147
|
+
klass = 'help-block' if method_name == 'help_block'
|
148
|
+
klass = 'add-on' if method_name == 'append' || method_name == 'prepend'
|
149
|
+
content_tag(:span, value, :class => klass)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def extras(&block)
|
154
|
+
[prepend, (yield if block_given?), append, help_inline, error, success, warning, help_block].join('').html_safe
|
155
|
+
end
|
156
|
+
|
157
|
+
def objectify_options(options)
|
158
|
+
super.except(:label, :help_inline, :error, :success, :warning, :help_block, :prepend, :append)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bootstrap_forms/form_builder'
|
2
|
+
|
3
|
+
module ActionView
|
4
|
+
module Helpers
|
5
|
+
module FormHelper
|
6
|
+
def bootstrap_form_for(record, options = {}, &block)
|
7
|
+
options[:builder] = BootstrapForms::FormBuilder
|
8
|
+
form_for(record, options) do |f|
|
9
|
+
f.error_messages.html_safe + capture(f, &block).html_safe
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def bootstrap_fields_for(record_name, record_object = nil, options = {}, &block)
|
14
|
+
options[:builder] = BootstrapForms::FormBuilder
|
15
|
+
fields_for(record_name, record_object, options, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# don't wrap in those special divs
|
22
|
+
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
|
23
|
+
html_tag
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bootstrap_forms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Seth Vargo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Bootstrap Forms makes Twitter's Bootstrap on Rails easy to use by creating
|
15
|
+
helpful form builders that minimize markup in your views.
|
16
|
+
email: sethvargo@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.markdown
|
25
|
+
- Rakefile
|
26
|
+
- bootstrap_forms.gemspec
|
27
|
+
- lib/bootstrap_forms.rb
|
28
|
+
- lib/bootstrap_forms/form_builder.rb
|
29
|
+
- lib/bootstrap_forms/initializer.rb
|
30
|
+
- lib/bootstrap_forms/railtie.rb
|
31
|
+
homepage: https://github.com/sethvargo/bootstrap_forms
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.15
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Bootstrap Forms makes Twitter's Bootstrap on Rails easy!
|
55
|
+
test_files: []
|