merb-helpers 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. data/LICENSE +20 -0
  2. data/README +180 -0
  3. data/Rakefile +77 -0
  4. data/TODO +5 -0
  5. data/lib/merb-helpers.rb +38 -0
  6. data/lib/merb-helpers/core_ext.rb +57 -0
  7. data/lib/merb-helpers/core_ext/numeric.rb +388 -0
  8. data/lib/merb-helpers/date_time_formatting.rb +127 -0
  9. data/lib/merb-helpers/date_time_helpers.rb +190 -0
  10. data/lib/merb-helpers/form/builder.rb +410 -0
  11. data/lib/merb-helpers/form/helpers.rb +448 -0
  12. data/lib/merb-helpers/form_helpers.rb +24 -0
  13. data/lib/merb-helpers/tag_helpers.rb +61 -0
  14. data/lib/merb-helpers/text_helpers.rb +61 -0
  15. data/lib/merb-helpers/time_dsl.rb +59 -0
  16. data/spec/core_ext_spec.rb +19 -0
  17. data/spec/fixture/app/controllers/application.rb +4 -0
  18. data/spec/fixture/app/controllers/bound_check_box.rb +2 -0
  19. data/spec/fixture/app/controllers/bound_file_field.rb +2 -0
  20. data/spec/fixture/app/controllers/bound_hidden_field.rb +2 -0
  21. data/spec/fixture/app/controllers/bound_option_tag.rb +2 -0
  22. data/spec/fixture/app/controllers/bound_password_field.rb +2 -0
  23. data/spec/fixture/app/controllers/bound_radio_button.rb +2 -0
  24. data/spec/fixture/app/controllers/bound_radio_group.rb +2 -0
  25. data/spec/fixture/app/controllers/bound_select.rb +2 -0
  26. data/spec/fixture/app/controllers/bound_text_area.rb +2 -0
  27. data/spec/fixture/app/controllers/bound_text_field.rb +2 -0
  28. data/spec/fixture/app/controllers/button.rb +2 -0
  29. data/spec/fixture/app/controllers/check_box.rb +2 -0
  30. data/spec/fixture/app/controllers/custom_builder.rb +2 -0
  31. data/spec/fixture/app/controllers/delete_button.rb +2 -0
  32. data/spec/fixture/app/controllers/exceptions.rb +25 -0
  33. data/spec/fixture/app/controllers/field_set.rb +2 -0
  34. data/spec/fixture/app/controllers/fields_for.rb +3 -0
  35. data/spec/fixture/app/controllers/file_field.rb +2 -0
  36. data/spec/fixture/app/controllers/foo.rb +23 -0
  37. data/spec/fixture/app/controllers/form.rb +2 -0
  38. data/spec/fixture/app/controllers/form_for.rb +2 -0
  39. data/spec/fixture/app/controllers/hidden_field.rb +2 -0
  40. data/spec/fixture/app/controllers/label.rb +3 -0
  41. data/spec/fixture/app/controllers/numeric_ext.rb +3 -0
  42. data/spec/fixture/app/controllers/option_tag.rb +2 -0
  43. data/spec/fixture/app/controllers/password_field.rb +2 -0
  44. data/spec/fixture/app/controllers/radio_button.rb +2 -0
  45. data/spec/fixture/app/controllers/radio_group.rb +2 -0
  46. data/spec/fixture/app/controllers/select.rb +2 -0
  47. data/spec/fixture/app/controllers/specs_controller.rb +11 -0
  48. data/spec/fixture/app/controllers/submit.rb +2 -0
  49. data/spec/fixture/app/controllers/tag_helper.rb +21 -0
  50. data/spec/fixture/app/controllers/text_area.rb +2 -0
  51. data/spec/fixture/app/controllers/text_field.rb +3 -0
  52. data/spec/fixture/app/helpers/global_helpers.rb +8 -0
  53. data/spec/fixture/app/models/fake_dm_model.rb +25 -0
  54. data/spec/fixture/app/models/first_generic_fake_model.rb +57 -0
  55. data/spec/fixture/app/models/second_generic_fake_model.rb +18 -0
  56. data/spec/fixture/app/models/third_generic_fake_model.rb +3 -0
  57. data/spec/fixture/app/views/bound_check_box_specs/basic.html.erb +4 -0
  58. data/spec/fixture/app/views/bound_check_box_specs/checked.html.erb +4 -0
  59. data/spec/fixture/app/views/bound_check_box_specs/errors.html.erb +4 -0
  60. data/spec/fixture/app/views/bound_check_box_specs/label.html.erb +3 -0
  61. data/spec/fixture/app/views/bound_check_box_specs/on_and_off.html.erb +3 -0
  62. data/spec/fixture/app/views/bound_check_box_specs/raise_value_error.html.erb +3 -0
  63. data/spec/fixture/app/views/bound_file_field_specs/additional_attributes.html.erb +3 -0
  64. data/spec/fixture/app/views/bound_file_field_specs/takes_string.html.erb +3 -0
  65. data/spec/fixture/app/views/bound_file_field_specs/with_label.html.erb +3 -0
  66. data/spec/fixture/app/views/bound_hidden_field_specs/basic.html.erb +3 -0
  67. data/spec/fixture/app/views/bound_hidden_field_specs/errors.html.erb +3 -0
  68. data/spec/fixture/app/views/bound_hidden_field_specs/hidden_error.html.erb +3 -0
  69. data/spec/fixture/app/views/bound_hidden_field_specs/label.html.erb +3 -0
  70. data/spec/fixture/app/views/bound_option_tag_specs/grouped.html.erb +3 -0
  71. data/spec/fixture/app/views/bound_option_tag_specs/nested.html.erb +3 -0
  72. data/spec/fixture/app/views/bound_option_tag_specs/text_and_value.html.erb +4 -0
  73. data/spec/fixture/app/views/bound_password_field_specs/attributes.html.erb +3 -0
  74. data/spec/fixture/app/views/bound_password_field_specs/basic.html.erb +3 -0
  75. data/spec/fixture/app/views/bound_password_field_specs/label.html.erb +3 -0
  76. data/spec/fixture/app/views/bound_radio_button_specs/basic.html.erb +3 -0
  77. data/spec/fixture/app/views/bound_radio_group_specs/basic.html.erb +3 -0
  78. data/spec/fixture/app/views/bound_radio_group_specs/hashes.html.erb +3 -0
  79. data/spec/fixture/app/views/bound_radio_group_specs/mixed.html.erb +4 -0
  80. data/spec/fixture/app/views/bound_radio_group_specs/override_id.html.erb +3 -0
  81. data/spec/fixture/app/views/bound_select_specs/basic.html.erb +3 -0
  82. data/spec/fixture/app/views/bound_select_specs/blank.html.erb +3 -0
  83. data/spec/fixture/app/views/bound_select_specs/multiple.html.erb +3 -0
  84. data/spec/fixture/app/views/bound_select_specs/prompt.html.erb +3 -0
  85. data/spec/fixture/app/views/bound_select_specs/with_options.html.erb +3 -0
  86. data/spec/fixture/app/views/bound_select_specs/with_options_with_blank.html.erb +3 -0
  87. data/spec/fixture/app/views/bound_text_area_specs/basic.html.erb +3 -0
  88. data/spec/fixture/app/views/bound_text_field_specs/basic.html.erb +3 -0
  89. data/spec/fixture/app/views/button_specs/button_with_label.html.erb +1 -0
  90. data/spec/fixture/app/views/button_specs/button_with_values.html.erb +1 -0
  91. data/spec/fixture/app/views/button_specs/disabled_button.html.erb +1 -0
  92. data/spec/fixture/app/views/check_box_specs/basic.html.erb +1 -0
  93. data/spec/fixture/app/views/check_box_specs/boolean.html.erb +1 -0
  94. data/spec/fixture/app/views/check_box_specs/disabled.html.erb +1 -0
  95. data/spec/fixture/app/views/check_box_specs/label.html.erb +1 -0
  96. data/spec/fixture/app/views/check_box_specs/on_off_is_boolean.html.erb +1 -0
  97. data/spec/fixture/app/views/check_box_specs/raise_unless_both_on_and_off.html.erb +2 -0
  98. data/spec/fixture/app/views/check_box_specs/raises_error_if_not_boolean.html.erb +1 -0
  99. data/spec/fixture/app/views/check_box_specs/raises_error_if_on_off_and_boolean_false.html.erb +1 -0
  100. data/spec/fixture/app/views/check_box_specs/simple.html.erb +1 -0
  101. data/spec/fixture/app/views/check_box_specs/to_string.html.erb +8 -0
  102. data/spec/fixture/app/views/check_box_specs/unchecked.html.erb +2 -0
  103. data/spec/fixture/app/views/custom_builder_specs/everything.html.erb +10 -0
  104. data/spec/fixture/app/views/delete_button_specs/delete_with_explicit_url.html.erb +1 -0
  105. data/spec/fixture/app/views/delete_button_specs/delete_with_extra_params.html.erb +1 -0
  106. data/spec/fixture/app/views/delete_button_specs/delete_with_label.html.erb +1 -0
  107. data/spec/fixture/app/views/delete_button_specs/simple_delete.html.erb +1 -0
  108. data/spec/fixture/app/views/exeptions/client_error.html.erb +37 -0
  109. data/spec/fixture/app/views/exeptions/internal_server_error.html.erb +216 -0
  110. data/spec/fixture/app/views/exeptions/not_acceptable.html.erb +38 -0
  111. data/spec/fixture/app/views/exeptions/not_found.html.erb +40 -0
  112. data/spec/fixture/app/views/fields_for_specs/basic.html.erb +3 -0
  113. data/spec/fixture/app/views/fields_for_specs/midstream.html.erb +7 -0
  114. data/spec/fixture/app/views/fields_for_specs/nil.html.erb +3 -0
  115. data/spec/fixture/app/views/fieldset_specs/legend.html.erb +3 -0
  116. data/spec/fixture/app/views/file_field_specs/disabled.html.erb +1 -0
  117. data/spec/fixture/app/views/file_field_specs/makes_multipart.html.erb +3 -0
  118. data/spec/fixture/app/views/file_field_specs/with_label.html.erb +1 -0
  119. data/spec/fixture/app/views/file_field_specs/with_values.html.erb +1 -0
  120. data/spec/fixture/app/views/foo/bar.html.erb +0 -0
  121. data/spec/fixture/app/views/form_for_specs/basic.html.erb +3 -0
  122. data/spec/fixture/app/views/form_specs/create_a_form.html.erb +3 -0
  123. data/spec/fixture/app/views/form_specs/create_a_multipart_form.html.erb +3 -0
  124. data/spec/fixture/app/views/form_specs/fake_delete_if_set.html.erb +3 -0
  125. data/spec/fixture/app/views/form_specs/fake_put_if_set.html.erb +3 -0
  126. data/spec/fixture/app/views/form_specs/get_if_set.html.erb +3 -0
  127. data/spec/fixture/app/views/form_specs/post_by_default.html.erb +3 -0
  128. data/spec/fixture/app/views/form_specs/resourceful_form.html.erb +3 -0
  129. data/spec/fixture/app/views/hidden_field_specs/basic.html.erb +1 -0
  130. data/spec/fixture/app/views/hidden_field_specs/disabled.html.erb +1 -0
  131. data/spec/fixture/app/views/hidden_field_specs/label.html.erb +1 -0
  132. data/spec/fixture/app/views/label_specs/basic.html.erb +1 -0
  133. data/spec/fixture/app/views/layout/application.html.erb +11 -0
  134. data/spec/fixture/app/views/numeric_ext_specs/minutes_to_hours.html.erb +1 -0
  135. data/spec/fixture/app/views/numeric_ext_specs/to_concurrency_default.html.erb +1 -0
  136. data/spec/fixture/app/views/numeric_ext_specs/two_digits.html.erb +1 -0
  137. data/spec/fixture/app/views/option_tag_specs/array.html.erb +1 -0
  138. data/spec/fixture/app/views/option_tag_specs/clean.html.erb +1 -0
  139. data/spec/fixture/app/views/option_tag_specs/collection.html.erb +1 -0
  140. data/spec/fixture/app/views/option_tag_specs/multiple_selects.html.erb +1 -0
  141. data/spec/fixture/app/views/option_tag_specs/no_extra_attributes.html.erb +1 -0
  142. data/spec/fixture/app/views/option_tag_specs/optgroups.html.erb +1 -0
  143. data/spec/fixture/app/views/option_tag_specs/selected.html.erb +1 -0
  144. data/spec/fixture/app/views/option_tag_specs/with_blank.html.erb +1 -0
  145. data/spec/fixture/app/views/option_tag_specs/with_prompt.html.erb +1 -0
  146. data/spec/fixture/app/views/password_field_specs/basic.html.erb +1 -0
  147. data/spec/fixture/app/views/password_field_specs/disabled.html.erb +1 -0
  148. data/spec/fixture/app/views/radio_button_specs/basic.html.erb +1 -0
  149. data/spec/fixture/app/views/radio_button_specs/disabled.html.erb +1 -0
  150. data/spec/fixture/app/views/radio_button_specs/label.html.erb +1 -0
  151. data/spec/fixture/app/views/radio_group_specs/attributes.html.erb +1 -0
  152. data/spec/fixture/app/views/radio_group_specs/basic.html.erb +1 -0
  153. data/spec/fixture/app/views/radio_group_specs/hash.html.erb +1 -0
  154. data/spec/fixture/app/views/radio_group_specs/specific_attributes.html.erb +1 -0
  155. data/spec/fixture/app/views/select_specs/blank.html.erb +1 -0
  156. data/spec/fixture/app/views/select_specs/multiple.html.erb +1 -0
  157. data/spec/fixture/app/views/submit_specs/disabled_submit.html.erb +1 -0
  158. data/spec/fixture/app/views/submit_specs/submit_with_label.html.erb +1 -0
  159. data/spec/fixture/app/views/submit_specs/submit_with_values.html.erb +1 -0
  160. data/spec/fixture/app/views/tag_helper/nested_tags.html.erb +5 -0
  161. data/spec/fixture/app/views/tag_helper/tag_with_attributes.html.erb +1 -0
  162. data/spec/fixture/app/views/tag_helper/tag_with_content.html.erb +1 -0
  163. data/spec/fixture/app/views/tag_helper/tag_with_content_in_the_block.html.erb +3 -0
  164. data/spec/fixture/app/views/text_area_specs/basic.html.erb +1 -0
  165. data/spec/fixture/app/views/text_area_specs/disabled.html.erb +1 -0
  166. data/spec/fixture/app/views/text_area_specs/label.html.erb +1 -0
  167. data/spec/fixture/app/views/text_area_specs/nil.html.erb +1 -0
  168. data/spec/fixture/app/views/text_field_specs/basic.html.erb +1 -0
  169. data/spec/fixture/app/views/text_field_specs/class.html.erb +1 -0
  170. data/spec/fixture/app/views/text_field_specs/disabled.html.erb +1 -0
  171. data/spec/fixture/app/views/text_field_specs/label.html.erb +1 -0
  172. data/spec/fixture/config/environments/development.rb +6 -0
  173. data/spec/fixture/config/environments/production.rb +5 -0
  174. data/spec/fixture/config/environments/test.rb +6 -0
  175. data/spec/fixture/config/init.rb +46 -0
  176. data/spec/fixture/config/rack.rb +11 -0
  177. data/spec/fixture/config/router.rb +36 -0
  178. data/spec/fixture/log/merb.main.pid +1 -0
  179. data/spec/fixture/log/merb_test.log +624 -0
  180. data/spec/fixture/public/images/merb.jpg +0 -0
  181. data/spec/fixture/public/merb.fcgi +4 -0
  182. data/spec/fixture/public/stylesheets/master.css +119 -0
  183. data/spec/merb_helpers_config_spec.rb +82 -0
  184. data/spec/merb_helpers_date_time_spec.rb +257 -0
  185. data/spec/merb_helpers_form_spec.rb +1266 -0
  186. data/spec/merb_helpers_tag_helper_spec.rb +46 -0
  187. data/spec/merb_helpers_text_spec.rb +67 -0
  188. data/spec/numeric_extlib_spec.rb +135 -0
  189. data/spec/ordinalize_spec.rb +51 -0
  190. data/spec/spec_helper.rb +149 -0
  191. data/spec/time_dsl_spec.rb +43 -0
  192. metadata +303 -0
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ Merb::Router.prepare do
4
+ default_routes
5
+ end
6
+
7
+ describe Merb::Helpers::Tag do
8
+ include Merb::Helpers::Tag
9
+
10
+ describe "#tag" do
11
+ it 'generates <div>content</div> from tag :div, "content"' do
12
+ response = get "/tag_helper/tag_with_content"
13
+
14
+ response.body.should match_tag(:div)
15
+ response.body.should include("Astral Projection ~ Dancing Galaxy")
16
+ end
17
+
18
+ it 'outputs content returned by the block when block is given' do
19
+ response = get "/tag_helper/tag_with_content_in_the_block"
20
+
21
+ response.body.should match_tag(:div)
22
+ response.body.should include("Astral Projection ~ Trust in Trance 1")
23
+ end
24
+
25
+ it 'generates tag attributes for all of keys of last Hash' do
26
+ response = get "/tag_helper/tag_with_attributes"
27
+
28
+ doc = Hpricot(response.body)
29
+ (doc/"div.psy").size.should == 1
30
+ (doc/"div#bands").size.should == 1
31
+ (doc/"div[@invalid_attr='at least in html']").size.should == 1
32
+ end
33
+
34
+ it 'handles nesting of tags/blocks' do
35
+ response = get "/tag_helper/nested_tags"
36
+
37
+ doc = Hpricot(response.body)
38
+ (doc/"div.discography").size.should == 1
39
+ (doc/"div.discography"/"ul.albums").size.should == 1
40
+ (doc/"div.discography"/"ul.albums"/"li.first").size.should == 1
41
+
42
+ (doc/"#tit").size.should == 1
43
+ (doc/"#tit").first.inner_html.should == "Trust in Trance 2"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "cycle" do
4
+ include Merb::Helpers::Text
5
+
6
+ it "do basic cycling" do
7
+ cycle("one", 2, "3").should == 'one'
8
+ cycle("one", 2, "3").should == '2'
9
+ cycle("one", 2, "3").should == '3'
10
+ cycle("one", 2, "3").should == 'one'
11
+ cycle("one", 2, "3").should == '2'
12
+ cycle("one", 2, "3").should == '3'
13
+ end
14
+
15
+ it "should reset with new values" do
16
+ cycle("even", "odd").should == 'even'
17
+ cycle("even", "odd").should == 'odd'
18
+ cycle("even", "odd").should == 'even'
19
+ cycle("even", "odd").should == 'odd'
20
+ cycle(1, 2, 3).should == '1'
21
+ cycle(1, 2, 3).should == '2'
22
+ cycle(1, 2, 3).should == '3'
23
+ cycle(1, 2, 3).should == '1'
24
+ end
25
+
26
+ it "should support named cycles" do
27
+ cycle(1, 2, 3, :name => "numbers").should == "1"
28
+ cycle("red", "blue", :name => "colors").should == "red"
29
+ cycle(1, 2, 3, :name => "numbers").should == "2"
30
+ cycle("red", "blue", :name => "colors").should == "blue"
31
+ cycle(1, 2, 3, :name => "numbers").should == "3"
32
+ cycle("red", "blue", :name => "colors").should == "red"
33
+ end
34
+
35
+ it "should use a named cycle called 'default' by default" do
36
+ cycle(1, 2, 3).should == "1"
37
+ cycle(1, 2, 3, :name => "default").should == "2"
38
+ cycle(1, 2, 3).should == "3"
39
+ end
40
+
41
+ it "should be able to be reset" do
42
+ cycle(1, 2, 3).should == "1"
43
+ cycle(1, 2, 3).should == "2"
44
+ reset_cycle
45
+ cycle(1, 2, 3).should == "1"
46
+ end
47
+
48
+ it "should be able to reset a named cycle" do
49
+ cycle(1, 2, 3, :name => "numbers").should == "1"
50
+ cycle("red", "blue", :name => "colors").should == "red"
51
+ reset_cycle("numbers")
52
+ cycle(1, 2, 3, :name => "numbers").should == "1"
53
+ cycle("red", "blue", :name => "colors").should == "blue"
54
+ cycle(1, 2, 3, :name => "numbers").should == "2"
55
+ cycle("red", "blue", :name => "colors").should == "red"
56
+ end
57
+
58
+ it "should work with things other than strings" do
59
+ class Red; def to_s; 'red'; end; end;
60
+ class Blue; def to_s; 'blue'; end; end;
61
+ red = Red.new
62
+ blue = Blue.new
63
+ cycle(red, blue).should == "red"
64
+ cycle(red, blue).should == "blue"
65
+ cycle(red, blue).should == "red"
66
+ end
67
+ end
@@ -0,0 +1,135 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "Numeric helpers" do
4
+
5
+ before :each do
6
+ @controller = NumericExtSpecs.new(Merb::Request.new({}))
7
+ end
8
+
9
+ describe "with_delimiter" do
10
+
11
+ before(:each) do
12
+ @number = 12345678
13
+ end
14
+
15
+ it "should use the default formatting for numbers" do
16
+ @number.with_delimiter.should == "12,345,678"
17
+ @number.with_delimiter.should == @number.with_delimiter(:us)
18
+ end
19
+
20
+ it "should support passing another format" do
21
+ @number.with_delimiter(:fr).should == "12 345 678"
22
+ end
23
+
24
+ it "should support passing overwriting options" do
25
+ @number.with_delimiter(:fr, :delimiter => ',').should == "12,345,678"
26
+ 12345678.9.with_delimiter(:fr, :separator => ' et ').should == "12 345 678 et 9"
27
+ end
28
+ end
29
+
30
+
31
+ describe "with_precision" do
32
+ it "should use a default precision" do
33
+ 111.2345.with_precision.should == "111.235"
34
+ end
35
+
36
+ it "should support other precision formats" do
37
+ 111.2345.with_precision(:uk).should == "111.235"
38
+ end
39
+
40
+ it "should support overwriting precision options" do
41
+ 111.2345.with_precision(:uk, :precision => 1).should == "111.2"
42
+ 1234.567.with_precision(:us, :precision => 1, :separator => ',', :delimiter => '-').should == "1-234,6"
43
+ end
44
+ end
45
+
46
+
47
+ describe "to_concurrency" do
48
+
49
+ before(:each) do
50
+ @number = 1234567890.50
51
+ end
52
+
53
+ it "should use the US$ by default" do
54
+ @number.to_currency.should == "$1,234,567,890.50"
55
+ @number.to_currency.should == @number.to_currency(:us)
56
+ @number.to_currency.should == @number.to_currency(:default)
57
+ result = @controller.render :to_concurrency_default
58
+ result.should == "$1,234,567,890.50"
59
+ end
60
+
61
+ it "should use the precision settings of the format" do
62
+ 1234567890.506.to_currency(:us).should == "$1,234,567,890.51"
63
+ end
64
+
65
+ it "should support other formats" do
66
+ @number.to_currency(:uk).should == "&pound;1,234,567,890.50"
67
+ @number.to_currency(:fr).should == "1 234 567 890,50€"
68
+ end
69
+
70
+ it "should support overwriting options" do
71
+ 1234567890.506.to_currency(:us, :precision => 1).should == "$1,234,567,890.5"
72
+ 1234567890.516.to_currency(:us, :unit => "€").should == "€1,234,567,890.52"
73
+ 1234567890.506.to_currency(:us, :precision => 3, :unit => "€").should == "€1,234,567,890.506"
74
+ 1234567890.506.to_currency(:aus, :unit => "$AUD", :format => '%n %u').should == "1,234,567,890.51 $AUD"
75
+ end
76
+
77
+ end
78
+
79
+ describe "Numeric::Transformer formats" do
80
+
81
+ it "should be able to add a new format" do
82
+ Numeric::Transformer.default_format.should be_instance_of(Hash)
83
+ end
84
+
85
+ it "should be able to change the default format" do
86
+ original_default_format = Numeric::Transformer.default_format
87
+ original_default_format[:currency][:unit].should == "$"
88
+ Numeric::Transformer.change_default_format(:fr)
89
+ Numeric::Transformer.default_format.should_not == original_default_format
90
+ Numeric::Transformer.default_format[:currency][:unit].should == "€"
91
+ end
92
+
93
+ it "should be able to add a format" do
94
+ merb_format = {:merb =>
95
+ { :number => {
96
+ :precision => 3,
97
+ :delimiter => ' ',
98
+ :separator => ','
99
+ },
100
+ :currency => {
101
+ :unit => 'Merbollars',
102
+ :format => '%n %u',
103
+ :precision => 2
104
+ }
105
+ }
106
+ }
107
+ Numeric::Transformer.add_format(merb_format)
108
+ Numeric::Transformer.change_default_format(:merb)
109
+ 12345.to_currency.should == "12 345,00 Merbollars"
110
+ end
111
+
112
+ end
113
+
114
+ describe "other misc numeric helpers" do
115
+
116
+ describe "two_digits" do
117
+ it "should convert a numeric value in a 2 digit string (prepend with a zero if needed)" do
118
+ 7.two_digits.should == "07"
119
+ result = @controller.render :two_digits
120
+ result.should == "07"
121
+ end
122
+ end
123
+
124
+ describe "minutes_to_hours" do
125
+ it "should convert a number of minutes into an hour representation" do
126
+ result = @controller.render :minutes_to_hours
127
+ result.should == "05:15"
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+
135
+ end
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "#strftime_ordinalized" do
4
+
5
+ before(:all) do
6
+ @date = Date.parse('2008-5-3')
7
+ @time = Time.parse('2008-5-3 14:00')
8
+ end
9
+
10
+ it "should ordinalize a date even without a locale passed" do
11
+ @date.strftime_ordinalized('%b %d, %Y').should == "May 3rd, 2008"
12
+ end
13
+
14
+ end
15
+
16
+ describe "to_ordinalized_s" do
17
+
18
+ before(:each) do
19
+ @date = Date.parse('2008-5-3')
20
+ @time = Time.parse('2008-5-3 14:00')
21
+ end
22
+
23
+ it "should render a date using #to_s if no format is passed" do
24
+ @date.to_ordinalized_s.should == @date.to_s
25
+ end
26
+
27
+ it "should render a time using #to_s if no format is passed" do
28
+ @time.to_ordinalized_s.should == @time.to_s
29
+ end
30
+
31
+ it "should render a date or time using the db format" do
32
+ @date.to_ordinalized_s(:db).should == "2008-05-03 00:00:00"
33
+ @time.to_ordinalized_s(:db).should == "2008-05-03 14:00:00"
34
+ end
35
+
36
+ it "should render a date or time using the long format" do
37
+ @date.to_ordinalized_s(:long).should == "May 3rd, 2008 00:00"
38
+ @time.to_ordinalized_s(:long).should == "May 3rd, 2008 14:00"
39
+ end
40
+
41
+ it "should render a date or time using the time format" do
42
+ @date.to_ordinalized_s(:time).should == "00:00"
43
+ @time.to_ordinalized_s(:time).should == "14:00"
44
+ end
45
+
46
+ it "should render a date or a time using the short format" do
47
+ @date.to_ordinalized_s(:short).should == "3rd May 00:00"
48
+ @time.to_ordinalized_s(:short).should == "3rd May 14:00"
49
+ end
50
+
51
+ end
@@ -0,0 +1,149 @@
1
+ require "rubygems"
2
+ require "spec"
3
+ require "merb-core"
4
+ require File.join(File.dirname(__FILE__),"..",'lib',"merb-helpers")
5
+ require "date"
6
+
7
+ # Please read merb_helpers_form_spec.rb
8
+ # for more info on how to test helpers
9
+ # full stack specs are required
10
+ # use the app in spec/fixture to test helpers
11
+
12
+
13
+ default_options = {:environment => 'test', :adapter => 'runner'}.merge({:merb_root => File.dirname(__FILE__) / 'fixture'})
14
+ options = default_options.merge($START_OPTIONS || {})
15
+ Merb.start_environment(options)
16
+
17
+ def unload_merb_helpers
18
+ Merb.class_eval do
19
+ remove_const("Helpers") if defined?(Merb::Helpers)
20
+ end
21
+ end
22
+
23
+ def reload_merb_helpers
24
+ unload_merb_helpers
25
+ load(MERB_HELPERS_ROOT + "/lib/merb-helpers.rb")
26
+ Merb::Helpers.load
27
+ end
28
+
29
+ class FakeErrors
30
+
31
+ def initialize(model)
32
+ @model = model
33
+ end
34
+
35
+ def on(name)
36
+ name.to_s.include?("bad")
37
+ end
38
+
39
+ end
40
+
41
+ class FakeColumn
42
+ attr_accessor :name, :type
43
+
44
+ def initialize(name, type)
45
+ @name, @type = name, type
46
+ end
47
+
48
+ end
49
+
50
+
51
+
52
+
53
+ # -- Global custom matchers --
54
+
55
+ # A better +be_kind_of+ with more informative error messages.
56
+ #
57
+ # The default +be_kind_of+ just says
58
+ #
59
+ # "expected to return true but got false"
60
+ #
61
+ # This one says
62
+ #
63
+ # "expected File but got Tempfile"
64
+
65
+ module Merb
66
+ module Test
67
+ module RspecMatchers
68
+ class IncludeLog
69
+ def initialize(expected)
70
+ @expected = expected
71
+ end
72
+
73
+ def matches?(target)
74
+ target.log.rewind
75
+ @text = target.log.read
76
+ @text =~ (String === @expected ? /#{Regexp.escape @expected}/ : @expected)
77
+ end
78
+
79
+ def failure_message
80
+ "expected to find `#{@expected}' in the log but got:\n" <<
81
+ @text.map {|s| " #{s}" }.join
82
+ end
83
+
84
+ def negative_failure_message
85
+ "exected not to find `#{@expected}' in the log but got:\n" <<
86
+ @text.map {|s| " #{s}" }.join
87
+ end
88
+
89
+ def description
90
+ "include #{@text} in the log"
91
+ end
92
+ end
93
+
94
+ class BeKindOf
95
+ def initialize(expected) # + args
96
+ @expected = expected
97
+ end
98
+
99
+ def matches?(target)
100
+ @target = target
101
+ @target.kind_of?(@expected)
102
+ end
103
+
104
+ def failure_message
105
+ "expected #{@expected} but got #{@target.class}"
106
+ end
107
+
108
+ def negative_failure_message
109
+ "expected #{@expected} to not be #{@target.class}"
110
+ end
111
+
112
+ def description
113
+ "be_kind_of #{@target}"
114
+ end
115
+ end
116
+
117
+ def be_kind_of(expected) # + args
118
+ BeKindOf.new(expected)
119
+ end
120
+
121
+ def include_log(expected)
122
+ IncludeLog.new(expected)
123
+ end
124
+ end
125
+
126
+ module Helper
127
+ def running(&blk) blk; end
128
+
129
+ def executing(&blk) blk; end
130
+
131
+ def doing(&blk) blk; end
132
+
133
+ def calling(&blk) blk; end
134
+ end
135
+ end
136
+ end
137
+
138
+ Spec::Runner.configure do |config|
139
+ config.include Merb::Test::Helper
140
+ config.include Merb::Test::RspecMatchers
141
+ config.include Merb::Test::Rspec::ViewMatchers
142
+ config.include Merb::Test::RequestHelper
143
+
144
+ def with_level(level)
145
+ Merb.logger = Merb::Logger.new(StringIO.new, level)
146
+ yield
147
+ Merb.logger
148
+ end
149
+ end