dry_crud 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/README.rdoc +25 -10
  2. data/Rakefile +30 -8
  3. data/VERSION +1 -1
  4. data/lib/generators/dry_crud/dry_crud_generator.rb +15 -2
  5. data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +18 -10
  6. data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +14 -11
  7. data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +71 -29
  8. data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +40 -3
  9. data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +8 -27
  10. data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +13 -6
  11. data/lib/generators/dry_crud/templates/app/views/crud/_form.html.erb +1 -1
  12. data/lib/generators/dry_crud/templates/app/views/crud/_form.html.haml +1 -1
  13. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +4 -4
  14. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.haml +5 -5
  15. data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +433 -0
  16. data/lib/generators/dry_crud/templates/spec/helpers/crud_helper_spec.rb +146 -0
  17. data/lib/generators/dry_crud/templates/spec/helpers/list_helper_spec.rb +154 -0
  18. data/lib/generators/dry_crud/templates/spec/helpers/standard_form_builder_spec.rb +215 -0
  19. data/lib/generators/dry_crud/templates/spec/helpers/standard_helper_spec.rb +387 -0
  20. data/lib/generators/dry_crud/templates/spec/helpers/standard_table_builder_spec.rb +120 -0
  21. data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +244 -0
  22. data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +160 -0
  23. data/lib/generators/dry_crud/templates/test/crud_test_model.rb +45 -18
  24. data/lib/generators/dry_crud/templates/test/functional/crud_controller_test_helper.rb +19 -9
  25. data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +11 -14
  26. data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +26 -13
  27. data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +0 -4
  28. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +6 -0
  29. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +9 -35
  30. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb +5 -4
  31. data/test/templates/Gemfile +3 -1
  32. data/test/templates/app/controllers/people_controller.rb +1 -1
  33. data/test/templates/app/controllers/vips_controller.rb +1 -1
  34. data/test/templates/app/models/city.rb +1 -1
  35. data/test/templates/app/views/admin/cities/_form.html.erb +1 -1
  36. data/test/templates/app/views/admin/cities/_form.html.haml +1 -1
  37. data/test/templates/app/views/admin/countries/_list.html.erb +3 -4
  38. data/test/templates/app/views/admin/countries/_list.html.haml +3 -4
  39. data/test/templates/app/views/ajax/_form.html.erb +1 -1
  40. data/test/templates/app/views/ajax/_form.html.haml +1 -1
  41. data/test/templates/spec/controllers/admin/cities_controller_spec.rb +74 -0
  42. data/test/templates/spec/controllers/admin/countries_controller_spec.rb +56 -0
  43. data/test/templates/spec/controllers/people_controller_spec.rb +80 -0
  44. data/test/templates/spec/routing/cities_routing_spec.rb +11 -0
  45. data/test/templates/spec/routing/countries_routing_spec.rb +11 -0
  46. data/test/templates/test/functional/admin/cities_controller_test.rb +1 -1
  47. data/test/templates/test/functional/admin/countries_controller_test.rb +1 -1
  48. data/test/templates/test/functional/people_controller_test.rb +3 -3
  49. metadata +18 -7
@@ -10,7 +10,7 @@ class StandardTableBuilder
10
10
 
11
11
  # Delegate called methods to template.
12
12
  # including StandardHelper would lead to problems with indirectly called methods.
13
- delegate :content_tag, :format_attr, :column_type, :association,
13
+ delegate :content_tag, :format_attr, :column_type, :association, :dom_id,
14
14
  :captionize, :add_css_class, :content_tag_nested, :to => :template
15
15
 
16
16
  def initialize(entries, template, options = {})
@@ -48,9 +48,10 @@ class StandardTableBuilder
48
48
  # Define a column for the given attribute and an optional header.
49
49
  # If no header is given, the attribute name is used. The cell will
50
50
  # contain the formatted attribute value for the current entry.
51
- def attr(a, header = nil)
51
+ def attr(a, header = nil, &block)
52
52
  header ||= attr_header(a)
