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,244 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe FormatHelper do
5
+
6
+ include UtilityHelper
7
+ include I18nHelper
8
+ include CrudTestHelper
9
+
10
+ before(:all) do
11
+ reset_db
12
+ setup_db
13
+ create_test_data
14
+ end
15
+
16
+ after(:all) { reset_db }
17
+
18
+ # define some test format_ methods
19
+ def format_size(obj) #:nodoc:
20
+ "#{f(obj.size)} items"
21
+ end
22
+
23
+ def format_string_size(obj) #:nodoc:
24
+ "#{f(obj.size)} chars"
25
+ end
26
+
27
+ describe '#labeled' do
28
+ context 'regular' do
29
+ subject { labeled('label') { 'value' } }
30
+
31
+ it { should be_html_safe }
32
+ its(:squish) do
33
+ should =~ /^<dt>label<\/dt>
34
+ \ <dd\ class=['"]value['"]>value<\/dd>$/x
35
+ end
36
+ end
37
+
38
+ context 'with empty value' do
39
+ subject { labeled('label') { '' } }
40
+
41
+ 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
47
+ end
48
+ end
49
+
50
+ context 'with unsafe value' do
51
+ subject { labeled('label') { 'value <unsafe>' } }
52
+
53
+ 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
59
+ end
60
+ end
61
+ end
62
+
63
+ describe '#labeled_attr' do
64
+ subject { labeled_attr('foo', :size) }
65
+
66
+ it { should be_html_safe }
67
+ its(:squish) do
68
+ should =~ /<dt>Size<\/dt>
69
+ \ <dd\ class=['"]value['"]>3\ chars<\/dd>$/x
70
+ end
71
+ end
72
+
73
+ describe '#f' do
74
+
75
+ unless ENV['NON_LOCALIZED'] # localization dependent tests
76
+ context 'Floats' do
77
+ it 'should add two digits' do
78
+ f(1.0).should == '1.000'
79
+ end
80
+
81
+ it 'should truncate to two digits' do
82
+ f(3.14159).should == '3.142'
83
+ end
84
+
85
+ it 'should add delimiters' do
86
+ f(12345.6789).should == '12,345.679'
87
+ end
88
+ end
89
+
90
+ context 'Booleans' do
91
+ it 'true should print yes' do
92
+ f(true).should == 'yes'
93
+ end
94
+
95
+ it 'false should print no' do
96
+ f(false).should == 'no'
97
+ end
98
+ end
99
+
100
+ context 'Dates' do
101
+ it 'prints regular date' do
102
+ f(Date.new(2013, 6, 9)).should == '2013-06-09'
103
+ end
104
+ end
105
+
106
+ context 'Times' do
107
+ it 'prints regular date' do
108
+ f(Time.utc(2013, 6, 9, 21, 25)).should == '2013-06-09 21:25'
109
+ end
110
+ end
111
+ end
112
+
113
+ context 'nil' do
114
+ it 'should print an empty string' do
115
+ f(nil).should == UtilityHelper::EMPTY_STRING
116
+ end
117
+ end
118
+
119
+ context 'Strings' do
120
+ it 'should print regular strings unchanged' do
121
+ f('blah blah').should == 'blah blah'
122
+ end
123
+
124
+ it 'should not be html safe' do
125
+ f('<injection>').should_not be_html_safe
126
+ end
127
+ end
128
+
129
+ end
130
+
131
+ describe '#format_attr' do
132
+ it 'should use #f' do
133
+ format_attr('12.342', :to_f).should == f(12.342)
134
+ end
135
+
136
+ it 'should use object attr format method if it exists' do
137
+ format_attr('abcd', :size).should == '4 chars'
138
+ end
139
+
140
+ it 'should use general attr format method if it exists' do
141
+ format_attr([1, 2], :size).should == '2 items'
142
+ end
143
+
144
+ it 'should format empty belongs_to' do
145
+ format_attr(crud_test_models(:AAAAA), :companion).should ==
146
+ t('global.associations.no_entry')
147
+ end
148
+
149
+ it 'should format existing belongs_to' do
150
+ string = format_attr(crud_test_models(:BBBBB), :companion)
151
+ string.should == 'AAAAA'
152
+ end
153
+
154
+ it 'should format existing has_many' do
155
+ string = format_attr(crud_test_models(:CCCCC), :others)
156
+ string.should be_html_safe
157
+ string.should == '<ul><li>AAAAA</li><li>BBBBB</li></ul>'
158
+ end
159
+ end
160
+
161
+ describe '#column_type' do
162
+ let(:model) { crud_test_models(:AAAAA) }
163
+
164
+ it 'should recognize types' do
165
+ column_type(model, :name).should == :string
166
+ column_type(model, :children).should == :integer
167
+ column_type(model, :companion_id).should == :integer
168
+ column_type(model, :rating).should == :float
169
+ column_type(model, :income).should == :decimal
170
+ column_type(model, :birthdate).should == :date
171
+ column_type(model, :gets_up_at).should == :time
172
+ column_type(model, :last_seen).should == :datetime
173
+ column_type(model, :human).should == :boolean
174
+ column_type(model, :remarks).should == :text
175
+ column_type(model, :companion).should be_nil
176
+ end
177
+ end
178
+
179
+ describe '#format_type' do
180
+ let(:model) { crud_test_models(:AAAAA) }
181
+
182
+ it 'should format integers' do
183
+ model.children = 10000
184
+ format_type(model, :children).should == '10000'
185
+ end
186
+
187
+ unless ENV['NON_LOCALIZED'] # localization dependent tests
188
+ it 'should format floats' do
189
+ format_type(model, :rating).should == '1.100'
190
+ end
191
+
192
+ it 'should format decimals' do
193
+ format_type(model, :income).should == '10,000,000.1111'
194
+ end
195
+
196
+ it 'should format dates' do
197
+ format_type(model, :birthdate).should == '1910-01-01'
198
+ end
199
+
200
+ it 'should format times' do
201
+ format_type(model, :gets_up_at).should == '01:01'
202
+ end
203
+
204
+ it 'should format datetimes' do
205
+ format_type(model, :last_seen).should == '2010-01-01 11:21'
206
+ end
207
+ end
208
+
209
+ it 'should format texts' do
210
+ string = format_type(model, :remarks)
211
+ string.should be_html_safe
212
+ string.should == "<p>AAAAA BBBBB CCCCC\n<br />AAAAA BBBBB CCCCC\n</p>"
213
+ end
214
+
215
+ it 'should escape texts' do
216
+ model.remarks = '<unsecure>bla'
217
+ string = format_type(model, :remarks)
218
+ string.should be_html_safe
219
+ string.should == '<p>&lt;unsecure&gt;bla</p>'
220
+ end
221
+
222
+ it 'should format empty texts' do
223
+ model.remarks = ' '
224
+ string = format_type(model, :remarks)
225
+ string.should be_html_safe
226
+ string.should == UtilityHelper::EMPTY_STRING
227
+ end
228
+ end
229
+
230
+ describe '#captionize' do
231
+ it 'should handle symbols' do
232
+ captionize(:camel_case).should == 'Camel Case'
233
+ end
234
+
235
+ it 'should render all upper case' do
236
+ captionize('all upper case').should == 'All Upper Case'
237
+ end
238
+
239
+ it 'should render human attribute name' do
240
+ captionize(:gets_up_at, CrudTestModel).should == 'Gets up at'
241
+ end
242
+ end
243
+
244
+ end
@@ -0,0 +1,132 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe I18nHelper do
5
+
6
+ include CrudTestHelper
7
+
8
+ describe '#translate_inheritable' do
9
+ before { @controller = CrudTestModelsController.new }
10
+
11
+ before do
12
+ I18n.backend.store_translations(
13
+ I18n.locale,
14
+ global: {
15
+ test_key: 'global' })
16
+ end
17
+ subject { ti(:test_key) }
18
+
19
+ it { should == 'global' }
20
+
21
+ context 'with list key' do
22
+ before do
23
+ I18n.backend.store_translations(
24
+ I18n.locale,
25
+ list: {
26
+ global: {
27
+ test_key: 'list global' } })
28
+ end
29
+ it { should == 'list global' }
30
+
31
+ context 'and list action key' do
32
+ before do
33
+ I18n.backend.store_translations(
34
+ I18n.locale,
35
+ list: {
36
+ index: {
37
+ test_key: 'list index' } })
38
+ end
39
+ it { should == 'list index' }
40
+
41
+ context 'and crud global key' do
42
+ before do
43
+ I18n.backend.store_translations(
44
+ I18n.locale,
45
+ crud: {
46
+ global: {
47
+ test_key: 'crud global' } })
48
+ end
49
+ it { should == 'crud global' }
50
+
51
+ context 'and crud action key' do
52
+ before do
53
+ I18n.backend.store_translations(
54
+ I18n.locale,
55
+ crud: {
56
+ index: {
57
+ test_key: 'crud index' } })
58
+ end
59
+ it { should == 'crud index' }
60
+
61
+ context 'and controller global key' do
62
+ before do
63
+ I18n.backend.store_translations(
64
+ I18n.locale,
65
+ crud_test_models: {
66
+ global: {
67
+ test_key: 'test global' } })
68
+ end
69
+ it { should == 'test global' }
70
+
71
+ context 'and controller action key' do
72
+ before do
73
+ I18n.backend.store_translations(
74
+ I18n.locale,
75
+ crud_test_models: {
76
+ index: {
77
+ test_key: 'test index' } })
78
+ end
79
+ it { should == 'test index' }
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ describe '#translate_association' do
89
+ let(:assoc) { CrudTestModel.reflect_on_association(:companion) }
90
+ subject { ta(:test_key, assoc) }
91
+
92
+ before do
93
+ I18n.backend.store_translations(
94
+ I18n.locale,
95
+ global: {
96
+ associations: {
97
+ test_key: 'global' } })
98
+ end
99
+ it { should == 'global' }
100
+
101
+ context 'with model key' do
102
+ before do
103
+ I18n.backend.store_translations(
104
+ I18n.locale,
105
+ activerecord: {
106
+ associations: {
107
+ crud_test_model: {
108
+ test_key: 'model' } } })
109
+ end
110
+
111
+ it { should == 'model' }
112
+
113
+ context 'and assoc key' do
114
+ before do
115
+ I18n.backend.store_translations(
116
+ I18n.locale,
117
+ activerecord: {
118
+ associations: {
119
+ models: {
120
+ crud_test_model: {
121
+ companion: {
122
+ test_key: 'companion' } } } } })
123
+ end
124
+
125
+ it { should == 'companion' }
126
+ it 'should use global without assoc' do
127
+ ta(:test_key).should == 'global'
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,265 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe TableHelper do
5
+
6
+ include FormatHelper
7
+ include UtilityHelper
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_table' do
20
+ subject { plain_table(%w(foo bar), :size) { |t| t.attrs :upcase } }
21
+
22
+ it 'should contain attrs' do
23
+ should match(/<th>Size<\/th>/)
24
+ end
25
+
26
+ it 'should contain block' do
27
+ should match(/<th>Upcase<\/th>/)
28
+ end
29
+ end
30
+
31
+ describe '#plain_table_or_message' do
32
+ context 'with empty data' do
33
+ subject { plain_table_or_message([]) }
34
+
35
+ it { should be_html_safe }
36
+
37
+ it 'should handle empty data' do
38
+ should match(/div class=.table.\>.+\<\/div\>/)
39
+ end
40
+ end
41
+
42
+ context 'with data' do
43
+ subject do
44
+ plain_table_or_message(%w(foo bar), :size) { |t| t.attrs :upcase }
45
+ end
46
+
47
+ it { should be_html_safe }
48
+
49
+ it 'should render table' do
50
+ should match(/^\<table.*\<\/table\>$/)
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '#list_table' do
56
+ let(:entries) { CrudTestModel.all }
57
+
58
+ context 'default' do
59
+ subject do
60
+ with_test_routing { list_table }
61
+ end
62
+
63
+ it 'has 7 rows' do
64
+ subject.scan(REGEXP_ROWS).size.should == 7
65
+ end
66
+
67
+ it 'has 14 sortable headers' do
68
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 14
69
+ end
70
+ end
71
+
72
+ context 'with custom attributes' do
73
+ subject do
74
+ with_test_routing { list_table(:name, :children, :companion_id) }
75
+ end
76
+
77
+ it 'has 7 rows' do
78
+ subject.scan(REGEXP_ROWS).size.should == 7
79
+ end
80
+
81
+ it 'has 3 sortable headers' do
82
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 3
83
+ end
84
+ end
85
+
86
+ context 'with custom block' do
87
+ subject do
88
+ with_test_routing do
89
+ list_table do |t|
90
+ t.attrs :name, :children, :companion_id
91
+ t.col('head') { |e| content_tag(:span, e.income.to_s) }
92
+ end
93
+ end
94
+ end
95
+
96
+ it 'has 7 rows' do
97
+ subject.scan(REGEXP_ROWS).size.should == 7
98
+ end
99
+
100
+ it 'has 4 headers' do
101
+ subject.scan(REGEXP_HEADERS).size.should == 4
102
+ end
103
+
104
+ it 'has 0 sortable headers' do
105
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 0
106
+ end
107
+
108
+ it 'has 6 spans' do
109
+ subject.scan(/<span>.+?<\/span>/).size.should == 6
110
+ end
111
+ end
112
+
113
+ context 'with custom attributes and block' do
114
+ subject do
115
+ with_test_routing do
116
+ list_table(:name, :children, :companion_id) do |t|
117
+ t.col('head') { |e| content_tag(:span, e.income.to_s) }
118
+ end
119
+ end
120
+ end
121
+
122
+ it 'has 7 rows' do
123
+ subject.scan(REGEXP_ROWS).size.should == 7
124
+ end
125
+
126
+ it 'has 4 headers' do
127
+ subject.scan(REGEXP_HEADERS).size.should == 4
128
+ end
129
+
130
+ it 'has 3 sortable headers' do
131
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 3
132
+ end
133
+
134
+ it 'has 6 spans' do
135
+ subject.scan(/<span>.+?<\/span>/).size.should == 6
136
+ end
137
+ end
138
+
139
+ context 'with ascending sort params' do
140
+ let(:params) { { sort: 'children', sort_dir: 'asc' } }
141
+ subject do
142
+ with_test_routing { list_table }
143
+ end
144
+
145
+ it 'has 13 sortable headers' do
146
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 13
147
+ end
148
+
149
+ it 'has 1 ascending sort headers' do
150
+ subject.scan(/<th><a .*?sort_dir=desc.*?>Children<\/a> &darr;<\/th>/)
151
+ .size.should == 1
152
+ end
153
+ end
154
+
155
+ context 'with descending sort params' do
156
+ let(:params) { { sort: 'children', sort_dir: 'desc' } }
157
+ subject do
158
+ with_test_routing { list_table }
159
+ end
160
+
161
+ it 'has 13 sortable headers' do
162
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 13
163
+ end
164
+
165
+ it 'has 1 descending sort headers' do
166
+ subject.scan(/<th><a .*?sort_dir=asc.*?>Children<\/a> &uarr;<\/th>/)
167
+ .size.should == 1
168
+ end
169
+ end
170
+
171
+ context 'with custom column sort params' do
172
+ let(:params) { { sort: 'chatty', sort_dir: 'asc' } }
173
+ subject do
174
+ with_test_routing { list_table(:name, :children, :chatty) }
175
+ end
176
+
177
+ it 'has 2 sortable headers' do
178
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 2
179
+ end
180
+
181
+ it 'has 1 ascending sort headers' do
182
+ subject.scan(/<th><a .*?sort_dir=desc.*?>Chatty<\/a> &darr;<\/th>/)
183
+ .size.should == 1
184
+ end
185
+ end
186
+ end
187
+
188
+ describe '#crud_table' do
189
+ let(:entries) { CrudTestModel.all }
190
+
191
+ context 'default' do
192
+ subject do
193
+ with_test_routing { crud_table }
194
+ end
195
+
196
+ it 'has 7 rows' do
197
+ subject.scan(REGEXP_ROWS).size.should == 7
198
+ end
199
+
200
+ it 'has 14 sort headers' do
201
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 14
202
+ end
203
+
204
+ it 'has 12 action cells' do
205
+ subject.scan(REGEXP_ACTION_CELL).size.should == 12
206
+ end
207
+ end
208
+
209
+ context 'with custom attrs' do
210
+ subject do
211
+ with_test_routing { crud_table(:name, :children, :companion_id) }
212
+ end
213
+
214
+ it 'has 3 sort headers' do
215
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 3
216
+ end
217
+ end
218
+
219
+ context 'with custom block' do
220
+ subject do
221
+ with_test_routing do
222
+ crud_table do |t|
223
+ t.attrs :name, :children, :companion_id
224
+ t.col('head') { |e| content_tag(:span, e.income.to_s) }
225
+ end
226
+ end
227
+ end
228
+
229
+ it 'has 4 headers' do
230
+ subject.scan(REGEXP_HEADERS).size.should == 6
231
+ end
232
+
233
+ it 'has 6 custom col spans' do
234
+ subject.scan(/<span>.+?<\/span>/m).size.should == 6
235
+ end
236
+
237
+ it 'has 12 action cells' do
238
+ subject.scan(REGEXP_ACTION_CELL).size.should == 12
239
+ end
240
+ end
241
+
242
+ context 'with custom attributes and block' do
243
+ subject do
244
+ with_test_routing do
245
+ crud_table(:name, :children, :companion_id) do |t|
246
+ t.col('head') { |e| content_tag(:span, e.income.to_s) }
247
+ end
248
+ end
249
+ end
250
+
251
+ it 'has 3 sort headers' do
252
+ subject.scan(REGEXP_SORT_HEADERS).size.should == 3
253
+ end
254
+
255
+ it 'has 6 custom col spans' do
256
+ subject.scan(/<span>.+?<\/span>/m).size.should == 6
257
+ end
258
+
259
+ it 'has 12 action cells' do
260
+ subject.scan(REGEXP_ACTION_CELL).size.should == 12
261
+ end
262
+ end
263
+ end
264
+
265
+ end