dry_crud 1.7.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +15 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +126 -87
  4. data/VERSION +1 -1
  5. data/lib/generators/dry_crud/dry_crud_generator.rb +42 -22
  6. data/lib/generators/dry_crud/templates/INSTALL +5 -5
  7. data/lib/generators/dry_crud/templates/app/assets/stylesheets/crud.scss +0 -20
  8. data/lib/generators/dry_crud/templates/app/assets/stylesheets/sample.scss +24 -4
  9. data/lib/generators/dry_crud/templates/app/controllers/crud/generic_model.rb +89 -0
  10. data/lib/generators/dry_crud/templates/app/controllers/crud/nestable.rb +70 -0
  11. data/lib/generators/dry_crud/templates/app/controllers/crud/rememberable.rb +64 -0
  12. data/lib/generators/dry_crud/templates/app/controllers/crud/render_callbacks.rb +46 -0
  13. data/lib/generators/dry_crud/templates/app/controllers/crud/responder.rb +31 -0
  14. data/lib/generators/dry_crud/templates/app/controllers/crud/searchable.rb +55 -0
  15. data/lib/generators/dry_crud/templates/app/controllers/crud/sortable.rb +63 -0
  16. data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +66 -69
  17. data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +23 -326
  18. data/lib/generators/dry_crud/templates/app/helpers/actions_helper.rb +64 -0
  19. data/lib/generators/dry_crud/templates/app/helpers/crud/form_builder.rb +331 -0
  20. data/lib/generators/dry_crud/templates/app/helpers/crud/table_builder.rb +280 -0
  21. data/lib/generators/dry_crud/templates/app/helpers/form_helper.rb +52 -0
  22. data/lib/generators/dry_crud/templates/app/helpers/format_helper.rb +164 -0
  23. data/lib/generators/dry_crud/templates/app/helpers/i18n_helper.rb +85 -0
  24. data/lib/generators/dry_crud/templates/app/helpers/table_helper.rb +83 -0
  25. data/lib/generators/dry_crud/templates/app/helpers/utility_helper.rb +84 -0
  26. data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.erb +3 -3
  27. data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.haml +3 -3
  28. data/lib/generators/dry_crud/templates/app/views/crud/_actions_index.html.erb +1 -1
  29. data/lib/generators/dry_crud/templates/app/views/crud/_actions_index.html.haml +1 -1
  30. data/lib/generators/dry_crud/templates/app/views/crud/_actions_show.html.erb +3 -3
  31. data/lib/generators/dry_crud/templates/app/views/crud/_actions_show.html.haml +3 -3
  32. data/lib/generators/dry_crud/templates/app/views/crud/_attrs.html.erb +1 -1
  33. data/lib/generators/dry_crud/templates/app/views/crud/_attrs.html.haml +1 -1
  34. data/lib/generators/dry_crud/templates/app/views/crud/_form.html.erb +1 -1
  35. data/lib/generators/dry_crud/templates/app/views/crud/_form.html.haml +1 -1
  36. data/lib/generators/dry_crud/templates/app/views/crud/edit.html.erb +1 -1
  37. data/lib/generators/dry_crud/templates/app/views/crud/edit.html.haml +1 -1
  38. data/lib/generators/dry_crud/templates/app/views/crud/new.html.erb +2 -2
  39. data/lib/generators/dry_crud/templates/app/views/crud/new.html.haml +2 -2
  40. data/lib/generators/dry_crud/templates/app/views/crud/show.html.erb +1 -1
  41. data/lib/generators/dry_crud/templates/app/views/crud/show.html.haml +1 -1
  42. data/lib/generators/dry_crud/templates/app/views/layouts/_flash.html.haml +1 -1
  43. data/lib/generators/dry_crud/templates/app/views/layouts/_nav.html.erb +1 -1
  44. data/lib/generators/dry_crud/templates/app/views/layouts/_nav.html.haml +1 -1
  45. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +15 -7
  46. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.haml +18 -8
  47. data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +3 -3
  48. data/lib/generators/dry_crud/templates/app/views/list/_search.html.haml +3 -3
  49. data/lib/generators/dry_crud/templates/app/views/list/index.html.erb +1 -1
  50. data/lib/generators/dry_crud/templates/app/views/list/index.html.haml +1 -1
  51. data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +1 -1
  52. data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.haml +1 -1
  53. data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.erb +2 -4
  54. data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.haml +2 -3
  55. data/lib/generators/dry_crud/templates/config/initializers/field_error_proc.rb +5 -1
  56. data/lib/generators/dry_crud/templates/config/locales/crud.de.yml +64 -0
  57. data/lib/generators/dry_crud/templates/config/locales/{en_crud.yml → crud.en.yml} +2 -2
  58. data/lib/generators/dry_crud/templates/spec/controllers/crud_test_models_controller_spec.rb +241 -231
  59. data/lib/generators/dry_crud/templates/spec/helpers/crud/form_builder_spec.rb +226 -0
  60. data/lib/generators/dry_crud/templates/spec/helpers/{standard_table_builder_spec.rb → crud/table_builder_spec.rb} +36 -34
  61. data/lib/generators/dry_crud/templates/spec/helpers/form_helper_spec.rb +238 -0
  62. data/lib/generators/dry_crud/templates/spec/helpers/format_helper_spec.rb +244 -0
  63. data/lib/generators/dry_crud/templates/spec/helpers/i18n_helper_spec.rb +132 -0
  64. data/lib/generators/dry_crud/templates/spec/helpers/table_helper_spec.rb +265 -0
  65. data/lib/generators/dry_crud/templates/spec/helpers/utility_helper_spec.rb +74 -0
  66. data/lib/generators/dry_crud/templates/spec/support/crud_controller_examples.rb +185 -100
  67. data/lib/generators/dry_crud/templates/spec/support/crud_controller_test_helper.rb +58 -49
  68. data/lib/generators/dry_crud/templates/test/{functional → controllers}/crud_test_models_controller_test.rb +112 -91
  69. data/lib/generators/dry_crud/templates/test/{unit/helpers/standard_form_builder_test.rb → helpers/crud/form_builder_test.rb} +79 -62
  70. data/lib/generators/dry_crud/templates/test/{unit/helpers/standard_table_builder_test.rb → helpers/crud/table_builder_test.rb} +31 -28
  71. data/lib/generators/dry_crud/templates/test/helpers/custom_assertions_test.rb +85 -0
  72. data/lib/generators/dry_crud/templates/test/helpers/form_helper_test.rb +129 -0
  73. data/lib/generators/dry_crud/templates/test/helpers/format_helper_test.rb +163 -0
  74. data/lib/generators/dry_crud/templates/test/helpers/i18n_helper_test.rb +79 -0
  75. data/lib/generators/dry_crud/templates/test/helpers/table_helper_test.rb +217 -0
  76. data/lib/generators/dry_crud/templates/test/helpers/utility_helper_test.rb +63 -0
  77. data/lib/generators/dry_crud/templates/test/{functional → support}/crud_controller_test_helper.rb +70 -59
  78. data/lib/generators/dry_crud/templates/test/{crud_test_model.rb → support/crud_test_model.rb} +107 -75
  79. data/lib/generators/dry_crud/templates/test/support/custom_assertions.rb +83 -0
  80. metadata +83 -146
  81. data/Rakefile +0 -211
  82. data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +0 -168
  83. data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +0 -27
  84. data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +0 -261
  85. data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +0 -304
  86. data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +0 -178
  87. data/lib/generators/dry_crud/templates/spec/helpers/crud_helper_spec.rb +0 -146
  88. data/lib/generators/dry_crud/templates/spec/helpers/list_helper_spec.rb +0 -154
  89. data/lib/generators/dry_crud/templates/spec/helpers/standard_form_builder_spec.rb +0 -215
  90. data/lib/generators/dry_crud/templates/spec/helpers/standard_helper_spec.rb +0 -387
  91. data/lib/generators/dry_crud/templates/test/custom_assertions.rb +0 -78
  92. data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +0 -117
  93. data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +0 -111
  94. data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +0 -123
  95. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +0 -281
  96. data/test/templates/Gemfile +0 -46
  97. data/test/templates/app/controllers/admin/cities_controller.rb +0 -7
  98. data/test/templates/app/controllers/admin/countries_controller.rb +0 -13
  99. data/test/templates/app/controllers/ajax_controller.rb +0 -9
  100. data/test/templates/app/controllers/people_controller.rb +0 -13
  101. data/test/templates/app/controllers/vips_controller.rb +0 -19
  102. data/test/templates/app/helpers/cities_helper.rb +0 -9
  103. data/test/templates/app/helpers/people_helper.rb +0 -8
  104. data/test/templates/app/models/city.rb +0 -28
  105. data/test/templates/app/models/country.rb +0 -16
  106. data/test/templates/app/models/person.rb +0 -12
  107. data/test/templates/app/views/admin/cities/_actions_index.html.erb +0 -2
  108. data/test/templates/app/views/admin/cities/_actions_index.html.haml +0 -2
  109. data/test/templates/app/views/admin/cities/_attrs.html.erb +0 -1
  110. data/test/templates/app/views/admin/cities/_attrs.html.haml +0 -1
  111. data/test/templates/app/views/admin/cities/_form.html.erb +0 -7
  112. data/test/templates/app/views/admin/cities/_form.html.haml +0 -5
  113. data/test/templates/app/views/admin/cities/_hello.html.erb +0 -1
  114. data/test/templates/app/views/admin/cities/_hello.html.haml +0 -1
  115. data/test/templates/app/views/admin/cities/_list.html.erb +0 -3
  116. data/test/templates/app/views/admin/cities/_list.html.haml +0 -3
  117. data/test/templates/app/views/admin/countries/_list.html.erb +0 -4
  118. data/test/templates/app/views/admin/countries/_list.html.haml +0 -3
  119. data/test/templates/app/views/ajax/_actions_index.html.erb +0 -8
  120. data/test/templates/app/views/ajax/_actions_index.html.haml +0 -8
  121. data/test/templates/app/views/ajax/_actions_show.html.erb +0 -4
  122. data/test/templates/app/views/ajax/_actions_show.html.haml +0 -4
  123. data/test/templates/app/views/ajax/_form.html.erb +0 -2
  124. data/test/templates/app/views/ajax/_form.html.haml +0 -2
  125. data/test/templates/app/views/ajax/_hello.html.erb +0 -1
  126. data/test/templates/app/views/ajax/_hello.html.haml +0 -1
  127. data/test/templates/app/views/ajax/ajax.js.erb +0 -1
  128. data/test/templates/app/views/ajax/ajax.js.haml +0 -1
  129. data/test/templates/app/views/ajax/edit.js.erb +0 -1
  130. data/test/templates/app/views/ajax/edit.js.haml +0 -1
  131. data/test/templates/app/views/ajax/show.js.erb +0 -1
  132. data/test/templates/app/views/ajax/show.js.haml +0 -1
  133. data/test/templates/app/views/ajax/update.js.erb +0 -5
  134. data/test/templates/app/views/ajax/update.js.haml +0 -5
  135. data/test/templates/app/views/layouts/_nav.html.erb +0 -6
  136. data/test/templates/app/views/layouts/_nav.html.haml +0 -5
  137. data/test/templates/app/views/layouts/bootstrap.html.erb +0 -68
  138. data/test/templates/app/views/layouts/bootstrap.html.haml +0 -49
  139. data/test/templates/app/views/people/_attrs.html.erb +0 -5
  140. data/test/templates/app/views/people/_attrs.html.haml +0 -4
  141. data/test/templates/app/views/people/_list.html.erb +0 -1
  142. data/test/templates/app/views/people/_list.html.haml +0 -1
  143. data/test/templates/config/database.yml +0 -21
  144. data/test/templates/config/locales/en_cities.yml +0 -56
  145. data/test/templates/config/routes.rb +0 -32
  146. data/test/templates/db/migrate/20100511174904_create_people_and_cities.rb +0 -26
  147. data/test/templates/db/seeds.rb +0 -74
  148. data/test/templates/spec/controllers/admin/cities_controller_spec.rb +0 -74
  149. data/test/templates/spec/controllers/admin/countries_controller_spec.rb +0 -56
  150. data/test/templates/spec/controllers/people_controller_spec.rb +0 -80
  151. data/test/templates/spec/routing/cities_routing_spec.rb +0 -11
  152. data/test/templates/spec/routing/countries_routing_spec.rb +0 -11
  153. data/test/templates/test/fixtures/cities.yml +0 -11
  154. data/test/templates/test/fixtures/countries.yml +0 -11
  155. data/test/templates/test/fixtures/people.yml +0 -14
  156. data/test/templates/test/functional/admin/cities_controller_test.rb +0 -59
  157. data/test/templates/test/functional/admin/countries_controller_test.rb +0 -42
  158. data/test/templates/test/functional/people_controller_test.rb +0 -68