53
- col(header, :class => align_class(a)) { |e| format_attr(e, a) }
53
+ block ||= lambda { |e| format_attr(e, a) }
54
+ col(header, :class => align_class(a), &block)
54
55
  end
55
56
 
56
57
  # Renders the table as HTML.
@@ -85,11 +86,17 @@ class StandardTableBuilder
85
86
  end
86
87
 
87
88
  def html_row(entry)
88
- content_tag_nested(:tr, cols) { |c| c.html_cell(entry) }
89
+ attrs = {}
90
+ attrs[:id] = dom_id(entry) if entry.respond_to?(:dom_id)
91
+ content_tag_nested(:tr, cols, attrs) { |c| c.html_cell(entry) }
89
92
  end
90
93
 
91
94
  def entry_class
92
- entries.first.class
95
+ if entries.respond_to?(:klass)
96
+ entries.klass
97
+ else
98
+ entries.first.class
99
+ end
93
100
  end
94
101
 
95
102
  # Helper class to store column information.
@@ -98,7 +105,7 @@ class StandardTableBuilder
98
105
  delegate :content_tag, :to => :template
99
106
 
100
107
  def content(entry)
101
- block.call(entry)
108
+ entry.nil? ? '' : template.capture(entry, &block)
102
109
  end
103
110
 
104
111
  def html_header
@@ -1 +1 @@
1
- <%= crud_form %>
1
+ <%= entry_form %>
@@ -1 +1 @@
1
- = crud_form
1
+ = entry_form
@@ -14,10 +14,10 @@
14
14
  <%= stylesheet_link_tag 'application', :media => 'all' %>
15
15
 
16
16
  <!-- fav and touch icons -->
17
- <link href="images/favicon.ico" rel="shortcut icon">
18
- <link href="images/apple-touch-icon.png" rel="apple-touch-icon">
19
- <link href="images/apple-touch-icon-72x72.png" rel="apple-touch-icon" sizes="72x72">
20
- <link href="images/apple-touch-icon-114x114.png" rel="apple-touch-icon" sizes="114x114">
17
+ <%= favicon_link_tag %>
18
+ <%= favicon_link_tag '/apple-touch-icon.png', :rel => 'apple-touch-icon', :type => 'image/png' %>
19
+ <%= favicon_link_tag '/apple-touch-icon-72x72.png', :rel => 'apple-touch-icon', :type => 'image/png', :sizes => "72x72" %>
20
+ <%= favicon_link_tag '/apple-touch-icon-114x114.png', :rel => 'apple-touch-icon', :type => 'image/png', :sizes => "114x114" %>
21
21
 
22
22
  <%= yield :head %>
23
23
  </head>
@@ -12,11 +12,11 @@
12
12
  <![endif]-->
13
13
 
14
14
  = stylesheet_link_tag 'application', :media => 'all'
15
-
16
- %link{:href => "images/favicon.ico", :rel => "shortcut icon"}
17
- %link{:href => "images/apple-touch-icon.png", :rel => "apple-touch-icon"}
18
- %link{:href => "images/apple-touch-icon-72x72.png", :rel => "apple-touch-icon", :sizes => "72x72"}
19
- %link{:href => "images/apple-touch-icon-114x114.png", :rel => "apple-touch-icon", :sizes => "114x114"}
15
+
16
+ = favicon_link_tag
17
+ = favicon_link_tag '/apple-touch-icon.png', :rel => 'apple-touch-icon', :type => 'image/png'
18
+ = favicon_link_tag '/apple-touch-icon-72x72.png', :rel => 'apple-touch-icon', :type => 'image/png', :sizes => "72x72"
19
+ = favicon_link_tag '/apple-touch-icon-114x114.png', :rel => 'apple-touch-icon', :type => 'image/png', :sizes => "114x114"
20
20
 
21
21
  = yield :head
22
22
 
