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,63 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+ require 'support/crud_test_model'
4
+
5
+ # Test UtilityHelper
6
+ class UtilityHelperTest < ActionView::TestCase
7
+
8
+ include CrudTestHelper
9
+
10
+ test 'content_tag_nested escapes safe correctly' do
11
+ html = content_tag_nested(:div, %w(a b)) { |e| content_tag(:span, e) }
12
+ assert_equal '<div><span>a</span><span>b</span></div>', html
13
+ end
14
+
15
+ test 'content_tag_nested escapes unsafe correctly' do
16
+ html = content_tag_nested(:div, %w(a b)) { |e| "<#{e}>" }
17
+ assert_equal '<div>&lt;a&gt;&lt;b&gt;</div>', html
18
+ end
19
+
20
+ test 'content_tag_nested without block' do
21
+ html = content_tag_nested(:div, %w(a b))
22
+ assert_equal '<div>ab</div>', html
23
+ end
24
+
25
+ test 'safe_join without block' do
26
+ html = safe_join(['<a>', '<b>'.html_safe])
27
+ assert_equal '&lt;a&gt;<b>', html
28
+ end
29
+
30
+ test 'safe_join with block' do
31
+ html = safe_join(%w(a b)) { |e| content_tag(:span, e) }
32
+ assert_equal '<span>a</span><span>b</span>', html
33
+ end
34
+
35
+ test 'default attributes do not include id and password' do
36
+ assert_equal [:name, :email, :whatever, :children, :companion_id, :rating,
37
+ :income, :birthdate, :gets_up_at, :last_seen, :human,
38
+ :remarks, :created_at, :updated_at],
39
+ default_crud_attrs
40
+ end
41
+
42
+ test 'column types' do
43
+ reset_db
44
+ setup_db
45
+ create_test_data
46
+
47
+ m = crud_test_models(:AAAAA)
48
+ assert_equal :string, column_type(m, :name)
49
+ assert_equal :integer, column_type(m, :children)
50
+ assert_equal :integer, column_type(m, :companion_id)
51
+ assert_equal nil, column_type(m, :companion)
52
+ assert_equal :float, column_type(m, :rating)
53
+ assert_equal :decimal, column_type(m, :income)
54
+ assert_equal :date, column_type(m, :birthdate)
55
+ assert_equal :time, column_type(m, :gets_up_at)
56
+ assert_equal :datetime, column_type(m, :last_seen)
57
+ assert_equal :boolean, column_type(m, :human)
58
+ assert_equal :text, column_type(m, :remarks)
59
+
60
+ reset_db
61
+ end
62
+
63
+ end
@@ -1,80 +1,82 @@
1
- # A module to include into your functional tests for your crud controller subclasses.
2
- # Simply implement the two methods :test_entry and :test_entry_attrs to test the basic
3
- # crud functionality. Override the test methods if you changed the behaviour in your subclass
4
- # controller.
1
+ # encoding: UTF-8
2
+
3
+ # A module to include into your functional tests for your crud controller
4
+ # subclasses. Simply implement the two methods #test_entry and
5
+ # #test_entry_attrs to test the basic crud functionality. Override the test
6
+ # methods if you changed the behaviour in your subclass controller.
5
7
  module CrudControllerTestHelper
6
8
 
7
- def test_index
9
+ def test_index # :nodoc:
8
10
  get :index, test_params
9
11
  assert_response :success
10
12
  assert_template 'index'
11
- assert_present entries
13
+ assert entries.present?
12
14
  end
13
15
 
14
- def test_index_json
15
- get :index, test_params(:format => 'json')
16
+ def test_index_json # :nodoc:
17
+ get :index, test_params(format: 'json')
16
18
  assert_response :success
17
- assert_present entries
18
- assert @response.body.starts_with?("[{"), @response.body
19
+ assert entries.present?
20
+ assert @response.body.starts_with?('[{'), @response.body
19
21
  end
20
22
 
21
- def test_index_search
23
+ def test_index_search # :nodoc:
22
24
  field = @controller.search_columns.first
23
25
  val = field && test_entry[field].to_s
24
26
  return if val.blank? # does not support search or no value in this field
25
27
 
26
- get :index, test_params(:q => val[0..((val.size + 1)/ 2)])
28
+ get :index, test_params(q: val[0..((val.size + 1) / 2)])
27
29
  assert_response :success
28
- assert_present entries
30
+ assert entries.present?
29
31
  assert entries.include?(test_entry)
30
32
  end
31
33
 
32
- def test_index_sort_asc
34
+ def test_index_sort_asc # :nodoc:
33
35
  col = model_class.column_names.first
34
- get :index, test_params(:sort => col, :sort_dir => 'asc')
36
+ get :index, test_params(sort: col, sort_dir: 'asc')
35
37
  assert_response :success
36
- assert_present entries
37
- sorted = entries.sort_by &(col.to_sym)
38
- assert_equal sorted, entries
38
+ assert entries.present?
39
+ sorted = entries.sort_by(&(col.to_sym))
40
+ assert_equal sorted, entries.to_a
39
41
  end
40
42
 
41
- def test_index_sort_desc
43
+ def test_index_sort_desc # :nodoc:
42
44
  col = model_class.column_names.first
43
- get :index, test_params(:sort => col, :sort_dir => 'desc')
45
+ get :index, test_params(sort: col, sort_dir: 'desc')
44
46
  assert_response :success
45
- assert_present entries
46
- sorted = entries.sort_by &(col.to_sym)
47
- assert_equal sorted.reverse, entries
47
+ assert entries.present?
48
+ sorted = entries.to_a.sort_by(&(col.to_sym))
49
+ assert_equal sorted.reverse, entries.to_a
48
50
  end
49
51
 
50
- def test_show
51
- get :show, test_params(:id => test_entry.id)
52
+ def test_show # :nodoc:
53
+ get :show, test_params(id: test_entry.id)
52
54
  assert_response :success
53
55
  assert_template 'show'
54
56
  assert_equal test_entry, entry
55
57
  end
56
58
 
57
- def test_show_json
58
- get :show, test_params(:id => test_entry.id, :format => 'json')
59
+ def test_show_json # :nodoc:
60
+ get :show, test_params(id: test_entry.id, format: 'json')
59
61
  assert_response :success
60
62
  assert_equal test_entry, entry
61
- assert @response.body.starts_with?("{")
63
+ assert @response.body.starts_with?('{')
62
64
  end
63
65
 
64
- def test_show_with_non_existing_id_raises_RecordNotFound
66
+ def test_show_with_non_existing_id_raises_record_not_found # :nodoc:
65
67
  assert_raise(ActiveRecord::RecordNotFound) do
66
- get :show, test_params(:id => 9999)
68
+ get :show, test_params(id: 9999)
67
69
  end
68
70
  end
69
71
 
70
- def test_new
72
+ def test_new # :nodoc:
71
73
  get :new, test_params
72
74
  assert_response :success
73
75
  assert_template 'new'
74
76
  assert entry.new_record?
75
77
  end
76
78
 
77
- def test_create
79
+ def test_create # :nodoc:
78
80
  assert_difference("#{model_class.name}.count") do
79
81
  post :create, test_params(model_identifier => new_entry_attrs)
80
82
  end
@@ -83,66 +85,75 @@ module CrudControllerTestHelper
83
85
  assert_attrs_equal(new_entry_attrs)
84
86
  end
85
87
 
86
- def test_create_json
88
+ def test_create_json # :nodoc:
87
89
  assert_difference("#{model_class.name}.count") do
88
- post :create, test_params(model_identifier => new_entry_attrs, :format => 'json')
90
+ post :create, test_params(model_identifier => new_entry_attrs,
91
+ format: 'json')
89
92
  end
90
93
  assert_response :success
91
- assert @response.body.starts_with?("{")
94
+ assert @response.body.starts_with?('{')
92
95
  end
93
96
 
94
- def test_edit
95
- get :edit, test_params(:id => test_entry.id)
97
+ def test_edit # :nodoc:
98
+ get :edit, test_params(id: test_entry.id)
96
99
  assert_response :success
97
100
  assert_template 'edit'
98
101
  assert_equal test_entry, entry
99
102
  end
100
103
 
101
- def test_update
104
+ def test_update # :nodoc:
102
105
  assert_no_difference("#{model_class.name}.count") do
103
- put :update, test_params(:id => test_entry.id, model_identifier => edit_entry_attrs)
106
+ put :update, test_params(id: test_entry.id,
107
+ model_identifier => edit_entry_attrs)
104
108
  end
105
109
  assert_attrs_equal(edit_entry_attrs)
106
110
  assert_redirected_to_show entry
107
111
  end
108
112
 
109
- def test_update_json
113
+ def test_update_json # :nodoc:
110
114
  assert_no_difference("#{model_class.name}.count") do
111
- put :update, test_params(:id => test_entry.id, model_identifier => edit_entry_attrs, :format => 'json')
115
+ put :update, test_params(id: test_entry.id,
116
+ model_identifier => edit_entry_attrs,
117
+ format: 'json')
112
118
  end
113
119
  assert_response :success
114
- assert_equal "", @response.body.strip
120
+ assert_equal '', @response.body.strip
115
121
  end
116
122
 
117
- def test_destroy
123
+ def test_destroy # :nodoc:
118
124
  assert_difference("#{model_class.name}.count", -1) do
119
- delete :destroy, test_params(:id => test_entry.id)
125
+ delete :destroy, test_params(id: test_entry.id)
120
126
  end
121
127
  assert_redirected_to_index
122
128
  end
123
129
 
124
- def test_destroy_json
130
+ def test_destroy_json # :nodoc:
125
131
  assert_difference("#{model_class.name}.count", -1) do
126
- delete :destroy, test_params(:id => test_entry.id, :format => 'json')
132
+ delete :destroy, test_params(id: test_entry.id,
133
+ format: 'json')
127
134
  end
128
135
  assert_response :success
129
- assert_equal "", @response.body.strip
136
+ assert_equal '', @response.body.strip
130
137
  end
131
138
 
132
139
  private
133
140
 
134
- def assert_redirected_to_index
135
- assert_redirected_to test_params(:action => 'index', :returning => true)
141
+ def assert_redirected_to_index # :nodoc:
142
+ assert_redirected_to test_params(action: 'index',
143
+ returning: true)
136
144
  end
137
145
 
138
- def assert_redirected_to_show(entry)
139
- assert_redirected_to test_params(:action => 'show', :id => entry.id)
146
+ def assert_redirected_to_show(entry) # :nodoc:
147
+ assert_redirected_to test_params(action: 'show',
148
+ id: entry.id)
140
149
  end
141
150
 
142
- def assert_attrs_equal(attrs)
151
+ def assert_attrs_equal(attrs) # :nodoc:
143
152
  attrs.each do |key, value|
144
153
  actual = entry.send(key)
145
- assert_equal value, actual, "#{key} is expected to be <#{value.inspect}>, got <#{actual.inspect}>"
154
+ assert_equal value, actual,
155
+ "#{key} is expected to be <#{value.inspect}>, " +
156
+ "got <#{actual.inspect}>"
146
157
  end
147
158
  end
148
159
 
@@ -150,7 +161,7 @@ module CrudControllerTestHelper
150
161
  def model_class
151
162
  @controller.model_class
152
163
  end
153
-
164
+
154
165
  # The param key for model attributes.
155
166
  def model_identifier
156
167
  @controller.model_identifier
@@ -168,14 +179,14 @@ module CrudControllerTestHelper
168
179
 
169
180
  # Test object used in several tests.
170
181
  def test_entry
171
- raise "Implement this method in your test class"
182
+ raise 'Implement this method in your test class'
172
183
  end
173
184
 
174
185
  # Attribute hash used in several tests.
175
186
  def test_entry_attrs
176
- raise "Implement this method in your test class"
187
+ raise 'Implement this method in your test class'
177
188
  end
178
-
189
+
179
190
  # Attribute hash used in edit/update tests.
180
191
  def edit_entry_attrs
181
192
  test_entry_attrs
@@ -191,10 +202,10 @@ module CrudControllerTestHelper
191
202
  nesting_params.merge(params)
192
203
  end
193
204
 
205
+ # For nested controllers, collect hash with parent ids.
194
206
  def nesting_params
195
207
  params = {}
196
- # for nested controllers, add parent ids to each request
197
- Array(@controller.nesting).reverse.inject(test_entry) do |parent, p|
208
+ Array(@controller.nesting).reverse.reduce(test_entry) do |parent, p|
198
209
  if p.is_a?(Class) && p < ActiveRecord::Base
199
210
  assoc = p.name.underscore
200
211
  params["#{assoc}_id"] = parent.send(:"#{assoc}_id")
@@ -1,18 +1,19 @@
1
+ # encoding: UTF-8
2
+
1
3
  # A dummy model used for general testing.
2
4
  class CrudTestModel < ActiveRecord::Base #:nodoc:
3
5
 
4
- attr_accessible :name, :whatever, :children, :rating, :income, :birthdate, :gets_up_at, :last_seen, :human, :remarks
5
-
6
- belongs_to :companion, :class_name => 'CrudTestModel'
7
- has_and_belongs_to_many :others, :class_name => 'OtherCrudTestModel'
8
- has_many :mores, :class_name => 'OtherCrudTestModel', :foreign_key => :more_id
6
+ belongs_to :companion, class_name: 'CrudTestModel'
7
+ has_and_belongs_to_many :others, class_name: 'OtherCrudTestModel'
8
+ has_many :mores, class_name: 'OtherCrudTestModel',
9
+ foreign_key: :more_id
9
10
 
10
11
  before_destroy :protect_if_companion
11
12
 
12
- validates :name, :presence => true
13
- validates :rating, :inclusion => { :in => 1..10 }
13
+ validates :name, presence: true
14
+ validates :rating, inclusion: { in: 1..10 }
14
15
 
15
- default_scope order('name')
16
+ attr_protected nil if Rails.version < '4.0'
16
17
 
17
18
  def to_s
18
19
  name
@@ -33,12 +34,13 @@ class CrudTestModel < ActiveRecord::Base #:nodoc:
33
34
 
34
35
  end
35
36
 
37
+ # Second dummy model to test associations.
36
38
  class OtherCrudTestModel < ActiveRecord::Base #:nodoc:
37
39
 
38
- attr_protected nil
40
+ has_and_belongs_to_many :others, class_name: 'CrudTestModel'
41
+ belongs_to :more, foreign_key: :more_id, class_name: 'CrudTestModel'
39
42
 
40
- has_and_belongs_to_many :others, :class_name => 'CrudTestModel'
41
- belongs_to :more, :foreign_key => :more_id, :class_name => 'CrudTestModel'
43
+ attr_protected nil if Rails.version < '4.0'
42
44
 
43
45
  def to_s
44
46
  name
@@ -50,7 +52,13 @@ class CrudTestModelsController < CrudController #:nodoc:
50
52
  HANDLE_PREFIX = 'handle_'
51
53
 
52
54
  self.search_columns = [:name, :whatever, :remarks]
53
- self.sort_mappings = {:chatty => 'length(remarks)'}
55
+ self.sort_mappings = { chatty: 'length(remarks)' }
56
+ self.default_sort = 'name'
57
+ <% if Rails.version >= '4.0' -%>
58
+ self.permitted_attrs = [:name, :email, :password, :whatever, :children,
59
+ :companion_id, :rating, :income, :birthdate,
60
+ :gets_up_at, :last_seen, :human, :remarks]
61
+ <% end -%>
54
62
 
55
63
  before_create :possibly_redirect
56
64
  before_create :handle_name
@@ -70,13 +78,13 @@ class CrudTestModelsController < CrudController #:nodoc:
70
78
 
71
79
  def index
72
80
  super do |format|
73
- format.js { render :text => 'index js'}
81
+ format.js { render text: 'index js'}
74
82
  end
75
83
  end
76
84
 
77
85
  def show
78
86
  super do |format|
79
- format.html { render :text => 'custom html' } if entry.name == 'BBBBB'
87
+ format.html { render text: 'custom html' } if entry.name == 'BBBBB'
80
88
  end
81
89
  end
82
90
 
@@ -91,7 +99,9 @@ class CrudTestModelsController < CrudController #:nodoc:
91
99
  def list_entries
92
100
  entries = super
93
101
  if params[:filter]
94
- entries = entries.where(['rating < ?', 3]).except(:order).order('children DESC')
102
+ entries = entries.where(['rating < ?', 3])
103
+ .except(:order)
104
+ .order('children DESC')
95
105
  end
96
106
  entries
97
107
  end
@@ -100,29 +110,30 @@ class CrudTestModelsController < CrudController #:nodoc:
100
110
 
101
111
  def build_entry
102
112
  entry = super
103
- entry.companion_id = model_params.delete(:companion_id) if model_params
113
+ if params[model_identifier]
114
+ entry.companion_id = model_params.delete(:companion_id)
115
+ end
104
116
  entry
105
117
  end
106
118
 
107
119
  # custom callback
108
120
  def handle_name
109
121
  if entry.name == 'illegal'
110
- flash[:alert] = "illegal name"
122
+ flash[:alert] = 'illegal name'
111
123
  false
112
124
  end
113
125
  end
114
126
 
115
127
  # callback to redirect if @should_redirect is set
116
128
  def possibly_redirect
117
- redirect_to :action => 'index' if should_redirect && !performed?
129
+ redirect_to action: 'index' if should_redirect && !performed?
118
130
  !should_redirect
119
131
  end
120
132
 
121
133
  def set_companions
122
- @companions = CrudTestModel.all :conditions => {:human => true}
134
+ @companions = CrudTestModel.where(human: true)
123
135
  end
124
136
 
125
-
126
137
  # create callback methods that record the before/after callbacks
127
138
  [:create, :update, :save, :destroy].each do |a|
128
139
  callback = "before_#{a.to_s}"
@@ -139,7 +150,9 @@ class CrudTestModelsController < CrudController #:nodoc:
139
150
 
140
151
  # handle the called callbacks
141
152
  def method_missing(sym, *args)
142
- called_callback(sym.to_s[HANDLE_PREFIX.size..-1].to_sym) if sym.to_s.starts_with?(HANDLE_PREFIX)
153
+ if sym.to_s.starts_with?(HANDLE_PREFIX)
154
+ called_callback(sym.to_s[HANDLE_PREFIX.size..-1].to_sym)
155
+ end
143
156
  end
144
157
 
145
158
  # records a callback
@@ -149,14 +162,13 @@ class CrudTestModelsController < CrudController #:nodoc:
149
162
  end
150
163
 
151
164
  end
152
-
165
+
153
166
  #:nodoc:
154
167
  REGEXP_ROWS = /<tr.+?<\/tr>/m #:nodoc:
155
168
  REGEXP_HEADERS = /<th.+?<\/th>/m #:nodoc:
156
- REGEXP_SORT_HEADERS = /<th><a .*?sort_dir=asc.*?>.*?<\/a><\/th>/m #:nodoc:
157
- REGEXP_ACTION_CELL = /<td class=\"action\"><a href.+?<\/a><\/td>/m #:nodoc:
158
-
159
-
169
+ REGEXP_SORT_HEADERS = /<th.*?><a .*?sort_dir=asc.*?>.*?<\/a><\/th>/m #:nodoc:
170
+ REGEXP_ACTION_CELL = /<td class=\"action\"><a .*?href.+?<\/a><\/td>/m #:nodoc:
171
+
160
172
  # A simple test helper to prepare the test database with a CrudTestModel model.
161
173
  # This helper is used to test the CrudController and various helpers
162
174
  # without the need for an application based model.
@@ -199,50 +211,66 @@ module CrudTestHelper
199
211
  def setup_db
200
212
  without_transaction do
201
213
  silence_stream(STDOUT) do
202
- ActiveRecord::Base.connection.create_table :crud_test_models, :force => true do |t|
203
- t.string :name, :null => false, :limit => 50
204
- t.string :password
205
- t.string :whatever
206
- t.integer :children
207
- t.integer :companion_id
208
- t.float :rating
209
- t.decimal :income, :precision => 14, :scale => 2
210
- t.date :birthdate
211
- t.time :gets_up_at
212
- t.datetime :last_seen
213
- t.boolean :human, :default => true
214
- t.text :remarks
215
-
216
- t.timestamps
217
- end
218
- end
219
- ActiveRecord::Base.connection.create_table :other_crud_test_models, :force => true do |t|
220
- t.string :name, :null => false, :limit => 50
221
- t.integer :more_id
222
- end
223
- ActiveRecord::Base.connection.create_table :crud_test_models_other_crud_test_models, :force => true do |t|
224
- t.belongs_to :crud_test_model
225
- t.belongs_to :other_crud_test_model
214
+ c = ActiveRecord::Base.connection
215
+
216
+ create_crud_test_models(c)
217
+ create_other_crud_test_models(c)
218
+ create_crud_test_models_other_crud_test_models(c)
226
219
  end
227
220
 
228
221
  CrudTestModel.reset_column_information
229
222
  end
230
223
  end
231
224
 
225
+ def create_crud_test_models(c)
226
+ c.create_table :crud_test_models, force: true do |t|
227
+ t.string :name, null: false, limit: 50
228
+ t.string :email
229
+ t.string :password
230
+ t.string :whatever
231
+ t.integer :children
232
+ t.integer :companion_id
233
+ t.float :rating
234
+ t.decimal :income, precision: 14, scale: 4
235
+ t.date :birthdate
236
+ t.time :gets_up_at
237
+ t.datetime :last_seen
238
+ t.boolean :human, default: true
239
+ t.text :remarks
240
+
241
+ t.timestamps
242
+ end
243
+ end
244
+
245
+ def create_other_crud_test_models(c)
246
+ c.create_table :other_crud_test_models, force: true do |t|
247
+ t.string :name, null: false, limit: 50
248
+ t.integer :more_id
249
+ end
250
+ end
251
+
252
+ def create_crud_test_models_other_crud_test_models(c)
253
+ c.create_table :crud_test_models_other_crud_test_models,
254
+ force: true do |t|
255
+ t.belongs_to :crud_test_model
256
+ t.belongs_to :other_crud_test_model
257
+ end
258
+ end
259
+
232
260
  # Removes the crud_test_models table from the database.
233
261
  def reset_db
234
262
  c = ActiveRecord::Base.connection
235
- [:crud_test_models, :other_crud_test_models, :crud_test_models_other_crud_test_models].each do |table|
236
- if c.table_exists?(table)
237
- c.drop_table(table) rescue nil
238
- end
263
+ [:crud_test_models,
264
+ :other_crud_test_models,
265
+ :crud_test_models_other_crud_test_models].each do |table|
266
+ c.drop_table(table) if c.table_exists?(table)
239
267
  end
240
268
  end
241
269
 
242
270
  # Creates 6 dummy entries for the crud_test_models table.
243
271
  def create_test_data
244
- (1..6).inject(nil) {|prev, i| create(i, prev) }
245
- (1..6).each {|i| create_other(i) }
272
+ (1..6).reduce(nil) { |a, e| create(e, a) }
273
+ (1..6).each { |i| create_other(i) }
246
274
  end
247
275
 
248
276
  # Fixture-style accessor method to get CrudTestModel instances by name
@@ -254,13 +282,14 @@ module CrudTestHelper
254
282
  with_routing do |set|
255
283
  set.draw { resources :crud_test_models }
256
284
  # used to define a controller in these tests
257
- set.default_url_options = {:controller => 'crud_test_models'}
285
+ set.default_url_options = { controller: 'crud_test_models' }
258
286
  yield
259
287
  end
260
288
  end
261
289
 
262
290
  def special_routing
263
- controller = @controller || controller # test:unit uses instance variable, rspec the method
291
+ # test:unit uses instance variable, rspec the method
292
+ controller = @controller || controller
264
293
  @routes = ActionDispatch::Routing::RouteSet.new
265
294
  _routes = @routes
266
295
 
@@ -274,18 +303,19 @@ module CrudTestHelper
274
303
 
275
304
  def create(index, companion)
276
305
  c = str(index)
277
- m = CrudTestModel.new(:name => c,
278
- :children => 10 - index,
279
- :rating => "#{index}.#{index}".to_f,
280
- :income => 10000000 * index + 0.1 * index,
281
- :birthdate => "#{1900 + 10 * index}-#{index}-#{index}",
282
- # store entire date to avoid time zone issues
283
- :gets_up_at => RUBY_VERSION.include?('1.9.') ?
284
- Time.local(2000,1,1,index,index) :
285
- Time.utc(2000,1,1,index,index),
286
- :last_seen => "#{2000 + 10 * index}-#{index}-#{index} 1#{index}:2#{index}",
287
- :human => index % 2 == 0,
288
- :remarks => "#{c} #{str(index + 1)} #{str(index + 2)}\n" * (index % 3 + 1))
306
+ m = CrudTestModel.new(
307
+ name: c,
308
+ children: 10 - index,
309
+ rating: "#{index}.#{index}".to_f,
310
+ income: 10_000_000 * index + 0.1111 * index,
311
+ birthdate: "#{1900 + 10 * index}-#{index}-#{index}",
312
+ # store entire date to avoid time zone issues
313
+ gets_up_at: Time.zone.local(2000, 1, 1, index, index),
314
+ last_seen: "#{2000 + 10 * index}-#{index}-#{index} " +
315
+ "1#{index}:2#{index}",
316
+ human: index % 2 == 0,
317
+ remarks: "#{c} #{str(index + 1)} #{str(index + 2)}\n" *
318
+ (index % 3 + 1))
289
319
  m.companion = companion
290
320
  m.save!
291
321
  m
@@ -293,27 +323,29 @@ module CrudTestHelper
293
323
 
294
324
  def create_other(index)
295
325
  c = str(index)
296
- others = CrudTestModel.all[index..(index+2)]
297
- OtherCrudTestModel.create!(:name => c, :other_ids => others.collect(&:id), :more_id => others.first.try(:id))
326
+ others = CrudTestModel.all[index..(index + 2)]
327
+ OtherCrudTestModel.create!(name: c,
328
+ other_ids: others.map(&:id),
329
+ more_id: others.first.try(:id))
298
330
  end
299
331
 
300
332
  def str(index)
301
333
  (index + 64).chr * 5
302
334
  end
303
335
 
304
- # hack to avoid ddl in transaction issues with mysql.
336
+ # A hack to avoid ddl in transaction issues with mysql.
305
337
  def without_transaction
306
338
  c = ActiveRecord::Base.connection
307
339
  start_transaction = false
308
340
  if c.adapter_name.downcase.include?('mysql') && c.open_transactions > 0
309
341
  # in transactional tests, we may simply rollback
310
- c.execute("ROLLBACK")
342
+ c.execute('ROLLBACK')
311
343
  start_transaction = true
312
344
  end
313
345
 
314
346
  yield
315
347
 
316
- c.execute("BEGIN") if start_transaction
348
+ c.execute('BEGIN') if start_transaction
317
349
  end
318
350
 
319
351
  end