bootstraps_bootstraps 0.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +17 -6
- data/bootstraps_bootstraps.gemspec +1 -1
- data/lib/bootstraps_bootstraps.rb +3 -3
- data/lib/bootstraps_bootstraps/bootstrap_form_builder.rb +124 -33
- data/lib/bootstraps_bootstraps/railtie.rb +4 -0
- data/lib/version.rb +1 -1
- metadata +4 -4
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
|
7
7
|
To get the best experience out of both worlds Rails needs a bit of bootstrapping to get a form builder that plays nice with Bootstrap. This gem provides the bootstraps that Bootstrap needs to make Rails+Bootstrap a potent combination.
|
8
8
|
|
9
|
+
Development of this gem more or less follows [Bootstrap-sass](https://github.com/thomas-mcdonald/bootstrap-sass). As such the version number of this project correspond to the Twitter Bootstrap version numbers for both the Major and Minor. The Patch number will track independently.
|
10
|
+
|
9
11
|
##Current Features
|
10
12
|
|
11
13
|
An extended form_for called `bootstrap_form` with all the classic functionality yet extended to provide correct Bootstrap 2 syntax for horizontal forms (classed with .form_horizontal) for most elements.
|
@@ -19,24 +21,33 @@ More or less I'm writing this library as needed. Each time I find myself trying
|
|
19
21
|
Things that I know aren't in the library yet:
|
20
22
|
|
21
23
|
- Collection methods (select box, check boxes, radio buttons)
|
22
|
-
- Check boxes in general
|
23
|
-
- Full implementation of an optional attribute to make bootstrapped_form silence and revert to the default for a specific form field
|
24
|
-
- `.form-vertical`, `.form-search`, and `.form-inline` specific html syntax
|
25
24
|
|
26
25
|
##Use
|
27
26
|
|
28
|
-
Add
|
27
|
+
Add it to your gemfile: `gem 'bootstraps_bootstraps'`
|
29
28
|
|
30
29
|
To use the Bootstrap 2 form helper in your views:
|
31
30
|
|
32
31
|
```ruby
|
33
|
-
|
32
|
+
|
33
|
+
# set the form class as you would for the Bootstrap library. Bootstraps_bootstraps will use that to determine the html elements required.
|
34
|
+
= bootstrapped_form @user, html: {class: 'form-horizontal'} do |f|
|
35
|
+
# Labels are added automatically
|
34
36
|
= f.text_field :first_name
|
35
37
|
= f.text_field :last_name
|
38
|
+
|
39
|
+
# Override the label text with :label in the options hash
|
36
40
|
= f.collection_select :occupations, Occupation.all, :id, :name, :label => 'Job'
|
37
41
|
= f.text_field :address, :label => "Address Line 1"
|
38
42
|
= f.text_field :address2, :label => "Line 2"
|
39
|
-
|
43
|
+
|
44
|
+
#make an inline field group inside of the horizontal form
|
45
|
+
= f.inline_inputs label: 'Expiration Date' do |inline|
|
46
|
+
= inline.collection_select :month, (1..12), :to_s, :to_s
|
47
|
+
\-
|
48
|
+
= inline.collection_select :year, (Time.now.year...Time.now.year + 10), :to_s, :to_s
|
49
|
+
|
50
|
+
= f.submit
|
40
51
|
```
|
41
52
|
|
42
53
|
## Contributing
|
@@ -4,7 +4,7 @@ require File.expand_path('../lib/version', __FILE__)
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.authors = ["Robert L. Carpenter"]
|
6
6
|
gem.email = ["codemonkey@robacarp.com"]
|
7
|
-
gem.description = %q{A gem to make working with Bootstrap 2.0 in Rails 3 easier and better. Includes a new bootstrapped_form replacement for form_for which
|
7
|
+
gem.description = %q{A gem to make working with Bootstrap 2.0 in Rails 3 easier and better. Includes a new bootstrapped_form replacement for form_for which gives error text handling and html formatting to match Bootstrap's syntax.}
|
8
8
|
gem.summary = %q{Bootstrap 2.0 html helpers for Rails 3}
|
9
9
|
gem.homepage = "https://github.com/robacarp/bootstraps_bootstraps"
|
10
10
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'version.rb'
|
2
2
|
|
3
3
|
module BootstrapsBootstraps
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
load 'bootstraps_bootstraps/bootstrap_form_builder.rb'
|
5
|
+
load 'bootstraps_bootstraps/bootstrap_form_helper.rb'
|
6
|
+
load 'bootstraps_bootstraps/railtie.rb'
|
7
7
|
end
|
@@ -1,21 +1,46 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
module FormHelper
|
4
|
+
|
5
|
+
def wrapped_inputs object_name, object = nil, options = {}, wrapper, &block
|
6
|
+
builder = instantiate_builder(object_name, object, options, &block)
|
7
|
+
fields = capture(builder, &block)
|
8
|
+
wrapper.call(fields)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
1
16
|
module BootstrapsBootstraps
|
17
|
+
#
|
18
|
+
# This class started out as a gist somewhere but I can't seem to find the
|
19
|
+
# original. At any rate its been modified over time to make it not break
|
20
|
+
# and to update it to make it compatible with Bootstrap2.
|
21
|
+
#
|
22
|
+
# If I'm being brutally honest its a bit of a cluster of metaprogramming
|
23
|
+
# and it can be pretty difficult to understand. BUT it does seem to work
|
24
|
+
# so far.
|
25
|
+
#
|
2
26
|
class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
|
3
27
|
|
4
28
|
delegate :capture, :content_tag, :tag, to: :@template
|
5
29
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
%w[text_field text_area password_field collection_select].each do |method_name|
|
30
|
+
def initialize(object_name, object, template, options, block)
|
31
|
+
super(object_name, object, template, options, block)
|
32
|
+
|
33
|
+
@form_mode = :vertical #default value
|
34
|
+
@form_mode = :horizontal if options[:horizontal] || detect_html_class(options, 'form-horizontal')
|
35
|
+
@form_mode = :search if options[:search] || detect_html_class(options, 'form-search')
|
36
|
+
@form_mode = :inline if options[:inline] || detect_html_class(options, 'form-inline')
|
37
|
+
end
|
38
|
+
|
39
|
+
%w[text_field text_area password_field number_field telephone_field url_field email_field range_field collection_select].each do |method_name|
|
16
40
|
define_method(method_name) do |name, *args|
|
17
41
|
options = args.extract_options!
|
18
42
|
|
43
|
+
#abort and just let the rails formbuilder do its thing
|
19
44
|
if options[:vanilla]
|
20
45
|
return super(name, *(args.push(options)))
|
21
46
|
end
|
@@ -24,35 +49,54 @@ module BootstrapsBootstraps
|
|
24
49
|
error_msg = object.errors[name].any? ? content_tag(:span, object.errors[name].join(","), class: 'help-inline') : ''
|
25
50
|
|
26
51
|
help_text = options[:help_text].blank? ? '' : content_tag(:span,options[:help_text], class: 'help-block')
|
27
|
-
label = options[:label] == '' ? ''.html_safe : field_label(name, options)
|
28
52
|
|
29
|
-
|
30
|
-
options[:class] = [options[:class]] || []
|
31
|
-
options[:class].push 'xxlarge'
|
32
|
-
end
|
53
|
+
label = options[:label] == false ? ''.html_safe : field_label(name, options.except(:class))
|
33
54
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
55
|
+
options[:class] = [options[:class]] unless options[:class].kind_of?(Array)
|
56
|
+
options[:class].push 'xxlarge' if options[:large] || method_name == 'text_area'
|
57
|
+
options[:class].push 'inline' if @form_mode == :inline
|
58
|
+
|
59
|
+
args.push(options).unshift(name)
|
60
|
+
|
61
|
+
field = super(*args) + ' ' + error_msg
|
62
|
+
|
63
|
+
#wrap it in div.controls
|
64
|
+
field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal
|
65
|
+
|
66
|
+
#tack on the label
|
67
|
+
field = label + field unless @form_mode == :inline
|
68
|
+
|
69
|
+
#wrap it in div.control-group
|
70
|
+
field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal
|
71
|
+
|
72
|
+
field
|
39
73
|
end
|
40
74
|
end
|
41
75
|
|
42
76
|
#TODO update for bootstrap 2
|
43
77
|
def check_box(name, *args)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
78
|
+
if options[:vanilla]
|
79
|
+
return super *args
|
80
|
+
end
|
81
|
+
|
82
|
+
options = args.extract_options!
|
83
|
+
if options.has_key? :class
|
84
|
+
unless options[:class].kind_of? Array
|
85
|
+
options[:class] = [options[:class]]
|
55
86
|
end
|
87
|
+
else
|
88
|
+
options[:class] = []
|
89
|
+
end
|
90
|
+
|
91
|
+
options[:class].push :checkbox
|
92
|
+
|
93
|
+
text = options[:label] || ''
|
94
|
+
options.delete :label
|
95
|
+
|
96
|
+
args = args.push options
|
97
|
+
|
98
|
+
field_label(name, *args) do
|
99
|
+
super(name, *args) + text
|
56
100
|
end
|
57
101
|
end
|
58
102
|
|
@@ -67,20 +111,67 @@ module BootstrapsBootstraps
|
|
67
111
|
super *args
|
68
112
|
end
|
69
113
|
end
|
114
|
+
end
|
70
115
|
|
116
|
+
def inline_inputs field_options = {}, &block
|
117
|
+
field_options[:inline] = true
|
118
|
+
wrapper = lambda { |content|
|
119
|
+
field_group = div_with_class('controls', content: content)
|
120
|
+
field_group = label( field_options[:label], field_options[:label], class: 'control-label' ) + field_group
|
121
|
+
field_group = div_with_class('control-group', content: field_group)
|
122
|
+
}
|
123
|
+
@template.wrapped_inputs(@object_name, @object, @options.merge(field_options), wrapper, &block)
|
71
124
|
end
|
72
125
|
|
73
126
|
private
|
74
127
|
|
75
|
-
def field_label(name, *args)
|
128
|
+
def field_label(name, *args, &block)
|
76
129
|
options = args.extract_options!
|
130
|
+
clss = options[:class] || []
|
131
|
+
|
77
132
|
required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator}
|
78
|
-
|
133
|
+
clss.push 'required' if required
|
134
|
+
|
135
|
+
|
136
|
+
label(name, options[:label], class: clss, &block)
|
79
137
|
end
|
80
138
|
|
81
139
|
def objectify_options(options)
|
82
140
|
super.except(:label)
|
83
141
|
end
|
84
142
|
|
143
|
+
def detect_html_class options_hash, css_class
|
144
|
+
if options[:html] && options[:html][:class]
|
145
|
+
if options[:html][:class].kind_of? Array
|
146
|
+
options[:html][:class].include css_class
|
147
|
+
elsif options[:html][:class].kind_of? String
|
148
|
+
options[:html][:class] == css_class
|
149
|
+
else
|
150
|
+
false
|
151
|
+
end
|
152
|
+
else
|
153
|
+
false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def div_with_class clss, options = {}, &block
|
158
|
+
options[:class] = [options[:class]] || []
|
159
|
+
options[:class].push clss
|
160
|
+
|
161
|
+
classes = options[:class].join(' ').strip
|
162
|
+
|
163
|
+
if block_given?
|
164
|
+
content_tag :div, :class => classes, &block
|
165
|
+
elsif options[:content]
|
166
|
+
content_tag :div, :class => classes do
|
167
|
+
options[:content]
|
168
|
+
end
|
169
|
+
else
|
170
|
+
content_tag :div, :class => classes do
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
|
85
176
|
end
|
86
177
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'ruby-debug'
|
2
|
+
|
1
3
|
module BootstrapsBootstraps
|
2
4
|
class Railtie < Rails::Railtie
|
3
5
|
initializer "bootstraps_bootstraps.configure_rails_initialization" do |application|
|
@@ -8,6 +10,8 @@ module BootstrapsBootstraps
|
|
8
10
|
end
|
9
11
|
|
10
12
|
config.to_prepare do
|
13
|
+
#RELOAD THE GEM ALL THE TIME WITHOUT RESTARTING THE SERVER :O (without restarting the server)
|
14
|
+
::ActionView::Helpers.send(:load, 'bootstraps_bootstraps/bootstrap_form_builder.rb')
|
11
15
|
::ActionView::Helpers.send(:include, BootstrapsBootstraps::BootstrapFormHelper)
|
12
16
|
end
|
13
17
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -3,10 +3,10 @@ name: bootstraps_bootstraps
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 2
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.1
|
9
|
+
version: 2.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Robert L. Carpenter
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2012-02-
|
17
|
+
date: 2012-02-09 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: A gem to make working with Bootstrap 2.0 in Rails 3 easier and better. Includes a new bootstrapped_form replacement for form_for which
|
21
|
+
description: A gem to make working with Bootstrap 2.0 in Rails 3 easier and better. Includes a new bootstrapped_form replacement for form_for which gives error text handling and html formatting to match Bootstrap's syntax.
|
22
22
|
email:
|
23
23
|
- codemonkey@robacarp.com
|
24
24
|
executables: []
|