dry_crud 2.1.1 → 2.1.2

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.
Files changed (37) hide show
  1. checksums.yaml +9 -9
  2. data/VERSION +1 -1
  3. data/app/assets/stylesheets/sample.scss +1 -1
  4. data/app/controllers/crud_controller.rb +1 -1
  5. data/app/controllers/dry_crud/generic_model.rb +45 -45
  6. data/app/controllers/dry_crud/nestable.rb +1 -1
  7. data/app/controllers/dry_crud/render_callbacks.rb +2 -0
  8. data/app/controllers/dry_crud/responder.rb +3 -0
  9. data/app/controllers/list_controller.rb +1 -1
  10. data/app/helpers/dry_crud/form/builder.rb +286 -261
  11. data/app/helpers/dry_crud/form/control.rb +156 -153
  12. data/app/helpers/dry_crud/table/actions.rb +69 -67
  13. data/app/helpers/dry_crud/table/builder.rb +96 -95
  14. data/app/helpers/dry_crud/table/col.rb +17 -16
  15. data/app/helpers/dry_crud/table/sorting.rb +48 -46
  16. data/app/helpers/format_helper.rb +2 -2
  17. data/app/helpers/table_helper.rb +2 -2
  18. data/lib/generators/dry_crud/file_generator.rb +2 -2
  19. data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +4 -6
  20. data/lib/generators/dry_crud/templates/spec/helpers/dry_crud/form/builder_spec.rb +11 -11
  21. data/lib/generators/dry_crud/templates/spec/helpers/form_helper_spec.rb +21 -21
  22. data/lib/generators/dry_crud/templates/spec/helpers/format_helper_spec.rb +21 -17
  23. data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +8 -8
  24. data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +11 -1
  25. data/lib/generators/dry_crud/templates/test/controllers/crud_test_models_controller_test.rb +6 -6
  26. data/lib/generators/dry_crud/templates/test/helpers/custom_assertions_test.rb +1 -0
  27. data/lib/generators/dry_crud/templates/test/helpers/dry_crud/form/builder_test.rb +250 -245
  28. data/lib/generators/dry_crud/templates/test/helpers/dry_crud/table/builder_test.rb +141 -128
  29. data/lib/generators/dry_crud/templates/test/helpers/form_helper_test.rb +54 -54
  30. data/lib/generators/dry_crud/templates/test/helpers/format_helper_test.rb +4 -4
  31. data/lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb +1 -1
  32. data/lib/generators/dry_crud/templates/test/support/crud_controller_test_helper.rb +3 -2
  33. data/lib/generators/dry_crud/templates/test/support/crud_test_helper.rb +8 -8
  34. data/lib/generators/dry_crud/templates/test/support/crud_test_model.rb +0 -2
  35. data/lib/generators/dry_crud/templates/test/support/crud_test_models_controller.rb +7 -7
  36. data/lib/generators/dry_crud/templates/test/support/custom_assertions.rb +3 -3
  37. metadata +6 -6
@@ -30,7 +30,7 @@ describe 'DryCrud::Form::Builder' do
30
30
 
31
31
  it 'dispatches name attr to string field' do
32
32
  form.should_receive(:string_field)
33
- .with(:name, class: 'form-control', required: 'required')
33
+ .with(:name, required: 'required')
34
34
  .and_return('<input>')
35
35
  form.input_field(:name)
36
36
  end
@@ -47,10 +47,10 @@ describe 'DryCrud::Form::Builder' do
47
47
  last_seen: :datetime_field,
48
48
  companion_id: :belongs_to_field,
49
49
  other_ids: :has_many_field,
50
- more_ids: :has_many_field,
50
+ more_ids: :has_many_field
51
51
  }.each do |attr, method|
52
52
  it 'dispatches #{attr} attr to #{method}' do
53
- form.should_receive(method).with(attr, class: 'form-control')
53
+ form.should_receive(method).with(attr, {})
54
54
  form.input_field(attr)
55
55
  end
56
56
 
@@ -137,7 +137,7 @@ describe 'DryCrud::Form::Builder' do
137
137
 
138
138
  describe '#string_field' do
139
139
  it 'sets maxlength if attr has a limit' do
140
- form.string_field(:name).should match /maxlength="50"/
140
+ form.string_field(:name).should match(/maxlength="50"/)
141
141
  end
142
142
  end
143
143
 
@@ -147,7 +147,7 @@ describe 'DryCrud::Form::Builder' do
147
147
 
148
148
  it { should be_html_safe }
149
149
  it 'provides the same interface as rails' do
150
- should match /label [^>]*for.+Gugus dada/
150
+ should match(/label [^>]*for.+Gugus dada/)
151
151
  end
152
152
  end
153
153
 
@@ -156,7 +156,7 @@ describe 'DryCrud::Form::Builder' do
156
156
 
