sinatra_more 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -231,6 +231,10 @@ The following are fields provided by AbstractFormBuilder that can be used within
231
231
  * <tt>f.password_field :secret, :class => 'long'</tt>
232
232
  * <tt>file_field(field, options={})</tt>
233
233
  * <tt>f.file_field :photo, :class => 'long'</tt>
234
+ * <tt>select(field, options={})</tt>
235
+ * <tt>f.select(:state, :options => ['California', 'Texas', 'Wyoming'])</tt>
236
+ * <tt>f.select(:state, :collection => @states, :fields => [:name, :id])</tt>
237
+ * <tt>f.select(:state, :options => [...], :include_blank => true)</tt>
234
238
  * <tt>submit(caption, options={})</tt>
235
239
  * <tt>f.submit "Update", :class => 'long'</tt>
236
240
  * <tt>image_submit(source, options={})</tt>
@@ -252,6 +256,9 @@ A form_for using these basic fields might look like:
252
256
  %p
253
257
  = f.label :is_admin, :caption => "Admin User?"
254
258
  = f.check_box :is_admin
259
+ $p
260
+ = f.label :color, :caption => "Favorite Color?"
261
+ = f.select :color, :options => ['red', 'black']
255
262
  %p
256
263
  = f.submit "Create", :class => 'button'
257
264
 
@@ -265,6 +272,10 @@ There is also a StandardFormBuilder which builds on the abstract fields that can
265
272
  * <tt>password_field_block(:code, :class => 'big')</tt>
266
273
  * <tt>file_field_block(field, options={}, label_options={})</tt>
267
274
  * <tt>file_field_block(:photo, :class => 'big')</tt>
275
+ * <tt>check_box_block(field, options={}, label_options={})</tt>
276
+ * <tt>check_box_block(:remember_me, :class => 'big')</tt>
277
+ * <tt>select_block(field, options={}, label_options={})</tt>
278
+ * <tt>select_block(:country, :option => ['USA', 'Canada'])</tt>
268
279
  * <tt>submit_block(caption, options={})</tt>
269
280
  * <tt>submit_block(:username, :class => 'big')</tt>
270
281
  * <tt>image_submit_block(source, options={})</tt>
@@ -276,6 +287,8 @@ A form_for using these standard fields might look like:
276
287
  = f.error_messages
277
288
  = f.text_field_block :name, :caption => "Full name"
278
289
  = f.text_field_block :email
290
+ = f.check_box_block :remember_me
291
+ = f.select_block :fav_color, :options => ['red', 'blue']
279
292
  = f.password_field_block :password
280
293
  = f.submit_block "Create", :class => 'button'
281
294
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -43,6 +43,13 @@ class AbstractFormBuilder
43
43
  @template.password_field_tag field_name(field), options
44
44
  end
45
45
 
46
+ # f.select :color, :options => ['red', 'green'], :include_blank => true
47
+ # f.select :color, :collection => @colors, :fields => [:name, :id]
48
+ def select(field, options={})
49
+ options.reverse_merge!(:id => field_id(field), :selected => field_value(field))
50
+ @template.select_tag field_name(field), options
51
+ end
52
+
46
53
  # f.check_box :remember_me, :value => 'true', :uncheck_value => '0'
47
54
  def check_box(field, options={})
48
55
  unchecked_value = options.delete(:uncheck_value) || '0'
@@ -79,7 +86,7 @@ class AbstractFormBuilder
79
86
 
80
87
  # Returns the known field types for a formbuilder
81
88
  def self.field_types
82
- [:text_field, :text_area, :password_field, :file_field, :hidden_field, :check_box]
89
+ [:hidden_field, :text_field, :text_area, :password_field, :file_field, :radio_button, :check_box, :select]
83
90
  end
84
91
 
85
92
  # Returns the object's models name
@@ -1,10 +1,12 @@
1
1
  class StandardFormBuilder < AbstractFormBuilder
2
2
 
3
3
  # text_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
4
- # text_area_block(:username, { :class => 'long' }, { :class => 'wide-label' })
5
- # password_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
6
- # file_field_block(:username, { :class => 'long' }, { :class => 'wide-label' })
7
- (self.field_types - [:hidden_field]).each do |field_type|
4
+ # text_area_block(:summary, { :class => 'long' }, { :class => 'wide-label' })
5
+ # password_field_block(:password, { :class => 'long' }, { :class => 'wide-label' })
6
+ # file_field_block(:photo, { :class => 'long' }, { :class => 'wide-label' })
7
+ # check_box_block(:remember_me, { :class => 'long' }, { :class => 'wide-label' })
8
+ # select_block(:color, :options => ['green', 'black'])
9
+ (self.field_types - [ :hidden_field, :radio_button ]).each do |field_type|
8
10
  class_eval <<-EOF
9
11
  def #{field_type}_block(field, options={}, label_options={})
10
12
  label_options.reverse_merge!(:caption => options.delete(:caption)) if options[:caption]
@@ -86,8 +86,12 @@ module SinatraMore
86
86
  # options = [['caption', 'value'], ['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
87
87
  # options = ['option', 'red', 'yellow' ]
88
88
  # select_tag(:favorite_color, :options => ['red', 'yellow'], :selected => 'green1')
89
+ # select_tag(:country, :collection => @countries, :fields => [:name, :code])
89
90
  def select_tag(name, options={})
90
91
  options.reverse_merge!(:name => name)
92
+ collection, fields = options.delete(:collection), options.delete(:fields)
93
+ options[:options] = options_from_collection(collection, fields) if collection
94
+ options[:options].unshift('') if options.delete(:include_blank)
91
95
  select_options_html = options_for_select(options.delete(:options), options.delete(:selected))
92
96
  options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
93
97
  content_tag(:select, select_options_html, options)
@@ -130,11 +134,19 @@ module SinatraMore
130
134
 
131
135
  protected
132
136
 
137
+ # Returns an array of option items for a select field based on the given collection
138
+ # fields is an array containing the fields to display from each item in the collection
139
+ def options_from_collection(collection, fields)
140
+ return '' if collection.blank?
141
+ collection.collect { |item| [ item.send(fields.first), item.send(fields.last) ] }
142
+ end
143
+
133
144
  # Returns the options tags for a select based on the given option items
134
145
  def options_for_select(option_items, selected_value=nil)
135
146
  return '' if option_items.blank?
136
147
  option_items.collect do |caption, value|
137
- content_tag(:option, caption, :value => (value || caption), :selected => (selected_value == (value || caption)))
148
+ value ||= caption
149
+ content_tag(:option, caption, :value => value, :selected => selected_value.to_s =~ /#{value}|#{caption}/)
138
150
  end
139
151
  end
140
152
 
data/sinatra_more.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_more}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Esquenazi"]
@@ -27,6 +27,11 @@
27
27
  <%= f.radio_button :gender, :value => 'female' %>
28
28
  </p>
29
29
  <p>
30
+ <%= f.label :country, :caption => "Your country" %>
31
+ <%= f.select :country, :options => ['USA', 'Canada', 'Mexico'], :selected => 'USA', :class => 'selector' %>
32
+ </p>
33
+ <p>
34
+ <%= f.label :remember_me %>
30
35
  <%= f.check_box :remember_me, :value => '1' %>
31
36
  </p>
32
37
  <p><%= f.submit "Create", :class => 'success', :id => 'demo-button' %></p>
@@ -40,6 +45,8 @@
40
45
  <%= f.password_field_block :code, { :class => 'input' } %>
41
46
  <%= f.text_area_block :about, { :class => 'textarea' } %>
42
47
  <%= f.file_field_block :photo, { :class => 'upload' } %>
48
+ <%= f.check_box_block :remember_me, { :class => 'checker' } %>
49
+ <%= f.select_block :state, :options => ['California', 'Texas'], :class => 'selector' %>
43
50
  <%= f.submit_block "Create", { :class => 'button' } %>
44
51
  <%= f.image_submit_block "buttons/ok.png", { :class => 'image' } %>
45
52
  <% end %>
@@ -17,10 +17,14 @@
17
17
  = f.label :about, :caption => "About Me"
18
18
  = f.text_area :about, :class => 'user-about'
19
19
  %p
20
- = f.label :gender, :caption => "Your gender:"
20
+ = f.label :gender, :caption => "Your gender"
21
21
  = f.radio_button :gender, :value => 'male'
22
22
  = f.radio_button :gender, :value => 'female'
23
23
  %p
24
+ = f.label :country, :caption => "Your country"
25
+ = f.select :country, :options => ['USA', 'Canada', 'Mexico'], :selected => 'USA', :class => 'selector'
26
+ %p
27
+ = f.label :remember_me
24
28
  = f.check_box :remember_me, :value => "1"
25
29
  %p
26
30
  = f.submit "Create", :class => 'success', :id => 'demo-button'
@@ -34,5 +38,7 @@
34
38
  = f.password_field_block :code, { :class => 'input' }
35
39
  = f.text_area_block :about, { :class => 'textarea' }
36
40
  = f.file_field_block :photo, { :class => 'upload' }
41
+ = f.check_box_block :remember_me, { :class => 'checker' }
42
+ = f.select_block :state, :options => ['California', 'Texas'], :class => 'selector'
37
43
  = f.submit_block "Create", { :class => 'button' }
38
44
  = f.image_submit_block "buttons/ok.png", { :class => 'image' }
@@ -9,7 +9,10 @@ class TestFormBuilder < Test::Unit::TestCase
9
9
  end
10
10
 
11
11
  def setup
12
- @user = stub(:errors => stub(:full_messages => ["1", "2"], :none? => false), :class => 'User', :first_name => "Joe", :session_id => 54)
12
+ error_stub = stub(:full_messages => ["1", "2"], :none? => false)
13
+ role_types = [stub(:name => 'Admin', :id => 1), stub(:name => 'Moderate', :id => 2), stub(:name => 'Limited', :id => 3)]
14
+ @user = stub(:errors => error_stub, :class => 'User', :first_name => "Joe", :session_id => 54)
15
+ @user.stubs(:role_types => role_types, :role => "1")
13
16
  @user_none = stub(:errors => stub(:none? => true), :class => 'User')
14
17
  end
15
18
 
@@ -292,6 +295,53 @@ class TestFormBuilder < Test::Unit::TestCase
292
295
  end
293
296
  end
294
297
 
298
+ context 'for #select method' do
299
+ should "display correct select html" do
300
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :class => 'selecty')
301
+ assert_has_tag('select.selecty', :id => 'user_state', :name => 'user[state]') { actual_html }
302
+ assert_has_tag('select.selecty option', :count => 3) { actual_html }
303
+ assert_has_tag('select.selecty option', :value => 'California', :content => 'California') { actual_html }
304
+ assert_has_tag('select.selecty option', :value => 'Texas', :content => 'Texas') { actual_html }
305
+ assert_has_tag('select.selecty option', :value => 'Wyoming', :content => 'Wyoming') { actual_html }
306
+ end
307
+
308
+ should "display correct select html with selected item if it matches value" do
309
+ @user.stubs(:state => 'California')
310
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'])
311
+ assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
312
+ assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
313
+ assert_has_tag('select option', :value => 'California', :selected => 'selected') { actual_html }
314
+ end
315
+
316
+ should "display correct select html with include_blank" do
317
+ actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :include_blank => true)
318
+ assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
319
+ assert_has_tag('select option', :count => 4) { actual_html }
320
+ assert_has_tag('select option:first-child', :content => '') { actual_html }
321
+ end
322
+
323
+ should "display correct select html with collection passed in" do
324
+ actual_html = standard_builder.select(:role, :collection => @user.role_types, :fields => [:name, :id])
325
+ assert_has_tag('select', :id => 'user_role', :name => 'user[role]') { actual_html }
326
+ assert_has_tag('select option', :count => 3) { actual_html }
327
+ assert_has_tag('select option', :value => '1', :content => 'Admin', :selected => 'selected') { actual_html }
328
+ assert_has_tag('select option', :value => '2', :content => 'Moderate') { actual_html }
329
+ assert_has_tag('select option', :value => '3', :content => 'Limited') { actual_html }
330
+ end
331
+
332
+ should "display correct select in haml" do
333
+ visit '/haml/form_for'
334
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
335
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
336
+ end
337
+
338
+ should "display correct select in erb" do
339
+ visit '/erb/form_for'
340
+ assert_have_selector '#demo textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'user-about'
341
+ assert_have_selector '#demo2 textarea', :name => 'markup_user[about]', :id => 'markup_user_about', :class => 'textarea'
342
+ end
343
+ end
344
+
295
345
  context 'for #submit method' do