@@ -0,0 +1,433 @@
1
+ require 'spec_helper'
2
+
3
+ # Tests all actions of the CrudController based on a dummy model
4
+ # (CrudTestModel). This is useful to test the general behavior
5
+ # of CrudController.
6
+
7
+ describe CrudTestModelsController do
8
+
9
+ include CrudTestHelper
10
+
11
+ before(:all) do
12
+ reset_db
13
+ setup_db
14
+ create_test_data
15
+ end
16
+
17
+ after(:all) { reset_db }
18
+
19
+ before { special_routing }
20
+
21
+
22
+ include_examples 'crud controller', {}
23
+
24
+ let(:test_entry) { crud_test_models(:AAAAA) }
25
+ let(:new_entry_attrs) do
26
+ {:name => 'foo',
27
+ :children => 42,
28
+ :companion_id => 3,
29
+ :rating => 8.5,
30
+ :income => 2.42,
31
+ :birthdate => '31-12-1999'.to_date,
32
+ :human => true,
33
+ :remarks => "some custom\n\tremarks"}
34
+ end
35
+ let(:edit_entry_attrs) do
36
+ {:name => 'foo',
37
+ :children => 42,
38
+ :rating => 8.5,
39
+ :income => 2.42,
40
+ :birthdate => '31-12-1999'.to_date,
41
+ :human => true,
42
+ :remarks => "some custom\n\tremarks"}
43
+ end
44
+
45
+
46
+ describe "setup" do
47
+ it "model count should be correct" do
48
+ CrudTestModel.count.should == 6
49
+ end
50
+
51
+ it "should have models_label" do
52
+ controller.models_label.should == 'Crud Test Models'
53
+ end
54
+
55
+ it "should have models_label singular" do
56
+ controller.models_label(false).should == 'Crud Test Model'
57
+ end
58
+
59
+ it "should route index" do
60
+ { :get => "/crud_test_models" }.should route_to(
61
+ :controller => "crud_test_models",
62
+ :action => "index"
63
+ )
64
+ end
65
+
66
+ it "should route show" do
67
+ { :get => "/crud_test_models/1" }.should route_to(
68
+ :controller => "crud_test_models",
69
+ :action => "show",
70
+ :id => '1'
71
+ )
72
+ end
73
+ end
74
+
75
+ describe_action :get, :index do
76
+ context '.html', :format => :html do
77
+ context 'plain', :combine => 'ihp' do
78
+ it "should contain all entries" do
79
+ entries.size.should == 6
80
+ end
81
+
82
+ it "session should have empty list_params" do
83
+ session[:list_params].should == Hash.new
84
+ end
85
+
86
+ it "should provide entries helper method" do
87
+ should render_template('index')
88
+ entries.should be(controller.send(:entries))
89
+ end
90
+ end
91
+
92
+ context "search" do
93
+ let(:params) { {:q => search_value} }
94
+
95
+ context "regular", :combine => 'ihse' do
96
+ it "entries should only contain test_entry" do
97
+ entries.should == [test_entry]
98
+ end
99
+
100
+ it "session should have query list param" do
101
+ session[:list_params]['/crud_test_models.html'].should == {:q => 'AAAA'}
102
+ end
103
+ end
104
+
105
+ context "with custom options", :combine => 'ihsec' do
106
+ let(:params) { {:q => 'DDD', :filter => true} }
107
+
108
+ it_should_respond
109
+
110
+ it "entries should have one item" do
111
+ entries.should == [CrudTestModel.find_by_name('BBBBB')]
112
+ end
113
+
114
+ it "session should have query list param" do
115
+ session[:list_params]['/crud_test_models.html'].should == {:q => 'DDD'}
116
+ end
117
+ end
118
+ end
119
+
120
+ context "sort" do
121
+ context "for given column", :combine => 'ihsog' do
122
+ let(:params) { {:sort => 'children', :sort_dir => 'asc'} }
123
+
124
+ it_should_respond
125
+
126
+ it "entries should be in correct order" do
127
+ entries.should == CrudTestModel.all.sort_by(&:children)
128
+ end
129
+
130
+ it "session should have sort list param" do
131
+ session[:list_params]['/crud_test_models.html'].should == {:sort => 'children', :sort_dir => 'asc'}
132
+ end
133
+ end
134
+
135
+ context "for virtual column", :combine => 'ihsov' do
136
+ let(:params) { {:sort => 'chatty', :sort_dir => 'desc'} }
137
+
138
+ it_should_respond
139
+
140
+ it "entries should be in correct order" do
141
+ names = entries.collect(&:name)
142
+ assert names.index('BBBBB') < names.index('AAAAA')
143
+ assert names.index('BBBBB') < names.index('DDDDD')
144
+ assert names.index('EEEEE') < names.index('AAAAA')
145
+ assert names.index('EEEEE') < names.index('DDDDD')
146
+ assert names.index('AAAAA') < names.index('CCCCC')
147
+ assert names.index('DDDDD') < names.index('CCCCC')
148
+ end
149
+
150
+ it "session should have sort list param" do
151
+ session[:list_params]['/crud_test_models.html'].should == {:sort => 'chatty', :sort_dir => 'desc'}
152
+ end
153
+ end
154
+
155
+ context "with search", :combine => 'ihsose' do
156
+ let(:params) { {:q => 'DDD', :sort => 'chatty', :sort_dir => 'asc'} }
157
+
158
+ it_should_respond
159
+
160
+ it "entries should be in correct order" do
161
+ entries.collect(&:name).should == ['CCCCC', 'DDDDD', 'BBBBB']
162
+ end
163
+
164
+ it "session should have sort list param" do
165
+ session[:list_params]['/crud_test_models.html'].should == {:q => 'DDD', :sort => 'chatty', :sort_dir => 'asc'}
166
+ end
167
+ end
168
+ end
169
+
170
+ context "with custom options", :combine => 'ihsoco' do
171
+ let(:params) { {:filter => true} }
172
+
173
+ it_should_respond
174
+
175
+ context "entries" do
176
+ subject { entries }
177
+ it { should have(2).items }
178
+ it { should == entries.sort_by(&:children).reverse }
179
+ end
180
+ end
181
+
182
+ context "returning", :perform_request => false do
183
+ before do
184
+ session[:list_params] = {}
185
+ session[:list_params]['/crud_test_models'] = {:q => 'DDD', :sort => 'chatty', :sort_dir => 'desc'}
186
+ get :index, :returning => true
187
+ end
188
+
189
+ it_should_respond
190
+
191
+ it "entries should be in correct order" do
192
+ entries.collect(&:name).should == ['BBBBB', 'DDDDD', 'CCCCC']
193
+ end
194
+
195
+ it "params should be set" do
196
+ controller.params[:q].should == 'DDD'
197
+ controller.params[:sort].should == 'chatty'
198
+ controller.params[:sort_dir].should == 'desc'
199
+ end
200
+ end
201
+ end
202
+
203
+ context ".js", :format => :js, :combine => 'ijs' do
204
+ it_should_respond
205
+ it_should_assign_entries
206
+ its(:body) { should == 'index js' }
207
+ end
208
+ end
209
+
210
+ describe_action :get, :new do
211
+ context "plain", :combine => 'new' do
212
+ it "should assign companions" do
213
+ assigns(:companions).should be_present
214
+ end
215
+
216
+ it "should have called two render callbacks" do
217
+ controller.called_callbacks.should == [:before_render_new, :before_render_form]
218
+ end
219
+ end
220
+
221
+ context "with before_render callback redirect", :perform_request => false do
222
+ before do
223
+ controller.should_redirect = true
224
+ get :new
225
+ end
226
+
227
+ it { should redirect_to(crud_test_models_path) }
228
+
229
+ it "should not set companions" do
230
+ assigns(:companions).should be_nil
231
+ end
232
+ end
233
+ end
234
+
235
+ describe_action :post, :create do
236
+ let(:params) { {model_identifier => new_entry_attrs} }
237
+
238
+ it "should have called the correct callbacks" do
239
+ controller.called_callbacks.should == [:before_create, :before_save, :after_save, :after_create]
240
+ end
241
+
242
+ context "with before callback" do
243
+ let(:params) { {:crud_test_model => {:name => 'illegal', :children => 2}} }
244
+ it "should not create entry", :perform_request => false do
245
+ expect { perform_request }.to change { CrudTestModel.count }.by(0)
246
+ end
247
+
248
+ context "plain", :combine => 'chcp' do
249
+ it_should_respond
250
+ it_should_render('new')
251
+ it_should_persist_entry(false)
252
+ it_should_have_flash(:alert)
253
+
254
+ it "should set entry name" do
255
+ entry.name.should == 'illegal'
256
+ end
257
+
258
+ it "should assign companions" do
259
+ assigns(:companions).should be_present
260
+ end
261
+
262
+ it "should have called the correct callbacks" do
263
+ controller.called_callbacks.should == [:before_render_new, :before_render_form]
264
+ end
265
+ end
266
+
267
+ context "redirect", :perform_request => false do
268
+ before { controller.should_redirect = true }
269
+
270
+ it "should not create entry" do
271
+ expect { perform_request }.to change { CrudTestModel.count }.by(0)
272
+ end
273
+
274
+ it { perform_request; should redirect_to(crud_test_models_path) }
275
+
276
+ it "should have called no callbacks" do
277
+ perform_request
278
+ controller.called_callbacks.should be_nil
279
+ end
280
+ end
281
+ end
282
+
283
+ context "with invalid params" do
284
+ let(:params) { {:crud_test_model => {:children => 2}} }
285
+
286
+ context ".html" do
287
+ it "should not create entry", :perform_request => false do
288
+ expect { perform_request }.to change { CrudTestModel.count }.by(0)
289
+ end
290
+
291
+ context "plain", :combine => 'chip' do
292
+ it_should_respond
293
+ it_should_render('new')
294
+ it_should_persist_entry(false)
295
+ it_should_not_have_flash(:notice)
296
+ it_should_not_have_flash(:alert)
297
+
298
+ it "should assign companions" do
299
+ assigns(:companions).should be_present
300
+ end
301
+
302
+ it "should have called the correct callbacks" do
303
+ controller.called_callbacks.should == [:before_create, :before_save, :before_render_new, :before_render_form]
304
+ end
305
+ end
306
+ end
307
+
308
+ context ".json", :format => :json do
309
+ it "should not create entry", :perform_request => false do
310
+ expect { perform_request }.to change { CrudTestModel.count }.by(0)
311
+ end
312
+
313
+ context "plain", :combine => 'cjcb' do
314
+ it_should_respond(422)
315
+ it_should_persist_entry(false)
316
+ it_should_not_have_flash(:notice)
317
+ it_should_not_have_flash(:alert)
318
+
319
+ it "should not assign companions" do
320
+ assigns(:companions).should be_nil
321
+ end
322
+
323
+ it "should have called the correct callbacks" do
324
+ controller.called_callbacks.should == [:before_create, :before_save]
325
+ end
326
+
327
+ its(:body) { should match(/errors/) }
328
+ end
329
+ end
330
+ end
331
+
332
+ end
333
+
334
+ describe_action :get, :edit, :id => true do
335
+ it "should have called the correct callbacks" do
336
+ controller.called_callbacks.should == [:before_render_edit, :before_render_form]
337
+ end
338
+ end
339
+
340
+ describe_action :put, :update, :id => true do
341
+ let(:params) { {model_identifier => edit_entry_attrs} }
342
+
343
+ it "should have called the correct callbacks" do
344
+ controller.called_callbacks.should == [:before_update, :before_save, :after_save, :after_update]
345
+ end
346
+
347
+ context "with invalid params" do
348
+ let(:params) { {:crud_test_model => {:rating => 20}} }
349
+
350
+ context ".html", :combine => 'uhivp' do
351
+ it_should_respond
352
+ it_should_render('edit')
353
+ it_should_not_have_flash(:notice)
354
+
355
+ it "should change entry" do
356
+ entry.should be_changed
357
+ end
358
+
359
+ it "should set entry rating" do
360
+ entry.rating.should == 20
361
+ end
362
+
363
+ it "should have called the correct callbacks" do
364
+ controller.called_callbacks.should == [:before_update, :before_save, :before_render_edit, :before_render_form]
365
+ end
366
+ end
367
+
368
+ context ".json", :format => :json, :combine => 'ujivp' do
369
+ it_should_respond(422)
370
+ it_should_not_have_flash(:notice)
371
+
372
+ it "should have called the correct callbacks" do
373
+ controller.called_callbacks.should == [:before_update, :before_save]
374
+ end
375
+
376
+ its(:body) { should match(/errors/) }
377
+ end
378
+ end
379
+
380
+ end
381
+
382
+ describe_action :delete, :destroy, :id => true do
383
+ it "should have called the correct callbacks" do
384
+ controller.called_callbacks.should == [:before_destroy, :after_destroy]
385
+ end
386
+
387
+ context "with failure" do
388
+ let(:test_entry) { crud_test_models(:BBBBB) }
389
+ context ".html" do
390
+ it "should not delete entry from database", :perform_request => false do
391
+ expect { perform_request }.to change { CrudTestModel.count }.by(0)
392
+ end
393
+
394
+ it "should redirect to referer", :perform_request => false do
395
+ ref = @request.env['HTTP_REFERER'] = crud_test_model_url(test_entry)
396
+ perform_request
397
+ should redirect_to(ref)
398
+ end
399
+
400
+ it_should_have_flash(:alert, /companion/)
401
+ it_should_not_have_flash(:notice)
402
+ end
403
+
404
+ context ".json", :format => :json, :combine => 'djf' do
405
+ it_should_respond(422)
406
+ it_should_not_have_flash(:notice)
407
+ its(:body) { should match(/errors/) }
408
+ end
409
+
410
+ context "callback", :perform_request => false do
411
+ before do
412
+ test_entry.update_attribute :name, 'illegal'
413
+ end
414
+
415
+ it "should not delete entry from database" do
416
+ expect { perform_request }.to change { CrudTestModel.count }.by(0)
417
+ end
418
+
419
+ it "should redirect to index" do
420
+ perform_request
421
+ should redirect_to(crud_test_models_path(:returning => true))
422
+ end
423
+
424
+ it "should have flash alert" do
425
+ perform_request
426
+ flash[:alert].should match(/illegal name/)
427
+ end
428
+ end
429
+ end
430
+
431
+ end
432
+
433
+ end
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe CrudHelper do
5
+
6
+ include StandardHelper
7
+ include ListHelper
8
+ include CrudTestHelper
9
+
10
+
11
+ before(:all) do
12
+ reset_db
13
+ setup_db
14
+ create_test_data
15
+ end
16
+
17
+ after(:all) { reset_db }
18
+
19
+
20
+ describe "#crud_table" do
21
+ let(:entries) { CrudTestModel.all }
22
+
23
+ context "default" do
24
+ subject do
25
+ with_test_routing { crud_table }
26
+ end
27
+
28
+ it "has 7 rows" do
29
+ subject.scan(REGEXP_ROWS).size.should == 7
30
+ end
31
+
32
+ it "has 13 sort headers" do
33
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 13
34
+ end
35
+
36
+ it "has 12 action cells" do
37
+ subject.scan(REGEXP_ACTION_CELL).size.should == 12
38
+ end
39
+ end
40
+
41
+ context "with custom attrs" do
42
+ subject do
43
+ with_test_routing { crud_table(:name, :children, :companion_id) }
44
+ end
45
+
46
+ it "has 3 sort headers" do
47
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 3
48
+ end
49
+ end
50
+
51
+ context "with custom block" do
52
+ subject do
53
+ with_test_routing do
54
+ crud_table do |t|
55
+ t.attrs :name, :children, :companion_id
56
+ t.col("head") {|e| content_tag :span, e.income.to_s }
57
+ end
58
+ end
59
+ end
60
+
61
+ it "has 4 headers" do
62
+ subject.scan(REGEXP_HEADERS).size.should == 6
63
+ end
64
+
65
+ it "has 6 custom col spans" do
66
+ subject.scan(/<span>.+?<\/span>/m).size.should == 6
67
+ end
68
+
69
+ it "has 12 action cells" do
70
+ subject.scan(REGEXP_ACTION_CELL).size.should == 12
71
+ end
72
+ end
73
+
74
+ context "with custom attributes and block" do
75
+ subject do
76
+ with_test_routing do
77
+ crud_table(:name, :children, :companion_id) do |t|
78
+ t.col("head") {|e| content_tag :span, e.income.to_s }
79
+ end
80
+ end
81
+ end
82
+
83
+ it "has 3 sort headers" do
84
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 3
85
+ end
86
+
87
+ it "has 6 custom col spans" do
88
+ subject.scan(/<span>.+?<\/span>/m).size.should == 6
89
+ end
90
+
91
+ it "has 12 action cells" do
92
+ subject.scan(REGEXP_ACTION_CELL).size.should == 12
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "#entry_form" do
98
+ let(:entry) { CrudTestModel.first }
99
+ subject do
100
+ with_test_routing { entry_form }
101
+ end
102
+
103
+ it { should match /form .*?action="\/crud_test_models\/#{entry.id}"/ }
104
+ it { should match /input .*?name="crud_test_model\[name\]" .*?type="text"/ }
105
+ it { should match /input .*?name="crud_test_model\[whatever\]" .*?type="text"/ }
106
+ it { should match /input .*?name="crud_test_model\[children\]" .*?type="number"/ }
107
+ it { should match /input .*?name="crud_test_model\[rating\]" .*?type="number"/ }
108
+ it { should match /input .*?name="crud_test_model\[income\]" .*?type="number"/ }
109
+ it { should match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/ }
110
+ it { should match /input .*?name="crud_test_model\[human\]" .*?type="checkbox"/ }
111
+ it { should match /select .*?name="crud_test_model\[companion_id\]"/ }
112
+ it { should match /textarea .*?name="crud_test_model\[remarks\]"/ }
113
+ it { should match(/a .*href="\/crud_test_models\/#{entry.id}\?returning=true".*>Cancel<\/a>/) }
114
+ end
115
+
116
+ describe "#crud_form" do
117
+
118
+ context "for existing entry" do
119
+ subject do
120
+ with_test_routing do
121
+ capture do
122
+ crud_form(entry,
123
+ :name, :children, :birthdate, :human,
124
+ :cancel_url => "/somewhere",
125
+ :html => {:class => 'special'})
126
+ end
127
+ end
128
+ end
129
+
130
+ let(:entry) { crud_test_models(:AAAAA) }
131
+
132
+ it { should match(/form .*?action="\/crud_test_models\/#{entry.id}" .?class="special form-horizontal" .*?method="post"/) }
133
+ it { should match(/input .*?name="_method" .*?type="hidden" .*?value="put"/) }
134
+ it { should match(/input .*?name="crud_test_model\[name\]" .*?type="text" .*?value="AAAAA"/) }
135
+ it { should match(/select .*?name="crud_test_model\[birthdate\(1i\)\]"/) }
136
+ it { should match(/option selected="selected" value="1910">1910<\/option>/) }
137
+ it { should match(/option selected="selected" value="1">January<\/option>/) }
138
+ it { should match(/option selected="selected" value="1">1<\/option>/) }
139
+ it { should match(/input .*?name="crud_test_model\[children\]" .*?type="number" .*?value=\"9\"/) }
140
+ it { should match(/input .*?name="crud_test_model\[human\]" .*?type="checkbox"/) }
141
+ it { should match(/button .*?type="submit">Save<\/button>/) }
142
+ it { should match(/a .*href="\/somewhere".*>Cancel<\/a>/) }
143
+ end
144
+ end
145
+
146
+ end