157
157
  it { should be_html_safe }
158
158
  it 'provides the same interface as rails' do
159
- should match /label [^>]*for.+hoho/
159
+ should match(/label [^>]*for.+hoho/)
160
160
  end
161
161
  end
162
162
 
@@ -168,7 +168,7 @@ describe 'DryCrud::Form::Builder' do
168
168
 
169
169
  it { should be_html_safe }
170
170
  it 'provides the same interface as rails' do
171
- should match /label [^>]*for.+input/m
171
+ should match(/label [^>]*for.+input/m)
172
172
  end
173
173
  end
174
174
 
@@ -178,7 +178,7 @@ describe 'DryCrud::Form::Builder' do
178
178
  end
179
179
 
180
180
  it { should be_html_safe }
181
- it { should match /label [^>]*for.+<input/m }
181
+ it { should match(/label [^>]*for.+<input/m) }
182
182
  end
183
183
 
184
184
  context 'with custom content in block' do
@@ -189,7 +189,7 @@ describe 'DryCrud::Form::Builder' do
189
189
  end
190
190
 
191
191
  it { should be_html_safe }
192
- it { should match /label [^>]*for.+<input/m }
192
+ it { should match(/label [^>]*for.+<input/m) }
193
193
  end
194
194
 
195
195
  context 'with caption and content in argument' do
@@ -200,7 +200,7 @@ describe 'DryCrud::Form::Builder' do
200
200
  end
201
201
 
202
202
  it { should be_html_safe }
203
- it { should match /label [^>]*for.+>Caption<\/label>.*<input/m }
203
+ it { should match(/label [^>]*for.+>Caption<\/label>.*<input/m) }
204
204
  end
205
205
 
206
206
  context 'with caption and content in block' do
@@ -211,7 +211,7 @@ describe 'DryCrud::Form::Builder' do
211
211
  end
212
212
 
213
213
  it { should be_html_safe }
214
- it { should match /label [^>]*for.+>Caption<\/label>.*<input/m }
214
+ it { should match(/label [^>]*for.+>Caption<\/label>.*<input/m) }
215
215
  end
216
216
  end
217
217
 
@@ -153,60 +153,60 @@ describe FormHelper do
153
153
  end
154
154
 
155
155
  it do
156
- should match /form .*?action="\/crud_test_models\/#{entry.id}"/
156
+ should match(/form .*?action="\/crud_test_models\/#{entry.id}"/)
157
157
  end
158
158
 
159
159
  it do
160
- should match /input .*?name="crud_test_model\[name\]"
161
- .*?type="text"/x
160
+ should match(/input .*?name="crud_test_model\[name\]"
161
+ .*?type="text"/x)
162
162
  end
163
163
 
164
164
  it do
165
- should match /input .*?name="crud_test_model\[whatever\]"
166
- .*?type="text"/x
165
+ should match(/input .*?name="crud_test_model\[whatever\]"
166
+ .*?type="text"/x)
167
167
  end
168
168
 
169
169
  it do
170
- should match /input .*?name="crud_test_model\[children\]"
171
- .*?type="number"/x
170
+ should match(/input .*?name="crud_test_model\[children\]"
171
+ .*?type="number"/x)
172
172
  end
173
173
 
174
174
  it do
175
- should match /input .*?name="crud_test_model\[rating\]"
176
- .*?type="number"/x
175
+ should match(/input .*?name="crud_test_model\[rating\]"
176
+ .*?type="number"/x)
177
177
  end
178
178
 
179
179
  it do
180
- should match /input .*?name="crud_test_model\[income\]"
181
- .*?type="number"/x
180
+ should match(/input .*?name="crud_test_model\[income\]"
181
+ .*?type="number"/x)
182
182
  end
183
183
 
184
184
  it do
185
- should match /input .*?name="crud_test_model\[birthdate\]"
186
- .*?type="date"/x
185
+ should match(/input .*?name="crud_test_model\[birthdate\]"
186
+ .*?type="date"/x)
187
187
  end
188
188
 
189
189
  it do
190
- should match /input .*?name="crud_test_model\[gets_up_at\]"
191
- .*?type="time"/x
190
+ should match(/input .*?name="crud_test_model\[gets_up_at\]"
191
+ .*?type="time"/x)
192
192
  end
193
193
 
194
194
  it do
195
- should match /input .*?name="crud_test_model\[last_seen\]"
196
- .*?type="datetime"/x
195
+ should match(/input .*?name="crud_test_model\[last_seen\]"
196
+ .*?type="datetime"/x)
197
197
  end
198
198
 
199
199
  it do
200
- should match /input .*?name="crud_test_model\[human\]"
201
- .*?type="checkbox"/x
200
+ should match(/input .*?name="crud_test_model\[human\]"
201
+ .*?type="checkbox"/x)
202
202
  end
203
203
 
204
204
  it do