@@ -0,0 +1,226 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe 'Crud::FormBuilder' do
5
+
6
+ include FormatHelper
7
+ include FormHelper
8
+ include UtilityHelper
9
+ include I18nHelper
10
+ include CrudTestHelper
11
+
12
+ before(:all) do
13
+ reset_db
14
+ setup_db
15
+ create_test_data
16
+ end
17
+
18
+ after(:all) { reset_db }
19
+
20
+ let(:entry) { CrudTestModel.first }
21
+ <% if Rails.version < '4.0' -%>
22
+ let(:form) do
23
+ Crud::FormBuilder.new(:entry, entry, self, {}, ->(form) { form })
24
+ end
25
+ <% else -%>
26
+ let(:form) { Crud::FormBuilder.new(:entry, entry, self, {}) }
27
+ <% end -%>
28
+
29
+ describe '#input_field' do
30
+
31
+ { name: :string_field,
32
+ password: :password_field,
33
+ email: :email_field,
34
+ remarks: :text_area,
35
+ children: :integer_field,
36
+ human: :boolean_field,
37
+ birthdate: :date_field,
38
+ gets_up_at: :time_field,
39
+ companion_id: :belongs_to_field,
40
+ other_ids: :has_many_field,
41
+ more_ids: :has_many_field,
42
+ }.each do |attr, method|
43
+ it 'dispatches #{attr} attr to #{method}' do
44
+ form.should_receive(method).with(attr, {})
45
+ form.input_field(attr)
46
+ end
47
+
48
+ it { form.input_field(attr).should be_html_safe }
49
+ end
50
+
51
+ end
52
+
53
+ describe '#labeled_input_fields' do
54
+ subject { form.labeled_input_fields(:name, :remarks, :children) }
55
+
56
+ it { should be_html_safe }
57
+ it { should include(form.input_field(:name, required: 'required')) }
58
+ it { should include(form.input_field(:remarks)) }
59
+ it { should include(form.input_field(:children)) }
60
+ end
61
+
62
+ describe '#labeled_input_field' do
63
+ context 'when required' do
64
+ subject { form.labeled_input_field(:name) }
65
+ it { should include(Crud::FormBuilder::REQUIRED_MARK) }
66
+ end
67
+
68
+ context 'when not required' do
69
+ subject { form.labeled_input_field(:remarks) }
70
+ it { should_not include(Crud::FormBuilder::REQUIRED_MARK) }
71
+ end
72
+
73
+ context 'with help text' do
74
+ subject { form.labeled_input_field(:name, help: 'Some Help') }
75
+ it { should include(form.help_block('Some Help')) }
76
+ end
77
+ end
78
+
79
+ describe '#belongs_to_field' do
80
+ it 'has all options by default' do
81
+ f = form.belongs_to_field(:companion_id)
82
+ f.scan('</option>').should have(7).items
83
+ end
84
+
85
+ it 'with has options from :list option' do
86
+ list = CrudTestModel.all
87
+ f = form.belongs_to_field(:companion_id,
88
+ list: [list.first, list.second])
89
+ f.scan('</option>').should have(3).items
90
+ end
91
+
92
+ it 'with empty instance list has no select' do
93
+ assign(:companions, [])
94
+ @companions = []
95
+ f = form.belongs_to_field(:companion_id)
96
+ f.should match t('global.associations.none_available')
97
+ f.scan('</option>').should have(0).items
98
+ end
99
+ end
100
+
101
+ describe '#has_and_belongs_to_many_field' do
102
+ let(:others) { OtherCrudTestModel.all[0..1] }
103
+
104
+ it 'has all options by default' do
105
+ f = form.has_many_field(:other_ids)
106
+ f.scan('</option>').should have(6).items
107
+ end
108
+
109
+ it 'uses options from :list option if given' do
110
+ f = form.has_many_field(:other_ids, list: others)
111
+ f.scan('</option>').should have(2).items
112
+ end
113
+
114
+ it 'uses options form instance variable if given' do
115
+ assign(:others, others)
116
+ @others = others
117
+ f = form.has_many_field(:other_ids)
118
+ f.scan('</option>').should have(2).items
119
+ end
120
+
121
+ it 'displays a message for an empty list' do
122
+ @others = []
123
+ f = form.has_many_field(:other_ids)
124
+ f.should match t('global.associations.none_available')
125
+ f.scan('</option>').should have(0).items
126
+ end
127
+ end
128
+
129
+ describe '#string_field' do
130
+ it 'sets maxlength if attr has a limit' do
131
+ form.string_field(:name).should match /maxlength="50"/
132
+ end
133
+ end
134
+
135
+ describe '#label' do
136
+ context 'only with attr' do
137
+ subject { form.label(:gugus_dada) }
138
+
139
+ it { should be_html_safe }
140
+ it 'provides the same interface as rails' do
141
+ should match /label [^>]*for.+Gugus dada/
142
+ end
143
+ end
144
+
145
+ context 'with attr and text' do
146
+ subject { form.label(:gugus_dada, 'hoho') }
147
+
148
+ it { should be_html_safe }
149
+ it 'provides the same interface as rails' do
150
+ should match /label [^>]*for.+hoho/
151
+ end
152
+ end
153
+
154
+ end
155
+
156
+ describe '#labeled' do
157
+ context 'in labeled_ method' do
158
+ subject { form.labeled_string_field(:name) }
159
+
160
+ it { should be_html_safe }
161
+ it 'provides the same interface as rails' do
162
+ should match /label [^>]*for.+input/m
163
+ end
164
+ end
165
+
166
+ context 'with custom content in argument' do
167
+ subject do
168
+ form.labeled('gugus', "<input type='text' name='gugus' />".html_safe)
169
+ end
170
+
171
+ it { should be_html_safe }
172
+ it { should match /label [^>]*for.+<input/m }
173
+ end
174
+
175
+ context 'with custom content in block' do
176
+ subject do
177
+ form.labeled('gugus') do
178
+ "<input type='text' name='gugus' />".html_safe
179
+ end
180
+ end
181
+
182
+ it { should be_html_safe }
183
+ it { should match /label [^>]*for.+<input/m }
184
+ end
185
+
186
+ context 'with caption and content in argument' do
187
+ subject do
188
+ form.labeled('gugus',
189
+ 'Caption',
190
+ "<input type='text' name='gugus' />".html_safe)
191
+ end
192
+
193
+ it { should be_html_safe }
194
+ it { should match /label [^>]*for.+>Caption<\/label>.*<input/m }
195
+ end
196
+
197
+ context 'with caption and content in block' do
198
+ subject do
199
+ form.labeled('gugus', 'Caption') do
200
+ "<input type='text' name='gugus' />".html_safe
201
+ end
202
+ end
203
+
204
+ it { should be_html_safe }
205
+ it { should match /label [^>]*for.+>Caption<\/label>.*<input/m }
206
+ end
207
+ end
208
+
209
+ it 'handles missing methods' do
210
+ expect { form.blabla }.to raise_error(NoMethodError)
211
+ end
212
+
213
+ context '#respond_to?' do
214
+ it 'returns false for non existing methods' do
215
+ form.respond_to?(:blabla).should be_false
216
+ end
217
+
218
+ it 'returns true for existing methods' do
219
+ form.respond_to?(:text_field).should be_true
220
+ end
221
+
222
+ it 'returns true for labeled_ methods' do
223
+ form.respond_to?(:labeled_text_field).should be_true
224
+ end
225
+ end
226
+ end
@@ -1,55 +1,57 @@
1
+ # encoding: UTF-8
1
2
  require 'spec_helper'
2
3
 
3
- describe 'StandardTableBuilder' do
4
-
5
- include StandardHelper
6
-
4
+ describe 'Crud::TableBuilder' do
5
+
6
+ include FormatHelper
7
+ include UtilityHelper
8
+
7
9
  let(:entries) { %w(foo bahr) }
8
- let(:table) { StandardTableBuilder.new(entries, self) }
10
+ let(:table) { Crud::TableBuilder.new(entries, self) }
9
11
 
10
12
  def format_size(obj) #:nodoc:
11
13
  "#{obj.size} chars"
12
14
  end
13
15
 
14
- specify "#html_header" do
16
+ specify '#html_header' do
15
17
  table.attrs :upcase, :size
16
18
  dom = '<tr><th>Upcase</th><th>Size</th></tr>'
17
19
  assert_dom_equal dom, table.send(:html_header)
18
20
  end
19
-
20
- specify "single attr row" do
21
+
22
+ specify 'single attr row' do
21
23
  table.attrs :upcase, :size
22
24
  dom = '<tr><td>FOO</td><td>3 chars</td></tr>'
23
25
  assert_dom_equal dom, table.send(:html_row, entries.first)
24
26
  end
25
27
 
26
- specify "custom row" do
27
- table.col("Header", :class => 'hula') {|e| "Weights #{e.size} kg" }
28
+ specify 'custom row' do
29
+ table.col('Header', class: 'hula') { |e| "Weights #{e.size} kg" }
28
30
  dom = '<tr><td class="hula">Weights 3 kg</td></tr>'
29
31
  assert_dom_equal dom, table.send(:html_row, entries.first)
30
32
  end
31
33
 
32
- context "attr col" do
34
+ context 'attr col' do
33
35
  let(:col) { table.cols.first }
34
-
35
- context "output" do
36
+
37
+ context 'output' do
36
38
  before { table.attrs :upcase }
37
-
38
- it { col.html_header.should == "<th>Upcase</th>" }
39
- it { col.content('foo').should == "FOO" }
40
- it { col.html_cell('foo').should == "<td>FOO</td>" }
39
+
40
+ it { col.html_header.should == '<th>Upcase</th>' }
41
+ it { col.content('foo').should == 'FOO' }
42
+ it { col.html_cell('foo').should == '<td>FOO</td>' }
41
43
  end
42
44
 
43
- context "content with custom format_size method" do
45
+ context 'content with custom format_size method' do
44
46
  before { table.attrs :size }
45
-
46
- it { col.content("abcd").should == "4 chars" }
47
+
48
+ it { col.content('abcd').should == '4 chars' }
47
49
  end
48
50
  end
49
51
 
50
- specify "two x two table" do
52
+ specify 'two x two table' do
51
53
  dom = <<-FIN
52
- <table class="table">
54
+ <table>
53
55
  <thead>
54
56
  <tr><th>Upcase</th><th>Size</th></tr>
55
57
  </thead>
@@ -59,18 +61,18 @@ describe 'StandardTableBuilder' do
59
61
  </tbody>
60
62
  </table>
61
63
  FIN
62
- dom.gsub!(/[\n\t]/, "").gsub!(/\s{2,}/, "")
64
+ dom.gsub!(/[\n\t]/, '').gsub!(/\s{2,}/, '')
63
65
 
64
66
  table.attrs :upcase, :size
65
67
 
66
68
  assert_dom_equal dom, table.to_html
67
69
  end
68
70
 
69
- specify "table with before and after cells" do
71
+ specify 'table with before and after cells' do
70
72
  dom = <<-FIN
71
- <table class="table">
73
+ <table>
72
74
  <thead>
73
- <tr><th>head</th><th>Upcase</th><th>Size</th><th></th></tr>
75
+ <tr><th class="left">head</th><th>Upcase</th><th>Size</th><th></th></tr>
74
76
  </thead>
75
77
  <tbody>
76
78
  <tr>
@@ -88,29 +90,29 @@ describe 'StandardTableBuilder' do
88
90
  </tbody>
89
91
  </table>
90
92
  FIN
91
- dom.gsub!(/[\n\t]/, "").gsub!(/\s{2,}/, "")
93
+ dom.gsub!(/[\n\t]/, '').gsub!(/\s{2,}/, '')
92
94
 
93
- table.col('head', :class => 'left') { |e| link_to e, "/" }
95
+ table.col('head', class: 'left') { |e| link_to e, '/' }
94
96
  table.attrs :upcase, :size
95
97
  table.col { |e| "Never #{e}" }
96
98
 
97
99
  assert_dom_equal dom, table.to_html
98
100
  end
99
101
 
100
- specify "empty entries collection renders empty table" do
102
+ specify 'empty entries collection renders empty table' do
101
103
  dom = <<-FIN
102
- <table class="table">
104
+ <table>
103
105
  <thead>
104
- <tr><th>head</th><th>Upcase</th><th>Size</th><th></th></tr>
106
+ <tr><th class="left">head</th><th>Upcase</th><th>Size</th><th></th></tr>
105
107
  </thead>
106
108
  <tbody>
107
109
  </tbody>
108
110
  </table>
109
111
  FIN
110
- dom.gsub!(/[\n\t]/, "").gsub!(/\s{2,}/, "")
112
+ dom.gsub!(/[\n\t]/, '').gsub!(/\s{2,}/, '')
111
113
 
112
- table = StandardTableBuilder.new([], self)
113
- table.col('head', :class => 'left') { |e| link_to e, "/" }
114
+ table = Crud::TableBuilder.new([], self)
115
+ table.col('head', class: 'left') { |e| link_to e, '/' }
114
116
  table.attrs :upcase, :size
115
117
  table.col { |e| "Never #{e}" }
116
118
 
@@ -0,0 +1,238 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe FormHelper do
5
+
6
+ include UtilityHelper
7
+ include FormatHelper
8
+ include I18nHelper
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
+ describe '#plain_form' do
20
+ subject do
21
+ with_test_routing do
22
+ capture do
23
+ plain_form(entry, html: { class: 'special' }) do |f|
24
+ f.labeled_input_fields :name, :birthdate
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ context 'for existing entry' do
31
+ let(:entry) { crud_test_models(:AAAAA) }
32
+
33
+ it do
34
+ should match(/form .*?action="\/crud_test_models\/#{entry.id}"
35
+ .*?class="special\ form-horizontal"
36
+ .*?method="post"/x)
37
+ end
38
+
39
+ it do
40
+ should match(/input .*?name="_method"
41
+ .*?type="hidden"
42
+ .*?value="(put|patch)"/x)
43
+ end
44
+
45
+ it do
46
+ should match(/input .*?name="crud_test_model\[name\]"
47
+ .*?type="text"
48
+ .*?value="AAAAA"/x)
49
+ end
50
+
51
+ it do
52
+ should match(/select .*?name="crud_test_model
53
+ \[birthdate\(1i\)\]"/x)
54
+ end
55
+
56
+ it do
57
+ should match(/option\ selected="selected"
58
+ \ value="1910">1910<\/option>/x)
59
+ end
60
+
61
+ it do
62
+ should match(/option\ selected="selected"\ value="1">
63
+ #{t('date.month_names')[1]}
64
+ <\/option>/x)
65
+ end
66
+
67
+ it do
68
+ should match(/option\ selected="selected"
69
+ \ value="1">1<\/option>/x)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#standard_form' do
75
+
76
+ subject do
77
+ with_test_routing do
78
+ capture do
79
+ standard_form(entry,
80
+ :name, :children, :birthdate, :human,
81
+ cancel_url: '/somewhere',
82
+ html: { class: 'special' })
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'for existing entry' do
88
+ let(:entry) { crud_test_models(:AAAAA) }
89
+
90
+ it do
91
+ should match(/form .*?action="\/crud_test_models\/#{entry.id}"
92
+ .*?class="special\ form-horizontal"
93
+ .*?method="post"/x)
94
+ end
95
+
96
+ it do
97
+ should match(/input .*?name="_method"
98
+ .*?type="hidden"
99
+ .*?value="(put|patch)"/x)
100
+ end
101
+
102
+ it do
103
+ should match(/input .*?name="crud_test_model\[name\]"
104
+ .*?type="text"
105
+ .*?value="AAAAA"/x)
106
+ end
107
+
108
+ it do
109
+ should match(/select .*?name="crud_test_model
110
+ \[birthdate\(1i\)\]"/x)
111
+ end
112
+
113
+ it do
114
+ should match(/option\ selected="selected"
115
+ \ value="1910">1910<\/option>/x)
116
+ end
117
+
118
+ it do
119
+ should match(/option\ selected="selected"\ value="1">
120
+ #{t('date.month_names')[1]}
121
+ <\/option>/x)
122
+ end
123
+
124
+ it do
125
+ should match(/option\ selected="selected"
126
+ \ value="1">1<\/option>/x)
127
+ end
128
+
129
+ it do
130
+ should match(/input .*?name="crud_test_model\[children\]"
131
+ .*?type="number"
132
+ .*?value=\"9\"/x)
133
+ end
134
+
135
+ it do
136
+ should match(/input .*?name="crud_test_model\[human\]"
137
+ .*?type="checkbox"/x)
138
+ end
139
+
140
+ it do
141
+ should match(/button\ .*?type="submit">
142
+ #{t('global.button.save')}
143
+ <\/button>/x)
144
+ end
145
+
146
+ it do
147
+ should match(/a\ .*href="\/somewhere".*>
148
+ #{t('global.button.cancel')}
149
+ <\/a>/x)
150
+ end
151
+ end
152
+
153
+ context 'for invalid entry' do
154
+ let(:entry) do
155
+ e = crud_test_models(:AAAAA)
156
+ e.name = nil
157
+ e.valid?
158
+ e
159
+ end
160
+
161
+ it do
162
+ should match(/div[^>]* id='error_explanation'/)
163
+ end
164
+
165
+ it do
166
+ should match(/div\ class="control-group\ error"\>.*?
167
+ \<input .*?name="crud_test_model\[name\]"
168
+ .*?type="text"/x)
169
+ end
170
+
171
+ it do
172
+ should match(/input .*?name="_method"
173
+ .*?type="hidden"
174
+ .*?value="(put|patch)"/x)
175
+ end
176
+ end
177
+ end
178
+
179
+ describe '#crud_form' do
180
+ let(:entry) { CrudTestModel.first }
181
+ subject do
182
+ with_test_routing { crud_form }
183
+ end
184
+
185
+ it do
186
+ should match /form .*?action="\/crud_test_models\/#{entry.id}"/
187
+ end
188
+
189
+ it do
190
+ should match /input .*?name="crud_test_model\[name\]"
191
+ .*?type="text"/x
192
+ end
193
+
194
+ it do
195
+ should match /input .*?name="crud_test_model\[whatever\]"
196
+ .*?type="text"/x
197
+ end
198
+
199
+ it do
200
+ should match /input .*?name="crud_test_model\[children\]"
201
+ .*?type="number"/x
202
+ end
203
+
204
+ it do
205
+ should match /input .*?name="crud_test_model\[rating\]"
206
+ .*?type="number"/x
207
+ end
208
+
209
+ it do
210
+ should match /input .*?name="crud_test_model\[income\]"
211
+ .*?type="number"/x
212
+ end
213
+
214
+ it do
215
+ should match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/
216
+ end
217
+
218
+ it do
219
+ should match /input .*?name="crud_test_model\[human\]"
220
+ .*?type="checkbox"/x
221
+ end
222
+
223
+ it do
224
+ should match /select .*?name="crud_test_model\[companion_id\]"/
225
+ end
226
+
227
+ it do
228
+ should match /textarea .*?name="crud_test_model\[remarks\]"/
229
+ end
230
+
231
+ it do
232
+ should match(/a\ .*href="\/crud_test_models\/#{entry.id}
233
+ \?returning=true".*>
234
+ #{t('global.button.cancel')}<\/a>/x)
235
+ end
236
+ end
237
+
238
+ end