sinatra_more 0.1.10 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,5 +1,27 @@
1
1
  = sinatra_more
2
2
 
3
+ == Preface
4
+
5
+ This gem has been designed to work with Sinatra (http://www.sinatrarb.com).
6
+ Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort.
7
+ The canonical example of how to create an entire simple web application with Sinatra is something like:
8
+
9
+ # myapp.rb
10
+ require 'rubygems'
11
+ require 'sinatra'
12
+ get '/' do
13
+ 'Hello world!'
14
+ end
15
+
16
+ and then to run the application:
17
+
18
+ $ ruby myapp.rb
19
+
20
+ The extreme simplicity of the framework is quite refreshing. I have been using Sinatra a great deal
21
+ for recent projects. First for small and simple json and xml web services and then even
22
+ for more complex full-featured applications. This gem represents my attempt to make it as fun and easy
23
+ as possible to code increasingly advanced view-heavy web applications in Sinatra.
24
+
3
25
  == Introduction
4
26
 
5
27
  Note: This library is still experimental and may not be ready for production just yet.
@@ -7,8 +29,8 @@ That being said the gem is being actively used on a number of sinatra projects.
7
29
  In addition, the gem has fairly solid test coverage ensuring that everything works as expected.
8
30
 
9
31
  This will be a plugin which expands sinatra's capabilities in a variety of ways.
10
- Note that as much as possible, all helpers have been created to work with haml, erb, and erubis.
11
- This gem is intended to be template-agnostic in providing helpers as much as possible.
32
+ Note that all extensions have been created to work with haml, erb, and erubis.
33
+ This gem is intended to be template-agnostic in providing helpers wherever possible.
12
34
 
13
35
  Let me expand briefly on what I want to accomplish with this gem. I love sinatra but if I want to use it
14
36
  for any non-trivial application I very quickly miss a lot of the extra tools provided by rails.
@@ -139,6 +161,12 @@ methods should be very familiar to anyone who has used rails view helpers.
139
161
  * <tt>password_field_tag(name, options={})</tt>
140
162
  * Constructs a password field input from the given options
141
163
  * <tt>password_field_tag :password, :class => 'long'</tt>
164
+ * <tt>check_box_tag(name, options={})</tt>
165
+ * Constructs a checkbox input from the given options
166
+ * <tt>check_box_tag :remember_me, :checked => true</tt>
167
+ * <tt>radio_button_tag(name, options={})</tt>
168
+ * Constructs a radio button input from the given options
169
+ * <tt>radio_button_tag :gender, :value => 'male'</tt>
142
170
  * <tt>file_field_tag(name, options={})</tt>
143
171
  * Constructs a file field input from the given options
144
172
  * <tt>file_field_tag :photo, :class => 'long'</tt>
@@ -157,6 +185,8 @@ A form_tag might look like:
157
185
  %p
158
186
  = label_tag :password, :class => 'first'
159
187
  = password_field_tag :password, :value => params[:password]
188
+ %p
189
+ = check_box_tag :confirm_delete
160
190
  - field_set_tag(:class => 'buttons') do
161
191
  = submit_tag "Remove"
162
192
 
@@ -164,6 +194,8 @@ A form_tag might look like:
164
194
 
165
195
  * <tt>form_for(object, url, settings={}, &block)</tt>
166
196
  * Constructs a form using given or default form_builder
197
+ * Supports form methods 'put' and 'delete' through hidden field
198
+ * Defaults to StandardFormBuilder but you can easily create your own!
167
199
  * <tt>form_for(@user, '/register', :id => 'register') { |f| ...field-elements... }</tt>
168
200
 
169
201
  The following are fields provided by AbstractFormBuilder that can be used within a form_for:
@@ -175,6 +207,11 @@ The following are fields provided by AbstractFormBuilder that can be used within
175
207
  * <tt>f.label :name, :class => 'long'</tt>
176
208
  * <tt>text_field(field, options={})</tt>
177
209
  * <tt>f.text_field :username, :class => 'long'</tt>
210
+ * <tt>check_box(field, options={})</tt>
211
+ * Uses hidden field to provide a 'unchecked' value for field
212
+ * <tt>f.check_box :remember_me, :uncheck_value => 'false'</tt>
213
+ * <tt>radio_button(field, options={})</tt>
214
+ * <tt>f.radio_button :gender, :value => 'male'</tt>
178
215
  * <tt>hidden_field(field, options={})</tt>
179
216
  * <tt>f.hidden_field :session_id, :class => 'hidden'</tt>
180
217
  * <tt>text_area(field, options={})</tt>
@@ -199,6 +236,9 @@ A form_for using these basic fields might look like:
199
236
  %p
200
237
  = f.label :password
201
238
  = f.password_field :password
239
+ %p
240
+ = f.label :is_admin, :caption => "Admin User?"
241
+ = f.check_box :is_admin
202
242
  %p
203
243
  = f.submit "Create", :class => 'button'
204
244
 
@@ -232,6 +272,32 @@ and would generate this html:
232
272
  <p><input type="submit" value="Create" class="button"></p>
233
273
  </form>
234
274
 
275
+ You can also easily build your own FormBuilder which allows for customized fields:
276
+
277
+ class MyCustomFormBuilder < AbstractFormBuilder
278
+ # Here we have access to a number of useful variables
279
+ #
280
+ # * template (use this to invoke any helpers)(ex. template.hidden_field_tag(...))
281
+ # * object (the record for this form) (ex. object.valid?)
282
+ # * object_name (object's underscored type) (ex. object_name => 'admin_user')
283
+ #
284
+ # We also have access to self.field_types => [:text_field, :text_area, ...]
285
+ # In addition, we have access to all the existing field tag helpers (text_field, hidden_field, file_field, ...)
286
+ end
287
+
288
+ Once a custom builder is defined, any call to form_for can use the new builder:
289
+
290
+ - form_for @user, '/register', :builder => 'MyCustomFormBuilder', :id => 'register' do |f|
291
+ ...fields here...
292
+
293
+ The form builder can even be made into the default builder when form_for is invoked:
294
+
295
+ # anywhere in the sinatra application
296
+ set :default_builder, 'MyCustomFormBuilder'
297
+
298
+ And there you have it, a fairly complete form builder solution for sinatra.
299
+ I hope to create or merge in an even better 'default' form_builder in the near future.
300
+
235
301
  ==== Format Helpers
236
302
 
237
303
  * <tt>escape_html</tt> (alias <tt>h</tt> and <tt>h!</tt>)
@@ -264,6 +330,36 @@ such as partials (with support for :collection) into the templating system.
264
330
  * <tt>partial 'photo/_item', :object => @photo, :locals => { :foo => 'bar' }</tt>
265
331
  * <tt>partial 'photo/_item', :collection => @photos</tt>
266
332
 
333
+ Using render plugin helpers is extremely simple. If you want to render an erb template in your view path:
334
+
335
+ erb_template 'path/to/my/template'
336
+
337
+ or using haml templates works just as well:
338
+
339
+ haml_template 'path/to/haml/template'
340
+
341
+ There is also a method which renders the first view matching the path and removes the need to define an engine:
342
+
343
+ render_template 'path/to/any/template'
344
+
345
+ It is worth noting these are mostly for convenience. When I had more complex view files in sinatra, I got tired of writing:
346
+
347
+ haml :"the/path/to/file"
348
+ erb "/path/to/file".to_sym
349
+
350
+ Finally, we have the all-important partials support for rendering mini-templates onto a page:
351
+
352
+ partial 'photo/_item', :object => @photo, :locals => { :foo => 'bar' }
353
+ partial 'photo/_item', :collection => @photos
354
+
355
+ This works as you would expect and also supports the collection counter inside the partial <tt>item_counter</tt>
356
+
357
+ # /views/photo/_item.haml
358
+ # Access to collection counter with <partial_name>_counter i.e item_counter
359
+ # Access the object with the partial_name i.e item
360
+
361
+ And that concludes the render plugin for now but I am open to adding more methods as time progresses.
362
+
267
363
  === WardenPlugin
268
364
 
269
365
  This component provides out-of-the-box support for Warden authentication. With this
@@ -285,11 +381,18 @@ provided to make interacting with warden dead simple.
285
381
  * Forces a user to return to a fail path unless they are authorized
286
382
  * Used to require a user be authenticated before routing to an action
287
383
  * <tt>must_be_authorized!('/login')</tt>
384
+
385
+ I am aware of other sinatra/warden extensions which work very similarly to the system I outlined here.
386
+ Most notably is the sinatra_warden plugin by Justin Smestad (http://github.com/jsmestad/sinatra_warden)
387
+ Eventually I plan to vendor that gem or just remove support for this piece of my plugin completely.
388
+
389
+ If anybody has any thoughts on this, please let me know. There is something convenient to me about having
390
+ access to a plug-and-play warden solution directly from this gem.
288
391
 
289
392
  == Acknowledgements
290
393
 
291
394
  * Thanks to keldredd for the sinatra_helpers code that helped me to create erb capture and concat methods.
292
- * Thanks to sbfaulkner for the sinatra-helpers code that I looked while starting this library.
395
+ * Thanks to sbfaulkner for the sinatra-helpers code that I browsed through many times while starting this library.
293
396
 
294
397
  == Contributors
295
398
 
data/TODO CHANGED
@@ -5,12 +5,18 @@
5
5
  * Take advantage of shared strategies: http://github.com/hassox/warden_strategies/tree/master/lib/
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
- * Add support for missing formhelpers/fields (check_box, radio_button, ...)
8
+ * Add support for fields_for tag in formbuilder
9
+ * Add support for missing formhelpers/fields (radio_button_tag, select_tag)
10
+ * Add support for missing formbuilder fields (radio_button, select, and standard_form_builder methods i.e check_box_group)
9
11
  * Look into creating sinatra generators using rubigen (http://github.com/drnic/rubigen)
10
12
  * http://github.com/quirkey/sinatra-gen
13
+ * http://github.com/monkrb/monk
11
14
  * Look into adding any missing helpers from:
15
+ * http://github.com/brianjlandau/nice-n-easy
16
+ * http://github.com/brianjlandau/nice-n-easy/blob/master/lib/sinatra/nice_easy_helpers.rb
12
17
  * http://github.com/kelredd/sinatra-helpers/blob/master/lib/sinatra_helpers/erb/links.rb
13
18
  * http://github.com/kelredd/sinatra-helpers/blob/master/lib/sinatra_helpers/erb/forms.rb
19
+ * http://github.com/twilson63/sinatra-formhelpers/blob/master/lib/sinatra/formhelpers.rb
14
20
 
15
21
  = COMPLETED
16
22
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.10
1
+ 0.2.0
@@ -43,7 +43,23 @@ class AbstractFormBuilder
43
43
  @template.password_field_tag field_name(field), options
44
44
  end
45
45
 
46
- # f.file_field(:photo, :class => 'avatar')
46
+ # f.check_box :remember_me, :value => 'true', :uncheck_value => '0'
47
+ def check_box(field, options={})
48
+ unchecked_value = options.delete(:uncheck_value) || '0'
49
+ options.reverse_merge!(:id => field_id(field))
50
+ options.merge!(:checked => true) if values_matches_field?(field, options[:value])
51
+ html = @template.check_box_tag field_name(field), options
52
+ html << hidden_field(field, :value => unchecked_value, :id => nil)
53
+ end
54
+
55
+ # f.radio_button :gender, :value => 'male'
56
+ def radio_button(field, options={})
57
+ options.reverse_merge!(:id => field_id(field, options[:value]))
58
+ options.merge!(:checked => true) if values_matches_field?(field, options[:value])
59
+ @template.radio_button_tag field_name(field), options
60
+ end
61
+
62
+ # f.file_field :photo, :class => 'avatar'
47
63
  def file_field(field, options={})
48
64
  options.reverse_merge!(:id => field_id(field))
49
65
  @template.file_field_tag field_name(field), options
@@ -58,17 +74,21 @@ class AbstractFormBuilder
58
74
 
59
75
  # Returns the known field types for a formbuilder
60
76
  def self.field_types
61
- [:text_field, :text_area, :password_field, :file_field, :hidden_field]
77
+ [:text_field, :text_area, :password_field, :file_field, :hidden_field, :check_box]
62
78
  end
63
79
 
64
- private
65
-
66
80
  # Returns the object's models name
67
81
  # => user_assignment
68
82
  def object_name
69
83
  object.class.to_s.underscore
70
84
  end
71
85
 
86
+ # Returns true if the value matches the value in the field
87
+ # field_has_value?(:gender, 'male')
88
+ def values_matches_field?(field, value)
89
+ value.present? && field_value(field).to_s == value.to_s
90
+ end
91
+
72
92
  # Returns the value for the object's field
73
93
  # field_value(:username) => "Joey"
74
94
  def field_value(field)
@@ -83,7 +103,8 @@ class AbstractFormBuilder
83
103
 
84
104
  # Returns the id for the given field
85
105
  # field_id(:username) => "user_username"
86
- def field_id(field)
87
- "#{object_name}_#{field}"
106
+ # field_id(:gender, :male) => "user_gender_male"
107
+ def field_id(field, value=nil)
108
+ value.blank? ? "#{object_name}_#{field}" : "#{object_name}_#{field}_#{value}"
88
109
  end
89
110
  end
@@ -82,6 +82,20 @@ module SinatraMore
82
82
  input_tag(:password, options)
83
83
  end
84
84
 
85
+ # Constructs a check_box from the given options
86
+ # check_box_tag :remember_me, :value => 'Yes'
87
+ def check_box_tag(name, options={})
88
+ options.reverse_merge!(:name => name, :value => '1')
89
+ input_tag(:checkbox, options)
90
+ end
91
+
92
+ # Constructs a radio_button from the given options
93
+ # radio_button_tag :remember_me, :value => 'true'
94
+ def radio_button_tag(name, options={})
95
+ options.reverse_merge!(:name => name)
96
+ input_tag(:radio, options)
97
+ end
98
+
85
99
  # Constructs a file field input from the given options
86
100
  # file_field_tag :photo, :class => 'long'
87
101
  def file_field_tag(name, options={})
@@ -4,6 +4,7 @@ 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] }
7
8
  tag(:input, options)
8
9
  end
9
10
 
@@ -1,10 +1,10 @@
1
1
  # This is for adding specific methods that are required by sinatra_more if activesupport isn't required
2
2
 
3
- unless String.new.respond_to?(:titleize)
3
+ unless String.method_defined?(:titleize)
4
4
  require 'active_support/inflector'
5
5
  end
6
6
 
7
- unless Hash.new.respond_to?(:reverse_merge!)
7
+ unless Hash.method_defined?(:reverse_merge!)
8
8
  module HashExtensions
9
9
  def reverse_merge(other_hash)
10
10
  other_hash.merge(self)
@@ -15,7 +15,7 @@ unless Hash.new.respond_to?(:reverse_merge!)
15
15
  end
16
16
  end
17
17
 
18
- unless Hash.new.respond_to?(:symbolize_keys!)
18
+ unless Hash.method_defined?(:symbolize_keys!)
19
19
  module HashExtensions
20
20
  def symbolize_keys
21
21
  inject({}) do |options, (key, value)|
@@ -29,7 +29,7 @@ unless Hash.new.respond_to?(:symbolize_keys!)
29
29
  end
30
30
  end
31
31
 
32
- unless Array.new.respond_to?(:extract_options!)
32
+ unless Array.method_defined?(:extract_options!)
33
33
  module ArrayExtensions
34
34
  def extract_options!
35
35
  last.is_a?(::Hash) ? pop : {}
data/sinatra_more.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_more}
8
- s.version = "0.1.10"
8
+ s.version = "0.2.0"
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"]
12
- s.date = %q{2009-10-27}
12
+ s.date = %q{2009-10-28}
13
13
  s.description = %q{Expands sinatra with standard helpers and tools to allow for complex applications}
