twitter_bootstrap_form_for 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/README.markdown +61 -0
- data/app/views/twitter_bootstrap_form_for/_actions.html.haml +2 -0
- data/app/views/twitter_bootstrap_form_for/_input.html.haml +6 -0
- data/app/views/twitter_bootstrap_form_for/_inputs.html.haml +5 -0
- data/app/views/twitter_bootstrap_form_for/_toggle.html.haml +4 -0
- data/app/views/twitter_bootstrap_form_for/_toggles.html.haml +6 -0
- data/lib/twitter_bootstrap_form_for.rb +9 -0
- data/lib/twitter_bootstrap_form_for/form_builder.rb +162 -0
- data/lib/twitter_bootstrap_form_for/form_helpers.rb +16 -0
- data/lib/twitter_bootstrap_form_for/railtie.rb +10 -0
- data/lib/twitter_bootstrap_form_for/version.rb +3 -0
- data/twitter_bootstrap_form_for.gemspec +24 -0
- metadata +82 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@twitter_bootstrap_form_for
|
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
Twitter Bootstrap Form For
|
2
|
+
==========================
|
3
|
+
|
4
|
+
twitter_bootstrap_form_for is a Rails FormBuilder DSL that, like Formtastic,
|
5
|
+
makes it easier to create semantically awesome, readily-stylable, and
|
6
|
+
wonderfully accessible HTML forms in your Rails applications. It abides by
|
7
|
+
the markup expectations of [Twitter Bootstrap], make your forms look great right
|
8
|
+
out of the box.
|
9
|
+
|
10
|
+
However, it also tries not to do too much. Rather than try to guess at input
|
11
|
+
types and provide an exhaustive set of options for each tag helper (as
|
12
|
+
Formtastic does), it only lightly wraps the existing Rails form tag helpers.
|
13
|
+
|
14
|
+
Dependencies
|
15
|
+
============
|
16
|
+
|
17
|
+
Besides Rails, Twitter Bootstrap depends on Haml. You don't have to use it
|
18
|
+
yourself, but it made the views for this gem far, far cleaner and more
|
19
|
+
readable.
|
20
|
+
|
21
|
+
I may consider adding a dependency on `less-rails-bootstrap` in the future, to
|
22
|
+
tie this plugin to specific releases of Twitter Bootstrap.
|
23
|
+
|
24
|
+
Syntax
|
25
|
+
======
|
26
|
+
|
27
|
+
= twitter_bootstrap_form_for @user do |user|
|
28
|
+
|
29
|
+
/ wraps a section in a fieldset with the provided legend text
|
30
|
+
= user.inputs 'Sign up' do
|
31
|
+
|
32
|
+
/ generates an email field with descriptive help text and standard
|
33
|
+
/ HTML attributes
|
34
|
+
= user.email_field :email, :placeholder => 'me@example.com' do
|
35
|
+
%span.help-block No account? #{link_to('Sign up!', '#')}
|
36
|
+
|
37
|
+
/ generates a password field in a similar manner
|
38
|
+
= user.password_field :password do
|
39
|
+
%span.help-block= link_to('Forgot your password?', '#')
|
40
|
+
|
41
|
+
/ a field with a custom label
|
42
|
+
= user.password_field :password_confirmation, 'Confirm Password'
|
43
|
+
|
44
|
+
/ input fields with custom add-ons
|
45
|
+
= user.text_field :twitter_id, 'Twitter', :class => 'medium', :add_on => :prepend do
|
46
|
+
%span.add-on @
|
47
|
+
|
48
|
+
/ lists of checkboxes / radio buttons
|
49
|
+
= user.toggles 'Agreements' do
|
50
|
+
= user.check_box :agree, 'I agree to the Terms and Conditions'
|
51
|
+
= user.check_box :spam, 'I agree to receive all sorts of spam'
|
52
|
+
|
53
|
+
= user.actions do
|
54
|
+
= user.submit 'Sign up'
|
55
|
+
= button_tag 'Cancel', :type => 'reset', :class => 'btn'
|
56
|
+
|
57
|
+
That's it. All of the Rails field helpers you know and love work just like
|
58
|
+
their normal FormBuilder counterparts, but with minor extensions to expose
|
59
|
+
the functionality anticipated by Twitter Bootstrap.
|
60
|
+
|
61
|
+
[Twitter Bootstrap]: http://twitter.github.com/bootstrap/
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module TwitterBootstrapFormFor
|
2
|
+
require 'twitter_bootstrap_form_for/version'
|
3
|
+
|
4
|
+
autoload :FormBuilder, 'twitter_bootstrap_form_for/form_builder'
|
5
|
+
autoload :FormHelpers, 'twitter_bootstrap_form_for/form_helpers'
|
6
|
+
autoload :Railtie, 'twitter_bootstrap_form_for/railtie'
|
7
|
+
end
|
8
|
+
|
9
|
+
TwitterBootstrapFormFor::Railtie # trigger loading the Railtie
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'twitter_bootstrap_form_for'
|
2
|
+
|
3
|
+
class TwitterBootstrapFormFor::FormBuilder < ActionView::Helpers::FormBuilder
|
4
|
+
# TODO: support inline inputs
|
5
|
+
|
6
|
+
include TwitterBootstrapFormFor::FormHelpers
|
7
|
+
|
8
|
+
attr_reader :template
|
9
|
+
attr_reader :object
|
10
|
+
attr_reader :object_name
|
11
|
+
|
12
|
+
INPUTS = [
|
13
|
+
:select,
|
14
|
+
*ActionView::Helpers::FormBuilder.instance_methods.grep(%r{_area$}),
|
15
|
+
*ActionView::Helpers::FormBuilder.instance_methods.grep(%r{_button$}),
|
16
|
+
*ActionView::Helpers::FormBuilder.instance_methods.grep(%r{_field$}),
|
17
|
+
*ActionView::Helpers::FormBuilder.instance_methods.grep(%r{_select$}),
|
18
|
+
]
|
19
|
+
|
20
|
+
TOGGLES = [
|
21
|
+
:check_box,
|
22
|
+
:radio_button,
|
23
|
+
]
|
24
|
+
|
25
|
+
#
|
26
|
+
# Wraps the contents of the block passed in a fieldset with optional
|
27
|
+
# +legend+ text.
|
28
|
+
#
|
29
|
+
def inputs(legend = nil, &block)
|
30
|
+
# TODO: don't wrap error fields in field_with_error divs
|
31
|
+
# ActionView::Base.field_error_proc = ->(html_tag, instance) { html_tag }
|
32
|
+
|
33
|
+
template.render(
|
34
|
+
:layout => 'twitter_bootstrap_form_for/inputs',
|
35
|
+
:locals => { :legend => legend },
|
36
|
+
&block
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Wraps groups of toggles (radio buttons, checkboxes) with a single label
|
42
|
+
# and the appropriate markup. All toggle buttons should be rendered
|
43
|
+
# inside of here, and will not look correct unless they are.
|
44
|
+
#
|
45
|
+
def toggles(label = nil, &block)
|
46
|
+
template.render(
|
47
|
+
:layout => 'twitter_bootstrap_form_for/toggles',
|
48
|
+
:locals => { :label => label },
|
49
|
+
&block
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Wraps action buttons into their own styled container.
|
55
|
+
#
|
56
|
+
def actions(&block)
|
57
|
+
template.render(
|
58
|
+
:layout => 'twitter_bootstrap_form_for/actions',
|
59
|
+
:locals => { },
|
60
|
+
&block
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
#
|
65
|
+
# Renders a submit tag with default classes to style it as a primary form
|
66
|
+
# button.
|
67
|
+
#
|
68
|
+
def submit(value = nil, options = {})
|
69
|
+
options[:class] ||= 'btn primary'
|
70
|
+
|
71
|
+
super value, options
|
72
|
+
end
|
73
|
+
|
74
|
+
INPUTS.each do |input|
|
75
|
+
define_method input do |attribute, *args, &block|
|
76
|
+
options = args.extract_options!
|
77
|
+
|
78
|
+
template.render(
|
79
|
+
:layout => 'twitter_bootstrap_form_for/input',
|
80
|
+
:locals => {
|
81
|
+
:form => self,
|
82
|
+
:attribute => attribute,
|
83
|
+
# only skip label if it's explicitly false
|
84
|
+
:label => args.first.nil? ? '' : args.shift,
|
85
|
+
:add_on => (options[:add_on] && 'input-' << options.delete(:add_on).to_s),
|
86
|
+
:block => block
|
87
|
+
}
|
88
|
+
) { super attribute, *(args << options) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
TOGGLES.each do |toggle|
|
93
|
+
define_method toggle do |attribute, *args, &block|
|
94
|
+
options = args.extract_options!
|
95
|
+
|
96
|
+
template.render(
|
97
|
+
:layout => 'twitter_bootstrap_form_for/toggle',
|
98
|
+
:locals => {
|
99
|
+
:form => self,
|
100
|
+
:attribute => attribute,
|
101
|
+
:label => args.first.nil? ? '' : args.shift,
|
102
|
+
}
|
103
|
+
) { super attribute, *(args << options) }
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def div_wrapper(attribute, options = {}, &block)
|
108
|
+
options[:id] = _wrapper_id attribute, options[:id]
|
109
|
+
options[:class] = _wrapper_classes attribute, options[:class]
|
110
|
+
|
111
|
+
template.content_tag(:div, options, &block)
|
112
|
+
end
|
113
|
+
|
114
|
+
def errors_on?(attribute)
|
115
|
+
self.object.errors[attribute].present?
|
116
|
+
end
|
117
|
+
|
118
|
+
def errors_for(attribute)
|
119
|
+
self.object.errors[attribute].try(:join, ', ')
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
#
|
125
|
+
# Returns an HTML id to uniquely identify the markup around an input field.
|
126
|
+
# If a +default+ is provided, it uses that one instead.
|
127
|
+
#
|
128
|
+
def _wrapper_id(attribute, default = nil)
|
129
|
+
default || [
|
130
|
+
_object_name + _object_index,
|
131
|
+
_attribute_name(attribute),
|
132
|
+
'input'
|
133
|
+
].join('_')
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
# Returns any classes necessary for the wrapper div around fields for
|
138
|
+
# +attribute+, such as 'errors' if any errors are present on the attribute.
|
139
|
+
# This merges any +classes+ passed in.
|
140
|
+
#
|
141
|
+
def _wrapper_classes(attribute, *classes)
|
142
|
+
classes.tap do |classes|
|
143
|
+
classes.push 'error' if self.errors_on?(attribute)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def _attribute_name(attribute)
|
148
|
+
attribute.to_s.gsub(/[\?\/\-]$/, '')
|
149
|
+
end
|
150
|
+
|
151
|
+
def _object_name
|
152
|
+
self.object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
153
|
+
end
|
154
|
+
|
155
|
+
def _object_index
|
156
|
+
case
|
157
|
+
when options.has_key?(:index) then options[:index]
|
158
|
+
when defined?(@auto_index) then @auto_index
|
159
|
+
else nil
|
160
|
+
end.to_s
|
161
|
+
end
|
162
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'twitter_bootstrap_form_for'
|
2
|
+
|
3
|
+
module TwitterBootstrapFormFor::FormHelpers
|
4
|
+
[:form_for, :fields_for].each do |method|
|
5
|
+
module_eval do
|
6
|
+
define_method "twitter_bootstrap_#{method}" do |record, *args, &block|
|
7
|
+
# add the TwitterBootstrap builder to the options
|
8
|
+
options = args.extract_options!
|
9
|
+
options[:builder] = TwitterBootstrapFormFor::FormBuilder
|
10
|
+
|
11
|
+
# call the original method with our overridden options
|
12
|
+
send method, record, *(args << options), &block
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'twitter_bootstrap_form_for'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
class TwitterBootstrapFormFor::Railtie < Rails::Railtie
|
5
|
+
initializer 'twitter_bootstrap_form_for.initialize',
|
6
|
+
:after => :after_initialize do
|
7
|
+
ActionView::Base.send :include, TwitterBootstrapFormFor::FormHelpers
|
8
|
+
ActionController::Base.append_view_path Pathname.new(__FILE__).join('../../../app/views')
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path('../lib/twitter_bootstrap_form_for/version', __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'twitter_bootstrap_form_for'
|
5
|
+
s.version = TwitterBootstrapFormFor::VERSION
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = [ 'Stephen Touset' ]
|
8
|
+
s.email = [ 'stephen@touset.org' ]
|
9
|
+
s.homepage = 'http://github.com/stouset/twitter_bootstrap_form_for'
|
10
|
+
s.summary = 'Rails form builder optimized for Twitter Bootstrap'
|
11
|
+
s.description = 'A custom Rails FormBuilder that assumes the use of Twitter Bootstrap'
|
12
|
+
|
13
|
+
# s.required_rubygems_version = '>= 1.8.0'
|
14
|
+
# s.rubyforge_project = 'twitter_bootstrap_form_for'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.executables = `git ls-files`.split("\n").map {|f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
|
18
|
+
s.require_path = 'lib'
|
19
|
+
|
20
|
+
s.add_dependency 'rails', '~> 3'
|
21
|
+
s.add_dependency 'haml', '~> 3'
|
22
|
+
# TODO: uncomment the next line
|
23
|
+
# s.add_dependency 'twitter-bootstrap', '~> 1.3'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: twitter_bootstrap_form_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.rc1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stephen Touset
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-30 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70272000133280 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70272000133280
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: haml
|
27
|
+
requirement: &70272000132780 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70272000132780
|
36
|
+
description: A custom Rails FormBuilder that assumes the use of Twitter Bootstrap
|
37
|
+
email:
|
38
|
+
- stephen@touset.org
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rvmrc
|
45
|
+
- Gemfile
|
46
|
+
- README.markdown
|
47
|
+
- app/views/twitter_bootstrap_form_for/_actions.html.haml
|
48
|
+
- app/views/twitter_bootstrap_form_for/_input.html.haml
|
49
|
+
- app/views/twitter_bootstrap_form_for/_inputs.html.haml
|
50
|
+
- app/views/twitter_bootstrap_form_for/_toggle.html.haml
|
51
|
+
- app/views/twitter_bootstrap_form_for/_toggles.html.haml
|
52
|
+
- lib/twitter_bootstrap_form_for.rb
|
53
|
+
- lib/twitter_bootstrap_form_for/form_builder.rb
|
54
|
+
- lib/twitter_bootstrap_form_for/form_helpers.rb
|
55
|
+
- lib/twitter_bootstrap_form_for/railtie.rb
|
56
|
+
- lib/twitter_bootstrap_form_for/version.rb
|
57
|
+
- twitter_bootstrap_form_for.gemspec
|
58
|
+
homepage: http://github.com/stouset/twitter_bootstrap_form_for
|
59
|
+
licenses: []
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>'
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.3.1
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.8
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Rails form builder optimized for Twitter Bootstrap
|
82
|
+
test_files: []
|