205
- should match /select .*?name="crud_test_model\[companion_id\]"/
205
+ should match(/select .*?name="crud_test_model\[companion_id\]"/)
206
206
  end
207
207
 
208
208
  it do
209
- should match /textarea .*?name="crud_test_model\[remarks\]"/
209
+ should match(/textarea .*?name="crud_test_model\[remarks\]"/)
210
210
  end
211
211
 
212
212
  it do
@@ -29,9 +29,10 @@ describe FormatHelper do
29
29
  subject { labeled('label') { 'value' } }
30
30
 
31
31
  it { should be_html_safe }
32
- its(:squish) do
33
- should =~ /^<dt>label<\/dt>
34
- \ <dd\ class=['"]value['"]>value<\/dd>$/x
32
+ it do
33
+ subject.squish.should =~
34
+ /^<dt>label<\/dt>
35
+ \ <dd\ class=['"]value['"]>value<\/dd>$/x
35
36
  end
36
37
  end
37
38
 
@@ -39,11 +40,12 @@ describe FormatHelper do
39
40
  subject { labeled('label') { '' } }
40
41
 
41
42
  it { should be_html_safe }
42
- its(:squish) do
43
- should =~ /<dt>label<\/dt>
44
- \ <dd\ class=['"]value['"]>
45
- #{UtilityHelper::EMPTY_STRING}
46
- <\/dd>$/x
43
+ it do
44
+ subject.squish.should =~
45
+ /<dt>label<\/dt>
46
+ \ <dd\ class=['"]value['"]>
47
+ #{UtilityHelper::EMPTY_STRING}
48
+ <\/dd>$/x
47
49
  end
48
50
  end
49
51
 
@@ -51,11 +53,12 @@ describe FormatHelper do
51
53
  subject { labeled('label') { 'value <unsafe>' } }
52
54
 
53
55
  it { should be_html_safe }
54
- its(:squish) do
55
- should =~ /<dt>label<\/dt>
56
- \ <dd\ class=['"]value['"]>
57
- value\ &lt;unsafe&gt;
58
- <\/dd>$/x
56
+ it do
57
+ subject.squish.should =~
58
+ /<dt>label<\/dt>
59
+ \ <dd\ class=['"]value['"]>
60
+ value\ &lt;unsafe&gt;
61
+ <\/dd>$/x
59
62
  end
60
63
  end
61
64
  end
@@ -64,9 +67,10 @@ describe FormatHelper do
64
67
  subject { labeled_attr('foo', :size) }
65
68
 
66
69
  it { should be_html_safe }
67
- its(:squish) do
68
- should =~ /<dt>Size<\/dt>
69
- \ <dd\ class=['"]value['"]>3\ chars<\/dd>$/x
70
+ it do
71
+ subject.squish.should =~
72
+ /<dt>Size<\/dt>
73
+ \ <dd\ class=['"]value['"]>3\ chars<\/dd>$/x
70
74
  end
71
75
  end
72
76
 
@@ -83,7 +87,7 @@ describe FormatHelper do
83
87
  end
84
88
 
85
89
  it 'adds delimiters' do
86
- f(12345.6789).should == '12,345.679'
90
+ f(12_345.6789).should == '12,345.679'
87
91
  end
88
92
  end
89
93
 
@@ -107,7 +107,7 @@ shared_examples 'crud controller' do |options|
107
107
  combine: 'ij' do
108
108
  it_should_respond
109
109
  it_should_assign_entries
110
- its(:body) { should start_with('[{') }
110
+ it { response.body.should start_with('[{') }
111
111
  end
112
112
  end
113
113
 
@@ -145,7 +145,7 @@ shared_examples 'crud controller' do |options|
145
145
  combine: 'sj' do
146
146
  it_should_respond
147
147
  it_should_assign_entry
148
- its(:body) { should start_with('{') }
148
+ it_should_render_json
149
149
  end
150
150
  end
151
151
 
@@ -208,7 +208,7 @@ shared_examples 'crud controller' do |options|
208
208
  combine: 'cjv' do
209
209
  it_should_respond(201)
210
210
  it_should_set_attrs(:new)
211
- its(:body) { should start_with('{') }
211
+ it_should_render_json
212
212
  end
213
213
 
214
214
  context 'with invalid params',
@@ -217,7 +217,7 @@ shared_examples 'crud controller' do |options|
217
217
  combine: 'cji' do
218
218
  it_should_respond(422)
219
219
  it_should_set_attrs(:new)
220
- its(:body) { should match(/"errors":\{/) }
220
+ it_should_render_error_json
221
221
  it_should_persist_entry(false)
222
222
  end
223
223
  end
@@ -272,7 +272,7 @@ shared_examples 'crud controller' do |options|
272
272
  combine: 'ujv' do
273
273
  it_should_respond(204)
274
274
  it_should_set_attrs(:edit)
275
- its(:body) { should match(/s*/) }
275
+ it { response.body.should be_blank }
276
276
  it_should_persist_entry
277
277
  end
278
278
 
@@ -282,7 +282,7 @@ shared_examples 'crud controller' do |options|
282
282
  combine: 'uji' do
283
283
  it_should_respond(422)
284
284
  it_should_set_attrs(:edit)
285
- its(:body) { should match(/"errors":\{/) }
285
+ it_should_render_error_json
286
286
  end
287
287
  end
288
288
  end
@@ -316,12 +316,12 @@ shared_examples 'crud controller' do |options|
316
316
 
317
317
  context 'successfull', combine: 'djs' do
318
318
  it_should_respond(204)
319
- its(:body) { should match(/s*/) }
319
+ it { response.body.should be_blank }
320
320
  end
321
321
 
322
322
  context 'with failure', failing: true, combine: 'djf' do
323
323
  it_should_respond(422)
324
- its(:body) { should match(/"errors":\{/) }
324
+ it_should_render_error_json
325
325
  end
326
326
  end
327
327
  end
@@ -89,7 +89,7 @@ module CrudControllerTestHelper
89
89
 
90
90
  # Test the response status, default 200.
91
91
  def it_should_respond(status = 200)
92
- its(:status) { should == status }
92
+ it { response.status.should == status }
93
93
  end
94
94
 
95
95
  # Test that entries are assigned.
@@ -112,6 +112,16 @@ module CrudControllerTestHelper
112
112
  it { should render_template(template || example.metadata[:action]) }
113
113
  end
114
114
 
115
+ # Test that a json response is rendered.
116
+ def it_should_render_json
117
+ it { response.body.should start_with('{') }
118
+ end
119
+
120
+ # Test that a json response containing the error is rendered.
121
+ def it_should_render_error_json
122
+ it { response.body.should match(/"errors":\{/) }
123
+ end
124
+
115
125
  # Test that test_entry_attrs are set on entry.
116
126
  def it_should_set_attrs(action = nil)
117
127
  it 'should set params as entry attributes' do
@@ -149,7 +149,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
149
149
 
150
150
  def test_create
151
151
  super
152
- assert_match /model got created/, flash[:notice]
152
+ assert_match(/model got created/, flash[:notice])
153
153
  assert flash[:alert].blank?
154
154
  assert_equal [:before_create, :before_save, :after_save, :after_create],
155
155
  @controller.called_callbacks
@@ -234,7 +234,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
234
234
  end
235
235
  assert_response :unprocessable_entity
236
236
  assert entry.new_record?
237
- assert_match /errors/, @response.body
237
+ assert_match(/errors/, @response.body)
238
238
  assert_equal [:before_create, :before_save], @controller.called_callbacks
239
239
  end
240
240
 
@@ -257,7 +257,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
257
257
  format: 'json'
258
258
  assert_response :unprocessable_entity
259
259
  assert entry.changed?
260
- assert_match /errors/, @response.body
260
+ assert_match(/errors/, @response.body)
261
261
  assert flash[:notice].blank?
262
262
  assert_equal 20, entry.rating
263
263
  assert_equal [:before_update, :before_save], @controller.called_callbacks
@@ -270,7 +270,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
270
270
  delete :destroy, test_params(id: crud_test_models(:BBBBB).id)
271
271
  end
272
272
  assert_redirected_to_show(entry)
273
- assert_match /companion/, flash[:alert]
273
+ assert_match(/companion/, flash[:alert])
274
274
  assert flash[:notice].blank?
275
275
  end
276
276
 
@@ -281,7 +281,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
281
281
  delete :destroy, test_params(id: e.id)
282
282
  end
283
283
  assert_redirected_to_index
284
- assert_match /illegal name/, flash[:alert]
284
+ assert_match(/illegal name/, flash[:alert])
285
285
  assert flash[:notice].blank?
286
286
  end
287
287
 
@@ -291,7 +291,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
291
291
  format: 'json')
292
292
  end
293
293
  assert_response :unprocessable_entity
294
- assert_match /errors/, @response.body
294
+ assert_match(/errors/, @response.body)
295
295
  assert flash[:notice].blank?
296
296
  end
297
297
 
@@ -1,6 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  require 'test_helper'
3
3
  require 'support/custom_assertions'
4
+ require 'support/crud_test_helper'
4
5
  require 'support/crud_test_model'
5
6
 
6
7
  # Test CustomAssertions
@@ -2,251 +2,256 @@
2
2
  require 'test_helper'
3
3
  require 'support/crud_test_model'
4
4
 
5
- # Test DryCrud::Form::Builder
6
- class DryCrud::Form::BuilderTest < ActionView::TestCase
7
-
8
- include FormatHelper
9
- include I18nHelper
10
- include CrudTestHelper
11
-
12
- # set dummy helper class for ActionView::TestCase
13
- self.helper_class = UtilityHelper
14
-
15
- attr_reader :form, :entry
16
-
17
- setup :reset_db, :setup_db, :create_test_data, :create_form
18
- teardown :reset_db
19
-
20
- def create_form
21
- @entry = CrudTestModel.first
22
- if Rails.version < '4.0'
23
- @form = DryCrud::Form::Builder.new(:entry, @entry, self, {},
24
- ->(form) { form })
25
- else
26
- @form = DryCrud::Form::Builder.new(:entry, @entry, self, {})
27
- end
28
- end
29
-
30
- test 'input_field dispatches string attr to string_field' do
31
- assert_equal form.with_addon(
32
- form.string_field(:name,
33
- class: 'form-control',
34
- required: 'required'),
35
- '*'),
36
- form.input_field(:name)
37
- assert form.string_field(:name).html_safe?
38
- end
39
-
40
- test 'input_field dispatches password attr to password_field' do
41
- assert_equal form.password_field(:password, class: 'form-control'),
42
- form.input_field(:password)
43
- assert form.password_field(:name).html_safe?
44
- end
45
-
46
- test 'input_field dispatches email attr to email_field' do
47
- assert_equal form.email_field(:email, class: 'form-control'),
48
- form.input_field(:email)
49
- assert form.email_field(:name).html_safe?
50
- end
51
-
52
- test 'input_field dispatches text attr to text_area' do
53
- assert_equal form.text_area(:remarks, class: 'form-control'),
54
- form.input_field(:remarks)
55
- assert form.text_area(:remarks).html_safe?
56
- end
57
-
58
- test 'input_field dispatches integer attr to integer_field' do
59
- assert_equal form.integer_field(:children, class: 'form-control'),
60
- form.input_field(:children)
61
- assert form.integer_field(:children).html_safe?
62
- end
63
-
64
- test 'input_field dispatches boolean attr to boolean_field' do
65
- assert_equal form.boolean_field(:human, class: 'form-control'),
66
- form.input_field(:human)
67
- assert form.boolean_field(:human).html_safe?
68
- end
69
-
70
- test 'input_field dispatches date attr to date_field' do
71
- assert_equal form.date_field(:birthdate, class: 'form-control'),
72
- form.input_field(:birthdate)
73
- assert form.date_field(:birthdate).html_safe?
74
- end
75
-
76
- test 'input_field dispatches belongs_to attr to select field' do
77
- assert_equal form.belongs_to_field(:companion_id, class: 'form-control'),
78
- form.input_field(:companion_id)
79
- assert form.belongs_to_field(:companion_id).html_safe?
80
- end
81
-
82
- test 'input_field dispatches has_and_belongs_to_many attr to select field' do
83
- assert_equal form.has_many_field(:other_ids, class: 'form-control'),
84
- form.input_field(:other_ids)
85
- assert form.has_many_field(:other_ids).html_safe?
86
- end
87
-
88
- test 'input_field dispatches has_many attr to select field' do
89
- assert_equal form.has_many_field(:more_ids, class: 'form-control'),
90
- form.input_field(:more_ids)
91
- assert form.has_many_field(:more_ids).html_safe?
92
- end
93
-
94
- test 'input_fields concats multiple fields' do
95
- result = form.labeled_input_fields(:name, :remarks, :children)
96
- assert result.html_safe?
97
- assert result.include?(form.input_field(:name, required: 'required'))
98
- assert result.include?(form.input_field(:remarks))
99
- assert result.include?(form.input_field(:children))
100
- end
101
-
102
- test 'labeld_input_field adds required mark' do
103
- result = form.labeled_input_field(:name)
104
- assert result.include?('input-group-addon')
105
- result = form.labeled_input_field(:remarks)
106
- assert !result.include?('input-group-addon')
107
- end
108
-
109
- test 'labeld_input_field adds help text' do
110
- result = form.labeled_input_field(:name, help: 'Some Help')
111
- assert result.include?(form.help_block('Some Help'))
112
- assert result.include?('input-group-addon')
113
- end
114
-
115
- test 'belongs_to_field has all options by default' do
116
- f = form.belongs_to_field(:companion_id)
117
- assert_equal 7, f.scan('</option>').size
118
- end
119
-
120
- test 'belongs_to_field with :list option' do
121
- list = CrudTestModel.all
122
- f = form.belongs_to_field(:companion_id,
123
- list: [list.first, list.second])
124
- assert_equal 3, f.scan('</option>').size
125
- end
126
-
127
- test 'belongs_to_field with instance variable' do
128
- list = CrudTestModel.all
129
- @companions = [list.first, list.second]
130
- f = form.belongs_to_field(:companion_id)
131
- assert_equal 3, f.scan('</option>').size
132
- end
133
-
134
- test 'belongs_to_field with empty list' do
135
- @companions = []
136
- f = form.belongs_to_field(:companion_id)
137
- assert_match t('global.associations.none_available'), f
138
- assert_equal 0, f.scan('</option>').size
139
- end
140
-
141
- test 'has_and_belongs_to_many_field has all options by default' do
142
- f = form.has_many_field(:other_ids)
143
- assert_equal 6, f.scan('</option>').size
144
- end
145
-
146
- test 'has_and_belongs_to_many_field with :list option' do
147
- list = OtherCrudTestModel.all
148
- f = form.has_many_field(:other_ids, list: [list.first, list.second])
149
- assert_equal 2, f.scan('</option>').size
150
- end
151
-
152
- test 'has_and_belongs_to_many_field with instance variable' do
153
- list = OtherCrudTestModel.all
154
- @others = [list.first, list.second]
155
- f = form.has_many_field(:other_ids)
156
- assert_equal 2, f.scan('</option>').size
157
- end
158
-
159
- test 'has_and_belongs_to_many_field with empty list' do
160
- @others = []
161
- f = form.has_many_field(:other_ids)
162
- assert_match t('global.associations.none_available'), f
163
- assert_equal 0, f.scan('</option>').size
164
- end
165
-
166
- test 'has_many_field has all options by default' do
167
- f = form.has_many_field(:more_ids)
168
- assert_equal 6, f.scan('</option>').size
169
- end
170
-
171
- test 'has_many_field with :list option' do
172
- list = OtherCrudTestModel.all
173
- f = form.has_many_field(:more_ids, list: [list.first, list.second])
174
- assert_equal 2, f.scan('</option>').size
175
- end
176
-
177
- test 'has_many_field with instance variable' do
178
- list = OtherCrudTestModel.all
179
- @mores = [list.first, list.second]
180
- f = form.has_many_field(:more_ids)
181
- assert_equal 2, f.scan('</option>').size
182
- end
183
-
184
- test 'has_many_field with empty list' do
185
- @mores = []
186
- f = form.has_many_field(:more_ids)
187
- assert_match t('global.associations.none_available'), f
188
- assert_equal 0, f.scan('</option>').size
189
- end
190
-
191
- test 'string_field sets maxlength attribute if limit' do
192
- assert_match /maxlength="50"/, form.string_field(:name)
193
- end
194
-
195
- test 'label creates captionized label' do
196
- assert_match /label [^>]*for.+Gugus dada/, form.label(:gugus_dada)
197
- assert form.label(:gugus_dada).html_safe?
198
- end
199
-
200
- test 'classic label still works' do
201
- assert_match /label [^>]*for.+hoho/, form.label(:gugus_dada, 'hoho')
202
- assert form.label(:gugus_dada, 'hoho').html_safe?
203
- end
204
-
205
- test 'labeled_text_field create label' do
206
- assert_match /label [^>]*for.+input/m, form.labeled_string_field(:name)
207
- assert form.labeled_string_field(:name).html_safe?
208
- end
209
-
210
- test 'labeled field creates label' do
211
- result = form.labeled('gugus',
212
- "<input type='text' name='gugus' />".html_safe)
213
- assert result.html_safe?
214
- assert_match /label [^>]*for.+<input/m, result
215
- end
216
-
217
- test 'labeled field creates label and block' do
218
- result = form.labeled('gugus') do
219
- "<input type='text' name='gugus' />".html_safe
5
+ module DryCrud
6
+ module Form
7
+ # Test DryCrud::Form::Builder
8
+ class BuilderTest < ActionView::TestCase
9
+
10
+ include FormatHelper
11
+ include I18nHelper
12
+ include CrudTestHelper
13
+
14
+ # set dummy helper class for ActionView::TestCase
15
+ self.helper_class = UtilityHelper
16
+
17
+ attr_reader :form, :entry
18
+
19
+ setup :reset_db, :setup_db, :create_test_data, :create_form
20
+ teardown :reset_db
21
+
22
+ def create_form
23
+ @entry = CrudTestModel.first
24
+ if Rails.version < '4.0'
25
+ @form = DryCrud::Form::Builder.new(:entry, @entry, self, {},
26
+ ->(form) { form })
27
+ else
28
+ @form = DryCrud::Form::Builder.new(:entry, @entry, self, {})
29
+ end
30
+ end
31
+
32
+ test 'input_field dispatches string attr to string_field' do
33
+ assert_equal form.with_addon(
34
+ form.string_field(:name,
35
+ required: 'required'),
36
+ '*'),
37
+ form.input_field(:name)
38
+ assert form.string_field(:name).html_safe?
39
+ end
40
+
41
+ test 'input_field dispatches password attr to password_field' do
42
+ assert_equal form.password_field(:password),
43
+ form.input_field(:password)
44
+ assert form.password_field(:name).html_safe?
45
+ end
46
+
47
+ test 'input_field dispatches email attr to email_field' do
48
+ assert_equal form.email_field(:email),
49
+ form.input_field(:email)
50
+ assert form.email_field(:name).html_safe?
51
+ end
52
+
53
+ test 'input_field dispatches text attr to text_area' do
54
+ assert_equal form.text_area(:remarks),
55
+ form.input_field(:remarks)
56
+ assert form.text_area(:remarks).html_safe?
57
+ end
58
+
59
+ test 'input_field dispatches integer attr to integer_field' do
60
+ assert_equal form.integer_field(:children),
61
+ form.input_field(:children)
62
+ assert form.integer_field(:children).html_safe?
63
+ end
64
+
65
+ test 'input_field dispatches boolean attr to boolean_field' do
66
+ assert_equal form.boolean_field(:human),
67
+ form.input_field(:human)
68
+ assert form.boolean_field(:human).html_safe?
69
+ end
70
+
71
+ test 'input_field dispatches date attr to date_field' do
72
+ assert_equal form.date_field(:birthdate),
73
+ form.input_field(:birthdate)
74
+ assert form.date_field(:birthdate).html_safe?
75
+ end
76
+
77
+ test 'input_field dispatches belongs_to attr to select field' do
78
+ assert_equal form.belongs_to_field(:companion_id),
79
+ form.input_field(:companion_id)
80
+ assert form.belongs_to_field(:companion_id).html_safe?
81
+ end
82
+
83
+ test 'input_field dispatches has_and_belongs_to_many attr to ' \
84
+ 'select field' do
85
+ assert_equal form.has_many_field(:other_ids),
86
+ form.input_field(:other_ids)
87
+ assert form.has_many_field(:other_ids).html_safe?
88
+ end
89
+
90
+ test 'input_field dispatches has_many attr to select field' do
91
+ assert_equal form.has_many_field(:more_ids),
92
+ form.input_field(:more_ids)
93
+ assert form.has_many_field(:more_ids).html_safe?
94
+ end
95
+
96
+ test 'input_fields concats multiple fields' do
97
+ result = form.labeled_input_fields(:name, :remarks, :children)
98
+ assert result.html_safe?
99
+ assert result.include?(form.input_field(:name, required: 'required'))
100
+ assert result.include?(form.input_field(:remarks))
101
+ assert result.include?(form.input_field(:children))
102
+ end
103
+
104
+ test 'labeld_input_field adds required mark' do
105
+ result = form.labeled_input_field(:name)
106
+ assert result.include?('input-group-addon')
107
+ result = form.labeled_input_field(:remarks)
108
+ assert !result.include?('input-group-addon')
109
+ end
110
+
111
+ test 'labeld_input_field adds help text' do
112
+ result = form.labeled_input_field(:name, help: 'Some Help')
113
+ assert result.include?(form.help_block('Some Help'))
114
+ assert result.include?('input-group-addon')
115
+ end
116
+
117
+ test 'belongs_to_field has all options by default' do
118
+ f = form.belongs_to_field(:companion_id)
119
+ assert_equal 7, f.scan('</option>').size
120
+ end
121
+
122
+ test 'belongs_to_field with :list option' do
123
+ list = CrudTestModel.all
124
+ f = form.belongs_to_field(:companion_id,
125
+ list: [list.first, list.second])
126
+ assert_equal 3, f.scan('</option>').size
127
+ end
128
+
129
+ test 'belongs_to_field with instance variable' do
130
+ list = CrudTestModel.all
131
+ @companions = [list.first, list.second]
132
+ f = form.belongs_to_field(:companion_id)
133
+ assert_equal 3, f.scan('</option>').size
134
+ end
135
+
136
+ test 'belongs_to_field with empty list' do
137
+ @companions = []
138
+ f = form.belongs_to_field(:companion_id)
139
+ assert_match t('global.associations.none_available'), f
140
+ assert_equal 0, f.scan('</option>').size
141
+ end
142
+
143
+ test 'has_and_belongs_to_many_field has all options by default' do
144
+ f = form.has_many_field(:other_ids)
145
+ assert_equal 6, f.scan('</option>').size
146
+ end
147
+
148
+ test 'has_and_belongs_to_many_field with :list option' do
149
+ list = OtherCrudTestModel.all
150
+ f = form.has_many_field(:other_ids, list: [list.first, list.second])
151
+ assert_equal 2, f.scan('</option>').size
152
+ end
153
+
154
+ test 'has_and_belongs_to_many_field with instance variable' do
155
+ list = OtherCrudTestModel.all
156
+ @others = [list.first, list.second]
157
+ f = form.has_many_field(:other_ids)
158
+ assert_equal 2, f.scan('</option>').size
159
+ end
160
+
161
+ test 'has_and_belongs_to_many_field with empty list' do
162
+ @others = []
163
+ f = form.has_many_field(:other_ids)
164
+ assert_match t('global.associations.none_available'), f
165
+ assert_equal 0, f.scan('</option>').size
166
+ end
167
+
168
+ test 'has_many_field has all options by default' do
169
+ f = form.has_many_field(:more_ids)
170
+ assert_equal 6, f.scan('</option>').size
171
+ end
172
+
173
+ test 'has_many_field with :list option' do
174
+ list = OtherCrudTestModel.all
175
+ f = form.has_many_field(:more_ids, list: [list.first, list.second])
176
+ assert_equal 2, f.scan('</option>').size
177
+ end
178
+
179
+ test 'has_many_field with instance variable' do
180
+ list = OtherCrudTestModel.all
181
+ @mores = [list.first, list.second]
182
+ f = form.has_many_field(:more_ids)
183
+ assert_equal 2, f.scan('</option>').size
184
+ end
185
+
186
+ test 'has_many_field with empty list' do
187
+ @mores = []
188
+ f = form.has_many_field(:more_ids)
189
+ assert_match t('global.associations.none_available'), f
190
+ assert_equal 0, f.scan('</option>').size
191
+ end
192
+
193
+ test 'string_field sets maxlength attribute if limit' do
194
+ assert_match(/maxlength="50"/, form.string_field(:name))
195
+ end
196
+
197
+ test 'label creates captionized label' do
198
+ assert_match(/label [^>]*for.+Gugus dada/, form.label(:gugus_dada))
199
+ assert form.label(:gugus_dada).html_safe?
200
+ end
201
+
202
+ test 'classic label still works' do
203
+ assert_match(/label [^>]*for.+hoho/, form.label(:gugus_dada, 'hoho'))
204
+ assert form.label(:gugus_dada, 'hoho').html_safe?
205
+ end
206
+
207
+ test 'labeled_text_field create label' do
208
+ assert_match(/label [^>]*for.+input/m,
209
+ form.labeled_string_field(:name))
210
+ assert form.labeled_string_field(:name).html_safe?
211
+ end
212
+
213
+ test 'labeled field creates label' do
214
+ result = form.labeled('gugus',
215
+ "<input type='text' name='gugus' />".html_safe)
216
+ assert result.html_safe?
217
+ assert_match(/label [^>]*for.+<input/m, result)
218
+ end
219
+
220
+ test 'labeled field creates label and block' do
221
+ result = form.labeled('gugus') do
222
+ "<input type='text' name='gugus' />".html_safe
223
+ end
224
+ assert result.html_safe?
225
+ assert_match(/label [^>]*for.+<input/m, result)
226
+ end
227
+
228
+ test 'labeled field creates label with caption' do
229
+ result = form.labeled('gugus',
230
+ "<input type='text' name='gugus' />".html_safe,
231
+ caption: 'Caption')
232
+ assert result.html_safe?
233
+ assert_match(/label [^>]*for.+>Caption<\/label>.*<input/m, result)
234
+ end
235
+
236
+ test 'labeled field creates label with caption and block' do
237
+ result = form.labeled('gugus', caption: 'Caption') do
238
+ "<input type='text' name='gugus' />".html_safe
239
+ end
240
+ assert result.html_safe?
241
+ assert_match(/label [^>]*for.+>Caption<\/label>.*<input/m, result)
242
+ end
243
+
244
+ test 'method missing still works' do
245
+ assert_raise(NoMethodError) do
246
+ form.blabla
247
+ end
248
+ end
249
+
250
+ test 'respond to still works' do
251
+ assert !form.respond_to?(:blalba)
252
+ assert form.respond_to?(:text_field)
253
+ assert form.respond_to?(:labeled_text_field)
254
+ end
220
255
  end
221
- assert result.html_safe?
222
- assert_match /label [^>]*for.+<input/m, result
223
- end
224
-
225
- test 'labeled field creates label with caption' do
226
- result = form.labeled('gugus',
227
- "<input type='text' name='gugus' />".html_safe,
228
- caption: 'Caption')
229
- assert result.html_safe?
230
- assert_match /label [^>]*for.+>Caption<\/label>.*<input/m, result
231
- end
232
-
233
- test 'labeled field creates label with caption and block' do
234
- result = form.labeled('gugus', caption: 'Caption') do
235
- "<input type='text' name='gugus' />".html_safe
236
- end
237
- assert result.html_safe?
238
- assert_match /label [^>]*for.+>Caption<\/label>.*<input/m, result
239
- end
240
-
241
- test 'method missing still works' do
242
- assert_raise(NoMethodError) do
243
- form.blabla
244
- end
245
- end
246
-
247
- test 'respond to still works' do
248
- assert !form.respond_to?(:blalba)
249
- assert form.respond_to?(:text_field)
250
- assert form.respond_to?(:labeled_text_field)
251
256
  end
252
257
  end