sinatra-formhelpers-ng 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -5
- data/VERSION +1 -1
- data/lib/sinatra/form_helpers.rb +19 -24
- data/{sinatra-formhelpers.gemspec → sinatra-formhelpers-ng.gemspec} +8 -8
- data/spec/form_helpers_spec.rb +70 -34
- data/spec/spec_helper.rb +2 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -22,7 +22,7 @@ Usage
|
|
22
22
|
-----
|
23
23
|
With Bundler/Isolate:
|
24
24
|
|
25
|
-
gem 'sinatra-formhelpers'
|
25
|
+
gem 'sinatra-formhelpers-ng'
|
26
26
|
|
27
27
|
Then, include it in a Sinatra application:
|
28
28
|
|
@@ -83,10 +83,16 @@ This will create fields named <code>user[first\_name]</code>, <code>user[last\_n
|
|
83
83
|
|
84
84
|
Known Bugs
|
85
85
|
----------
|
86
|
-
* Currently <code>fieldset</code> does not return a <fieldset> tag properly
|
86
|
+
* Currently <code>fieldset</code> does not return a <fieldset> tag properly.
|
87
87
|
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
*
|
89
|
+
Fixed Bugs
|
90
|
+
----------
|
91
|
+
* The state of select tags was not persisted across form submissions.
|
92
|
+
|
93
|
+
|
94
|
+
Authors
|
95
|
+
-------
|
96
|
+
* [Initial efforts](https://github.com/twilson63/sinatra-formhelpers) (c) 2009 [Tom Wilson](https://github.com/twilson63).
|
97
|
+
* [Additional efforts](https://github.com/nateware/sinatra-formhelpers) (c) 2011 [Nate Wiger](http://nateware.com).
|
92
98
|
* Further efforts (c) 2013 [Cymen Vig](http://blog.cymen.org/).
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.6.0
|
data/lib/sinatra/form_helpers.rb
CHANGED
@@ -3,13 +3,13 @@ module Sinatra
|
|
3
3
|
# FormHelpers are a suite of helper methods
|
4
4
|
# built to make building forms in Sinatra
|
5
5
|
# a breeze.
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# link "jackhq", "http://www.jackhq.com"
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# label :person, :first_name
|
10
10
|
# input :person, :first_name
|
11
11
|
# textarea :person, :notes
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# etc.
|
14
14
|
def form(action, method=:get, options={}, &block)
|
15
15
|
method_input = ''
|
@@ -34,10 +34,10 @@ module Sinatra
|
|
34
34
|
out = yield Fieldset.new(self, obj)
|
35
35
|
'<fieldset>' + (legend.nil? ? '' : "<legend>#{fast_escape_html(legend)}</legend>") + out + '</fieldset>'
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
# Link to a URL
|
39
39
|
def link(content, href=content, options={})
|
40
|
-
tag :a, content, options.merge(:href => href)
|
40
|
+
tag :a, content, options.merge(:href => href)
|
41
41
|
end
|
42
42
|
|
43
43
|
# Link to an image
|
@@ -54,7 +54,8 @@ module Sinatra
|
|
54
54
|
def input(obj, field=nil, options={})
|
55
55
|
value = param_or_default(obj, field, options[:value])
|
56
56
|
single_tag :input, options.merge(
|
57
|
-
:type => "text",
|
57
|
+
:type => options[:type] || "text",
|
58
|
+
:id => css_id(obj, field),
|
58
59
|
:name => field.nil? ? obj : "#{obj}[#{field}]",
|
59
60
|
:value => value
|
60
61
|
)
|
@@ -62,12 +63,7 @@ module Sinatra
|
|
62
63
|
|
63
64
|
# Form password input. Specify the value as :value => 'foo'
|
64
65
|
def password(obj, field=nil, options={})
|
65
|
-
|
66
|
-
single_tag :input, options.merge(
|
67
|
-
:type => "password", :id => css_id(obj, field),
|
68
|
-
:name => field.nil? ? obj : "#{obj}[#{field}]",
|
69
|
-
:value => value
|
70
|
-
)
|
66
|
+
input(obj, field, options.merge(:type => 'password'))
|
71
67
|
end
|
72
68
|
|
73
69
|
# Form textarea box.
|
@@ -111,10 +107,10 @@ module Sinatra
|
|
111
107
|
(labs.nil? || labs == true ? label(obj, "#{field}_#{id.to_s.downcase}", text) : '')
|
112
108
|
end.join(join)
|
113
109
|
end
|
114
|
-
|
110
|
+
|
115
111
|
# Form radio input. Specify an array of values to get a radio group.
|
116
112
|
def radio(obj, field, values, options={})
|
117
|
-
#content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""
|
113
|
+
#content = @params[obj] && @params[obj][field.to_s] == value ? "true" : ""
|
118
114
|
# , :checked => content
|
119
115
|
join = options.delete(:join) || ' '
|
120
116
|
labs = options.delete(:label)
|
@@ -140,33 +136,32 @@ module Sinatra
|
|
140
136
|
end
|
141
137
|
tag :select, content, options.merge(:id => css_id(obj, field), :name => "#{obj}[#{field}]")
|
142
138
|
end
|
143
|
-
|
139
|
+
|
144
140
|
# Form hidden input. Specify value as :value => 'foo'
|
145
|
-
def hidden(obj, field=
|
146
|
-
|
147
|
-
single_tag :input, options.merge(:type => "hidden", :id => css_id(obj, field), :name => "#{obj}[#{field}]")
|
141
|
+
def hidden(obj, field = nil, options = {})
|
142
|
+
input(obj, field, options.merge(:type => 'hidden'))
|
148
143
|
end
|
149
|
-
|
144
|
+
|
150
145
|
# Standard open and close tags
|
151
146
|
# EX : tag :h1, "shizam", :title => "shizam"
|
152
147
|
# => <h1 title="shizam">shizam</h1>
|
153
148
|
def tag(name, content, options={})
|
154
149
|
"<#{name.to_s}" +
|
155
|
-
(options.length > 0 ? " #{hash_to_html_attrs(options)}" : '') +
|
150
|
+
(options.length > 0 ? " #{hash_to_html_attrs(options)}" : '') +
|
156
151
|
(content.nil? ? '>' : ">#{content}</#{name}>")
|
157
152
|
end
|
158
|
-
|
153
|
+
|
159
154
|
# Standard single closing tags
|
160
155
|
# single_tag :img, :src => "images/google.jpg"
|
161
156
|
# => <img src="images/google.jpg" />
|
162
157
|
def single_tag(name, options={})
|
163
158
|
"<#{name.to_s} #{hash_to_html_attrs(options)} />"
|
164
159
|
end
|
165
|
-
|
160
|
+
|
166
161
|
def fast_escape_html(text)
|
167
162
|
text.to_s.gsub(/\&/,'&').gsub(/\"/,'"').gsub(/>/,'>').gsub(/</,'<')
|
168
163
|
end
|
169
|
-
|
164
|
+
|
170
165
|
def titleize(text)
|
171
166
|
text.to_s.gsub(/_+/, ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
|
172
167
|
end
|
@@ -195,7 +190,7 @@ module Sinatra
|
|
195
190
|
[val, val]
|
196
191
|
end
|
197
192
|
end
|
198
|
-
|
193
|
+
|
199
194
|
def css_id(*things)
|
200
195
|
things.compact.map{|t| t.to_s}.join('_').downcase.gsub(/\W/,'_')
|
201
196
|
end
|
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name = "sinatra-formhelpers"
|
8
|
-
s.version = "
|
7
|
+
s.name = "sinatra-formhelpers-ng"
|
8
|
+
s.version = "1.6.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Nate Wiger", "Cymen Vig"]
|
12
|
-
s.date = "2013-03
|
13
|
-
s.description = "Simple, lightweight form helpers for Sinatra.
|
14
|
-
s.email = "
|
11
|
+
s.authors = ["twilson63", "Nate Wiger", "Cymen Vig"]
|
12
|
+
s.date = "2013-05-03"
|
13
|
+
s.description = "Simple, lightweight form helpers for Sinatra."
|
14
|
+
s.email = "cymenvig@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.md"
|
@@ -25,11 +25,11 @@ Gem::Specification.new do |s|
|
|
25
25
|
"Rakefile",
|
26
26
|
"VERSION",
|
27
27
|
"lib/sinatra/form_helpers.rb",
|
28
|
-
"sinatra-formhelpers.gemspec",
|
28
|
+
"sinatra-formhelpers-ng.gemspec",
|
29
29
|
"spec/form_helpers_spec.rb",
|
30
30
|
"spec/spec_helper.rb"
|
31
31
|
]
|
32
|
-
s.homepage = "http://github.com/
|
32
|
+
s.homepage = "http://github.com/cymen/sinatra-formhelpers"
|
33
33
|
s.require_paths = ["lib"]
|
34
34
|
s.rubygems_version = "1.8.25"
|
35
35
|
s.summary = "Form helpers for Sinatra"
|
data/spec/form_helpers_spec.rb
CHANGED
@@ -2,15 +2,15 @@ require File.expand_path 'spec_helper', File.dirname(__FILE__)
|
|
2
2
|
|
3
3
|
# class Application < Sinatra::Base
|
4
4
|
# register Sinatra::FormHelpers
|
5
|
-
#
|
5
|
+
#
|
6
6
|
# set :raise_errors, false
|
7
7
|
# set :show_exceptions, false
|
8
|
-
#
|
8
|
+
#
|
9
9
|
# get '/link' do
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# end
|
12
12
|
# end
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# class Bacon::Context
|
15
15
|
# include Rack::Test::Methods
|
16
16
|
# def app
|
@@ -38,7 +38,7 @@ describe "Sinatra::FormHelpers methods" do
|
|
38
38
|
fh.form(:person, :create) do |f|
|
39
39
|
# f.input(:first_name)
|
40
40
|
f.input(:last_name)
|
41
|
-
end.should == '<form action="/person" method="POST"><fieldset>' +
|
41
|
+
end.should == '<form action="/person" method="POST"><fieldset>' +
|
42
42
|
# '<input id="person_first_name" name="person[first_name]" type="text" />' +
|
43
43
|
'<input id="person_last_name" name="person[last_name]" type="text" />' +
|
44
44
|
'</fieldset></form>'
|
@@ -83,7 +83,7 @@ describe "Sinatra::FormHelpers methods" do
|
|
83
83
|
end
|
84
84
|
it 'supports multiple values for checkboxes' do
|
85
85
|
fh.params = {:user => {'devices' => ['iPhone', 'iPad'] }}
|
86
|
-
fh.checkbox(:user, :devices, ['iPhone', 'iPad', 'iPod', 'iPoop']).should ==
|
86
|
+
fh.checkbox(:user, :devices, ['iPhone', 'iPad', 'iPod', 'iPoop']).should ==
|
87
87
|
"<input checked=\"checked\" id=\"user_devices_iphone\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPhone\" /><label for=\"user_devices_iphone\">iPhone</label> <input checked=\"checked\" id=\"user_devices_ipad\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPad\" /><label for=\"user_devices_ipad\">iPad</label> <input id=\"user_devices_ipod\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPod\" /><label for=\"user_devices_ipod\">iPod</label> <input id=\"user_devices_ipoop\" name=\"user[devices][]\" type=\"checkbox\" value=\"iPoop\" /><label for=\"user_devices_ipoop\">iPoop</label>"
|
88
88
|
end
|
89
89
|
end
|
@@ -102,7 +102,7 @@ describe "Sinatra::FormHelpers in app" do
|
|
102
102
|
app.get '/label' do
|
103
103
|
erb "<%= label :person, :first_name %>"
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
get '/label'
|
107
107
|
last_response.body.should == %q(<label for="person_first_name">First Name</label>)
|
108
108
|
end
|
@@ -111,11 +111,11 @@ describe "Sinatra::FormHelpers in app" do
|
|
111
111
|
app.get '/hello' do
|
112
112
|
erb "<%= label :person, :first_name, 'Hello World'%>"
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
get '/hello'
|
116
116
|
last_response.body.should == %q(<label for="person_first_name">Hello World</label>)
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
it 'renders an input tag type text without @params' do
|
120
120
|
app.get '/text' do
|
121
121
|
erb "<%= input :person, :first_name %>"
|
@@ -126,34 +126,61 @@ describe "Sinatra::FormHelpers in app" do
|
|
126
126
|
last_response.body.should == %q(<input id="person_first_name" name="person[first_name]" type="text" />)
|
127
127
|
end
|
128
128
|
|
129
|
+
it 'accepts an input tag with custom type option' do
|
130
|
+
app.get '/input-with-custom-type' do
|
131
|
+
erb "<%= input :person, :password, type: 'password' %>"
|
132
|
+
end
|
133
|
+
|
134
|
+
get '/input-with-custom-type'
|
135
|
+
last_response.body.should == '<input id="person_password" name="person[password]" type="password" />'
|
136
|
+
end
|
137
|
+
|
129
138
|
it 'renders an input tag type text with single arg' do
|
130
139
|
app.get '/q' do
|
131
140
|
erb "<%= input :q %>"
|
132
141
|
end
|
133
|
-
|
142
|
+
|
134
143
|
get '/q'
|
135
144
|
last_response.body.should == %q(<input id="q" name="q" type="text" />)
|
136
145
|
end
|
137
|
-
|
146
|
+
|
138
147
|
it 'renders an input tag type text with @params' do
|
139
148
|
app.get '/tom' do
|
140
149
|
@params = { :person => {"first_name" => "Tom"}}
|
141
150
|
erb "<%= input :person, :first_name %>"
|
142
151
|
end
|
143
|
-
|
152
|
+
|
144
153
|
get '/tom'
|
145
154
|
last_response.body.should == %q(<input id="person_first_name" name="person[first_name]" type="text" value="Tom" />)
|
146
155
|
end
|
147
|
-
|
156
|
+
|
157
|
+
it 'renders a password tag type password' do
|
158
|
+
app.get '/password' do
|
159
|
+
erb "<%= password :person, :password %>"
|
160
|
+
end
|
161
|
+
|
162
|
+
get '/password'
|
163
|
+
last_response.body.should == '<input id="person_password" name="person[password]" type="password" />'
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'renders a button tag type button' do
|
167
|
+
app.get '/button' do
|
168
|
+
erb "<%= button :new %>"
|
169
|
+
end
|
170
|
+
|
171
|
+
get '/button'
|
172
|
+
last_response.body.should == '<input id="button_new" name="button" type="button" value="new" />'
|
173
|
+
end
|
174
|
+
|
148
175
|
it 'renders an textarea tag type text without @params' do
|
149
176
|
app.get '/notes' do
|
150
177
|
erb "<%= textarea :person, :notes %>"
|
151
178
|
end
|
152
|
-
|
179
|
+
|
153
180
|
get '/notes?person[notes]=Yeppers'
|
154
181
|
last_response.body.should == %q(<textarea id="person_notes" name="person[notes]">Yeppers</textarea>)
|
155
182
|
end
|
156
|
-
|
183
|
+
|
157
184
|
it 'renders a textarea tag with @params' do
|
158
185
|
app.get '/notes2' do
|
159
186
|
@params = { :person => {"notes" => "This is a note"}}
|
@@ -163,12 +190,12 @@ describe "Sinatra::FormHelpers in app" do
|
|
163
190
|
get '/notes2'
|
164
191
|
last_response.body.should == %q(<textarea id="person_notes" name="person[notes]">This is a note</textarea>)
|
165
192
|
end
|
166
|
-
|
193
|
+
|
167
194
|
it 'renders a textarea tag with @params' do
|
168
195
|
app.get '/img' do
|
169
196
|
erb "<%= image '/images/hello.png', :alt => 'Lolcatz' %>"
|
170
197
|
end
|
171
|
-
|
198
|
+
|
172
199
|
get '/img'
|
173
200
|
last_response.body.should == '<img alt="Lolcatz" src="/images/hello.png" />'
|
174
201
|
end
|
@@ -177,25 +204,25 @@ describe "Sinatra::FormHelpers in app" do
|
|
177
204
|
app.get '/sub' do
|
178
205
|
erb "<%= submit 'Create' %>"
|
179
206
|
end
|
180
|
-
|
207
|
+
|
181
208
|
get '/sub'
|
182
209
|
last_response.body.should == '<input id="button_create" name="submit" type="submit" value="Create" />'
|
183
210
|
end
|
184
|
-
|
211
|
+
|
185
212
|
it 'renders an input tag with a submit type with zero args' do
|
186
213
|
app.get '/create' do
|
187
214
|
erb "<%= submit %>"
|
188
215
|
end
|
189
|
-
|
216
|
+
|
190
217
|
get '/create'
|
191
218
|
last_response.body.should == '<input id="button_submit" name="submit" type="submit" value="Submit" />'
|
192
219
|
end
|
193
|
-
|
220
|
+
|
194
221
|
it 'renders an input tag with a checkbox type' do
|
195
222
|
app.get '/check' do
|
196
223
|
erb "<%= checkbox :person, :active, 'Yes' %>"
|
197
224
|
end
|
198
|
-
|
225
|
+
|
199
226
|
get '/check'
|
200
227
|
last_response.body.should ==
|
201
228
|
'<input id="person_active_yes" name="person[active]" type="checkbox" value="Yes" /><label for="person_active_yes">Yes</label>'
|
@@ -229,39 +256,48 @@ describe "Sinatra::FormHelpers in app" do
|
|
229
256
|
app.get '/select' do
|
230
257
|
erb "<%= select :person, :relationship, ['Friend','CoWorker','Lead'] %>"
|
231
258
|
end
|
232
|
-
|
259
|
+
|
233
260
|
get '/select'
|
234
261
|
last_response.body.should == '<select id="person_relationship" name="person[relationship]">' +
|
235
|
-
'<option value="Friend">Friend</option><option value="CoWorker">CoWorker</option>' +
|
262
|
+
'<option value="Friend">Friend</option><option value="CoWorker">CoWorker</option>' +
|
236
263
|
'<option value="Lead">Lead</option></select>'
|
237
264
|
end
|
238
|
-
|
265
|
+
|
239
266
|
it 'renders a select tag with selected option' do
|
240
267
|
app.get '/select2' do
|
241
268
|
@params = { :person => {"relationship" => "CoWorker"}}
|
242
269
|
erb "<%= select :person, :relationship, ['Friend','CoWorker','Lead'] %>"
|
243
270
|
end
|
244
|
-
|
271
|
+
|
245
272
|
get '/select2'
|
246
273
|
last_response.body.should == '<select id="person_relationship" name="person[relationship]">' +
|
247
|
-
'<option value="Friend">Friend</option><option selected="selected" value="CoWorker">CoWorker</option>' +
|
274
|
+
'<option value="Friend">Friend</option><option selected="selected" value="CoWorker">CoWorker</option>' +
|
248
275
|
'<option value="Lead">Lead</option></select>'
|
249
276
|
end
|
250
|
-
|
277
|
+
|
278
|
+
it 'renders a hidden tag with single arg' do
|
279
|
+
app.get '/hidden' do
|
280
|
+
erb "<%= hidden :q %>"
|
281
|
+
end
|
282
|
+
|
283
|
+
get '/hidden'
|
284
|
+
last_response.body.should == %q(<input id="q" name="q" type="hidden" />)
|
285
|
+
end
|
286
|
+
|
251
287
|
it 'renders a hidden tag with value' do
|
252
|
-
app.get '/
|
288
|
+
app.get '/hidden2' do
|
253
289
|
erb '<%= hidden :person, :id, :value => 1 %>'
|
254
290
|
end
|
255
|
-
|
256
|
-
get '/
|
291
|
+
|
292
|
+
get '/hidden2'
|
257
293
|
last_response.body.should == '<input id="person_id" name="person[id]" type="hidden" value="1" />'
|
258
294
|
end
|
259
|
-
|
295
|
+
|
260
296
|
it 'renders a form tag' do
|
261
297
|
app.get '/form' do
|
262
298
|
erb "<%= form :person, :create %>"
|
263
299
|
end
|
264
|
-
|
300
|
+
|
265
301
|
get '/form'
|
266
302
|
last_response.body.should == %q(<form action="/person" method="POST">)
|
267
303
|
end
|
@@ -276,7 +312,7 @@ describe "Sinatra::FormHelpers in app" do
|
|
276
312
|
# <% end %>
|
277
313
|
# EndTemplate
|
278
314
|
# end
|
279
|
-
#
|
315
|
+
#
|
280
316
|
# get '/form_for'
|
281
317
|
# last_response.body.should == %q(<form action="/person" method="POST">)
|
282
318
|
# end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-formhelpers-ng
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: bacon
|
@@ -77,7 +77,7 @@ files:
|
|
77
77
|
- Rakefile
|
78
78
|
- VERSION
|
79
79
|
- lib/sinatra/form_helpers.rb
|
80
|
-
- sinatra-formhelpers.gemspec
|
80
|
+
- sinatra-formhelpers-ng.gemspec
|
81
81
|
- spec/form_helpers_spec.rb
|
82
82
|
- spec/spec_helper.rb
|
83
83
|
homepage: http://github.com/cymen/sinatra-formhelpers
|
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
94
|
version: '0'
|
95
95
|
segments:
|
96
96
|
- 0
|
97
|
-
hash:
|
97
|
+
hash: 1135526224546882131
|
98
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
99
|
none: false
|
100
100
|
requirements:
|