dry_crud 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +24 -19
- data/Rakefile +12 -4
- data/VERSION +1 -1
- data/lib/generators/dry_crud/dry_crud_generator.rb +7 -7
- data/lib/generators/dry_crud/templates/app/assets/stylesheets/crud.scss +18 -7
- data/lib/generators/dry_crud/templates/app/assets/stylesheets/sample.scss +13 -18
- data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +74 -87
- data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +95 -50
- data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +11 -11
- data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +7 -7
- data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +55 -27
- data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +83 -30
- data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +5 -13
- data/lib/generators/dry_crud/templates/app/views/layouts/_flash.html.erb +1 -4
- data/lib/generators/dry_crud/templates/app/views/layouts/_flash.html.haml +1 -3
- data/lib/generators/dry_crud/templates/app/views/layouts/_nav.html.erb +6 -6
- data/lib/generators/dry_crud/templates/app/views/layouts/_nav.html.haml +5 -3
- data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +13 -11
- data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.haml +15 -15
- data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +10 -10
- data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.erb +2 -2
- data/lib/generators/dry_crud/templates/app/views/shared/_labeled.html.haml +2 -2
- data/lib/generators/dry_crud/templates/config/initializers/field_error_proc.rb +1 -0
- data/lib/generators/dry_crud/templates/config/locales/en_crud.yml +1 -0
- data/lib/generators/dry_crud/templates/test/crud_test_model.rb +72 -17
- data/lib/generators/dry_crud/templates/test/custom_assertions.rb +1 -1
- data/lib/generators/dry_crud/templates/test/functional/crud_controller_test_helper.rb +42 -26
- data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +135 -29
- data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +4 -4
- data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +4 -2
- data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +2 -0
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +94 -16
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +58 -18
- data/lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb +4 -4
- data/test/templates/Gemfile +1 -0
- data/test/templates/app/controllers/admin/cities_controller.rb +0 -7
- data/test/templates/app/controllers/admin/countries_controller.rb +2 -3
- data/test/templates/app/controllers/ajax_controller.rb +2 -0
- data/test/templates/app/controllers/people_controller.rb +1 -1
- data/test/templates/app/models/city.rb +2 -0
- data/test/templates/app/models/country.rb +2 -0
- data/test/templates/app/models/person.rb +2 -0
- data/test/templates/app/views/admin/cities/_attrs.html.erb +1 -0
- data/test/templates/app/views/admin/cities/_attrs.html.haml +1 -0
- data/test/templates/app/views/admin/cities/_form.html.erb +7 -1
- data/test/templates/app/views/admin/cities/_form.html.haml +5 -1
- data/test/templates/app/views/admin/cities/_list.html.erb +1 -4
- data/test/templates/app/views/admin/cities/_list.html.haml +1 -3
- data/test/templates/app/views/ajax/_actions_show.html.erb +4 -0
- data/test/templates/app/views/ajax/_actions_show.html.haml +4 -0
- data/test/templates/app/views/ajax/_form.html.erb +2 -0
- data/test/templates/app/views/ajax/_form.html.haml +2 -0
- data/test/templates/app/views/ajax/edit.js.erb +1 -0
- data/test/templates/app/views/ajax/edit.js.haml +1 -0
- data/test/templates/app/views/ajax/show.js.erb +1 -0
- data/test/templates/app/views/ajax/show.js.haml +1 -0
- data/test/templates/app/views/ajax/update.js.erb +5 -0
- data/test/templates/app/views/ajax/update.js.haml +5 -0
- data/test/templates/app/views/layouts/_nav.html.erb +6 -0
- data/test/templates/app/views/layouts/_nav.html.haml +5 -0
- data/test/templates/app/views/layouts/bootstrap.html.erb +68 -0
- data/test/templates/app/views/layouts/bootstrap.html.haml +49 -0
- data/test/templates/app/views/people/_attrs.html.erb +2 -2
- data/test/templates/app/views/people/_attrs.html.haml +2 -2
- data/test/templates/config/routes.rb +5 -5
- data/test/templates/db/migrate/20100511174904_create_people_and_cities.rb +1 -1
- data/test/templates/db/seeds.rb +52 -52
- data/test/templates/test/functional/admin/cities_controller_test.rb +15 -15
- data/test/templates/test/functional/admin/countries_controller_test.rb +4 -5
- data/test/templates/test/functional/people_controller_test.rb +32 -4
- metadata +22 -9
- data/lib/generators/dry_crud/templates/app/views/layouts/_menu.html.erb +0 -3
- data/lib/generators/dry_crud/templates/app/views/layouts/_menu.html.haml +0 -3
- data/test/templates/app/views/layouts/_menu.html.erb +0 -3
- data/test/templates/app/views/layouts/_menu.html.haml +0 -3
@@ -8,13 +8,13 @@ module CrudControllerTestHelper
|
|
8
8
|
get :index, test_params
|
9
9
|
assert_response :success
|
10
10
|
assert_template 'index'
|
11
|
-
assert_present
|
11
|
+
assert_present entries
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_index_json
|
15
15
|
get :index, test_params(:format => 'json')
|
16
16
|
assert_response :success
|
17
|
-
assert_present
|
17
|
+
assert_present entries
|
18
18
|
assert @response.body.starts_with?("[{"), @response.body
|
19
19
|
end
|
20
20
|
|
@@ -25,39 +25,39 @@ module CrudControllerTestHelper
|
|
25
25
|
|
26
26
|
get :index, test_params(:q => val[0..((val.size + 1)/ 2)])
|
27
27
|
assert_response :success
|
28
|
-
assert_present
|
29
|
-
assert
|
28
|
+
assert_present entries
|
29
|
+
assert entries.include?(test_entry)
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_index_sort_asc
|
33
33
|
col = model_class.column_names.first
|
34
34
|
get :index, test_params(:sort => col, :sort_dir => 'asc')
|
35
35
|
assert_response :success
|
36
|
-
assert_present
|
37
|
-
sorted =
|
38
|
-
assert_equal sorted,
|
36
|
+
assert_present entries
|
37
|
+
sorted = entries.sort_by &(col.to_sym)
|
38
|
+
assert_equal sorted, entries
|
39
39
|
end
|
40
40
|
|
41
41
|
def test_index_sort_desc
|
42
42
|
col = model_class.column_names.first
|
43
43
|
get :index, test_params(:sort => col, :sort_dir => 'desc')
|
44
44
|
assert_response :success
|
45
|
-
assert_present
|
46
|
-
sorted =
|
47
|
-
assert_equal sorted.reverse,
|
45
|
+
assert_present entries
|
46
|
+
sorted = entries.sort_by &(col.to_sym)
|
47
|
+
assert_equal sorted.reverse, entries
|
48
48
|
end
|
49
49
|
|
50
50
|
def test_show
|
51
51
|
get :show, test_params(:id => test_entry.id)
|
52
52
|
assert_response :success
|
53
53
|
assert_template 'show'
|
54
|
-
assert_equal test_entry,
|
54
|
+
assert_equal test_entry, entry
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_show_json
|
58
58
|
get :show, test_params(:id => test_entry.id, :format => 'json')
|
59
59
|
assert_response :success
|
60
|
-
assert_equal test_entry,
|
60
|
+
assert_equal test_entry, entry
|
61
61
|
assert @response.body.starts_with?("{")
|
62
62
|
end
|
63
63
|
|
@@ -71,15 +71,15 @@ module CrudControllerTestHelper
|
|
71
71
|
get :new, test_params
|
72
72
|
assert_response :success
|
73
73
|
assert_template 'new'
|
74
|
-
assert
|
74
|
+
assert entry.new_record?
|
75
75
|
end
|
76
76
|
|
77
77
|
def test_create
|
78
78
|
assert_difference("#{model_class.name}.count") do
|
79
79
|
post :create, test_params(model_identifier => test_entry_attrs)
|
80
80
|
end
|
81
|
-
assert_redirected_to_show
|
82
|
-
assert !
|
81
|
+
assert_redirected_to_show entry
|
82
|
+
assert ! entry.new_record?
|
83
83
|
assert_test_attrs_equal
|
84
84
|
end
|
85
85
|
|
@@ -95,7 +95,7 @@ module CrudControllerTestHelper
|
|
95
95
|
get :edit, test_params(:id => test_entry.id)
|
96
96
|
assert_response :success
|
97
97
|
assert_template 'edit'
|
98
|
-
assert_equal test_entry,
|
98
|
+
assert_equal test_entry, entry
|
99
99
|
end
|
100
100
|
|
101
101
|
def test_update
|
@@ -103,7 +103,7 @@ module CrudControllerTestHelper
|
|
103
103
|
put :update, test_params(:id => test_entry.id, model_identifier => test_entry_attrs)
|
104
104
|
end
|
105
105
|
assert_test_attrs_equal
|
106
|
-
assert_redirected_to_show
|
106
|
+
assert_redirected_to_show entry
|
107
107
|
end
|
108
108
|
|
109
109
|
def test_update_json
|
@@ -134,47 +134,63 @@ module CrudControllerTestHelper
|
|
134
134
|
def assert_redirected_to_index
|
135
135
|
assert_redirected_to test_params(:action => 'index', :returning => true)
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
def assert_redirected_to_show(entry)
|
139
139
|
assert_redirected_to test_params(:action => 'show', :id => entry.id)
|
140
140
|
end
|
141
141
|
|
142
142
|
def assert_test_attrs_equal
|
143
143
|
test_entry_attrs.each do |key, value|
|
144
|
-
actual =
|
144
|
+
actual = entry.send(key)
|
145
145
|
assert_equal value, actual, "#{key} is expected to be <#{value.inspect}>, got <#{actual.inspect}>"
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
149
|
+
# The model class under test.
|
149
150
|
def model_class
|
150
151
|
@controller.model_class
|
151
152
|
end
|
152
|
-
|
153
|
+
|
154
|
+
# The param key for model attributes.
|
153
155
|
def model_identifier
|
154
156
|
@controller.model_identifier
|
155
157
|
end
|
156
158
|
|
157
|
-
#
|
159
|
+
# The entry as set by the controller.
|
160
|
+
def entry
|
161
|
+
@controller.send(:entry)
|
162
|
+
end
|
163
|
+
|
164
|
+
# The entries as set by the controller.
|
165
|
+
def entries
|
166
|
+
@controller.send(:entries)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Test object used in several tests.
|
158
170
|
def test_entry
|
159
171
|
raise "Implement this method in your test class"
|
160
172
|
end
|
161
173
|
|
162
|
-
# Attribute hash used in several tests
|
174
|
+
# Attribute hash used in several tests.
|
163
175
|
def test_entry_attrs
|
164
176
|
raise "Implement this method in your test class"
|
165
177
|
end
|
166
|
-
|
178
|
+
|
179
|
+
# The params to pass to an action, including required nesting params.
|
167
180
|
def test_params(params = {})
|
168
181
|
nesting_params.merge(params)
|
169
182
|
end
|
170
|
-
|
183
|
+
|
171
184
|
def nesting_params
|
172
185
|
params = {}
|
173
186
|
# for nested controllers, add parent ids to each request
|
174
|
-
Array(@controller.nesting).
|
187
|
+
Array(@controller.nesting).reverse.inject(test_entry) do |parent, p|
|
175
188
|
if p.is_a?(Class) && p < ActiveRecord::Base
|
176
189
|
assoc = p.name.underscore
|
177
|
-
params["#{assoc}_id"] =
|
190
|
+
params["#{assoc}_id"] = parent.send(:"#{assoc}_id")
|
191
|
+
parent.send(assoc)
|
192
|
+
else
|
193
|
+
parent
|
178
194
|
end
|
179
195
|
end
|
180
196
|
params
|
@@ -28,14 +28,23 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
28
28
|
|
29
29
|
def test_index
|
30
30
|
super
|
31
|
-
assert_equal 6,
|
32
|
-
assert_equal
|
31
|
+
assert_equal 6, entries.size
|
32
|
+
assert_equal entries.sort_by(&:name), entries
|
33
33
|
assert_equal Hash.new, session[:list_params]
|
34
|
+
assert_equal entries, assigns(:crud_test_models)
|
35
|
+
assert_respond_to assigns(:crud_test_models), :klass
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_index_js
|
39
|
+
get :index, test_params(:format => 'js')
|
40
|
+
assert_response :success
|
41
|
+
assert_equal 'index js', @response.body
|
42
|
+
assert_present entries
|
34
43
|
end
|
35
44
|
|
36
45
|
def test_index_search
|
37
46
|
super
|
38
|
-
assert_equal 1,
|
47
|
+
assert_equal 1, entries.size
|
39
48
|
assert_equal({:q => 'AAAA'}, session[:list_params]['/crud_test_models'])
|
40
49
|
end
|
41
50
|
|
@@ -43,18 +52,18 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
43
52
|
get :index, :filter => true
|
44
53
|
assert_response :success
|
45
54
|
assert_template 'index'
|
46
|
-
assert_present
|
47
|
-
assert_equal 2,
|
48
|
-
assert_equal
|
55
|
+
assert_present entries
|
56
|
+
assert_equal 2, entries.size
|
57
|
+
assert_equal entries.sort_by(&:children).reverse, entries
|
49
58
|
end
|
50
59
|
|
51
60
|
def test_index_search_with_custom_options
|
52
61
|
get :index, :q => 'DDD', :filter => true
|
53
62
|
assert_response :success
|
54
63
|
assert_template 'index'
|
55
|
-
assert_present
|
56
|
-
assert_equal 1,
|
57
|
-
assert_equal [CrudTestModel.find_by_name('BBBBB')],
|
64
|
+
assert_present entries
|
65
|
+
assert_equal 1, entries.size
|
66
|
+
assert_equal [CrudTestModel.find_by_name('BBBBB')], entries
|
58
67
|
assert_equal({:q => 'DDD'}, session[:list_params]['/crud_test_models'])
|
59
68
|
end
|
60
69
|
|
@@ -62,9 +71,9 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
62
71
|
get :index, :sort => 'children', :sort_dir => 'asc'
|
63
72
|
assert_response :success
|
64
73
|
assert_template 'index'
|
65
|
-
assert_present
|
66
|
-
assert_equal 6,
|
67
|
-
assert_equal CrudTestModel.all.sort_by(&:children),
|
74
|
+
assert_present entries
|
75
|
+
assert_equal 6, entries.size
|
76
|
+
assert_equal CrudTestModel.all.sort_by(&:children), entries
|
68
77
|
assert_equal({:sort => 'children', :sort_dir => 'asc'}, session[:list_params]['/crud_test_models'])
|
69
78
|
end
|
70
79
|
|
@@ -72,14 +81,14 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
72
81
|
get :index, :sort => 'chatty', :sort_dir => 'desc'
|
73
82
|
assert_response :success
|
74
83
|
assert_template 'index'
|
75
|
-
assert_present
|
76
|
-
assert_equal 6,
|
84
|
+
assert_present entries
|
85
|
+
assert_equal 6, entries.size
|
77
86
|
assert_equal({:sort => 'chatty', :sort_dir => 'desc'}, session[:list_params]['/crud_test_models'])
|
78
87
|
|
79
88
|
sorted = CrudTestModel.all.sort_by(&:chatty)
|
80
89
|
|
81
90
|
# sort order is ambiguous, use index
|
82
|
-
names =
|
91
|
+
names = entries.collect(&:name)
|
83
92
|
assert names.index('BBBBB') < names.index('AAAAA')
|
84
93
|
assert names.index('BBBBB') < names.index('DDDDD')
|
85
94
|
assert names.index('EEEEE') < names.index('AAAAA')
|
@@ -92,9 +101,9 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
92
101
|
get :index, :q => 'DDD', :sort => 'chatty', :sort_dir => 'asc'
|
93
102
|
assert_response :success
|
94
103
|
assert_template 'index'
|
95
|
-
assert_present
|
96
|
-
assert_equal 3,
|
97
|
-
assert_equal ['CCCCC', 'DDDDD', 'BBBBB'],
|
104
|
+
assert_present entries
|
105
|
+
assert_equal 3, entries.size
|
106
|
+
assert_equal ['CCCCC', 'DDDDD', 'BBBBB'], entries.collect(&:name)
|
98
107
|
assert_equal({:sort => 'chatty', :sort_dir => 'asc', :q => 'DDD'}, session[:list_params]['/crud_test_models'])
|
99
108
|
end
|
100
109
|
|
@@ -104,9 +113,9 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
104
113
|
get :index, :returning => true
|
105
114
|
assert_response :success
|
106
115
|
assert_template 'index'
|
107
|
-
assert_present
|
108
|
-
assert_equal 3,
|
109
|
-
assert_equal ['BBBBB', 'DDDDD', 'CCCCC'],
|
116
|
+
assert_present entries
|
117
|
+
assert_equal 3, entries.size
|
118
|
+
assert_equal ['BBBBB', 'DDDDD', 'CCCCC'], entries.collect(&:name)
|
110
119
|
assert_equal 'DDD', @controller.params[:q]
|
111
120
|
assert_equal 'chatty', @controller.params[:sort]
|
112
121
|
assert_equal 'desc', @controller.params[:sort_dir]
|
@@ -115,45 +124,66 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
115
124
|
def test_new
|
116
125
|
super
|
117
126
|
assert assigns(:companions)
|
127
|
+
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
118
128
|
assert_equal [:before_render_new, :before_render_form], @controller.called_callbacks
|
119
129
|
end
|
120
130
|
|
131
|
+
def test_show
|
132
|
+
super
|
133
|
+
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_show_with_custom
|
137
|
+
get :show, test_params(:id => crud_test_models(:BBBBB).id)
|
138
|
+
assert_response :success
|
139
|
+
assert_equal 'custom html', @response.body
|
140
|
+
end
|
141
|
+
|
121
142
|
def test_create
|
122
143
|
super
|
144
|
+
assert_match /model got created/, flash[:notice]
|
145
|
+
assert_blank flash[:alert]
|
123
146
|
assert_equal [:before_create, :before_save, :after_save, :after_create], @controller.called_callbacks
|
124
147
|
end
|
125
148
|
|
126
149
|
def test_edit
|
127
150
|
super
|
151
|
+
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
128
152
|
assert_equal [:before_render_edit, :before_render_form], @controller.called_callbacks
|
129
153
|
end
|
130
154
|
|
131
155
|
def test_update
|
132
156
|
super
|
157
|
+
assert_match /successfully updated/, flash[:notice]
|
158
|
+
assert flash[:notice].html_safe?
|
159
|
+
assert_blank flash[:alert]
|
160
|
+
assert_equal @controller.send(:entry), assigns(:crud_test_model)
|
133
161
|
assert_equal [:before_update, :before_save, :after_save, :after_update], @controller.called_callbacks
|
134
162
|
end
|
135
163
|
|
136
164
|
def test_destroy
|
137
165
|
super
|
138
166
|
assert_equal [:before_destroy, :after_destroy], @controller.called_callbacks
|
139
|
-
|
167
|
+
assert_match 'successfully deleted', flash[:notice]
|
168
|
+
assert flash[:notice].html_safe?
|
140
169
|
end
|
141
170
|
|
142
171
|
def test_create_with_before_callback
|
143
|
-
assert_no_difference("
|
172
|
+
assert_no_difference("CrudTestModel.count") do
|
144
173
|
post :create, :crud_test_model => {:name => 'illegal', :children => 2}
|
145
174
|
end
|
175
|
+
assert_response :success
|
146
176
|
assert_template 'new'
|
147
|
-
assert
|
177
|
+
assert entry.new_record?
|
148
178
|
assert assigns(:companions)
|
149
|
-
|
150
|
-
assert_equal 'illegal',
|
179
|
+
assert_present flash[:alert]
|
180
|
+
assert_equal 'illegal', entry.name
|
151
181
|
assert_equal [:before_render_new, :before_render_form], @controller.called_callbacks
|
152
182
|
end
|
153
183
|
|
154
184
|
def test_create_with_before_callback_redirect
|
155
185
|
@controller.should_redirect = true
|
156
|
-
assert_no_difference("
|
186
|
+
assert_no_difference("CrudTestModel.count") do
|
157
187
|
post :create, :crud_test_model => {:name => 'illegal', :children => 2}
|
158
188
|
end
|
159
189
|
assert_redirected_to :action => 'index'
|
@@ -166,7 +196,83 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
166
196
|
assert_redirected_to :action => 'index'
|
167
197
|
assert_nil assigns(:companions)
|
168
198
|
end
|
169
|
-
|
199
|
+
|
200
|
+
def test_create_with_failure
|
201
|
+
assert_no_difference("CrudTestModel.count") do
|
202
|
+
post :create, :crud_test_model => {:children => 2}
|
203
|
+
end
|
204
|
+
assert_response :success
|
205
|
+
assert_template 'new'
|
206
|
+
assert entry.new_record?
|
207
|
+
assert assigns(:companions)
|
208
|
+
assert_blank flash[:notice]
|
209
|
+
assert_blank flash[:alert]
|
210
|
+
assert_blank entry.name
|
211
|
+
assert_equal [:before_create, :before_save, :before_render_new, :before_render_form], @controller.called_callbacks
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_create_with_failure_json
|
215
|
+
assert_no_difference("CrudTestModel.count") do
|
216
|
+
post :create, :crud_test_model => {:children => 2}, :format => 'json'
|
217
|
+
end
|
218
|
+
assert_response :unprocessable_entity
|
219
|
+
assert entry.new_record?
|
220
|
+
assert_match /errors/, @response.body
|
221
|
+
assert_equal [:before_create, :before_save], @controller.called_callbacks
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_update_with_failure
|
225
|
+
put :update, :id => test_entry.id, :crud_test_model => {:rating => 20}
|
226
|
+
assert_response :success
|
227
|
+
assert_template 'edit'
|
228
|
+
assert entry.changed?
|
229
|
+
assert_blank flash[:notice]
|
230
|
+
assert_blank flash[:alert]
|
231
|
+
assert_equal 20, entry.rating
|
232
|
+
assert_equal [:before_update, :before_save, :before_render_edit, :before_render_form], @controller.called_callbacks
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_update_with_failure_json
|
236
|
+
put :update, :id => test_entry.id, :crud_test_model => {:rating => 20}, :format => 'json'
|
237
|
+
assert_response :unprocessable_entity
|
238
|
+
assert entry.changed?
|
239
|
+
assert_match /errors/, @response.body
|
240
|
+
assert_blank flash[:notice]
|
241
|
+
assert_equal 20, entry.rating
|
242
|
+
assert_equal [:before_update, :before_save], @controller.called_callbacks
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_destroy_failure
|
246
|
+
assert_no_difference("#{model_class.name}.count") do
|
247
|
+
@request.env['HTTP_REFERER'] = crud_test_model_url(crud_test_models(:BBBBB))
|
248
|
+
delete :destroy, test_params(:id => crud_test_models(:BBBBB).id)
|
249
|
+
end
|
250
|
+
assert_redirected_to_show(entry)
|
251
|
+
assert_match /companion/, flash[:alert]
|
252
|
+
assert_blank flash[:notice]
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_destroy_failure_callback
|
256
|
+
e = crud_test_models(:AAAAA)
|
257
|
+
e.update_attribute :name, 'illegal'
|
258
|
+
assert_no_difference("#{model_class.name}.count") do
|
259
|
+
delete :destroy, test_params(:id => e.id)
|
260
|
+
end
|
261
|
+
assert_redirected_to_index
|
262
|
+
assert_match /illegal name/, flash[:alert]
|
263
|
+
assert_blank flash[:notice]
|
264
|
+
end
|
265
|
+
|
266
|
+
def test_destroy_failure_json
|
267
|
+
assert_no_difference("#{model_class.name}.count") do
|
268
|
+
delete :destroy, test_params(:id => crud_test_models(:BBBBB).id, :format => 'json')
|
269
|
+
end
|
270
|
+
assert_response :unprocessable_entity
|
271
|
+
assert_match /errors/, @response.body
|
272
|
+
assert_blank flash[:notice]
|
273
|
+
end
|
274
|
+
|
275
|
+
|
170
276
|
def test_models_label
|
171
277
|
assert_equal 'Crud Test Models', @controller.models_label
|
172
278
|
assert_equal 'Crud Test Model', @controller.models_label(false)
|
@@ -189,7 +295,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
|
|
189
295
|
def test_entry
|
190
296
|
crud_test_models(:AAAAA)
|
191
297
|
end
|
192
|
-
|
298
|
+
|
193
299
|
def test_entry_attrs
|
194
300
|
{:name => 'foo',
|
195
301
|
:children => 42,
|
@@ -10,11 +10,11 @@ class CustomAssertionsTest < ActiveSupport::TestCase
|
|
10
10
|
|
11
11
|
setup :reset_db, :setup_db, :create_test_data
|
12
12
|
teardown :reset_db
|
13
|
-
|
14
|
-
AssertionException = RUBY_VERSION.to_f == 1.9 ?
|
15
|
-
MiniTest::Assertion :
|
13
|
+
|
14
|
+
AssertionException = RUBY_VERSION.to_f == 1.9 ?
|
15
|
+
MiniTest::Assertion :
|
16
16
|
Test::Unit::AssertionFailedError
|
17
|
-
|
17
|
+
|
18
18
|
test "assert include succeeds if included" do
|
19
19
|
assert_nothing_raised do
|
20
20
|
assert_include [1,2,3], 2
|
@@ -14,6 +14,8 @@ class CrudHelperTest < ActionView::TestCase
|
|
14
14
|
include ListHelper
|
15
15
|
include CrudTestHelper
|
16
16
|
|
17
|
+
attr_reader :entries
|
18
|
+
|
17
19
|
setup :reset_db, :setup_db, :create_test_data
|
18
20
|
teardown :reset_db
|
19
21
|
|
@@ -78,7 +80,7 @@ class CrudHelperTest < ActionView::TestCase
|
|
78
80
|
capture { crud_form }
|
79
81
|
end
|
80
82
|
|
81
|
-
assert_match /form .*?action="\/crud_test_models\/#{
|
83
|
+
assert_match /form .*?action="\/crud_test_models\/#{entry.id}"/, f
|
82
84
|
assert_match /input .*?name="crud_test_model\[name\]" .*?type="text"/, f
|
83
85
|
assert_match /input .*?name="crud_test_model\[whatever\]" .*?type="text"/, f
|
84
86
|
assert_match /input .*?name="crud_test_model\[children\]" .*?type="number"/, f
|
@@ -89,7 +91,7 @@ class CrudHelperTest < ActionView::TestCase
|
|
89
91
|
assert_match /select .*?name="crud_test_model\[companion_id\]"/, f
|
90
92
|
assert_match /textarea .*?name="crud_test_model\[remarks\]"/, f
|
91
93
|
end
|
92
|
-
|
94
|
+
|
93
95
|
def entry
|
94
96
|
@entry ||= CrudTestModel.first
|
95
97
|
end
|
@@ -53,19 +53,29 @@ class StandardFormBuilderTest < ActionView::TestCase
|
|
53
53
|
assert form.belongs_to_field(:companion_id).html_safe?
|
54
54
|
end
|
55
55
|
|
56
|
+
test "input_field dispatches has_and_belongs_to_many attr to select field" do
|
57
|
+
assert_equal form.has_many_field(:other_ids), form.input_field(:other_ids)
|
58
|
+
assert form.has_many_field(:other_ids).html_safe?
|
59
|
+
end
|
60
|
+
|
61
|
+
test "input_field dispatches has_many attr to select field" do
|
62
|
+
assert_equal form.has_many_field(:more_ids), form.input_field(:more_ids)
|
63
|
+
assert form.has_many_field(:more_ids).html_safe?
|
64
|
+
end
|
65
|
+
|
56
66
|
test "input_fields concats multiple fields" do
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
67
|
+
result = form.labeled_input_fields(:name, :remarks, :children)
|
68
|
+
assert result.html_safe?
|
69
|
+
assert result.include?(form.input_field(:name))
|
70
|
+
assert result.include?(form.input_field(:remarks))
|
71
|
+
assert result.include?(form.input_field(:children))
|
62
72
|
end
|
63
73
|
|
64
74
|
test "labeld_input_field adds required mark" do
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
75
|
+
result = form.labeled_input_field(:name)
|
76
|
+
assert result.include?(StandardFormBuilder::REQUIRED_MARK)
|
77
|
+
result = form.labeled_input_field(:remarks)
|
78
|
+
assert !result.include?(StandardFormBuilder::REQUIRED_MARK)
|
69
79
|
end
|
70
80
|
|
71
81
|
test "belongs_to_field has all options by default" do
|
@@ -93,18 +103,68 @@ class StandardFormBuilderTest < ActionView::TestCase
|
|
93
103
|
assert_equal 0, f.scan('</option>').size
|
94
104
|
end
|
95
105
|
|
106
|
+
test "has_and_belongs_to_many_field has all options by default" do
|
107
|
+
f = form.has_many_field(:other_ids)
|
108
|
+
assert_equal 6, f.scan('</option>').size
|
109
|
+
end
|
110
|
+
|
111
|
+
test "has_and_belongs_to_many_field with :list option" do
|
112
|
+
list = OtherCrudTestModel.all
|
113
|
+
f = form.has_many_field(:other_ids, :list => [list.first, list.second])
|
114
|
+
assert_equal 2, f.scan('</option>').size
|
115
|
+
end
|
116
|
+
|
117
|
+
test "has_and_belongs_to_many_field with instance variable" do
|
118
|
+
list = OtherCrudTestModel.all
|
119
|
+
@others = [list.first, list.second]
|
120
|
+
f = form.has_many_field(:other_ids)
|
121
|
+
assert_equal 2, f.scan('</option>').size
|
122
|
+
end
|
123
|
+
|
124
|
+
test "has_and_belongs_to_many_field with empty list" do
|
125
|
+
@others = []
|
126
|
+
f = form.has_many_field(:other_ids)
|
127
|
+
assert_match /none available/m, f
|
128
|
+
assert_equal 0, f.scan('</option>').size
|
129
|
+
end
|
130
|
+
|
131
|
+
test "has_many_field has all options by default" do
|
132
|
+
f = form.has_many_field(:more_ids)
|
133
|
+
assert_equal 6, f.scan('</option>').size
|
134
|
+
end
|
135
|
+
|
136
|
+
test "has_many_field with :list option" do
|
137
|
+
list = OtherCrudTestModel.all
|
138
|
+
f = form.has_many_field(:more_ids, :list => [list.first, list.second])
|
139
|
+
assert_equal 2, f.scan('</option>').size
|
140
|
+
end
|
141
|
+
|
142
|
+
test "has_many_field with instance variable" do
|
143
|
+
list = OtherCrudTestModel.all
|
144
|
+
@mores = [list.first, list.second]
|
145
|
+
f = form.has_many_field(:more_ids)
|
146
|
+
assert_equal 2, f.scan('</option>').size
|
147
|
+
end
|
148
|
+
|
149
|
+
test "has_many_field with empty list" do
|
150
|
+
@mores = []
|
151
|
+
f = form.has_many_field(:more_ids)
|
152
|
+
assert_match /none available/m, f
|
153
|
+
assert_equal 0, f.scan('</option>').size
|
154
|
+
end
|
155
|
+
|
96
156
|
test "string_field sets maxlength attribute if limit" do
|
97
157
|
assert_match /maxlength="50"/, form.string_field(:name)
|
98
158
|
end
|
99
159
|
|
100
160
|
test "label creates captionized label" do
|
101
|
-
|
102
|
-
|
161
|
+
assert_match /label [^>]*for.+Gugus dada/, form.label(:gugus_dada)
|
162
|
+
assert form.label(:gugus_dada).html_safe?
|
103
163
|
end
|
104
164
|
|
105
165
|
test "classic label still works" do
|
106
|
-
|
107
|
-
|
166
|
+
assert_match /label [^>]*for.+hoho/, form.label(:gugus_dada, "hoho")
|
167
|
+
assert form.label(:gugus_dada, "hoho").html_safe?
|
108
168
|
end
|
109
169
|
|
110
170
|
test "labeled_text_field create label" do
|
@@ -113,9 +173,27 @@ class StandardFormBuilderTest < ActionView::TestCase
|
|
113
173
|
end
|
114
174
|
|
115
175
|
test "labeled field creates label" do
|
116
|
-
|
117
|
-
|
118
|
-
|
176
|
+
result = form.labeled("gugus", "<input type='text' name='gugus' />".html_safe)
|
177
|
+
assert result.html_safe?
|
178
|
+
assert_match /label [^>]*for.+<input/m, result
|
179
|
+
end
|
180
|
+
|
181
|
+
test "labeled field creates label and block" do
|
182
|
+
result = form.labeled("gugus") { "<input type='text' name='gugus' />".html_safe }
|
183
|
+
assert result.html_safe?
|
184
|
+
assert_match /label [^>]*for.+<input/m, result
|
185
|
+
end
|
186
|
+
|
187
|
+
test "labeled field creates label with caption" do
|
188
|
+
result = form.labeled("gugus", 'Caption', "<input type='text' name='gugus' />".html_safe)
|
189
|
+
assert result.html_safe?
|
190
|
+
assert_match /label [^>]*for.+>Caption<\/label>.*<input/m, result
|
191
|
+
end
|
192
|
+
|
193
|
+
test "labeled field creates label with caption and block" do
|
194
|
+
result = form.labeled("gugus", 'Caption') { "<input type='text' name='gugus' />".html_safe }
|
195
|
+
assert result.html_safe?
|
196
|
+
assert_match /label [^>]*for.+>Caption<\/label>.*<input/m, result
|
119
197
|
end
|
120
198
|
|
121
199
|
test "required mark is shown" do
|