14
14
  s.email = %q{nesquena@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,4 +1,4 @@
1
- unless 5.respond_to?(:days)
1
+ unless Fixnum.method_defined?(:days)
2
2
  require 'active_support/core_ext/object/misc'
3
3
  require 'active_support/core_ext/date'
4
4
  require 'active_support/core_ext/time'
@@ -44,6 +44,8 @@ end
44
44
  class MarkupUser
45
45
  def errors; Errors.new; end
46
46
  def session_id; 45; end
47
+ def gender; 'male'; end
48
+ def remember_me; '1'; end
47
49
  end
48
50
 
49
51
  class Errors < Array
@@ -21,6 +21,14 @@
21
21
  <%= f.label :about, :caption => "About Me" %>
22
22
  <%= f.text_area :about, :class => 'user-about' %>
23
23
  </p>
24
+ <p>
25
+ <%= f.label :gender, :caption => "Your gender:" %>
26
+ <%= f.radio_button :gender, :value => 'male' %>
27
+ <%= f.radio_button :gender, :value => 'female' %>
28
+ </p>
29
+ <p>
30
+ <%= f.check_box :remember_me, :value => '1' %>
31
+ </p>
24
32
  <p><%= f.submit "Create", :class => 'success', :id => 'demo-button' %></p>
25
33
  <% end %>
26
34
 
@@ -16,6 +16,12 @@
16
16
  %p
17
17
  = f.label :about, :caption => "About Me"
18
18
  = f.text_area :about, :class => 'user-about'
19
+ %p
20
+ = f.label :gender, :caption => "Your gender:"
21
+ = f.radio_button :gender, :value => 'male'
22
+ = f.radio_button :gender, :value => 'female'
23
+ %p
24
+ = f.check_box :remember_me, :value => "1"
19
25
  %p
20
26
  = f.submit "Create", :class => 'success', :id => 'demo-button'
21
27
 
@@ -6,6 +6,10 @@
6
6
  <%= text_field_tag :username %>
7
7
  <%= label_tag :password %>
8
8
  <%= password_field_tag :password %>
9
+ <%= check_box_tag :remember_me %>
10
+ <%= label_tag :gender %>
11
+ <%= radio_button_tag :gender, :value => 'male' %>
12
+ <%= radio_button_tag :gender, :value => 'female' %>
9
13
  <%= submit_tag %>
10
14
  <% end %>
11
15
  <% end %>
@@ -30,6 +34,14 @@
30
34
  <%= label_tag :photo, :class => 'photo' %>
31
35
  <%= file_field_tag :photo, :class => 'upload' %>
32
36
  </p>
37
+ <p>
38
+ <%= label_tag :gender, :class => 'gender' %>
39
+ <%= radio_button_tag :gender, :value => 'male', :checked => true %>
40
+ <%= radio_button_tag :remember_me, :value => 'female' %>
41
+ <p>
42
+ <p>
43
+ <%= check_box_tag :remember_me, :value => '1', :checked => true %>
44
+ <p>
33
45
  <% end %>
34
46
  <% field_set_tag(:class => 'buttons') do %>
35
47
  <%= submit_tag "Login" %>
@@ -6,6 +6,10 @@
6
6
  = text_field_tag :username
7
7
  = label_tag :password
8
8
  = password_field_tag :password
9
+ = label_tag :gender
10
+ = radio_button_tag :gender, :value => 'male'
11
+ = radio_button_tag :gender, :value => 'female'
12
+ = check_box_tag :remember_me
9
13
  = submit_tag
10
14
 
11
15
  - form_tag '/advanced', :id => 'advanced', :class => 'advanced-form', :method => 'get' do
@@ -21,8 +25,14 @@
21
25
  %p
22
26
  = label_tag :about, :class => 'about', :caption => "About Me"
23
27
  = text_area_tag :about, :class => 'large'
28
+ %p
29
+ = label_tag :gender, :class => 'gender'
30
+ = radio_button_tag :gender, :value => 'male', :checked => true
31
+ = radio_button_tag :gender, :value => 'female'
24
32
  %p
25
33
  = label_tag :photo, :class => 'photo'
26
34
  = file_field_tag :photo, :class => 'upload'
35
+ %p
36
+ = check_box_tag :remember_me, :value => "1", :checked => true
27
37
  - field_set_tag(:class => 'buttons') do
28
38
  = submit_tag "Login"
data/test/helper.rb CHANGED
@@ -35,6 +35,16 @@ class Test::Unit::TestCase
35
35
  raise "Please specify a block!" if html.blank?
36
36
  assert matcher.matches?(html), matcher.failure_message
37
37
  end
38
+
39
+ # assert_has_no_tag, tag(:h1, :content => "yellow") { "<h1>green</h1>" }
40
+ # In this case, block is the html to evaluate
41
+ def assert_has_no_tag(name, attributes = {}, &block)
42
+ html = block && block.call
43
+ attributes.merge!(:count => 0)
44
+ matcher = HaveSelector.new(name, attributes)
45
+ raise "Please specify a block!" if html.blank?
46
+ assert matcher.matches?(html), matcher.failure_message
47
+ end
38
48
  end
39
49
 
40
50
  module Webrat
@@ -157,6 +157,84 @@ class TestFormBuilder < Test::Unit::TestCase
157
157
  end
158
158
  end
159
159
 
160
+ context 'for #check_box method' do
161
+ should "display correct checkbox html" do
162
+ actual_html = standard_builder.check_box(:confirm_destroy, :class => 'large')
163
+ assert_has_tag('input.large[type=checkbox]', :id => 'user_confirm_destroy', :name => 'user[confirm_destroy]') { actual_html }
164
+ assert_has_tag('input[type=hidden]', :name => 'user[confirm_destroy]', :value => '0') { actual_html }
165
+ end
166
+
167
+ should "display correct checkbox html when checked" do
168
+ actual_html = standard_builder.check_box(:confirm_destroy, :checked => true)
169
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[confirm_destroy]') { actual_html }
170
+ end
171
+
172
+ should "display correct checkbox html as checked when object value matches" do
173
+ @user.stubs(:show_favorites => '1')
174
+ actual_html = standard_builder.check_box(:show_favorites, :value => '1')
175
+ assert_has_tag('input[type=checkbox]', :checked => 'checked', :name => 'user[show_favorites]') { actual_html }
176
+ end
177
+
178
+ should "display correct checkbox html as unchecked when object value doesn't match" do
179
+ @user.stubs(:show_favories => '0')
180
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'female')
181
+ assert_has_no_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
182
+ end
183
+
184
+ should "display correct unchecked hidden field when specified" do
185
+ actual_html = standard_builder.check_box(:show_favorites, :value => 'female', :uncheck_value => 'false')
186
+ assert_has_tag('input[type=hidden]', :name => 'user[show_favorites]', :value => 'false') { actual_html }
187
+ end
188
+
189
+ should "display correct checkbox in haml" do
190
+ visit '/haml/form_for'
191
+ assert_have_selector '#demo input[type=checkbox]', :checked => 'checked', :id => 'markup_user_remember_me', :name => 'markup_user[remember_me]'
192
+ end
193
+
194
+ should "display correct checkbox in erb" do
195
+ visit '/erb/form_for'
196
+ assert_have_selector '#demo input[type=checkbox]', :checked => 'checked', :id => 'markup_user_remember_me', :name => 'markup_user[remember_me]'
197
+ end
198
+ end
199
+
200
+ context 'for #radio_button method' do
201
+ should "display correct radio button html" do
202
+ actual_html = standard_builder.radio_button(:gender, :value => 'male', :class => 'large')
203
+ assert_has_tag('input.large[type=radio]', :id => 'user_gender_male', :name => 'user[gender]', :value => 'male') { actual_html }
204
+ end
205
+
206
+ should "display correct radio button html when checked" do
207
+ actual_html = standard_builder.radio_button(:gender, :checked => true)
208
+ assert_has_tag('input[type=radio]', :checked => 'checked', :name => 'user[gender]') { actual_html }
209
+ end
210
+
211
+ should "display correct radio button html as checked when object value matches" do
212
+ @user.stubs(:gender => 'male')
213
+ actual_html = standard_builder.radio_button(:gender, :value => 'male')
214
+ assert_has_tag('input[type=radio]', :checked => 'checked', :name => 'user[gender]') { actual_html }
215
+ end
216
+
217
+ should "display correct radio button html as unchecked when object value doesn't match" do
218
+ @user.stubs(:gender => 'male')
219
+ actual_html = standard_builder.radio_button(:gender, :value => 'female')
220
+ assert_has_no_tag('input[type=radio]', :checked => 'checked') { actual_html }
221
+ end
222
+
223
+ should "display correct radio button in haml" do
224
+ visit '/haml/form_for'
225
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_male', :name => 'markup_user[gender]', :value => 'male'
226
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_female', :name => 'markup_user[gender]', :value => 'female'
227
+ assert_have_selector '#demo input[type=radio][checked=checked]', :id => 'markup_user_gender_male'
228
+ end
229
+
230
+ should "display correct radio button in erb" do
231
+ visit '/erb/form_for'
232
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_male', :name => 'markup_user[gender]', :value => 'male'
233
+ assert_have_selector '#demo input[type=radio]', :id => 'markup_user_gender_female', :name => 'markup_user[gender]', :value => 'female'
234
+ assert_have_selector '#demo input[type=radio][checked=checked]', :id => 'markup_user_gender_male'
235
+ end
236
+ end
237
+
160
238
  context 'for #text_area method' do
