sinatra_more 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +12 -0
- data/TODO +3 -2
- data/VERSION +1 -1
- data/lib/sinatra_more/markup_plugin/form_helpers.rb +19 -0
- data/lib/sinatra_more/markup_plugin/tag_helpers.rb +8 -1
- data/sinatra_more.gemspec +1 -1
- data/test/fixtures/markup_app/views/form_tag.erb +6 -0
- data/test/fixtures/markup_app/views/form_tag.haml +6 -1
- data/test/markup_plugin/test_form_helpers.rb +58 -4
- data/test/markup_plugin/test_tag_helpers.rb +5 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -167,6 +167,11 @@ methods should be very familiar to anyone who has used rails view helpers.
|
|
167
167
|
* <tt>radio_button_tag(name, options={})</tt>
|
168
168
|
* Constructs a radio button input from the given options
|
169
169
|
* <tt>radio_button_tag :gender, :value => 'male'</tt>
|
170
|
+
* <tt>select_tag(name, settings={})</tt>
|
171
|
+
* Constructs a select tag with options from the given settings
|
172
|
+
* <tt>select_tag(:favorite_color, :options => ['1', '2', '3'], :selected => '1')</tt>
|
173
|
+
* <tt>select_tag(:more_color, :options => [['label', '1'], ['label2', '2']])</tt>
|
174
|
+
* <tt>select_tag(:multiple_color, :options => [...], :multiple => true)</tt>
|
170
175
|
* <tt>file_field_tag(name, options={})</tt>
|
171
176
|
* Constructs a file field input from the given options
|
172
177
|
* <tt>file_field_tag :photo, :class => 'long'</tt>
|
@@ -188,6 +193,9 @@ A form_tag might look like:
|
|
188
193
|
%p
|
189
194
|
= label_tag :password, :class => 'first'
|
190
195
|
= password_field_tag :password, :value => params[:password]
|
196
|
+
%p
|
197
|
+
= label_tag :strategy
|
198
|
+
= select_tag :strategy, :options => ['delete', 'destroy'], :selected => 'delete'
|
191
199
|
%p
|
192
200
|
= check_box_tag :confirm_delete
|
193
201
|
- field_set_tag(:class => 'buttons') do
|
@@ -225,6 +233,8 @@ The following are fields provided by AbstractFormBuilder that can be used within
|
|
225
233
|
* <tt>f.file_field :photo, :class => 'long'</tt>
|
226
234
|
* <tt>submit(caption, options={})</tt>
|
227
235
|
* <tt>f.submit "Update", :class => 'long'</tt>
|
236
|
+
* <tt>image_submit(source, options={})</tt>
|
237
|
+
* <tt>f.submit "submit.png", :class => 'long'</tt>
|
228
238
|
|
229
239
|
A form_for using these basic fields might look like:
|
230
240
|
|
@@ -257,6 +267,8 @@ There is also a StandardFormBuilder which builds on the abstract fields that can
|
|
257
267
|
* <tt>file_field_block(:photo, :class => 'big')</tt>
|
258
268
|
* <tt>submit_block(caption, options={})</tt>
|
259
269
|
* <tt>submit_block(:username, :class => 'big')</tt>
|
270
|
+
* <tt>image_submit_block(source, options={})</tt>
|
271
|
+
* <tt>image_submit_block('submit.png', :class => 'big')</tt>
|
260
272
|
|
261
273
|
A form_for using these standard fields might look like:
|
262
274
|
|
data/TODO
CHANGED
@@ -6,8 +6,7 @@
|
|
6
6
|
* Make warden password strategy support a callback which explains what to do with username, password
|
7
7
|
* WardenPlugin.authenticate_callback { |username, password| User.authenticate(username, password) }
|
8
8
|
* Add support for fields_for tag in formbuilder
|
9
|
-
* Add support for missing
|
10
|
-
* Add support for missing formbuilder fields (radio_button, select, and standard_form_builder methods i.e check_box_group)
|
9
|
+
* Add support for missing formbuilder fields (select, and standard_form_builder methods i.e check_box_group)
|
11
10
|
* Look into creating sinatra generators using rubigen (http://github.com/drnic/rubigen)
|
12
11
|
* http://github.com/quirkey/sinatra-gen
|
13
12
|
* http://github.com/monkrb/monk
|
@@ -20,6 +19,8 @@
|
|
20
19
|
|
21
20
|
= COMPLETED
|
22
21
|
|
22
|
+
* Add support for missing formhelpers/fields (radio_button_tag, select_tag)
|
23
|
+
* Add support for select_tag :multiple => true
|
23
24
|
* Add support for forms with method => put, delete using hidden field
|
24
25
|
* Remove dependency on activesupport! and enumerate dependencies in rakefile (as much as possible, need inflectors)
|
25
26
|
* Pull from sinatra-helpers and make erb templates work (and credit keldredd)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
@@ -82,6 +82,17 @@ module SinatraMore
|
|
82
82
|
input_tag(:password, options)
|
83
83
|
end
|
84
84
|
|
85
|
+
# Constructs a check_box from the given options
|
86
|
+
# options = [['caption', 'value'], ['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
|
87
|
+
# options = ['option', 'red', 'yellow' ]
|
88
|
+
# select_tag(:favorite_color, :options => ['red', 'yellow'], :selected => 'green1')
|
89
|
+
def select_tag(name, options={})
|
90
|
+
options.reverse_merge!(:name => name)
|
91
|
+
select_options_html = options_for_select(options.delete(:options), options.delete(:selected))
|
92
|
+
options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
|
93
|
+
content_tag(:select, select_options_html, options)
|
94
|
+
end
|
95
|
+
|
85
96
|
# Constructs a check_box from the given options
|
86
97
|
# check_box_tag :remember_me, :value => 'Yes'
|
87
98
|
def check_box_tag(name, options={})
|
@@ -119,6 +130,14 @@ module SinatraMore
|
|
119
130
|
|
120
131
|
protected
|
121
132
|
|
133
|
+
# Returns the options tags for a select based on the given option items
|
134
|
+
def options_for_select(option_items, selected_value=nil)
|
135
|
+
return '' if option_items.blank?
|
136
|
+
option_items.collect do |caption, value|
|
137
|
+
content_tag(:option, caption, :value => (value || caption), :selected => (selected_value == (value || caption)))
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
122
141
|
# returns the hidden method field for 'put' and 'delete' forms
|
123
142
|
# Only 'get' and 'post' are allowed within browsers;
|
124
143
|
# 'put' and 'delete' are just specified using hidden fields with form action still 'put'.
|
@@ -4,7 +4,6 @@ module SinatraMore
|
|
4
4
|
# input_tag :text, :class => "test"
|
5
5
|
def input_tag(type, options = {})
|
6
6
|
options.reverse_merge!(:type => type)
|
7
|
-
[:checked, :disabled].each { |attr| options[attr] = attr.to_s if options[attr] }
|
8
7
|
tag(:input, options)
|
9
8
|
end
|
10
9
|
|
@@ -25,9 +24,17 @@ module SinatraMore
|
|
25
24
|
# tag(:p, :content => "hello", :class => 'large')
|
26
25
|
def tag(name, options={})
|
27
26
|
content = options.delete(:content)
|
27
|
+
identity_tag_attributes.each { |attr| options[attr] = attr.to_s if options[attr] }
|
28
28
|
html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
|
29
29
|
base_tag = (html_attrs.present? ? "<#{name} #{html_attrs}" : "<#{name}")
|
30
30
|
base_tag << (content ? ">#{content}</#{name}>" : " />")
|
31
31
|
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
# Returns a list of attributes which can only contain an identity value (i.e selected)
|
36
|
+
def identity_tag_attributes
|
37
|
+
[:checked, :disabled, :selected, :multiple]
|
38
|
+
end
|
32
39
|
end
|
33
40
|
end
|
data/sinatra_more.gemspec
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
<%= password_field_tag :password %>
|
9
9
|
<%= check_box_tag :remember_me %>
|
10
10
|
<%= label_tag :gender %>
|
11
|
+
<%= label_tag :color %>
|
12
|
+
<%= select_tag :color, :options => ['green', 'orange', 'purple'] %>
|
11
13
|
<%= radio_button_tag :gender, :value => 'male' %>
|
12
14
|
<%= radio_button_tag :gender, :value => 'female' %>
|
13
15
|
<%= submit_tag %>
|
@@ -39,6 +41,10 @@
|
|
39
41
|
<%= radio_button_tag :gender, :value => 'male', :checked => true %>
|
40
42
|
<%= radio_button_tag :remember_me, :value => 'female' %>
|
41
43
|
<p>
|
44
|
+
<p>
|
45
|
+
<%= label_tag :fav_color %>
|
46
|
+
<%= select_tag :fav_color, :options => [ ['green', '1'], ['orange', '2'], ['purple', '3'] ], :selected => '2' %>
|
47
|
+
</p>
|
42
48
|
<p>
|
43
49
|
<%= check_box_tag :remember_me, :value => '1', :checked => true %>
|
44
50
|
<p>
|
@@ -6,7 +6,9 @@
|
|
6
6
|
= text_field_tag :username
|
7
7
|
= label_tag :password
|
8
8
|
= password_field_tag :password
|
9
|
-
= label_tag
|
9
|
+
= label_tag :color
|
10
|
+
= select_tag :color, :options => ['green', 'orange', 'purple']
|
11
|
+
= label_tag :gender
|
10
12
|
= radio_button_tag :gender, :value => 'male'
|
11
13
|
= radio_button_tag :gender, :value => 'female'
|
12
14
|
= check_box_tag :remember_me
|
@@ -32,6 +34,9 @@
|
|
32
34
|
%p
|
33
35
|
= label_tag :photo, :class => 'photo'
|
34
36
|
= file_field_tag :photo, :class => 'upload'
|
37
|
+
%p
|
38
|
+
= label_tag :fav_color
|
39
|
+
= select_tag :fav_color, :options => [ ['green', '1'], ['orange', '2'], ['purple', '3'] ], :selected => '2'
|
35
40
|
%p
|
36
41
|
= check_box_tag :remember_me, :value => "1", :checked => true
|
37
42
|
- field_set_tag(:class => 'buttons') do
|
@@ -113,14 +113,14 @@ class TestFormHelpers < Test::Unit::TestCase
|
|
113
113
|
|
114
114
|
should "display label tag in erb for simple form" do
|
115
115
|
visit '/erb/form_tag'
|
116
|
-
assert_have_selector 'form.simple-form label', :count =>
|
116
|
+
assert_have_selector 'form.simple-form label', :count => 4
|
117
117
|
assert_have_selector 'form.simple-form label', :content => "Username", :for => 'username'
|
118
118
|
assert_have_selector 'form.simple-form label', :content => "Password", :for => 'password'
|
119
119
|
assert_have_selector 'form.simple-form label', :content => "Gender", :for => 'gender'
|
120
120
|
end
|
121
121
|
should "display label tag in erb for advanced form" do
|
122
122
|
visit '/erb/form_tag'
|
123
|
-
assert_have_selector 'form.advanced-form label', :count =>
|
123
|
+
assert_have_selector 'form.advanced-form label', :count => 6
|
124
124
|
assert_have_selector 'form.advanced-form label.first', :content => "Nickname", :for => 'username'
|
125
125
|
assert_have_selector 'form.advanced-form label.first', :content => "Password", :for => 'password'
|
126
126
|
assert_have_selector 'form.advanced-form label.about', :content => "About Me", :for => 'about'
|
@@ -130,14 +130,14 @@ class TestFormHelpers < Test::Unit::TestCase
|
|
130
130
|
|
131
131
|
should "display label tag in haml for simple form" do
|
132
132
|
visit '/haml/form_tag'
|
133
|
-
assert_have_selector 'form.simple-form label', :count =>
|
133
|
+
assert_have_selector 'form.simple-form label', :count => 4
|
134
134
|
assert_have_selector 'form.simple-form label', :content => "Username", :for => 'username'
|
135
135
|
assert_have_selector 'form.simple-form label', :content => "Password", :for => 'password'
|
136
136
|
assert_have_selector 'form.simple-form label', :content => "Gender", :for => 'gender'
|
137
137
|
end
|
138
138
|
should "display label tag in haml for advanced form" do
|
139
139
|
visit '/haml/form_tag'
|
140
|
-
assert_have_selector 'form.advanced-form label', :count =>
|
140
|
+
assert_have_selector 'form.advanced-form label', :count => 6
|
141
141
|
assert_have_selector 'form.advanced-form label.first', :content => "Nickname", :for => 'username'
|
142
142
|
assert_have_selector 'form.advanced-form label.first', :content => "Password", :for => 'password'
|
143
143
|
assert_have_selector 'form.advanced-form label.about', :content => "About Me", :for => 'about'
|
@@ -289,6 +289,60 @@ class TestFormHelpers < Test::Unit::TestCase
|
|
289
289
|
end
|
290
290
|
end
|
291
291
|
|
292
|
+
context "for #select_tag method" do
|
293
|
+
should "display select tag in ruby" do
|
294
|
+
actual_html = select_tag(:favorite_color, :options => ['green', 'blue', 'black'])
|
295
|
+
assert_has_tag(:select, :name => 'favorite_color') { actual_html }
|
296
|
+
assert_has_tag('select option', :content => 'green', :value => 'green') { actual_html }
|
297
|
+
assert_has_tag('select option', :content => 'blue', :value => 'blue') { actual_html }
|
298
|
+
assert_has_tag('select option', :content => 'black', :value => 'black') { actual_html }
|
299
|
+
end
|
300
|
+
|
301
|
+
should "display select tag in ruby with extended attributes" do
|
302
|
+
actual_html = select_tag(:favorite_color, :disabled => true, :options => ['only', 'option'])
|
303
|
+
assert_has_tag(:select, :disabled => 'disabled') { actual_html }
|
304
|
+
end
|
305
|
+
|
306
|
+
should "display select tag in ruby with multiple attribute" do
|
307
|
+
actual_html = select_tag(:favorite_color, :multiple => true, :options => ['only', 'option'])
|
308
|
+
assert_has_tag(:select, :multiple => 'multiple', :name => 'favorite_color[]') { actual_html }
|
309
|
+
end
|
310
|
+
|
311
|
+
should "display options with values and selected" do
|
312
|
+
options = [['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
|
313
|
+
actual_html = select_tag(:favorite_color, :options => options, :selected => 'green1')
|
314
|
+
assert_has_tag(:select, :name => 'favorite_color') { actual_html }
|
315
|
+
assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
|
316
|
+
assert_has_tag('select option', :content => 'Green', :value => 'green1', :selected => 'selected') { actual_html }
|
317
|
+
assert_has_tag('select option', :content => 'Blue', :value => 'blue1') { actual_html }
|
318
|
+
assert_has_tag('select option', :content => 'Black', :value => 'black1') { actual_html }
|
319
|
+
end
|
320
|
+
|
321
|
+
should "display select tag in erb" do
|
322
|
+
visit '/erb/form_tag'
|
323
|
+
assert_have_selector 'form.simple-form select', :count => 1, :name => 'color'
|
324
|
+
assert_have_selector('select option', :content => 'green', :value => 'green')
|
325
|
+
assert_have_selector('select option', :content => 'orange', :value => 'orange')
|
326
|
+
assert_have_selector('select option', :content => 'purple', :value => 'purple')
|
327
|
+
assert_have_selector 'form.advanced-form select', :name => 'fav_color'
|
328
|
+
assert_have_selector('select option', :content => 'green', :value => '1')
|
329
|
+
assert_have_selector('select option', :content => 'orange', :value => '2', :selected => 'selected')
|
330
|
+
assert_have_selector('select option', :content => 'purple', :value => '3')
|
331
|
+
end
|
332
|
+
|
333
|
+
should "display select tag in haml" do
|
334
|
+
visit '/haml/form_tag'
|
335
|
+
assert_have_selector 'form.simple-form select', :count => 1, :name => 'color'
|
336
|
+
assert_have_selector('select option', :content => 'green', :value => 'green')
|
337
|
+
assert_have_selector('select option', :content => 'orange', :value => 'orange')
|
338
|
+
assert_have_selector('select option', :content => 'purple', :value => 'purple')
|
339
|
+
assert_have_selector 'form.advanced-form select', :name => 'fav_color'
|
340
|
+
assert_have_selector('select option', :content => 'green', :value => '1')
|
341
|
+
assert_have_selector('select option', :content => 'orange', :value => '2', :selected => 'selected')
|
342
|
+
assert_have_selector('select option', :content => 'purple', :value => '3')
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
292
346
|
context 'for #submit_tag method' do
|
293
347
|
should "display submit tag in ruby" do
|
294
348
|
actual_html = submit_tag("Update", :class => 'success')
|
@@ -11,9 +11,13 @@ class TestTagHelpers < Test::Unit::TestCase
|
|
11
11
|
assert_has_tag(:br) { tag(:br) }
|
12
12
|
end
|
13
13
|
should("support tags with no content with attributes") do
|
14
|
-
actual_html = tag(:br, :style => 'clear:both', :class => 'yellow')
|
14
|
+
actual_html = tag(:br, :style => 'clear:both', :class => 'yellow')
|
15
15
|
assert_has_tag(:br, :class => 'yellow', :style=>'clear:both') { actual_html }
|
16
16
|
end
|
17
|
+
should "support selected attribute by using 'selected' if true" do
|
18
|
+
actual_html = tag(:option, :selected => true)
|
19
|
+
assert_has_tag('option', :selected => 'selected') { actual_html }
|
20
|
+
end
|
17
21
|
should "support tags with content no attributes" do
|
18
22
|
assert_has_tag(:p, :content => "Demo String") { tag(:p, :content => "Demo String") }
|
19
23
|
end
|