bootstraps_bootstraps 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/lib/bootstraps_bootstraps/bootstrap_form_builder.rb +140 -38
- data/lib/version.rb +1 -1
- metadata +3 -3
data/.gitignore
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
*.swp
|
2
|
-
|
2
|
+
*.gem
|
@@ -34,9 +34,13 @@ module BootstrapsBootstraps
|
|
34
34
|
@form_mode = :horizontal if options[:horizontal] || detect_html_class(options, 'form-horizontal')
|
35
35
|
@form_mode = :search if options[:search] || detect_html_class(options, 'form-search')
|
36
36
|
@form_mode = :inline if options[:inline] || detect_html_class(options, 'form-inline')
|
37
|
+
|
38
|
+
@action_wrapped = options[:action_wrapped]
|
37
39
|
end
|
38
40
|
|
39
|
-
|
41
|
+
|
42
|
+
#the main method constructor which takes care of most of the use cases
|
43
|
+
%w[text_field text_area password_field number_field telephone_field url_field email_field range_field file_field].each do |method_name|
|
40
44
|
define_method(method_name) do |name, *args|
|
41
45
|
options = args.extract_options!
|
42
46
|
|
@@ -45,17 +49,17 @@ module BootstrapsBootstraps
|
|
45
49
|
return super(name, *(args.push(options)))
|
46
50
|
end
|
47
51
|
|
48
|
-
errors =
|
49
|
-
error_msg = object.errors[name].any? ? content_tag(:span, object.errors[name].join(","), class: 'help-inline') : ''
|
52
|
+
errors, error_msg = render_errors name
|
50
53
|
|
51
|
-
|
54
|
+
label_options = options
|
55
|
+
label_options[:class] = options[:label_class] unless options[:label_class].nil?
|
56
|
+
label = options[:label] == false ? ''.html_safe : field_label(name, label_options)
|
52
57
|
|
53
|
-
|
54
|
-
|
55
|
-
options[:class] = [options[:class]] unless options[:class].kind_of?(Array)
|
56
|
-
options[:class].push 'xxlarge' if options[:large] || method_name == 'text_area'
|
58
|
+
guarantee_html_class options
|
59
|
+
options[:class].push 'input-xlarge' if options[:large] || method_name == 'text_area'
|
57
60
|
options[:class].push 'inline' if @form_mode == :inline
|
58
61
|
|
62
|
+
options.delete :label
|
59
63
|
args.push(options).unshift(name)
|
60
64
|
|
61
65
|
field = super(*args) + ' ' + error_msg
|
@@ -73,46 +77,96 @@ module BootstrapsBootstraps
|
|
73
77
|
end
|
74
78
|
end
|
75
79
|
|
76
|
-
|
77
|
-
def check_box(name, *args)
|
80
|
+
def check_box method, options = {}, checked_value = 1, unchecked_value = 0
|
78
81
|
if options[:vanilla]
|
79
|
-
return super
|
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]]
|
86
|
-
end
|
87
|
-
else
|
88
|
-
options[:class] = []
|
82
|
+
return super
|
89
83
|
end
|
90
84
|
|
91
|
-
options
|
85
|
+
guarantee_html_class options, :checkbox
|
86
|
+
options[:class].push 'inline' if @form_mode == :inline
|
92
87
|
|
93
88
|
text = options[:label] || ''
|
94
89
|
options.delete :label
|
95
90
|
|
96
|
-
|
91
|
+
field_label(method, options) do
|
92
|
+
super(method,options,checked_value,unchecked_value) + text
|
93
|
+
end
|
94
|
+
end
|
97
95
|
|
98
|
-
|
99
|
-
|
96
|
+
def collection_select method, collection, value_method, text_method, options = {}, html_options = {}
|
97
|
+
#abort and just let the rails formbuilder do its thing
|
98
|
+
if options[:vanilla]
|
99
|
+
return super
|
100
100
|
end
|
101
|
+
|
102
|
+
errors, error_msg = render_errors method
|
103
|
+
|
104
|
+
label = options[:label] == false ? ''.html_safe : field_label(method, html_options)
|
105
|
+
|
106
|
+
guarantee_html_class options
|
107
|
+
options[:class].push 'input-xlarge' if options[:large]
|
108
|
+
options[:class].push 'inline' if @form_mode == :inline
|
109
|
+
|
110
|
+
options.delete :label
|
111
|
+
|
112
|
+
field = super(method,collection,value_method,text_method,options,html_options) + ' ' + error_msg
|
113
|
+
|
114
|
+
#wrap it in div.controls
|
115
|
+
field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal
|
116
|
+
|
117
|
+
#tack on the label
|
118
|
+
field = label + field unless @form_mode == :inline
|
119
|
+
|
120
|
+
#wrap it in div.control-group
|
121
|
+
field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal
|
122
|
+
|
123
|
+
field
|
101
124
|
end
|
102
125
|
|
103
|
-
def
|
104
|
-
|
105
|
-
args = args.push options
|
126
|
+
def date_select method, options = {}, html_options = {}
|
127
|
+
field = super
|
106
128
|
|
107
|
-
if options[:vanilla]
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
129
|
+
return field if options[:vanilla]
|
130
|
+
|
131
|
+
errors, error_msg = render_errors method
|
132
|
+
|
133
|
+
#hmm, apparently TODO
|
134
|
+
help_text options
|
135
|
+
|
136
|
+
label = options[:label] == false ? ''.html_safe : field_label(method, options.except(:class))
|
137
|
+
|
138
|
+
guarantee_html_class html_options, 'inline'
|
139
|
+
|
140
|
+
field += ' '.html_safe + error_msg
|
141
|
+
field = div_with_class(['controls',errors], :content => field) if @form_mode == :horizontal
|
142
|
+
field = label + field
|
143
|
+
field = div_with_class(['control-group',errors], :content => field) if @form_mode == :horizontal
|
144
|
+
field
|
145
|
+
end
|
146
|
+
|
147
|
+
def radio_button method, value, options = {}
|
148
|
+
guarantee_html_class options, 'radio'
|
149
|
+
error_class, error_message = render_errors method
|
150
|
+
|
151
|
+
text = options[:label] || ''
|
152
|
+
options.delete :label
|
153
|
+
|
154
|
+
content_tag(:label, class: options[:class]) do
|
155
|
+
super(method,value,options) + text
|
113
156
|
end
|
114
157
|
end
|
115
158
|
|
159
|
+
def submit method, options = {}
|
160
|
+
# options block gets stringify_keys called on it in the first super, no :keys exist after that
|
161
|
+
field = super
|
162
|
+
return field if options['vanilla']
|
163
|
+
field = div_with_class('form-actions', content: field) unless options['no_action_block'] || @action_wrapped || [:inline, :search].include?(@form_mode)
|
164
|
+
puts @action_wrapped
|
165
|
+
field
|
166
|
+
end
|
167
|
+
|
168
|
+
|
169
|
+
#input groups
|
116
170
|
def inline_inputs field_options = {}, &block
|
117
171
|
field_options[:inline] = true
|
118
172
|
wrapper = lambda { |content|
|
@@ -123,15 +177,34 @@ module BootstrapsBootstraps
|
|
123
177
|
@template.wrapped_inputs(@object_name, @object, @options.merge(field_options), wrapper, &block)
|
124
178
|
end
|
125
179
|
|
180
|
+
def grouped_inputs field_options = {}, &block
|
181
|
+
wrapper = lambda do |content|
|
182
|
+
field_group = div_with_class('controls', content:content)
|
183
|
+
if field_options[:label]
|
184
|
+
field_group = label( field_options[:label], field_options[:label], class: 'control-label') + field_group
|
185
|
+
end
|
186
|
+
field_group = div_with_class('control-group', content:field_group)
|
187
|
+
end
|
188
|
+
|
189
|
+
@template.wrapped_inputs(@object_name, @object, @options, wrapper, &block)
|
190
|
+
end
|
191
|
+
|
192
|
+
def form_actions field_options = {}, &block
|
193
|
+
field_options[:action_wrapped] = true
|
194
|
+
wrapper = lambda { |content|
|
195
|
+
field_group = div_with_class('form-actions', content: content)
|
196
|
+
}
|
197
|
+
|
198
|
+
@template.wrapped_inputs(@object_name, @object, @options.merge(field_options), wrapper, &block)
|
199
|
+
end
|
200
|
+
|
126
201
|
private
|
127
202
|
|
128
203
|
def field_label(name, *args, &block)
|
129
204
|
options = args.extract_options!
|
130
|
-
|
131
|
-
|
132
|
-
required
|
133
|
-
clss.push 'required' if required
|
134
|
-
|
205
|
+
guarantee_html_class options
|
206
|
+
clss = options[:class]
|
207
|
+
clss.push 'required' if object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator}
|
135
208
|
|
136
209
|
label(name, options[:label], class: clss, &block)
|
137
210
|
end
|
@@ -154,6 +227,22 @@ module BootstrapsBootstraps
|
|
154
227
|
end
|
155
228
|
end
|
156
229
|
|
230
|
+
def guarantee_html_class options_hash, *new_classes
|
231
|
+
if options_hash[:class].nil?
|
232
|
+
options_hash[:class] = []
|
233
|
+
elsif options_hash[:class].kind_of?(Symbol)|| options_hash[:class].kind_of?(String)
|
234
|
+
options_hash[:class] = [ options_hash[:class] ]
|
235
|
+
end
|
236
|
+
|
237
|
+
unless new_classes.empty?
|
238
|
+
new_classes.each do |clss|
|
239
|
+
options_hash[:class].push clss
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
options_hash
|
244
|
+
end
|
245
|
+
|
157
246
|
def div_with_class clss, options = {}, &block
|
158
247
|
options[:class] = [options[:class]] || []
|
159
248
|
options[:class].push clss
|
@@ -172,6 +261,19 @@ module BootstrapsBootstraps
|
|
172
261
|
end
|
173
262
|
end
|
174
263
|
|
264
|
+
def render_errors method
|
265
|
+
if object.nil?
|
266
|
+
return ['','']
|
267
|
+
end
|
268
|
+
|
269
|
+
errors = object.errors[method].any? ? 'error' : ''
|
270
|
+
error_msg = object.errors[method].any? ? content_tag(:span, object.errors[method].join(","), class: 'help-inline') : ''
|
271
|
+
|
272
|
+
[errors, error_msg]
|
273
|
+
end
|
175
274
|
|
275
|
+
def help_text options
|
276
|
+
options[:help_text].blank? ? '' : content_tag(:span,options[:help_text], class: 'help-block')
|
277
|
+
end
|
176
278
|
end
|
177
279
|
end
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 2
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 2.0.
|
8
|
+
- 1
|
9
|
+
version: 2.0.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Robert L. Carpenter
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2012-
|
17
|
+
date: 2012-03-20 00:00:00 -06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|