161
239
  should "display correct text_area html" do
162
240
  actual_html = standard_builder.text_area(:about, :class => 'large')
@@ -113,32 +113,36 @@ 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 => 2
116
+ assert_have_selector 'form.simple-form label', :count => 3
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
+ assert_have_selector 'form.simple-form label', :content => "Gender", :for => 'gender'
119
120
  end
120
121
  should "display label tag in erb for advanced form" do
121
122
  visit '/erb/form_tag'
122
- assert_have_selector 'form.advanced-form label', :count => 4
123
+ assert_have_selector 'form.advanced-form label', :count => 5
123
124
  assert_have_selector 'form.advanced-form label.first', :content => "Nickname", :for => 'username'
124
125
  assert_have_selector 'form.advanced-form label.first', :content => "Password", :for => 'password'
125
126
  assert_have_selector 'form.advanced-form label.about', :content => "About Me", :for => 'about'
126
127
  assert_have_selector 'form.advanced-form label.photo', :content => "Photo" , :for => 'photo'
128
+ assert_have_selector 'form.advanced-form label.gender', :content => "Gender" , :for => 'gender'
127
129
  end
128
130
 
129
131
  should "display label tag in haml for simple form" do
130
132
  visit '/haml/form_tag'
131
- assert_have_selector 'form.simple-form label', :count => 2
133
+ assert_have_selector 'form.simple-form label', :count => 3
132
134
  assert_have_selector 'form.simple-form label', :content => "Username", :for => 'username'
133
135
  assert_have_selector 'form.simple-form label', :content => "Password", :for => 'password'
136
+ assert_have_selector 'form.simple-form label', :content => "Gender", :for => 'gender'
134
137
  end
135
138
  should "display label tag in haml for advanced form" do
136
139
  visit '/haml/form_tag'
137
- assert_have_selector 'form.advanced-form label', :count => 4
140
+ assert_have_selector 'form.advanced-form label', :count => 5
138
141
  assert_have_selector 'form.advanced-form label.first', :content => "Nickname", :for => 'username'
139
142
  assert_have_selector 'form.advanced-form label.first', :content => "Password", :for => 'password'
140
143
  assert_have_selector 'form.advanced-form label.about', :content => "About Me", :for => 'about'
141
144
  assert_have_selector 'form.advanced-form label.photo', :content => "Photo" , :for => 'photo'
145
+ assert_have_selector 'form.advanced-form label.gender', :content => "Gender" , :for => 'gender'
142
146
  end
143
147
  end
144
148
 
@@ -233,6 +237,58 @@ class TestFormHelpers < Test::Unit::TestCase
233
237
  end
234
238
  end
235
239
 
240
+ context "for #check_box_tag method" do
241
+ should "display check_box tag in ruby" do
242
+ actual_html = check_box_tag("clear_session")
243
+ assert_has_tag(:input, :type => 'checkbox', :value => '1', :name => 'clear_session') { actual_html }
244
+ end
245
+
246
+ should "display check_box tag in ruby with extended attributes" do
247
+ actual_html = check_box_tag("clear_session", :disabled => true, :checked => true)
248
+ assert_has_tag(:input, :type => 'checkbox', :disabled => 'disabled', :checked => 'checked') { actual_html }
249
+ end
250
+
251
+ should "display check_box tag in erb" do
252
+ visit '/erb/form_tag'
253
+ assert_have_selector 'form.simple-form input[type=checkbox]', :count => 1
254
+ assert_have_selector 'form.advanced-form input[type=checkbox]', :value => "1", :checked => 'checked'
255
+ end
256
+
257
+ should "display check_box tag in haml" do
258
+ visit '/haml/form_tag'
259
+ assert_have_selector 'form.simple-form input[type=checkbox]', :count => 1
260
+ assert_have_selector 'form.advanced-form input[type=checkbox]', :value => "1", :checked => 'checked'
261
+ end
262
+ end
263
+
264
+ context "for #radio_button_tag method" do
265
+ should "display radio_button tag in ruby" do
266
+ actual_html = radio_button_tag("gender", :value => 'male')
267
+ assert_has_tag(:input, :type => 'radio', :value => 'male', :name => 'gender') { actual_html }
268
+ end
269
+
270
+ should "display radio_button tag in ruby with extended attributes" do
271
+ actual_html = radio_button_tag("gender", :disabled => true, :checked => true)
272
+ assert_has_tag(:input, :type => 'radio', :disabled => 'disabled', :checked => 'checked') { actual_html }
273
+ end
274
+
275
+ should "display radio_button tag in erb" do
276
+ visit '/erb/form_tag'
277
+ assert_have_selector 'form.simple-form input[type=radio]', :count => 1, :value => 'male'
278
+ assert_have_selector 'form.simple-form input[type=radio]', :count => 1, :value => 'female'
279
+ assert_have_selector 'form.advanced-form input[type=radio]', :value => "male", :checked => 'checked'
280
+ assert_have_selector 'form.advanced-form input[type=radio]', :value => "female"
281
+ end
282
+
283
+ should "display radio_button tag in haml" do
284
+ visit '/haml/form_tag'
285
+ assert_have_selector 'form.simple-form input[type=radio]', :count => 1, :value => 'male'
286
+ assert_have_selector 'form.simple-form input[type=radio]', :count => 1, :value => 'female'
287
+ assert_have_selector 'form.advanced-form input[type=radio]', :value => "male", :checked => 'checked'
288
+ assert_have_selector 'form.advanced-form input[type=radio]', :value => "female"
289
+ end
290
+ end
291
+
236
292
  context 'for #submit_tag method' do
