forme 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,274 +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'.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 inputs_block
61
- render :inline => <<END
62
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
63
- <%= f.inputs(:legend=>'FBB') do %>
64
- <%= f.input(:last) %>
65
- <% end %>
66
- <% end %>
67
- END
68
- end
69
-
70
- def inputs_block_wrapper
71
- render :inline => <<END
72
- <%= forme([:foo, :bar], {:action=>'/baz'}, :inputs_wrapper=>:fieldset_ol) do |f| %>
73
- <%= f.inputs(:legend=>'FBB') do %>
74
- <%= f.input(:last) %>
75
- <% end %>
76
- <% end %>
77
- END
78
- end
79
-
80
- def nest
81
- render :inline => <<END
82
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
83
- <%= f.tag(:p, {}, 'FBB') %>
84
- <%= f.tag(:div) do %>
85
- <%= f.input(:first) %>
86
- <%= f.input(:last) %>
87
- <% end %>
88
-
89
- <% end %>
90
- END
91
- end
92
-
93
- def nest_sep
94
- @nest = <<END
95
- n1
96
- <%= f.tag(:div) do %>
97
- n2
98
- <%= f.input(:first) %>
99
- <%= f.input(:last) %>
100
- n3
101
- <% end %>
102
- n4
103
- <%= f.inputs([:first, :last], :legend=>'Foo') %>
104
- n5
105
- END
106
- render :inline => <<END
107
- 0
108
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
109
- 1
110
- <%= f.tag(:p, {}, 'FBB') %>
111
- 2
112
- <%= render(:inline =>@nest, :locals=>{:f=>f}) %>
113
- 3
114
- <% end %>
115
- 4
116
- END
117
- end
118
-
119
- def nest_inputs
120
- @nest = <<END
121
- n1
122
- <%= f.inputs do %>
123
- n2
124
- <%= f.input(:first) %>
125
- <%= f.input(:last) %>
126
- n3
127
- <% end %>
128
- n4
129
- <%= f.inputs([:first, :last], :legend=>'Foo') %>
130
- n5
131
- END
132
- render :inline => <<END
133
- 0
134
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
135
- 1
136
- <%= f.tag(:p, {}, 'FBB') %>
137
- 2
138
- <%= render(:inline =>@nest, :locals=>{:f=>f}) %>
139
- 3
140
- <% end %>
141
- 4
142
- END
143
- end
144
-
145
- def nest_seq
146
- @album = Album.load(:name=>'N', :copies_sold=>2, :id=>1)
147
- @album.associations[:artist] = Artist.load(:name=>'A', :id=>2)
148
- @nest = <<END
149
- n1
150
- <%= f.subform(:artist) do %>
151
- n2
152
- <%= f.input(:name2) %>
153
- n3
154
- <% end %>
155
- n4
156
- <%= f.subform(:artist, :inputs=>[:name3], :legend=>'Bar') %>
157
- n5
158
- END
159
- render :inline => <<END
160
- 0
161
- <%= forme(@album, :action=>'/baz') do |f| %>
162
- 1
163
- <%= f.subform(:artist, :inputs=>[:name], :legend=>'Foo') %>
164
- 2
165
- <%= render(:inline=>@nest, :locals=>{:f=>f}) %>
166
- 3
167
- <% end %>
168
- 4
169
- END
170
- end
171
-
172
- def hash
173
- render :inline => "<%= forme({:action=>'/baz'}, :obj=>[:foo]) do |f| %> <%= f.input(:first) %> <% end %>"
174
- end
175
-
176
- def legend
177
- render :inline => <<END
178
- <%= forme([:foo, :bar], :action=>'/baz') do |f| %>
179
- <p>FBB</p>
180
- <%= f.inputs([:first, :last], :legend=>'Foo') %>
181
- <p>FBB2</p>
182
- <% end %>
183
- END
184
- end
185
-
186
- def combined
187
- render :inline => <<END
188
- <%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'123') do |f| %>
189
- <p>FBB</p>
190
- <%= f.input(:last) %>
191
- <% end %>
192
- END
193
- end
194
-
195
- def noblock
196
- render :inline => "<%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'123') %>"
197
- end
198
-
199
- def noblock_post
200
- render :inline => "<%= forme({:method=>'post'}, :button=>'xyz') %>"
201
- end
202
-
203
- def safe_buffer
204
- render :inline => "<%= forme([:foo, :bar], {:action=>'/baz'}, :inputs=>[:first], :button=>'xyz', :legend=>'<b>foo</b>'.html_safe) %>"
205
- end
206
- end
207
-
208
- describe "Forme Rails integration" do
209
- def sin_get(path)
210
- res = FormeRails.call(@rack.merge('PATH_INFO'=>path))
211
- p res unless res[0] == 200
212
- res[2].join.gsub(/\s+/, ' ').strip
213
- end
214
- before do
215
- o = Object.new
216
- def o.puts(*) end
217
- @rack = {'rack.input'=>'', 'REQUEST_METHOD'=>'GET', 'rack.errors'=>o}
218
- end
219
-
220
- it "#form should add start and end tags and yield Forme::Form instance" do
221
- 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>'
222
- end
223
-
224
- it "#form should have inputs work with a block" do
225
- 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>'
226
- end
227
-
228
- it "#form should have inputs with fieldset_ol wrapper work with block" do
229
- 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>'
230
- end
231
-
232
- it "#form should add start and end tags and yield Forme::Form instance" do
233
- 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>'
234
- end
235
-
236
- it "#form should correctly handle situation where multiple templates are used with same form object" do
237
- 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'
238
- end
239
-
240
- it "#form should correctly handle situation where multiple templates are used with same form object" do
241
- 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'
242
- end
243
-
244
- unless defined?(JRUBY_VERSION) && JRUBY_VERSION =~ /\A9\.0/ && defined?(ActionPack::VERSION::STRING) && ActionPack::VERSION::STRING >= '5.1'
245
- it "#form should correctly handle situation Sequel integration with subforms where multiple templates are used with same form object" do
246
- 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'
247
- end
248
-
249
- it "#form should work without a block with hidden tags" do
250
- 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>'
251
- end
252
- end
253
-
254
- it "#form should accept two hashes instead of requiring obj as first argument" do
255
- sin_get('/hash').must_equal '<form action="/baz"> <input id="first" name="first" type="text" value="foo"/> </form>'
256
- end
257
-
258
- it "#form should deal with emitted code" do
259
- 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>'
260
- end
261
-
262
- it "#form should work with :inputs, :button, and :legend options" do
263
- 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>'
264
- end
265
-
266
- it "#form should work without a block" do
267
- 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>'
268
- end
269
-
270
- it "#form should handle Rails SafeBuffers" do
271
- 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>'
272
- end
273
- end
274
- end