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,129 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+ require 'support/crud_test_model'
4
+
5
+ # Test FormHelper
6
+ class FormHelperTest < ActionView::TestCase
7
+
8
+ include UtilityHelper
9
+ include FormatHelper
10
+ include I18nHelper
11
+ include CrudTestHelper
12
+
13
+ setup :reset_db, :setup_db, :create_test_data
14
+ teardown :reset_db
15
+
16
+ test 'plain form for existing entry' do
17
+ e = crud_test_models('AAAAA')
18
+ f = with_test_routing do
19
+ capture do
20
+ plain_form(e, html: { class: 'special' }) do |form|
21
+ form.labeled_input_fields :name, :birthdate
22
+ end
23
+ end
24
+ end
25
+
26
+ assert_match /form .*?action="\/crud_test_models\/#{e.id}"
27
+ .*?class="special\ form-horizontal"
28
+ .*?method="post"/x, f
29
+ assert_match /input .*?name="_method"
30
+ .*?type="hidden"
31
+ .*?value="(patch|put)"/x, f
32
+ assert_match /input .*?name="crud_test_model\[name\]"
33
+ .*?type="text"
34
+ .*?value="AAAAA"/x, f
35
+ end
36
+
37
+ test 'standard form' do
38
+ e = crud_test_models('AAAAA')
39
+ f = with_test_routing do
40
+ capture do
41
+ standard_form(e,
42
+ :name, :children, :birthdate, :human,
43
+ cancel_url: '/somewhere',
44
+ html: { class: 'special' })
45
+ end
46
+ end
47
+
48
+ assert_match /form .*?action="\/crud_test_models\/#{e.id}"
49
+ .*?class="special\ form-horizontal"
50
+ .*?method="post"/x, f
51
+ assert_match /input .*?name="_method"
52
+ .*?type="hidden"
53
+ .*?value="(patch|put)"/x, f
54
+ assert_match /input .*?name="crud_test_model\[name\]"
55
+ .*?type="text"
56
+ .*?value="AAAAA"/x, f
57
+ assert_match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/, f
58
+ assert_match /input .*?name="crud_test_model\[children\]"
59
+ .*?type="number"
60
+ .*?value=\"9\"/x, f
61
+ assert_match /input .*?name="crud_test_model\[human\]"
62
+ .*?type="checkbox"/x, f
63
+ assert_match /button\ .*?type="submit"\>
64
+ #{t('global.button.save')}
65
+ \<\/button\>/x, f
66
+ assert_match /\<a\ .*href="\/somewhere".*\>
67
+ #{t('global.button.cancel')}
68
+ \<\/a\>/x, f
69
+ end
70
+
71
+ test 'standard form with errors' do
72
+ e = crud_test_models('AAAAA')
73
+ e.name = nil
74
+ assert !e.valid?
75
+
76
+ f = with_test_routing do
77
+ capture do
78
+ standard_form(e) do |form|
79
+ form.labeled_input_fields(:name, :birthdate)
80
+ end
81
+ end
82
+ end
83
+
84
+ assert_match /form .*?action="\/crud_test_models\/#{e.id}"
85
+ .*?method="post"/x, f
86
+ assert_match /input .*?name="_method"
87
+ .*?type="hidden"
88
+ .*?value="(patch|put)"/x, f
89
+ assert_match /div[^>]* id='error_explanation'/, f
90
+ assert_match /div\ class="control-group\ error"\>.*?
91
+ \<input .*?name="crud_test_model\[name\]"
92
+ .*?type="text"/x, f
93
+ assert_match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/, f
94
+ assert_match /option selected="selected" value="1910">1910<\/option>/, f
95
+ assert_match /option\ selected="selected"\ value="1"\>
96
+ #{t('date.month_names')[1]}
97
+ \<\/option\>/x, f
98
+ assert_match /option selected="selected" value="1">1<\/option>/, f
99
+ end
100
+
101
+ test 'crud form' do
102
+ f = with_test_routing do
103
+ capture { crud_form }
104
+ end
105
+
106
+ assert_match /form .*?action="\/crud_test_models\/#{entry.id}"/, f
107
+ assert_match /input .*?name="crud_test_model\[name\]"
108
+ .*?type="text"/x, f
109
+ assert_match /input .*?name="crud_test_model\[whatever\]"
110
+ .*?type="text"/x, f
111
+ assert_match /input .*?name="crud_test_model\[children\]"
112
+ .*?type="number"/x, f
113
+ assert_match /input .*?name="crud_test_model\[rating\]"
114
+ .*?type="number"/x, f
115
+ assert_match /input .*?name="crud_test_model\[income\]"
116
+ .*?type="number"/x, f
117
+ assert_match /select .*?name="crud_test_model\[birthdate\(1i\)\]"/, f
118
+ assert_match /input .*?name="crud_test_model\[human\]"
119
+ .*?type="checkbox"/x, f
120
+ assert_match /select .*?name="crud_test_model\[companion_id\]"/, f
121
+ assert_match /textarea .*?name="crud_test_model\[remarks\]"/, f
122
+ assert_match /a .*href="\/crud_test_models\/#{entry.id}\?returning=true"
123
+ .*>#{t('global.button.cancel')}<\/a>/x, f
124
+ end
125
+
126
+ def entry
127
+ @entry ||= CrudTestModel.first
128
+ end
129
+ end
@@ -0,0 +1,163 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+ require 'support/crud_test_model'
4
+
5
+ # Test FormatHelper
6
+ class FormatHelperTest < ActionView::TestCase
7
+
8
+ include UtilityHelper
9
+ include I18nHelper
10
+ include CrudTestHelper
11
+
12
+ setup :reset_db, :setup_db, :create_test_data
13
+ teardown :reset_db
14
+
15
+ def format_size(obj)
16
+ "#{f(obj.size)} items"
17
+ end
18
+
19
+ def format_string_size(obj)
20
+ "#{f(obj.size)} chars"
21
+ end
22
+
23
+ test 'labeled text as block' do
24
+ result = labeled('label') { 'value' }
25
+
26
+ assert result.html_safe?
27
+ assert_dom_equal '<dt>label</dt> ' +
28
+ "<dd class='value'>value</dd>",
29
+ result.squish
30
+ end
31
+
32
+ test 'labeled text empty' do
33
+ result = labeled('label', '')
34
+
35
+ assert result.html_safe?
36
+ assert_dom_equal '<dt>label</dt> ' +
37
+ "<dd class='value'>#{EMPTY_STRING}</dd>",
38
+ result.squish
39
+ end
40
+
41
+ test 'labeled text as content' do
42
+ result = labeled('label', 'value <unsafe>')
43
+
44
+ assert result.html_safe?
45
+ assert_dom_equal '<dt>label</dt> ' +
46
+ "<dd class='value'>value &lt;unsafe&gt;</dd>",
47
+ result.squish
48
+ end
49
+
50
+ test 'labeled attr' do
51
+ result = labeled_attr('foo', :size)
52
+ assert result.html_safe?
53
+ assert_dom_equal '<dt>Size</dt> ' +
54
+ "<dd class='value'>3 chars</dd>",
55
+ result.squish
56
+ end
57
+
58
+ test 'format nil' do
59
+ assert EMPTY_STRING.html_safe?
60
+ assert_equal EMPTY_STRING, f(nil)
61
+ end
62
+
63
+ test 'format Strings' do
64
+ assert_equal 'blah blah', f('blah blah')
65
+ assert_equal '<injection>', f('<injection>')
66
+ assert !f('<injection>').html_safe?
67
+ end
68
+
69
+ unless ENV['NON_LOCALIZED'] # localization dependent tests
70
+ test 'format Floats' do
71
+ assert_equal '1.000', f(1.0)
72
+ assert_equal '1.200', f(1.2)
73
+ assert_equal '3.142', f(3.14159)
74
+ end
75
+
76
+ test 'format Booleans' do
77
+ assert_equal 'yes', f(true)
78
+ assert_equal 'no', f(false)
79
+ end
80
+
81
+ test 'format attr with fallthrough to f' do
82
+ assert_equal '12.234', format_attr('12.23424', :to_f)
83
+ end
84
+ end
85
+
86
+ test 'format attr with custom format_string_size method' do
87
+ assert_equal '4 chars', format_attr('abcd', :size)
88
+ end
89
+
90
+ test 'format attr with custom format_size method' do
91
+ assert_equal '2 items', format_attr([1, 2], :size)
92
+ end
93
+
94
+ test 'format integer column' do
95
+ m = crud_test_models(:AAAAA)
96
+ assert_equal '9', format_type(m, :children)
97
+
98
+ m.children = 10000
99
+ assert_equal '10000', format_type(m, :children)
100
+ end
101
+
102
+ unless ENV['NON_LOCALIZED'] # localization dependent tests
103
+ test 'format float column' do
104
+ m = crud_test_models(:AAAAA)
105
+ assert_equal '1.100', format_type(m, :rating)
106
+
107
+ m.rating = 3.145001 # you never know with these floats..
108
+ assert_equal '3.145', format_type(m, :rating)
109
+ end
110
+
111
+ test 'format decimal column' do
112
+ m = crud_test_models(:AAAAA)
113
+ assert_equal '10,000,000.1111', format_type(m, :income)
114
+ end
115
+
116
+ test 'format date column' do
117
+ m = crud_test_models(:AAAAA)
118
+ assert_equal '1910-01-01', format_type(m, :birthdate)
119
+ end
120
+
121
+ test 'format datetime column' do
122
+ m = crud_test_models(:AAAAA)
123
+ assert_equal '2010-01-01 11:21', format_type(m, :last_seen)
124
+ end
125
+ end
126
+
127
+ test 'format time column' do
128
+ m = crud_test_models(:AAAAA)
129
+ assert_equal '01:01', format_type(m, :gets_up_at)
130
+ end
131
+
132
+ test 'format text column' do
133
+ m = crud_test_models(:AAAAA)
134
+ assert_equal "<p>AAAAA BBBBB CCCCC\n<br />AAAAA BBBBB CCCCC\n</p>",
135
+ format_type(m, :remarks)
136
+ assert format_type(m, :remarks).html_safe?
137
+ end
138
+
139
+ test 'format belongs to column without content' do
140
+ m = crud_test_models(:AAAAA)
141
+ assert_equal t('global.associations.no_entry'),
142
+ format_attr(m, :companion)
143
+ end
144
+
145
+ test 'format belongs to column with content' do
146
+ m = crud_test_models(:BBBBB)
147
+ assert_equal 'AAAAA', format_attr(m, :companion)
148
+ end
149
+
150
+ test 'format has_many column with content' do
151
+ m = crud_test_models(:CCCCC)
152
+ assert_equal '<ul><li>AAAAA</li><li>BBBBB</li></ul>',
153
+ format_attr(m, :others)
154
+ end
155
+
156
+ test 'captionize' do
157
+ assert_equal 'Camel Case', captionize(:camel_case)
158
+ assert_equal 'All Upper Case', captionize('all upper case')
159
+ assert_equal 'With Object', captionize('With object', Object.new)
160
+ assert !captionize('bad <title>').html_safe?
161
+ end
162
+
163
+ end
@@ -0,0 +1,79 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+ require 'support/crud_test_model'
4
+
5
+ # Test I18nHelper
6
+ class I18nHelperTest < ActionView::TestCase
7
+
8
+ include CrudTestHelper
9
+
10
+ test 'translate inheritable lookup' do
11
+ # current controller is :crud_test_models, action is :index
12
+ @controller = CrudTestModelsController.new
13
+
14
+ I18n.backend.store_translations(
15
+ I18n.locale,
16
+ global: { test_key: 'global' })
17
+ assert_equal 'global', ti(:test_key)
18
+
19
+ I18n.backend.store_translations(
20
+ I18n.locale,
21
+ list: { global: { test_key: 'list global' } })
22
+ assert_equal 'list global', ti(:test_key)
23
+
24
+ I18n.backend.store_translations(
25
+ I18n.locale,
26
+ list: { index: { test_key: 'list index' } })
27
+ assert_equal 'list index', ti(:test_key)
28
+
29
+ I18n.backend.store_translations(
30
+ I18n.locale,
31
+ crud: { global: { test_key: 'crud global' } })
32
+ assert_equal 'crud global', ti(:test_key)
33
+
34
+ I18n.backend.store_translations(
35
+ I18n.locale,
36
+ crud: { index: { test_key: 'crud index' } })
37
+ assert_equal 'crud index', ti(:test_key)
38
+
39
+ I18n.backend.store_translations(
40
+ I18n.locale,
41
+ crud_test_models: { global: { test_key: 'test global' } })
42
+ assert_equal 'test global', ti(:test_key)
43
+
44
+ I18n.backend.store_translations(
45
+ I18n.locale,
46
+ crud_test_models: { index: { test_key: 'test index' } })
47
+ assert_equal 'test index', ti(:test_key)
48
+ end
49
+
50
+ test 'translate association lookup' do
51
+ assoc = CrudTestModel.reflect_on_association(:companion)
52
+
53
+ I18n.backend.store_translations(
54
+ I18n.locale,
55
+ global: { associations: { test_key: 'global' } })
56
+ assert_equal 'global', ta(:test_key, assoc)
57
+
58
+ I18n.backend.store_translations(
59
+ I18n.locale,
60
+ activerecord: {
61
+ associations: {
62
+ crud_test_model: {
63
+ test_key: 'model' } } })
64
+ assert_equal 'model', ta(:test_key, assoc)
65
+
66
+ I18n.backend.store_translations(
67
+ I18n.locale,
68
+ activerecord: {
69
+ associations: {
70
+ models: {
71
+ crud_test_model: {
72
+ companion: {
73
+ test_key: 'companion' } } } } })
74
+ assert_equal 'companion', ta(:test_key, assoc)
75
+
76
+ assert_equal 'global', ta(:test_key)
77
+ end
78
+
79
+ end
@@ -0,0 +1,217 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+ require 'support/custom_assertions'
4
+ require 'support/crud_test_model'
5
+
6
+ # Test TableHelper
7
+ class TableHelperTest < ActionView::TestCase
8
+
9
+ include UtilityHelper
10
+ include FormatHelper
11
+ include I18nHelper
12
+ include CustomAssertions
13
+ include CrudTestHelper
14
+
15
+ setup :reset_db, :setup_db, :create_test_data
16
+ teardown :reset_db
17
+
18
+ attr_reader :entries
19
+
20
+ def format_size(obj)
21
+ "#{f(obj.size)} items"
22
+ end
23
+
24
+ def format_string_size(obj)
25
+ "#{f(obj.size)} chars"
26
+ end
27
+
28
+ test 'empty table should render message' do
29
+ result = plain_table_or_message([]) { }
30
+ assert result.html_safe?
31
+ assert_match /\<div class=["']table["']\>.*\<\/div\>/, result
32
+ end
33
+
34
+ test 'non empty table should render table' do
35
+ result = plain_table_or_message(%w(foo bar)) do |t|
36
+ t.attrs :size, :upcase
37
+ end
38
+ assert result.html_safe?
39
+ assert_match(/^\<table.*\<\/table\>$/, result)
40
+ end
41
+
42
+ test 'table with attrs' do
43
+ expected = Crud::TableBuilder.table(%w(foo bar),
44
+ self,
45
+ class: 'table') do |t|
46
+ t.attrs :size, :upcase
47
+ end
48
+ actual = plain_table(%w(foo bar), :size, :upcase)
49
+ assert actual.html_safe?
50
+ assert_equal expected, actual
51
+ end
52
+
53
+ test 'standard list table' do
54
+ @entries = CrudTestModel.all
55
+
56
+ table = with_test_routing do
57
+ list_table
58
+ end
59
+
60
+ assert_count 7, REGEXP_ROWS, table
61
+ assert_count 14, REGEXP_SORT_HEADERS, table
62
+ end
63
+
64
+ test 'custom list table with attributes' do
65
+ @entries = CrudTestModel.all
66
+
67
+ table = with_test_routing do
68
+ list_table :name, :children, :companion_id
69
+ end
70
+
71
+ assert_count 7, REGEXP_ROWS, table
72
+ assert_count 3, REGEXP_SORT_HEADERS, table
73
+ end
74
+
75
+ test 'custom list table with block' do
76
+ @entries = CrudTestModel.all
77
+
78
+ table = with_test_routing do
79
+ list_table do |t|
80
+ t.attrs :name, :children, :companion_id
81
+ t.col('head') { |e| content_tag :span, e.income.to_s }
82
+ end
83
+ end
84
+
85
+ assert_count 7, REGEXP_ROWS, table
86
+ assert_count 4, REGEXP_HEADERS, table
87
+ assert_count 0, REGEXP_SORT_HEADERS, table
88
+ assert_count 6, /<span>.+?<\/span>/, table
89
+ end
90
+
91
+ test 'custom list table with attributes and block' do
92
+ @entries = CrudTestModel.all
93
+
94
+ table = with_test_routing do
95
+ list_table :name, :children, :companion_id do |t|
96
+ t.col('head') { |e| content_tag :span, e.income.to_s }
97
+ end
98
+ end
99
+
100
+ assert_count 7, REGEXP_ROWS, table
101
+ assert_count 3, REGEXP_SORT_HEADERS, table
102
+ assert_count 4, REGEXP_HEADERS, table
103
+ assert_count 6, /<span>.+?<\/span>/, table
104
+ end
105
+
106
+ test 'standard list table with ascending sort params' do
107
+ def params
108
+ { sort: 'children', sort_dir: 'asc' }
109
+ end
110
+
111
+ @entries = CrudTestModel.all
112
+
113
+ table = with_test_routing do
114
+ list_table
115
+ end
116
+
117
+ sort_header_desc = %r{<th><a .*?sort_dir=desc.*?>Children</a> &darr;</th>}
118
+ assert_count 7, REGEXP_ROWS, table
119
+ assert_count 13, REGEXP_SORT_HEADERS, table
120
+ assert_count 1, sort_header_desc, table
121
+ end
122
+
123
+ test 'standard list table with descending sort params' do
124
+ def params
125
+ { sort: 'children', sort_dir: 'desc' }
126
+ end
127
+
128
+ @entries = CrudTestModel.all
129
+
130
+ table = with_test_routing do
131
+ list_table
132
+ end
133
+
134
+ sort_header_asc = %r{<th><a .*?sort_dir=asc.*?>Children</a> &uarr;</th>}
135
+ assert_count 7, REGEXP_ROWS, table
136
+ assert_count 13, REGEXP_SORT_HEADERS, table
137
+ assert_count 1, sort_header_asc, table
138
+ end
139
+
140
+ test 'list table with custom column sort params' do
141
+ def params
142
+ { sort: 'chatty', sort_dir: 'asc' }
143
+ end
144
+
145
+ @entries = CrudTestModel.all
146
+
147
+ table = with_test_routing do
148
+ list_table :name, :children, :chatty
149
+ end
150
+
151
+ sort_header_desc = %r{<th><a .*?sort_dir=desc.*?>Chatty</a> &darr;</th>}
152
+ assert_count 7, REGEXP_ROWS, table
153
+ assert_count 2, REGEXP_SORT_HEADERS, table
154
+ assert_count 1, sort_header_desc, table
155
+ end
156
+
157
+ test 'standard crud table' do
158
+ @entries = CrudTestModel.all
159
+
160
+ table = with_test_routing do
161
+ crud_table
162
+ end
163
+
164
+ assert_count 7, REGEXP_ROWS, table
165
+ assert_count 14, REGEXP_SORT_HEADERS, table
166
+ assert_count 12, REGEXP_ACTION_CELL, table # edit, delete links
167
+ end
168
+
169
+ test 'custom crud table with attributes' do
170
+ @entries = CrudTestModel.all
171
+
172
+ table = with_test_routing do
173
+ crud_table :name, :children, :companion_id
174
+ end
175
+
176
+ assert_count 7, REGEXP_ROWS, table
177
+ assert_count 3, REGEXP_SORT_HEADERS, table
178
+ assert_count 12, REGEXP_ACTION_CELL, table # edit, delete links
179
+ end
180
+
181
+ test 'custom crud table with block' do
182
+ @entries = CrudTestModel.all
183
+
184
+ table = with_test_routing do
185
+ crud_table do |t|
186
+ t.attrs :name, :children, :companion_id
187
+ t.col('head') { |e| content_tag :span, e.income.to_s }
188
+ end
189
+ end
190
+
191
+ assert_count 7, REGEXP_ROWS, table
192
+ assert_count 6, REGEXP_HEADERS, table
193
+ assert_count 6, /<span>.+?<\/span>/m, table
194
+ assert_count 12, REGEXP_ACTION_CELL, table # edit, delete links
195
+ end
196
+
197
+ test 'custom crud table with attributes and block' do
198
+ @entries = CrudTestModel.all
199
+
200
+ table = with_test_routing do
201
+ crud_table :name, :children, :companion_id do |t|
202
+ t.col('head') { |e| content_tag :span, e.income.to_s }
203
+ end
204
+ end
205
+
206
+ assert_count 7, REGEXP_ROWS, table
207
+ assert_count 3, REGEXP_SORT_HEADERS, table
208
+ assert_count 6, REGEXP_HEADERS, table
209
+ assert_count 6, /<span>.+?<\/span>/m, table
210
+ assert_count 12, REGEXP_ACTION_CELL, table # edit, delete links
211
+ end
212
+
213
+ def entry
214
+ @entry ||= CrudTestModel.first
215
+ end
216
+
217
+ end