237
293
  should "display submit tag in ruby" do
238
294
  actual_html = submit_tag("Update", :class => 'success')
@@ -251,4 +307,5 @@ class TestFormHelpers < Test::Unit::TestCase
251
307
  assert_have_selector 'form.advanced-form input[type=submit]', :count => 1, :value => "Login"
252
308
  end
253
309
  end
310
+
254
311
  end
@@ -56,6 +56,14 @@ class TestTagHelpers < Test::Unit::TestCase
56
56
  actual_html = input_tag(:text, :class => "first", :id => 'texter')
57
57
  assert_has_tag('input.first#texter[type=text]') { actual_html }
58
58
  end
59
+ should "support checked attribute by using 'checked' if true" do
60
+ actual_html = input_tag(:checkbox, :checked => true)
61
+ assert_has_tag('input[type=checkbox]', :checked => 'checked') { actual_html }
62
+ end
63
+ should "support disabled attribute by using 'disabled' if true" do
64
+ actual_html = input_tag(:checkbox, :disabled => true)
65
+ assert_has_tag('input[type=checkbox]', :disabled => 'disabled') { actual_html }
66
+ end
59
67
  end
60
68
 
61
69
  end
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.1.10
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-27 00:00:00 -07:00
12
+ date: 2009-10-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency