forme 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,286 +0,0 @@
1
- require_relative 'spec_helper'
2
- require_relative 'sequel_helper'
3
-
4
- begin
5
- require 'action_controller/railtie'
6
-
7
- begin
8
- require 'active_pack/gem_version'
9
- rescue LoadError
10
- end
11
- require_relative '../lib/forme/rails'
12
-
13
- if Rails.respond_to?(:version) && Rails.version.start_with?('4')
14
- # Work around issue in backported openssl environments where
15
- # secret is 64 bytes intead of 32 bytes
16
- require 'active_support/message_encryptor'
17
- def (ActiveSupport::MessageEncryptor).new(secret, *signature_key_or_options)
18
- obj = allocate
19
- obj.send(:initialize, secret[0, 32], *signature_key_or_options)
20
- obj
21
- end
22
- end
23
-
24
- class FormeRails < Rails::Application
25
- routes.append do
26
- %w'index inputs_block inputs_block_wrapper nest nest_sep nest_inputs nest_seq hash legend combined noblock noblock_post safe_buffer no_forgery_protection'.each do |action|
27
- get action, :controller=>'forme', :action=>action
28
- end
29
- end
30
- config.active_support.deprecation = :stderr
31
- config.middleware.delete(ActionDispatch::HostAuthorization) if defined?(ActionDispatch::HostAuthorization)
32
- config.middleware.delete(ActionDispatch::ShowExceptions)
33
- config.middleware.delete(Rack::Lock)
34
- config.secret_key_base = 'foo'*15
35
- config.secret_token = 'secret token'*15 if Rails.respond_to?(:version) && Rails.version < '5.2'
36
- config.eager_load = true
37
- begin
38
- initialize!
39
- rescue NoMethodError
40
- raise LoadError
41
- end
42
- end
43
- rescue LoadError
44
- warn "unable to load or setup rails, skipping rails spec"
45
- else
46
- class FormeController < ActionController::Base
47
- helper Forme::Rails::ERB
48
-
49
- def index
50
- render :inline => <<END
51
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
52
- <p>FBB</p>
53
- <%= f.input(:first) %>
54
- <%= f.input(:last) %>
55
- <%= f.button('Save') %>
56
- <% end %>
57
- END
58
- end
59
-
60
- def no_forgery_protection
61
- def self.protect_against_forgery?; false end
62
- render :inline => <<END
63
- <%= forme(:method=>'POST') do |f| %>
64
- <% end %>
65
- END
66
- end
67
-
68
- def inputs_block
69
- render :inline => <<END
70
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
71
- <%= f.inputs(:legend=>'FBB') do %>
72
- <%= f.input(:last) %>
73
- <% end %>
74
- <% end %>
75
- END
76
- end
77
-
78
- def inputs_block_wrapper
79
- render :inline => <<END
80
- <%= forme([:foo, :bar], {:action=>'/baz'}, :inputs_wrapper=>:fieldset_ol) do |f| %>
81
- <%= f.inputs(:legend=>'FBB') do %>
82
- <%= f.input(:last) %>
83
- <% end %>
84
- <% end %>
85
- END
86
- end
87
-
88
- def nest
89
- render :inline => <<END
90
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
91
- <%= f.tag(:p, {}, 'FBB') %>
92
- <%= f.tag(:div) do %>
93
- <%= f.input(:first) %>
94
- <%= f.input(:last) %>
95
- <% end %>
96
-
97
- <% end %>
98
- END
99
- end
100
-
101
- def nest_sep
102
- @nest = <<END
103
- n1
104
- <%= f.tag(:div) do %>
105
- n2
106
- <%= f.input(:first) %>
107
- <%= f.input(:last) %>
108
- n3
109
- <% end %>
110
- n4
111
- <%= f.inputs([:first, :last], :legend=>'Foo') %>
112
- n5
113
- END
114
- render :inline => <<END
115
- 0
116
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
117
- 1
118
- <%= f.tag(:p, {}, 'FBB') %>
119
- 2
120
- <%= render(:inline =>@nest, :locals=>{:f=>f}) %>
121
- 3
122
- <% end %>
123
- 4
124
- END
125
- end
126
-
127
- def nest_inputs
128
- @nest = <<END
129
- n1
130
- <%= f.inputs do %>
131
- n2
132
- <%= f.input(:first) %>
133
- <%= f.input(:last) %>
134
- n3
135
- <% end %>
136
- n4
137
- <%= f.inputs([:first, :last], :legend=>'Foo') %>
138
- n5
139
- END
140
- render :inline => <<END
141
- 0
142
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
143
- 1
144
- <%= f.tag(:p, {}, 'FBB') %>
145
- 2
146
- <%= render(:inline =>@nest, :locals=>{:f=>f}) %>
147
- 3
148
- <% end %>
149
- 4
150
- END
151
- end
152
-
153
- def nest_seq
154
- @album = Album.load(:name=>'N', :copies_sold=>2, :id=>1)
155
- @album.associations[:artist] = Artist.load(:name=>'A', :id=>2)
156
- @nest = <<END
157
- n1
158
- <%= f.subform(:artist) do %>
159
- n2
160
- <%= f.input(:name2) %>
161
- n3
162
- <% end %>
163
- n4
164
- <%= f.subform(:artist, :inputs=>[:name3], :legend=>'Bar') %>
165
- n5
166
- END
167
- render :inline => <<END
168
- 0
169
- <%= forme(@album, :action=>'/baz') do |f| %>
170
- 1
171
- <%= f.subform(:artist, :inputs=>[:name], :legend=>'Foo') %>
172
- 2
173
- <%= render(:inline=>@nest, :locals=>{:f=>f}) %>
174
- 3
175
- <% end %>
176
- 4
177
- END
178
- end
179
-
180
- def hash
181
- render :inline => "<%= forme({:action=>'/baz'}, :obj=>[:foo]) do |f| %> <%= f.input(:first) %> <% end %>"
182
- end
183
-
184
- def legend
185
- render :inline => <<END
186
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
187
- <p>FBB</p>
188
- <%= f.inputs([:first, :last], :legend=>'Foo') %>
189
- <p>FBB2</p>
190
- <% end %>
191
- END
192
- end
193
-
194
- def combined
195
- render :inline => <<END
196
- <%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'123') do |f| %>
197
- <p>FBB</p>
198
- <%= f.input(:last) %>
199
- <% end %>
200
- END
201
- end
202
-
203
- def noblock
204
- render :inline => "<%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'123') %>"
205
- end
206
-
207
- def noblock_post
208
- render :inline => "<%= forme({:method=>'post'}, :button=>'xyz') %>"
209
- end
210
-
211
- def safe_buffer
212
- render :inline => "<%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'<b>foo</b>'.html_safe) %>"
213
- end
214
- end
215
-
216
- describe "Forme Rails integration" do
217
- def sin_get(path)
218
- res = FormeRails.call(@rack.merge('PATH_INFO'=>path))
219
- p res unless res[0] == 200
220
- res[2].join.gsub(/\s+/, ' ').strip
221
- end
222
- before do
223
- o = Object.new
224
- def o.puts(*) end
225
- @rack = {'rack.input'=>'', 'REQUEST_METHOD'=>'GET', 'rack.errors'=>o}
226
- end
227
-
228
- it "#form should add start and end tags and yield Forme::Form instance" do
229
- sin_get('/index').must_equal '<form action="/baz"> <p>FBB</p> <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> <input type="submit" value="Save"/> </form>'
230
- end
231
-
232
- it "#form should have inputs work with a block" do
233
- sin_get('/inputs_block').must_equal '<form action="/baz"> <fieldset class="inputs"><legend>FBB</legend> <input id="last" name="last" type="text" value="bar"/> </fieldset></form>'
234
- end
235
-
236
- it "#form should have inputs with fieldset_ol wrapper work with block" do
237
- sin_get('/inputs_block_wrapper').must_equal '<form action="/baz"> <fieldset class="inputs"><legend>FBB</legend><ol> <input id="last" name="last" type="text" value="bar"/> </ol></fieldset></form>'
238
- end
239
-
240
- it "#form should add start and end tags and yield Forme::Form instance" do
241
- sin_get('/nest').must_equal '<form action="/baz"> <p>FBB</p> <div> <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> </div> </form>'
242
- end
243
-
244
- it "#form should correctly handle situation where multiple templates are used with same form object" do
245
- sin_get('/nest_sep').must_equal '0 <form action="/baz"> 1 <p>FBB</p> 2 n1 <div> n2 <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> n3 </div> n4 <fieldset class="inputs"><legend>Foo</legend><input id="first" name="first" type="text" value="foo"/><input id="last" name="last" type="text" value="bar"/></fieldset> n5 3 </form>4'
246
- end
247
-
248
- it "#form should correctly handle situation where multiple templates are used with same form object" do
249
- sin_get('/nest_inputs').must_equal '0 <form action="/baz"> 1 <p>FBB</p> 2 n1 <fieldset class="inputs"> n2 <input id="first" name="first" type="text" value="foo"/> <input id="last" name="last" type="text" value="bar"/> n3 </fieldset> n4 <fieldset class="inputs"><legend>Foo</legend><input id="first" name="first" type="text" value="foo"/><input id="last" name="last" type="text" value="bar"/></fieldset> n5 3 </form>4'
250
- end
251
-
252
- unless defined?(JRUBY_VERSION) && JRUBY_VERSION =~ /\A9\.0/ && defined?(ActionPack::VERSION::STRING) && ActionPack::VERSION::STRING >= '5.1'
253
- it "#form should correctly handle situation Sequel integration with subforms where multiple templates are used with same form object" do
254
- sin_get('/nest_seq').sub(%r{<input name="authenticity_token" type="hidden" value="([^"]+)"/>}, '<input name="authenticity_token" type="hidden" value="csrf"/>').must_equal '0 <form action="/baz" class="forme album" method="post"><input name="authenticity_token" type="hidden" value="csrf"/> 1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Foo</legend><label>Name: <input id="album_artist_attributes_name" maxlength="255" name="album[artist_attributes][name]" type="text" value="A"/></label></fieldset> 2 n1 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Artist</legend> n2 <label>Name2: <input id="album_artist_attributes_name2" name="album[artist_attributes][name2]" type="text" value="A2"/></label> n3 </fieldset> n4 <input id="album_artist_attributes_id" name="album[artist_attributes][id]" type="hidden" value="2"/><fieldset class="inputs"><legend>Bar</legend><label>Name3: <input id="album_artist_attributes_name3" name="album[artist_attributes][name3]" type="text" value="A3"/></label></fieldset> n5 3 </form>4'
255
- end
256
-
257
- it "#form should work without a block with hidden tags" do
258
- sin_get('/noblock_post').sub(%r{<input name="authenticity_token" type="hidden" value="([^"]+)"/>}, '<input name="authenticity_token" type="hidden" value="csrf"/>').must_equal '<form method="post"><input name="authenticity_token" type="hidden" value="csrf"/><input type="submit" value="xyz"/></form>'
259
- end
260
- end
261
-
262
- it "#form should accept two hashes instead of requiring obj as first argument" do
263
- sin_get('/hash').must_equal '<form action="/baz"> <input id="first" name="first" type="text" value="foo"/> </form>'
264
- end
265
-
266
- it "#form should deal with emitted code" do
267
- sin_get('/legend').must_equal '<form action="/baz"> <p>FBB</p> <fieldset class="inputs"><legend>Foo</legend><input id="first" name="first" type="text" value="foo"/><input id="last" name="last" type="text" value="bar"/></fieldset> <p>FBB2</p> </form>'
268
- end
269
-
270
- it "#form should work with :inputs, :button, and :legend options" do
271
- sin_get('/combined').must_equal '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset> <p>FBB</p> <input id="last" name="last" type="text" value="bar"/> <input type="submit" value="xyz"/></form>'
272
- end
273
-
274
- it "#form should work without a block" do
275
- sin_get('/noblock').must_equal '<form action="/baz"><fieldset class="inputs"><legend>123</legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
276
- end
277
-
278
- it "#form should handle Rails SafeBuffers" do
279
- sin_get('/safe_buffer').must_equal '<form action="/baz"><fieldset class="inputs"><legend><b>foo</b></legend><input id="first" name="first" type="text" value="foo"/></fieldset><input type="submit" value="xyz"/></form>'
280
- end
281
-
282
- it "#form should handle case where forgery protection is disabled" do
283
- sin_get('/no_forgery_protection').must_equal '<form method="POST"> </form>'
284
- end
285
- end
286
- end