296
346
  should "display correct submit button html with no options" do
297
347
  actual_html = standard_builder.submit
@@ -424,6 +474,50 @@ class TestFormBuilder < Test::Unit::TestCase
424
474
  end
425
475
  end
426
476
 
477
+ context 'for #check_box_block method' do
478
+ should "display correct check box block html" do
479
+ actual_html = standard_builder.check_box_block(:remember_me, :class => 'large', :caption => "Remember session")
480
+ assert_has_tag('p label', :for => 'user_remember_me', :content => "Remember session: ") { actual_html }
481
+ assert_has_tag('p input.large[type=checkbox]', :id => 'user_remember_me', :name => 'user[remember_me]') { actual_html }
482
+ end
483
+
484
+ should "display correct check box block in haml" do
485
+ visit '/haml/form_for'
486
+ assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember Me: "
487
+ assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
488
+ end
489
+
490
+ should "display correct check box block in erb" do
491
+ visit '/erb/form_for'
492
+ assert_have_selector '#demo2 p label', :for => 'markup_user_remember_me', :content => "Remember Me: "
493
+ assert_have_selector '#demo2 p input.checker', :type => 'checkbox', :name => 'markup_user[remember_me]'
494
+ end
495
+ end
496
+
497
+ context 'for #select_block method' do
498
+ should "display correct select_block block html" do
499
+ actual_html = standard_builder.select_block(:country, :options => ['USA', 'Canada'], :class => 'large', :caption => "Your country")
500
+ assert_has_tag('p label', :for => 'user_country', :content => "Your country: ") { actual_html }
501
+ assert_has_tag('p select', :id => 'user_country', :name => 'user[country]') { actual_html }
502
+ assert_has_tag('p select option', :content => 'USA') { actual_html }
503
+ assert_has_tag('p select option', :content => 'Canada') { actual_html }
504
+ end
505
+
506
+ should "display correct select_block block in haml" do
507
+ visit '/haml/form_for'
508
+ assert_have_selector '#demo2 p label', :for => 'markup_user_state', :content => "State: "
509
+ assert_have_selector '#demo2 p select', :name => 'markup_user[state]', :id => 'markup_user_state'
510
+ assert_have_selector '#demo2 p select option', :content => 'California'
511
+ assert_have_selector '#demo2 p select option', :content => 'Texas'
512
+ end
513
+
514
+ should "display correct select_block block in erb" do
515
+ visit '/erb/form_for'
516
+ assert_have_selector '#demo2 p label', :for => 'markup_user_state', :content => "State: "
517
+ assert_have_selector '#demo2 p select', :name => 'markup_user[state]', :id => 'markup_user_state'
518
+ end
519
+ end
520
+
427
521
  context 'for #submit_block method' do
428
522
  should "display correct submit block html" do
429
523
  actual_html = standard_builder.submit_block("Update", :class => 'large')
@@ -291,8 +291,9 @@ class TestFormHelpers < Test::Unit::TestCase
291
291
 
292
292
  context "for #select_tag method" do
293
293
  should "display select tag in ruby" do
294
- actual_html = select_tag(:favorite_color, :options => ['green', 'blue', 'black'])
294
+ actual_html = select_tag(:favorite_color, :options => ['green', 'blue', 'black'], :include_blank => true)
295
295
  assert_has_tag(:select, :name => 'favorite_color') { actual_html }
296
+ assert_has_tag('select option:first-child', :content => '') { actual_html }
296
297
  assert_has_tag('select option', :content => 'green', :value => 'green') { actual_html }
297
298
  assert_has_tag('select option', :content => 'blue', :value => 'blue') { actual_html }
298
299
  assert_has_tag('select option', :content => 'black', :value => 'black') { actual_html }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_more
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi