padrino-helpers-cj 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +21 -0
  4. data/.yardopts +1 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +239 -0
  7. data/Rakefile +5 -0
  8. data/lib/padrino-helpers.rb +62 -0
  9. data/lib/padrino-helpers/asset_tag_helpers.rb +390 -0
  10. data/lib/padrino-helpers/form_builder/abstract_form_builder.rb +283 -0
  11. data/lib/padrino-helpers/form_builder/deprecated_builder_methods.rb +92 -0
  12. data/lib/padrino-helpers/form_builder/standard_form_builder.rb +40 -0
  13. data/lib/padrino-helpers/form_helpers.rb +633 -0
  14. data/lib/padrino-helpers/form_helpers/errors.rb +138 -0
  15. data/lib/padrino-helpers/form_helpers/options.rb +97 -0
  16. data/lib/padrino-helpers/form_helpers/security.rb +70 -0
  17. data/lib/padrino-helpers/format_helpers.rb +372 -0
  18. data/lib/padrino-helpers/locale/cs.yml +103 -0
  19. data/lib/padrino-helpers/locale/da.yml +91 -0
  20. data/lib/padrino-helpers/locale/de.yml +81 -0
  21. data/lib/padrino-helpers/locale/en.yml +103 -0
  22. data/lib/padrino-helpers/locale/es.yml +103 -0
  23. data/lib/padrino-helpers/locale/fr.yml +79 -0
  24. data/lib/padrino-helpers/locale/hu.yml +103 -0
  25. data/lib/padrino-helpers/locale/it.yml +89 -0
  26. data/lib/padrino-helpers/locale/ja.yml +103 -0
  27. data/lib/padrino-helpers/locale/lv.yml +103 -0
  28. data/lib/padrino-helpers/locale/nl.yml +82 -0
  29. data/lib/padrino-helpers/locale/no.yml +91 -0
  30. data/lib/padrino-helpers/locale/pl.yml +95 -0
  31. data/lib/padrino-helpers/locale/pt_br.yml +103 -0
  32. data/lib/padrino-helpers/locale/ro.yml +103 -0
  33. data/lib/padrino-helpers/locale/ru.yml +103 -0
  34. data/lib/padrino-helpers/locale/sv.yml +103 -0
  35. data/lib/padrino-helpers/locale/tr.yml +103 -0
  36. data/lib/padrino-helpers/locale/uk.yml +103 -0
  37. data/lib/padrino-helpers/locale/zh_cn.yml +103 -0
  38. data/lib/padrino-helpers/locale/zh_tw.yml +103 -0
  39. data/lib/padrino-helpers/number_helpers.rb +283 -0
  40. data/lib/padrino-helpers/output_helpers.rb +226 -0
  41. data/lib/padrino-helpers/output_helpers/abstract_handler.rb +61 -0
  42. data/lib/padrino-helpers/output_helpers/erb_handler.rb +27 -0
  43. data/lib/padrino-helpers/output_helpers/haml_handler.rb +25 -0
  44. data/lib/padrino-helpers/output_helpers/slim_handler.rb +18 -0
  45. data/lib/padrino-helpers/render_helpers.rb +63 -0
  46. data/lib/padrino-helpers/tag_helpers.rb +294 -0
  47. data/lib/padrino-helpers/translation_helpers.rb +36 -0
  48. data/lib/padrino/rendering.rb +397 -0
  49. data/lib/padrino/rendering/erubis_template.rb +66 -0
  50. data/lib/padrino/rendering/haml_template.rb +26 -0
  51. data/lib/padrino/rendering/slim_template.rb +20 -0
  52. data/padrino-helpers.gemspec +29 -0
  53. data/test/fixtures/apps/.components +6 -0
  54. data/test/fixtures/apps/.gitignore +7 -0
  55. data/test/fixtures/apps/render.rb +25 -0
  56. data/test/fixtures/apps/views/article/comment/show.slim +1 -0
  57. data/test/fixtures/apps/views/blog/post.erb +1 -0
  58. data/test/fixtures/apps/views/layouts/specific.erb +1 -0
  59. data/test/fixtures/apps/views/test/post.erb +1 -0
  60. data/test/fixtures/layouts/layout.erb +1 -0
  61. data/test/fixtures/markup_app/app.rb +87 -0
  62. data/test/fixtures/markup_app/views/button_to.erb +8 -0
  63. data/test/fixtures/markup_app/views/button_to.haml +5 -0
  64. data/test/fixtures/markup_app/views/button_to.slim +6 -0
  65. data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
  66. data/test/fixtures/markup_app/views/capture_concat.haml +12 -0
  67. data/test/fixtures/markup_app/views/capture_concat.slim +12 -0
  68. data/test/fixtures/markup_app/views/content_for.erb +23 -0
  69. data/test/fixtures/markup_app/views/content_for.haml +19 -0
  70. data/test/fixtures/markup_app/views/content_for.slim +19 -0
  71. data/test/fixtures/markup_app/views/content_tag.erb +13 -0
  72. data/test/fixtures/markup_app/views/content_tag.haml +11 -0
  73. data/test/fixtures/markup_app/views/content_tag.slim +11 -0
  74. data/test/fixtures/markup_app/views/current_engine.erb +5 -0
  75. data/test/fixtures/markup_app/views/current_engine.haml +5 -0
  76. data/test/fixtures/markup_app/views/current_engine.slim +5 -0
  77. data/test/fixtures/markup_app/views/fields_for.erb +20 -0
  78. data/test/fixtures/markup_app/views/fields_for.haml +15 -0
  79. data/test/fixtures/markup_app/views/fields_for.slim +15 -0
  80. data/test/fixtures/markup_app/views/form_for.erb +72 -0
  81. data/test/fixtures/markup_app/views/form_for.haml +59 -0
  82. data/test/fixtures/markup_app/views/form_for.slim +59 -0
  83. data/test/fixtures/markup_app/views/form_tag.erb +95 -0
  84. data/test/fixtures/markup_app/views/form_tag.haml +78 -0
  85. data/test/fixtures/markup_app/views/form_tag.slim +79 -0
  86. data/test/fixtures/markup_app/views/link_to.erb +5 -0
  87. data/test/fixtures/markup_app/views/link_to.haml +4 -0
  88. data/test/fixtures/markup_app/views/link_to.slim +4 -0
  89. data/test/fixtures/markup_app/views/mail_to.erb +3 -0
  90. data/test/fixtures/markup_app/views/mail_to.haml +3 -0
  91. data/test/fixtures/markup_app/views/mail_to.slim +3 -0
  92. data/test/fixtures/markup_app/views/meta_tag.erb +3 -0
  93. data/test/fixtures/markup_app/views/meta_tag.haml +3 -0
  94. data/test/fixtures/markup_app/views/meta_tag.slim +3 -0
  95. data/test/fixtures/markup_app/views/partials/_erb.erb +1 -0
  96. data/test/fixtures/markup_app/views/partials/_haml.haml +1 -0
  97. data/test/fixtures/markup_app/views/partials/_slim.slim +1 -0
  98. data/test/fixtures/markup_app/views/simple_partial.erb +1 -0
  99. data/test/fixtures/markup_app/views/simple_partial.haml +1 -0
  100. data/test/fixtures/markup_app/views/simple_partial.slim +1 -0
  101. data/test/fixtures/render_app/app.rb +94 -0
  102. data/test/fixtures/render_app/views/_deep.erb +3 -0
  103. data/test/fixtures/render_app/views/_deep.haml +2 -0
  104. data/test/fixtures/render_app/views/_deep.slim +2 -0
  105. data/test/fixtures/render_app/views/_partial_block_erb.erb +10 -0
  106. data/test/fixtures/render_app/views/_partial_block_haml.haml +7 -0
  107. data/test/fixtures/render_app/views/_partial_block_slim.slim +7 -0
  108. data/test/fixtures/render_app/views/_unsafe.html.builder +2 -0
  109. data/test/fixtures/render_app/views/_unsafe_object.html.builder +2 -0
  110. data/test/fixtures/render_app/views/current_engine.haml +5 -0
  111. data/test/fixtures/render_app/views/current_engines/_erb.erb +1 -0
  112. data/test/fixtures/render_app/views/current_engines/_haml.haml +1 -0
  113. data/test/fixtures/render_app/views/current_engines/_slim.slim +1 -0
  114. data/test/fixtures/render_app/views/double_capture_erb.erb +3 -0
  115. data/test/fixtures/render_app/views/double_capture_haml.haml +2 -0
  116. data/test/fixtures/render_app/views/double_capture_slim.slim +2 -0
  117. data/test/fixtures/render_app/views/erb/test.erb +1 -0
  118. data/test/fixtures/render_app/views/explicit_engine.haml +5 -0
  119. data/test/fixtures/render_app/views/haml/test.haml +1 -0
  120. data/test/fixtures/render_app/views/render_block_erb.erb +5 -0
  121. data/test/fixtures/render_app/views/render_block_haml.haml +4 -0
  122. data/test/fixtures/render_app/views/render_block_slim.slim +4 -0
  123. data/test/fixtures/render_app/views/ruby_block_capture_erb.erb +1 -0
  124. data/test/fixtures/render_app/views/ruby_block_capture_haml.haml +1 -0
  125. data/test/fixtures/render_app/views/ruby_block_capture_slim.slim +1 -0
  126. data/test/fixtures/render_app/views/template/_user.haml +7 -0
  127. data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
  128. data/test/fixtures/render_app/views/template/some_template.haml +2 -0
  129. data/test/fixtures/render_app/views/wrong_capture_erb.erb +3 -0
  130. data/test/fixtures/render_app/views/wrong_capture_haml.haml +2 -0
  131. data/test/fixtures/render_app/views/wrong_capture_slim.slim +2 -0
  132. data/test/helper.rb +131 -0
  133. data/test/test_asset_tag_helpers.rb +406 -0
  134. data/test/test_form_builder.rb +1216 -0
  135. data/test/test_form_helpers.rb +1056 -0
  136. data/test/test_format_helpers.rb +251 -0
  137. data/test/test_helpers.rb +10 -0
  138. data/test/test_locale.rb +20 -0
  139. data/test/test_number_helpers.rb +142 -0
  140. data/test/test_output_helpers.rb +157 -0
  141. data/test/test_render_helpers.rb +215 -0
  142. data/test/test_rendering.rb +694 -0
  143. data/test/test_rendering_extensions.rb +14 -0
  144. data/test/test_tag_helpers.rb +131 -0
  145. metadata +241 -0
@@ -0,0 +1,215 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/render_app/app')
3
+
4
+ describe "RenderHelpers" do
5
+ def app
6
+ RenderDemo
7
+ end
8
+
9
+ describe 'for #partial method and object' do
10
+ before { visit '/partial/object' }
11
+ it 'should render partial html with object' do
12
+ assert_have_selector "h1", :content => "User name is John"
13
+ end
14
+ it 'should have no counter index for single item' do
15
+ assert_have_no_selector "p", :content => "My counter is 1", :count => 1
16
+ end
17
+ it 'should include extra locals information' do
18
+ assert_have_selector 'p', :content => "Extra is bar"
19
+ end
20
+ end
21
+
22
+ describe 'for #partial method and collection' do
23
+ before { visit '/partial/collection' }
24
+ it 'should render partial html with collection' do
25
+ assert_have_selector "h1", :content => "User name is John"
26
+ assert_have_selector "h1", :content => "User name is Billy"
27
+ end
28
+ it 'should include counter which contains item index' do
29
+ assert_have_selector "p", :content => "My counter is 1"
30
+ assert_have_selector "p", :content => "My counter is 2"
31
+ end
32
+ it 'should include extra locals information' do
33
+ assert_have_selector 'p', :content => "Extra is bar"
34
+ end
35
+ end
36
+
37
+ describe 'for #partial with ext and collection' do
38
+ before { visit '/partial/collection.ext' }
39
+ it 'should not fail horribly with `invalid locals key` RuntimeError' do
40
+ assert_have_selector "h1", :content => "User name is John"
41
+ end
42
+ end
43
+
44
+ describe 'for #partial method and locals' do
45
+ before { visit '/partial/locals' }
46
+ it 'should render partial html with locals' do
47
+ assert_have_selector "h1", :content => "User name is John"
48
+ end
49
+ it 'should have no counter index for single item' do
50
+ assert_have_no_selector "p", :content => "My counter is 1", :count => 1
51
+ end
52
+ it 'should include extra locals information' do
53
+ assert_have_selector 'p', :content => "Extra is bar"
54
+ end
55
+ end
56
+
57
+ describe 'for #partial method taking a path starting with forward slash' do
58
+ before { visit '/partial/foward_slash' }
59
+ it 'should render partial without throwing an error' do
60
+ assert_have_selector "h1", :content => "User name is John"
61
+ end
62
+ end
63
+
64
+ describe 'for #partial method with unsafe engine' do
65
+ it 'should render partial without escaping it' do
66
+ visit '/partial/unsafe'
67
+ assert_have_selector "h1", :content => "User name is John"
68
+ end
69
+ it 'should render partial object without escaping it' do
70
+ visit '/partial/unsafe_one'
71
+ assert_have_selector "h1", :content => "User name is Mary"
72
+ end
73
+ it 'should render partial collection without escaping it' do
74
+ visit '/partial/unsafe_many'
75
+ assert_have_selector "h1", :content => "User name is John"
76
+ assert_have_selector "h1", :content => "User name is Mary"
77
+ end
78
+ it 'should render unsafe partial without escaping it' do
79
+ visit '/partial/unsafe?block=%3Cevil%3E'
80
+ assert_have_selector "h1", :content => "User name is John"
81
+ assert_have_selector "evil"
82
+ end
83
+ it 'should render unsafe partial object without escaping it' do
84
+ visit '/partial/unsafe_one?block=%3Cevil%3E'
85
+ assert_have_selector "h1", :content => "User name is Mary"
86
+ assert_have_selector "evil"
87
+ end
88
+ it 'should render unsafe partial collection without escaping it' do
89
+ visit '/partial/unsafe_many?block=%3Cevil%3E'
90
+ assert_have_selector "h1", :content => "User name is John"
91
+ assert_have_selector "h1", :content => "User name is Mary"
92
+ assert_have_selector "evil"
93
+ end
94
+ end
95
+
96
+ describe 'render with block' do
97
+ it 'should render slim with block' do
98
+ visit '/render_block_slim'
99
+ assert_have_selector 'h1', :content => 'prefix'
100
+ assert_have_selector 'h3', :content => 'postfix'
101
+ assert_have_selector '.slim-block'
102
+ assert_have_selector 'div', :content => 'go block!'
103
+ end
104
+ it 'should render erb with block' do
105
+ visit '/render_block_erb'
106
+ assert_have_selector 'h1', :content => 'prefix'
107
+ assert_have_selector 'h3', :content => 'postfix'
108
+ assert_have_selector '.erb-block'
109
+ assert_have_selector 'div', :content => 'go block!'
110
+ end
111
+ it 'should render haml with block' do
112
+ visit '/render_block_haml'
113
+ assert_have_selector 'h1', :content => 'prefix'
114
+ assert_have_selector 'h3', :content => 'postfix'
115
+ assert_have_selector '.haml-block'
116
+ assert_have_selector 'div', :content => 'go block!'
117
+ end
118
+ end
119
+
120
+ describe 'partial with block' do
121
+ it 'should show partial slim with block' do
122
+ visit '/partial_block_slim'
123
+ assert_have_selector 'h1', :content => 'prefix'
124
+ assert_have_selector 'h3', :content => 'postfix'
125
+ assert_have_selector '.slim-block'
126
+ assert_have_selector 'div', :content => 'go block!'
127
+ assert_have_selector 'div.deep', :content => 'Done'
128
+ end
129
+ it 'should show partial erb with block' do
130
+ visit '/partial_block_erb'
131
+ assert_have_selector 'h1', :content => 'prefix'
132
+ assert_have_selector 'h3', :content => 'postfix'
133
+ assert_have_selector '.erb-block'
134
+ assert_have_selector 'div', :content => 'go block!'
135
+ assert_have_selector 'div.deep', :content => 'Done'
136
+ end
137
+ it 'should show partial haml with block' do
138
+ visit '/partial_block_haml'
139
+ assert_have_selector 'h1', :content => 'prefix'
140
+ assert_have_selector 'h3', :content => 'postfix'
141
+ assert_have_selector '.haml-block'
142
+ assert_have_selector 'div', :content => 'go block!'
143
+ assert_have_selector 'div.deep', :content => 'Done'
144
+ end
145
+ end
146
+
147
+ describe 'for #current_engine method' do
148
+ it 'should detect correctly current engine for a padrino application' do
149
+ visit '/current_engine'
150
+ assert_have_selector 'p.start', :content => "haml"
151
+ assert_have_selector 'p.haml span', :content => "haml"
152
+ assert_have_selector 'p.erb span', :content => "erb"
153
+ assert_have_selector 'p.slim span', :content => "slim"
154
+ assert_have_selector 'p.end', :content => "haml"
155
+ end
156
+
157
+ it 'should detect correctly current engine for explicit engine on partials' do
158
+ visit '/explicit_engine'
159
+ assert_have_selector 'p.start', :content => "haml"
160
+ assert_have_selector 'p.haml span', :content => "haml"
161
+ assert_have_selector 'p.erb span', :content => "erb"
162
+ assert_have_selector 'p.slim span', :content => "slim"
163
+ assert_have_selector 'p.end', :content => "haml"
164
+ end
165
+
166
+ it 'should capture slim template once and only once' do
167
+ $number_of_captures = 0
168
+ visit '/double_capture_slim'
169
+ assert_equal 1,$number_of_captures
170
+ end
171
+
172
+ it 'should capture haml template once and only once' do
173
+ $number_of_captures = 0
174
+ visit '/double_capture_haml'
175
+ assert_equal 1,$number_of_captures
176
+ end
177
+
178
+ it 'should capture erb template once and only once' do
179
+ $number_of_captures = 0
180
+ visit '/double_capture_erb'
181
+ assert_equal 1,$number_of_captures
182
+ end
183
+
184
+ it 'should fail on wrong erb usage' do
185
+ assert_raises(SyntaxError) do
186
+ visit '/wrong_capture_erb'
187
+ end
188
+ end
189
+
190
+ it 'should ignore wrong haml usage' do
191
+ visit '/wrong_capture_haml'
192
+ assert_have_no_selector 'p', :content => 'this is wrong'
193
+ end
194
+
195
+ it 'should ignore wrong slim usage' do
196
+ visit '/wrong_capture_slim'
197
+ assert_have_no_selector 'p', :content => 'this is wrong'
198
+ end
199
+
200
+ it 'should support weird ruby blocks in erb' do
201
+ visit '/ruby_block_capture_erb'
202
+ assert_have_selector 'b', :content => 'c'
203
+ end
204
+
205
+ it 'should support weird ruby blocks in haml' do
206
+ visit '/ruby_block_capture_haml'
207
+ assert_have_selector 'b', :content => 'c'
208
+ end
209
+
210
+ it 'should support weird ruby blocks in slim' do
211
+ visit '/ruby_block_capture_slim'
212
+ assert_have_selector 'b', :content => 'c'
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,694 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+ require 'slim'
3
+
4
+ describe "Rendering" do
5
+ def setup
6
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = false
7
+ I18n.enforce_available_locales = true
8
+ end
9
+
10
+ def teardown
11
+ I18n.locale = :en
12
+ remove_views
13
+ end
14
+
15
+ describe 'for application layout functionality' do
16
+
17
+ it 'should get no layout' do
18
+ mock_app do
19
+ get("/"){ "no layout" }
20
+ end
21
+
22
+ get "/"
23
+ assert_equal "no layout", body
24
+ end
25
+
26
+ it 'should be compatible with sinatra layout' do
27
+ mock_app do
28
+ layout do
29
+ "this is a <%= yield %>"
30
+ end
31
+
32
+ get("/"){ render :erb, "sinatra layout", :layout => true }
33
+ end
34
+
35
+ get "/"
36
+ assert_equal "this is a sinatra layout", body
37
+ end
38
+
39
+ it 'should use rails way layout' do
40
+ with_layout :application, "this is a <%= yield %>" do
41
+ mock_app do
42
+ get("/"){ render :erb, "rails way layout" }
43
+ end
44
+
45
+ get "/"
46
+ assert_equal "this is a rails way layout", body
47
+ end
48
+ end
49
+
50
+ it 'should use rails way for a custom layout' do
51
+ with_layout "layouts/custom", "this is a <%= yield %>" do
52
+ mock_app do
53
+ layout :custom
54
+ get("/"){ render :erb, "rails way custom layout" }
55
+ end
56
+
57
+ get "/"
58
+ assert_equal "this is a rails way custom layout", body
59
+ end
60
+ end
61
+
62
+ it 'should not use layout' do
63
+ with_layout :application, "this is an <%= yield %>" do
64
+ with_view :index, "index" do
65
+ mock_app do
66
+ get("/with/layout"){ render :index }
67
+ get("/without/layout"){ render :index, :layout => false }
68
+ end
69
+ get "/with/layout"
70
+ assert_equal "this is an index", body
71
+ get "/without/layout"
72
+ assert_equal "index", body
73
+ end
74
+ end
75
+ end
76
+
77
+ it 'should not use layout with js format' do
78
+ create_layout :application, "this is an <%= yield %>"
79
+ create_view :foo, "erb file"
80
+ create_view :foo, "js file", :format => :js
81
+ mock_app do
82
+ get('/layout_test', :provides => [:html, :js]){ render :foo }
83
+ end
84
+ get "/layout_test"
85
+ assert_equal "this is an erb file", body
86
+ get "/layout_test.js"
87
+ assert_equal "js file", body
88
+ end
89
+
90
+ it 'should use correct layout for each format' do
91
+ create_layout :application, "this is an <%= yield %>"
92
+ create_layout :application, "document start <%= yield %> end", :format => :xml
93
+ create_view :foo, "erb file"
94
+ create_view :foo, "xml file", :format => :xml
95
+ mock_app do
96
+ get('/layout_test', :provides => [:html, :xml]){ render :foo }
97
+ end
98
+ get "/layout_test"
99
+ assert_equal "this is an erb file", body
100
+ get "/layout_test.xml"
101
+ assert_equal "document start xml file end", body
102
+ end
103
+
104
+ it 'should by default use html file when no other is given' do
105
+ create_layout :baz, "html file", :format => :html
106
+
107
+ mock_app do
108
+ get('/content_type_test', :provides => [:html, :xml]) { render :baz }
109
+ end
110
+
111
+ get "/content_type_test"
112
+ assert_equal "html file", body
113
+ get "/content_type_test.html"
114
+ assert_equal "html file", body
115
+ get "/content_type_test.xml"
116
+ assert_equal "html file", body
117
+ end
118
+
119
+ it 'should not use html file when DEFAULT_RENDERING_OPTIONS[:strict_format] == true' do
120
+ create_layout :foo, "html file", :format => :html
121
+
122
+ mock_app do
123
+ get('/default_rendering_test', :provides => [:html, :xml]) { render :foo }
124
+ end
125
+
126
+ @save = Padrino::Rendering::DEFAULT_RENDERING_OPTIONS
127
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS[:strict_format] = true
128
+
129
+ get "/default_rendering_test"
130
+ assert_equal "html file", body
131
+ assert_raises Padrino::Rendering::TemplateNotFound do
132
+ get "/default_rendering_test.xml"
133
+ end
134
+
135
+ Padrino::Rendering::DEFAULT_RENDERING_OPTIONS.merge!(@save)
136
+ end
137
+
138
+ it 'should use correct layout with each controller' do
139
+ create_layout :foo, "foo layout at <%= yield %>"
140
+ create_layout :bar, "bar layout at <%= yield %>"
141
+ create_layout :baz, "baz layout at <%= yield %>"
142
+ create_layout :application, "default layout at <%= yield %>"
143
+ mock_app do
144
+ get("/"){ render :erb, "application" }
145
+ controller :foo do
146
+ layout :foo
147
+ get("/"){ render :erb, "foo" }
148
+ end
149
+ controller :bar do
150
+ layout :bar
151
+ get("/"){ render :erb, "bar" }
152
+ end
153
+ controller :baz do
154
+ layout :baz
155
+ get("/"){ render :erb, "baz", :layout => true }
156
+ end
157
+ controller :none do
158
+ get("/") { render :erb, "none" }
159
+ get("/with_foo_layout") { render :erb, "none with layout", :layout => :foo }
160
+ end
161
+ end
162
+ get "/foo"
163
+ assert_equal "foo layout at foo", body
164
+ get "/bar"
165
+ assert_equal "bar layout at bar", body
166
+ get "/baz"
167
+ assert_equal "baz layout at baz", body
168
+ get "/none"
169
+ assert_equal "default layout at none", body
170
+ get "/none/with_foo_layout"
171
+ assert_equal "foo layout at none with layout", body
172
+ get "/"
173
+ assert_equal "default layout at application", body
174
+ end
175
+ end
176
+
177
+ it 'should solve layout in layouts paths' do
178
+ create_layout :foo, "foo layout <%= yield %>"
179
+ create_layout :"layouts/bar", "bar layout <%= yield %>"
180
+ mock_app do
181
+ get("/") { render :erb, "none" }
182
+ get("/foo") { render :erb, "foo", :layout => :foo }
183
+ get("/bar") { render :erb, "bar", :layout => :bar }
184
+ end
185
+ get "/"
186
+ assert_equal "none", body
187
+ get "/foo"
188
+ assert_equal "foo layout foo", body
189
+ get "/bar"
190
+ assert_equal "bar layout bar", body
191
+ end
192
+
193
+ it 'should allow to render template with layout option that using other template engine.' do
194
+ create_layout :"layouts/foo", "application layout for <%= yield %>", :format => :erb
195
+ create_view :slim, "| slim", :format => :slim
196
+ create_view :haml, "haml", :format => :haml
197
+ create_view :erb, "erb", :format => :erb
198
+ mock_app do
199
+ get("/slim") { render("slim.slim", :layout => "foo.erb") }
200
+ get("/haml") { render("haml.haml", :layout => "foo.erb") }
201
+ get("/erb") { render("erb.erb", :layout => "foo.erb") }
202
+ end
203
+ get "/slim"
204
+ assert_equal "application layout for slim", body.chomp
205
+ get "/haml"
206
+ assert_equal "application layout for haml", body.chomp
207
+ get "/erb"
208
+ assert_equal "application layout for erb", body.chomp
209
+ end
210
+
211
+ it 'should allow to use extension with layout method.' do
212
+ create_layout :"layouts/bar", "application layout for <%= yield %>", :format => :erb
213
+ create_view :slim, "| slim", :format => :slim
214
+ create_view :haml, "haml", :format => :haml
215
+ create_view :erb, "erb", :format => :erb
216
+ mock_app do
217
+ layout "bar.erb"
218
+ get("/slim") { render("slim.slim") }
219
+ get("/haml") { render("haml.haml") }
220
+ get("/erb") { render("erb.erb") }
221
+ end
222
+ get "/slim"
223
+ assert_equal "application layout for slim", body.chomp
224
+ get "/haml"
225
+ assert_equal "application layout for haml", body.chomp
226
+ get "/erb"
227
+ assert_equal "application layout for erb", body.chomp
228
+ end
229
+
230
+ it 'should find a layout by symbol' do
231
+ create_layout :"layouts/bar", "application layout for <%= yield %>", :format => :erb
232
+ create_view :slim, "| slim", :format => :slim
233
+ create_view :haml, "haml", :format => :haml
234
+ create_view :erb, "erb", :format => :erb
235
+ mock_app do
236
+ layout :bar
237
+ get("/slim") { render("slim.slim") }
238
+ get("/haml") { render("haml.haml") }
239
+ get("/erb") { render("erb.erb") }
240
+ end
241
+ get "/slim"
242
+ assert_equal "application layout for slim", body.chomp
243
+ get "/haml"
244
+ assert_equal "application layout for haml", body.chomp
245
+ get "/erb"
246
+ assert_equal "application layout for erb", body.chomp
247
+ end
248
+
249
+ it 'should not apply default layout to unsupported layout engines' do
250
+ create_layout :application, "erb template <%= yield %>", :format => :erb
251
+ create_view 'foo', "xml.instruct!", :format => :builder
252
+ mock_app do
253
+ get('/layout_test.xml' ){ render :foo }
254
+ end
255
+ get "/layout_test.xml"
256
+ refute_match /erb template/, body
257
+ assert_match '<?xml', body
258
+ end
259
+
260
+ describe 'for application render functionality' do
261
+
262
+ it 'should work properly with logging and missing layout' do
263
+ create_view :index, "<%= foo %>"
264
+ mock_app do
265
+ enable :logging
266
+ get("/") { render "index", { :layout => nil }, { :foo => "bar" } }
267
+ end
268
+ get "/"
269
+ assert_equal "bar", body
270
+ end
271
+
272
+ it 'should work properly with logging and layout' do
273
+ create_layout :application, "layout <%= yield %>"
274
+ create_view :index, "<%= foo %>"
275
+ mock_app do
276
+ enable :logging
277
+ get("/") { render "index", { :layout => true }, { :foo => "bar" } }
278
+ end
279
+ get "/"
280
+ assert_equal "layout bar", body
281
+ end
282
+
283
+ it 'should be compatible with sinatra render' do
284
+ mock_app do
285
+ get("/"){ render :erb, "<%= 1+2 %>" }
286
+ end
287
+ get "/"
288
+ assert_equal "3", body
289
+ end
290
+
291
+ it 'should support passing locals into render' do
292
+ create_layout :application, "layout <%= yield %>"
293
+ create_view :index, "<%= foo %>"
294
+ mock_app do
295
+ get("/") { render "index", { :layout => true }, { :foo => "bar" } }
296
+ end
297
+ get "/"
298
+ assert_equal "layout bar", body
299
+ end
300
+
301
+ it 'should support passing locals into sinatra render' do
302
+ create_layout :application, "layout <%= yield %>"
303
+ create_view :index, "<%= foo %>"
304
+ mock_app do
305
+ get("/") { render :erb, :index, { :layout => true }, { :foo => "bar" } }
306
+ end
307
+ get "/"
308
+ assert_equal "layout bar", body
309
+ end
310
+
311
+ it 'should support passing locals into special nil engine render' do
312
+ create_layout :application, "layout <%= yield %>"
313
+ create_view :index, "<%= foo %>"
314
+ mock_app do
315
+ get("/") { render nil, :index, { :layout => true }, { :foo => "bar" } }
316
+ end
317
+ get "/"
318
+ assert_equal "layout bar", body
319
+ end
320
+
321
+ it 'should be compatible with sinatra views' do
322
+ with_view :index, "<%= 1+2 %>" do
323
+ mock_app do
324
+ get("/foo") { render :erb, :index }
325
+ get("/bar") { erb :index }
326
+ get("/dir") { "3" }
327
+ get("/inj") { erb "<%= 2+1 %>" }
328
+ get("/rnj") { render :erb, "<%= 2+1 %>" }
329
+ end
330
+ get "/foo"
331
+ assert_equal "3", body
332
+ get "/bar"
333
+ assert_equal "3", body
334
+ get "/dir"
335
+ assert_equal "3", body
336
+ get "/inj"
337
+ assert_equal "3", body
338
+ get "/rnj"
339
+ assert_equal "3", body
340
+ end
341
+ end
342
+
343
+ it 'should resolve template engine' do
344
+ with_view :index, "<%= 1+2 %>" do
345
+ mock_app do
346
+ get("/foo") { render :index }
347
+ get("/bar") { render "/index" }
348
+ end
349
+ get "/foo"
350
+ assert_equal "3", body
351
+ get "/bar"
352
+ assert_equal "3", body
353
+ end
354
+ end
355
+
356
+ it 'should resolve template content type' do
357
+ create_view :foo, "Im Js", :format => :js
358
+ create_view :foo, "Im Erb"
359
+ mock_app do
360
+ get("/foo", :provides => :js) { render :foo }
361
+ get("/bar.js") { render :foo }
362
+ end
363
+ get "/foo.js"
364
+ assert_equal "Im Js", body
365
+ # TODO: implement this!
366
+ # get "/bar.js"
367
+ # assert_equal "Im Js", body
368
+ end
369
+
370
+ it 'should resolve with explicit template format' do
371
+ create_view :foo, "Im Js", :format => :js
372
+ create_view :foo, "Im Haml", :format => :haml
373
+ create_view :foo, "Im Xml", :format => :xml
374
+ mock_app do
375
+ get("/foo_normal", :provides => :js) { render 'foo' }
376
+ get("/foo_haml", :provides => :js) { render 'foo.haml' }
377
+ get("/foo_xml", :provides => :js) { render 'foo.xml' }
378
+ end
379
+ get "/foo_normal.js"
380
+ assert_equal "Im Js", body
381
+ get "/foo_haml.js"
382
+ assert_equal "Im Haml\n", body
383
+ get "/foo_xml.js"
384
+ assert_equal "Im Xml", body
385
+ end
386
+
387
+ it 'should resolve without explict template format' do
388
+ create_view :foo, "Im Html"
389
+ create_view :foo, "xml.rss", :format => :rss
390
+ mock_app do
391
+ get(:index, :map => "/", :provides => [:html, :rss]){ render 'foo' }
392
+ end
393
+ get "/", {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
394
+ assert_equal "Im Html", body
395
+ get ".rss"
396
+ assert_equal "<rss/>\n", body
397
+ end
398
+
399
+ it 'should ignore files ending in tilde and not render them' do
400
+ create_view :foo, "Im Wrong", :format => 'haml~'
401
+ create_view :foo, "Im Haml", :format => :haml
402
+ create_view :bar, "Im Haml backup", :format => 'haml~'
403
+ mock_app do
404
+ get('/foo') { render 'foo' }
405
+ get('/bar') { render 'bar' }
406
+ end
407
+ get '/foo'
408
+ assert_equal "Im Haml\n", body
409
+ assert_raises(Padrino::Rendering::TemplateNotFound) { get '/bar' }
410
+ end
411
+
412
+ it 'should resolve template locale' do
413
+ create_view :foo, "Im English", :locale => :en
414
+ create_view :foo, "Im Italian", :locale => :it
415
+ mock_app do
416
+ get("/foo") { render :foo }
417
+ end
418
+ I18n.locale = :en
419
+ get "/foo"
420
+ assert_equal "Im English", body
421
+ I18n.locale = :it
422
+ get "/foo"
423
+ assert_equal "Im Italian", body
424
+ end
425
+
426
+ it 'should resolve template content_type and locale' do
427
+ create_view :foo, "Im Js", :format => :js
428
+ create_view :foo, "Im Erb"
429
+ create_view :foo, "Im English Erb", :locale => :en
430
+ create_view :foo, "Im Italian Erb", :locale => :it
431
+ create_view :foo, "Im English Js", :format => :js, :locale => :en
432
+ create_view :foo, "Im Italian Js", :format => :js, :locale => :it
433
+ mock_app do
434
+ get("/foo", :provides => [:html, :js]) { render :foo }
435
+ end
436
+
437
+ I18n.enforce_available_locales = false
438
+ I18n.locale = :none
439
+ get "/foo.js"
440
+ assert_equal "Im Js", body
441
+ get "/foo"
442
+ assert_equal "Im Erb", body
443
+ I18n.enforce_available_locales = true
444
+
445
+ I18n.locale = :en
446
+ get "/foo"
447
+ assert_equal "Im English Erb", body
448
+ I18n.locale = :it
449
+ get "/foo"
450
+ assert_equal "Im Italian Erb", body
451
+ I18n.locale = :en
452
+ get "/foo.js"
453
+ assert_equal "Im English Js", body
454
+ I18n.locale = :it
455
+ get "/foo.js"
456
+ assert_equal "Im Italian Js", body
457
+ I18n.locale = :en
458
+ get "/foo.pk"
459
+ assert_equal 404, status
460
+ end
461
+
462
+ it 'should resolve layouts from specific application' do
463
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/apps/render')
464
+ @app = RenderDemo2
465
+ get '/blog/override'
466
+ assert_equal 'otay', body
467
+ end
468
+
469
+ it 'should resolve templates and layouts located in absolute paths' do
470
+ mock_app do
471
+ get("/foo") { render 'apps/views/blog/post', :layout => 'layout', :views => File.dirname(__FILE__)+'/fixtures' }
472
+ end
473
+ get '/foo'
474
+ assert_match /okay absolute layout/, body
475
+ end
476
+
477
+ it 'should resolve template content_type and locale with layout' do
478
+ create_layout :foo, "Hello <%= yield %> in a Js layout", :format => :js
479
+ create_layout :foo, "Hello <%= yield %> in a Js-En layout", :format => :js, :locale => :en
480
+ create_layout :foo, "Hello <%= yield %> in a Js-It layout", :format => :js, :locale => :it
481
+ create_layout :foo, "Hello <%= yield %> in a Erb-En layout", :locale => :en
482
+ create_layout :foo, "Hello <%= yield %> in a Erb-It layout", :locale => :it
483
+ create_layout :foo, "Hello <%= yield %> in a Erb layout"
484
+ create_view :bar, "Im Js", :format => :js
485
+ create_view :bar, "Im Erb"
486
+ create_view :bar, "Im English Erb", :locale => :en
487
+ create_view :bar, "Im Italian Erb", :locale => :it
488
+ create_view :bar, "Im English Js", :format => :js, :locale => :en
489
+ create_view :bar, "Im Italian Js", :format => :js, :locale => :it
490
+ create_view :bar, "Im a json", :format => :json
491
+ mock_app do
492
+ layout :foo
493
+ get("/bar", :provides => [:html, :js, :json]) { render :bar }
494
+ end
495
+
496
+ I18n.enforce_available_locales = false
497
+ I18n.locale = :none
498
+ get "/bar.js"
499
+ assert_equal "Hello Im Js in a Js layout", body
500
+ get "/bar"
501
+ assert_equal "Hello Im Erb in a Erb layout", body
502
+ I18n.enforce_available_locales = true
503
+
504
+ I18n.locale = :en
505
+ get "/bar"
506
+ assert_equal "Hello Im English Erb in a Erb-En layout", body
507
+ I18n.locale = :it
508
+ get "/bar"
509
+ assert_equal "Hello Im Italian Erb in a Erb-It layout", body
510
+ I18n.locale = :en
511
+ get "/bar.js"
512
+ assert_equal "Hello Im English Js in a Js-En layout", body
513
+ I18n.locale = :it
514
+ get "/bar.js"
515
+ assert_equal "Hello Im Italian Js in a Js-It layout", body
516
+ I18n.locale = :en
517
+ get "/bar.json"
518
+ assert_equal "Im a json", body
519
+ get "/bar.pk"
520
+ assert_equal 404, status
521
+
522
+ end
523
+
524
+ it 'should resolve template location relative to controller name' do
525
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/apps/render')
526
+ @app = RenderDemo2
527
+ get '/blog'
528
+ assert_equal 'okay', body
529
+ end
530
+
531
+ it 'should resolve nested template location relative to controller name' do
532
+ require File.expand_path(File.dirname(__FILE__) + '/fixtures/apps/render')
533
+ @app = RenderDemo2
534
+ get '/article/comment'
535
+ assert_equal 'okay comment', body
536
+ end
537
+
538
+ it 'should renders erb with blocks' do
539
+ mock_app do
540
+ def container
541
+ @_out_buf << "THIS."
542
+ yield
543
+ @_out_buf << "SPARTA!"
544
+ end
545
+ def is; "IS."; end
546
+ get '/' do
547
+ render :erb, '<% container do %> <%= is %> <% end %>'
548
+ end
549
+ end
550
+ get '/'
551
+ assert ok?
552
+ assert_equal 'THIS. IS. SPARTA!', body
553
+ end
554
+
555
+ it 'should render erb to a SafeBuffer' do
556
+ mock_app do
557
+ layout do
558
+ "this is a <%= yield %>"
559
+ end
560
+ get '/' do
561
+ render :erb, '<p><%= %q{<script lang="ronin">alert("https://github.com/ronin-ruby/ronin")</script>} %></p>', :layout => false
562
+ end
563
+ get '/with_layout' do
564
+ render :erb, '<span>span</span>', :layout => true
565
+ end
566
+ end
567
+ get '/'
568
+ assert ok?
569
+ assert_equal '<p>&lt;script lang=&quot;ronin&quot;&gt;alert(&quot;https://github.com/ronin-ruby/ronin&quot;)&lt;/script&gt;</p>', body
570
+
571
+ get '/with_layout'
572
+ assert ok?
573
+ assert_equal 'this is a <span>span</span>', body
574
+ end
575
+
576
+ it 'should render haml to a SafeBuffer' do
577
+ mock_app do
578
+ layout do
579
+ "%p= yield"
580
+ end
581
+ get '/' do
582
+ render :haml, '%p= %s{<script lang="ronin">alert("https://github.com/ronin-ruby/ronin")</script>}', :layout => false
583
+ end
584
+ get '/with_layout' do
585
+ render :haml, "%div\n foo", :layout => true
586
+ end
587
+ end
588
+ get '/'
589
+ assert ok?
590
+ assert_equal '<p>&lt;script lang=&quot;ronin&quot;&gt;alert(&quot;https://github.com/ronin-ruby/ronin&quot;)&lt;/script&gt;</p>', body.strip
591
+
592
+ get 'with_layout'
593
+ assert ok?
594
+ assert_equal '<p><div>foo</div></p>', body.gsub(/\s+/, "")
595
+ end
596
+
597
+ it 'should render slim to a SafeBuffer' do
598
+ mock_app do
599
+ layout do
600
+ "p= yield"
601
+ end
602
+ get '/' do
603
+ render :slim, 'p = %q{<script lang="ronin">alert("https://github.com/ronin-ruby/ronin")</script>}', :layout => false
604
+ end
605
+ get "/with_layout" do
606
+ render :slim, 'div foo', :layout => true
607
+ end
608
+ end
609
+ get '/'
610
+ assert ok?
611
+ assert_equal '<p>&lt;script lang=&quot;ronin&quot;&gt;alert(&quot;https://github.com/ronin-ruby/ronin&quot;)&lt;/script&gt;</p>', body.strip
612
+
613
+ get '/with_layout'
614
+ assert ok?
615
+ assert_equal '<p><div>foo</div></p>', body.strip
616
+ end
617
+
618
+ it 'should render correct erb when use sinatra as middleware' do
619
+ class Bar < Sinatra::Base
620
+ get "/" do
621
+ render :erb, "<&'>"
622
+ end
623
+ end
624
+ mock_app do
625
+ use Bar
626
+ end
627
+ get "/"
628
+ assert_equal "<&'>", body
629
+ end
630
+ end
631
+
632
+ describe 'standalone Sinatra usage of Rendering' do
633
+ before do
634
+ Sinatra::Request.class_eval{ alias_method :monkey_controller, :controller; undef :controller }
635
+ end
636
+ after do
637
+ Sinatra::Request.class_eval{ alias_method :controller, :monkey_controller; undef :monkey_controller }
638
+ end
639
+ it 'should work with Sinatra::Base' do
640
+ class Application < Sinatra::Base
641
+ register Padrino::Rendering
642
+ get '/' do
643
+ render :post, :views => File.dirname(__FILE__)+'/fixtures/apps/views/blog'
644
+ end
645
+ end
646
+ @app = Application.new
647
+ get '/'
648
+ assert_equal 'okay', body
649
+ end
650
+ end
651
+
652
+ describe 'locating of template paths' do
653
+ it 'should locate controller templates' do
654
+ mock_app do
655
+ disable :reload_templates
656
+ set :views, File.dirname(__FILE__)+'/fixtures/apps/views'
657
+ controller :test do
658
+ get :index do
659
+ render 'test/post'
660
+ end
661
+ end
662
+ end
663
+ get '/test'
664
+ end
665
+
666
+ it 'should properly cache template path' do
667
+ mock_app do
668
+ disable :reload_templates
669
+ set :views, File.dirname(__FILE__)+'/fixtures/apps/views'
670
+ controller :blog do
671
+ get :index do
672
+ render :post
673
+ end
674
+ end
675
+ controller :test do
676
+ get :index do
677
+ render 'post'
678
+ end
679
+ end
680
+ end
681
+ get '/blog'
682
+ get '/test'
683
+ assert_equal 'test', body
684
+ end
685
+ end
686
+
687
+ describe 'rendering bug in some' do
688
+ it 'should raise error on registering things to Padrino::Application' do
689
+ assert_raises(RuntimeError) do
690
+ Padrino::Application.register Padrino::Rendering
691
+ end
692
+ end
693
+ end
694
+ end