bootstrap_form_helper 2.0.2.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,183 @@
1
+ require 'action_view/helpers/form_helper'
2
+ require 'bootstrap_form_helper'
3
+
4
+ class BootstrapFormHelper::FormBuilder < ActionView::Helpers::FormBuilder
5
+
6
+ INPUTS = (%w(select collection_select grouped_collection_select time_zone_select).map(&:to_sym) +
7
+ field_helpers -
8
+ %w(label check_box radio_button fields_for hidden_field).map(&:to_sym))
9
+
10
+ ##
11
+ # Wraps the contents of the block passed to the method in a +fieldset+ tag with
12
+ # a optional +legend+ text.
13
+ #
14
+ def fieldset(legend = nil, options = {})
15
+ @template.content_tag(:fieldset, options) do
16
+ @template.concat @template.content_tag(:legend, legend) unless legend.nil?
17
+ yield
18
+ end
19
+ end
20
+
21
+ ##
22
+ # Wraps action buttons into their own styled container.
23
+ #
24
+ def actions(&block)
25
+ @template.content_tag(:div, class: 'form-actions', &block)
26
+ end
27
+
28
+ INPUTS.each do |input|
29
+ define_method input do |attribute, *args, &block|
30
+ options = args.extract_options!
31
+ text = args.any? ? args.shift : ''
32
+
33
+ label attribute, text do |builder|
34
+ builder.send input, attribute, *(args << options), &block
35
+ end
36
+ end
37
+ end
38
+
39
+ ##
40
+ # Attaches a label to the inputs rendered inside of the block passed to it.
41
+ # Associates the label with the input for the +attribute+ given. If +text+
42
+ # is passed, uses that as the text for the label; otherwise humanizes the
43
+ # +attribute+ name.
44
+ #
45
+ def label(attribute, text = '', options = {}, &block)
46
+ text, attribute = attribute, nil if attribute.kind_of? String
47
+
48
+ options = { class: 'control-label' }.merge(options)
49
+ id = wrapper_id attribute, 'control_group'
50
+ classes = wrapper_classes attribute, 'control-group'
51
+ fields_options = options.merge(builder: BootstrapFormHelper::FormControls)
52
+
53
+ @template.content_tag(:div, id: id, class: classes) do
54
+ @template.concat case
55
+ when attribute && text then super(attribute, text, options, &nil)
56
+ when attribute then super(attribute, nil, options, &nil)
57
+ when text then @template.label_tag(nil, text, options, &nil)
58
+ end
59
+
60
+ @template.concat @template.content_tag(:div, class: 'controls') {
61
+ @template.fields_for object_name, object, fields_options, &block
62
+ }
63
+ end
64
+ end
65
+
66
+ ##
67
+ # Renders a button with default classes to style it as a form button.
68
+ #
69
+ def button(value = nil, options = {})
70
+ super value, { type: 'button', class: 'btn' }.merge(options)
71
+ end
72
+
73
+ ##
74
+ # Renders a submit tag with default classes to style it as a primary form
75
+ # button.
76
+ #
77
+ def submit(value = nil, options = {})
78
+ button value, { type: 'submit', class: 'btn btn-primary' }.merge(options)
79
+ end
80
+
81
+ private
82
+
83
+ def errors_on?(attribute)
84
+ object.errors[attribute].present? if object.respond_to?(:errors)
85
+ end
86
+
87
+ def wrapper_id(attribute, suffix = nil)
88
+ [
89
+ bootstrap_object_name + object_index,
90
+ attribute_name(attribute),
91
+ suffix,
92
+ ].compact.join('_') if attribute
93
+ end
94
+
95
+ def wrapper_classes(attribute, *classes)
96
+ classes.push 'error' if attribute and errors_on? attribute
97
+ classes.join(' ')
98
+ end
99
+
100
+ def attribute_name(attribute)
101
+ attribute.to_s.sub(/[\?\/\-]$/, '')
102
+ end
103
+
104
+ def bootstrap_object_name
105
+ object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, '_').sub(/_$/, '')
106
+ end
107
+
108
+ def object_index
109
+ case
110
+ when options.has_key?(:index) then options[:index]
111
+ when defined?(@auto_index) then @auto_index
112
+ else nil
113
+ end.to_s
114
+ end
115
+ end
116
+
117
+ class BootstrapFormHelper::FormControls < ActionView::Helpers::FormBuilder
118
+ BootstrapFormHelper::FormBuilder::INPUTS.each do |input|
119
+ define_method input do |attribute, *args, &block|
120
+ options = args.extract_options!
121
+ input_prepend = options.delete :input_prepend
122
+ input_append = options.delete :input_append
123
+
124
+ @template.capture do
125
+ if input_prepend.present? || input_append.present?
126
+ classes = []
127
+ classes << 'input-prepend' if input_prepend.present?
128
+ classes << 'input-append' if input_append.present?
129
+ classes = classes.join(' ')
130
+ @template.concat @template.content_tag(:div, class: classes) {
131
+ if input_prepend.present?
132
+ @template.concat @template.content_tag(:span, input_prepend, class: 'add-on')
133
+ end
134
+ @template.concat super(attribute, *(args << options))
135
+ if input_append.present?
136
+ @template.concat @template.content_tag(:span, input_append, class: 'add-on')
137
+ end
138
+ }
139
+ else
140
+ @template.concat super(attribute, *(args << options))
141
+ end
142
+ @template.concat error_span(attribute) if errors_on? attribute
143
+ block.call if block.present?
144
+ end
145
+ end
146
+ end
147
+
148
+ def check_box(attribute, text, options = {}, checked_value = 1, unchecked_value = 0)
149
+ klasses = 'checkbox'
150
+ klasses += ' inline' if options.delete :inline
151
+
152
+ label attribute, class: klasses do
153
+ template.concat super(attribute, options, checked_value, unchecked_value)
154
+ template.concat text
155
+ yield if block_given?
156
+ end
157
+ end
158
+
159
+ def radio_button(attribute, value, text = nil, options = {})
160
+ klasses = 'radio'
161
+ klasses += ' inline' if options.delete :inline
162
+
163
+ label attribute, class: klasses do
164
+ template.concat super(attribute, value, options)
165
+ template.concat text || value.to_s.humanize.titleize
166
+ yield if block_given?
167
+ end
168
+ end
169
+
170
+ private
171
+
172
+ def error_span(attribute)
173
+ @template.content_tag :span, errors_for(attribute), class: 'help-inline'
174
+ end
175
+
176
+ def errors_for(attribute)
177
+ object.errors[attribute].try(:join, ', ')
178
+ end
179
+
180
+ def errors_on?(attribute)
181
+ object.errors[attribute].present? if object.respond_to?(:errors)
182
+ end
183
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+ require 'bootstrap_form_helper'
3
+
4
+ module BootstrapFormHelper::FormHelper
5
+ def bootstrap_form_for(*args, &proc)
6
+ options = args.extract_options!
7
+ options.merge!(builder: BootstrapFormHelper::FormBuilder)
8
+ set_noop_field_error_proc do
9
+ form_for(*(args << options), &proc)
10
+ end
11
+ end
12
+
13
+ def bootstrap_fields_for(*args, &proc)
14
+ options = args.extract_options!
15
+ options.merge!(builder: BootstrapFormHelper::FormBuilder)
16
+ set_noop_field_error_proc do
17
+ fields_for(*(args << options), &proc)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ NOOP_FIELD_ERROR_PROC = Proc.new { |x| x }
24
+
25
+ def set_noop_field_error_proc
26
+ saved = ActionView::Base.field_error_proc
27
+ ActionView::Base.field_error_proc = NOOP_FIELD_ERROR_PROC
28
+ yield
29
+ ensure
30
+ ActionView::Base.field_error_proc = saved
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ require 'action_view'
2
+ require 'bootstrap_form_helper'
3
+ require 'rails/railtie'
4
+
5
+ class BootstrapFormHelper::Railtie < Rails::Railtie
6
+ config.after_initialize do
7
+ ActionView::Base.send :include, BootstrapFormHelper::FormHelper
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ module BootstrapFormHelper
2
+ autoload :FormBuilder, 'bootstrap_form_helper/form_builder'
3
+ autoload :FormHelper, 'bootstrap_form_helper/form_helper'
4
+ autoload :Railtie, 'bootstrap_form_helper/railtie'
5
+ end
6
+
7
+ BootstrapFormHelper::Railtie
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap_form_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.2.0.beta1
5
+ prerelease: 8
6
+ platform: ruby
7
+ authors:
8
+ - Ehren Kret
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: '4'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: '4'
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionpack
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '3'
44
+ - - <
45
+ - !ruby/object:Gem::Version
46
+ version: '4'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - - <
56
+ - !ruby/object:Gem::Version
57
+ version: '4'
58
+ description: ! 'A rails form helper and builder for use with Twitter''s Bootstrap.
59
+ It wraps
60
+
61
+ form elements with the DOM necessary to render them correctly using
62
+
63
+ Bootstrap and automatically renders validation errors using Bootstrap.
64
+
65
+ '
66
+ email: ehren.kret@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/bootstrap_form_helper.rb
72
+ - lib/bootstrap_form_helper/form_builder.rb
73
+ - lib/bootstrap_form_helper/form_helper.rb
74
+ - lib/bootstrap_form_helper/railtie.rb
75
+ homepage: https://github.com/eakret/bootstrap_form_helper
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '1.9'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>'
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.1
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Rails form helper for Twitter's Bootstrap
99
+ test_files: []