dry_crud 1.2.7 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. data/README.rdoc +60 -27
  2. data/Rakefile +3 -1
  3. data/VERSION +1 -1
  4. data/lib/generators/dry_crud/dry_crud_generator.rb +3 -3
  5. data/lib/generators/dry_crud/templates/INSTALL +3 -1
  6. data/lib/generators/dry_crud/templates/app/controllers/crud_controller.rb +106 -90
  7. data/lib/generators/dry_crud/templates/app/controllers/list_controller.rb +90 -74
  8. data/lib/generators/dry_crud/templates/app/controllers/render_inheritable.rb +34 -33
  9. data/lib/generators/dry_crud/templates/app/helpers/crud_helper.rb +39 -23
  10. data/lib/generators/dry_crud/templates/app/helpers/list_helper.rb +11 -9
  11. data/lib/generators/dry_crud/templates/app/helpers/standard_form_builder.rb +55 -47
  12. data/lib/generators/dry_crud/templates/app/helpers/standard_helper.rb +134 -86
  13. data/lib/generators/dry_crud/templates/app/helpers/standard_table_builder.rb +41 -35
  14. data/lib/generators/dry_crud/templates/app/views/crud/_actions_edit.html.erb +1 -0
  15. data/lib/generators/dry_crud/templates/app/views/crud/edit.html.erb +3 -3
  16. data/lib/generators/dry_crud/templates/app/views/crud/new.html.erb +2 -2
  17. data/lib/generators/dry_crud/templates/app/views/crud/show.html.erb +3 -3
  18. data/lib/generators/dry_crud/templates/app/views/layouts/crud.html.erb +9 -7
  19. data/lib/generators/dry_crud/templates/app/views/list/_search.html.erb +1 -1
  20. data/lib/generators/dry_crud/templates/app/views/list/index.html.erb +4 -4
  21. data/lib/generators/dry_crud/templates/app/views/shared/_error_messages.html.erb +3 -1
  22. data/lib/generators/dry_crud/templates/config/locales/en_crud.yml +63 -0
  23. data/lib/generators/dry_crud/templates/test/crud_test_model.rb +93 -58
  24. data/lib/generators/dry_crud/templates/test/custom_assertions.rb +24 -13
  25. data/lib/generators/dry_crud/templates/test/functional/crud_controller_test_helper.rb +26 -56
  26. data/lib/generators/dry_crud/templates/test/functional/crud_test_models_controller_test.rb +47 -41
  27. data/lib/generators/dry_crud/templates/test/unit/custom_assertions_test.rb +28 -24
  28. data/lib/generators/dry_crud/templates/test/unit/helpers/crud_helper_test.rb +20 -34
  29. data/lib/generators/dry_crud/templates/test/unit/helpers/list_helper_test.rb +39 -53
  30. data/lib/generators/dry_crud/templates/test/unit/helpers/render_inheritable_test.rb +33 -33
  31. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_form_builder_test.rb +27 -27
  32. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_helper_test.rb +103 -50
  33. data/lib/generators/dry_crud/templates/test/unit/helpers/standard_table_builder_test.rb +52 -24
  34. data/test/templates/Gemfile +34 -0
  35. data/test/templates/app/controllers/ajax_controller.rb +3 -3
  36. data/test/templates/app/controllers/application_controller.rb +1 -1
  37. data/test/templates/app/controllers/cities_controller.rb +2 -5
  38. data/test/templates/app/controllers/people_controller.rb +5 -5
  39. data/test/templates/app/controllers/vips_controller.rb +6 -11
  40. data/test/templates/app/helpers/people_helper.rb +2 -2
  41. data/test/templates/app/models/city.rb +9 -9
  42. data/test/templates/app/models/person.rb +5 -4
  43. data/test/templates/app/views/ajax/_actions_index.html.erb +2 -2
  44. data/test/templates/app/views/cities/_form.html.erb +5 -1
  45. data/test/templates/app/views/layouts/_menu.html.erb +3 -3
  46. data/test/templates/app/views/people/_attrs.html.erb +3 -3
  47. data/test/templates/config/database.yml +22 -0
  48. data/test/templates/config/locales/en_cities.yml +56 -0
  49. data/test/templates/config/routes.rb +5 -5
  50. data/test/templates/db/migrate/20100511174904_create_people_and_cities.rb +5 -2
  51. data/test/templates/db/seeds.rb +38 -29
  52. data/test/templates/test/functional/cities_controller_test.rb +12 -12
  53. data/test/templates/test/functional/people_controller_test.rb +10 -10
  54. metadata +11 -7
@@ -4,43 +4,43 @@
4
4
  # Ideally, include this module into your test_helper.rb file:
5
5
  # # at the beginning of the file:
6
6
  # require 'custom_assertions'
7
- #
7
+ #
8
8
  # # inside the class definition:
9
9
  # include CustomAssertions
10
10
  module CustomAssertions
11
-
11
+
12
12
  # Asserts that the element is included in the collection.
13
13
  def assert_include(collection, element, message = "")
14
- full_message = build_message(message, "<?> expected to be included in \n<?>.",
14
+ full_message = build_message(message, "<?> expected to be included in \n<?>.",
15
15
  element, collection)
16
16
  assert_block(full_message) { collection.include?(element) }
17
17
  end
18
-
18
+
19
19
  # Asserts that the element is not included in the collection.
20
20
  def assert_not_include(collection, element, message = "")
21
- full_message = build_message(message, "<?> expected not to be included in \n<?>.",
21
+ full_message = build_message(message, "<?> expected not to be included in \n<?>.",
22
22
  element, collection)
23
23
  assert_block(full_message) { !collection.include?(element) }
24
24
  end
25
-
25
+
26
26
  # Asserts that regexp occurs exactly expected times in string.
27
27
  def assert_count(expected, regexp, string, message = "")
28
28
  actual = string.scan(regexp).size
29
- full_message = build_message(message, "<?> expected to occur ? time(s), but occured ? time(s) in \n<?>.",
29
+ full_message = build_message(message, "<?> expected to occur ? time(s), but occured ? time(s) in \n<?>.",
30
30
  regexp, expected, actual, string)
31
31
  assert_block(full_message) { expected == actual }
32
32
  end
33
-
33
+
34
34
  # Asserts that the given active model record is valid.
35
35
  # This method used to be part of Rails but was deprecated, no idea why.
36
36
  def assert_valid(record, message = "")
37
37
  record.valid?
38
- full_message = build_message(message,
39
- "? expected to be valid, but has the following errors: \n ?.",
38
+ full_message = build_message(message,
39
+ "? expected to be valid, but has the following errors: \n ?.",
40
40
  record, record.errors.full_messages.join("\n"))
41
41
  assert_block(full_message) { record.valid? }
42
42
  end
43
-
43
+
44
44
  # Asserts that the given active model record is not valid.
45
45
  # If you provide a set of invalid attribute symbols, all of and only these
46
46
  # attributes are expected to have errors. If no invalid attributes are
@@ -48,13 +48,13 @@ module CustomAssertions
48
48
  def assert_not_valid(record, *invalid_attrs)
49
49
  message = build_message("", "? expected to be invalid, but is valid.", record)
50
50
  assert_block(message) { !record.valid? }
51
-
51
+
52
52
  # assert that the given attributes have errors.
53
53
  invalid_attrs.each do |a|
54
54
  message = build_message("", "Attribute <?> expected to be invalid, but is valid.", a)
55
55
  assert_block(message) { record.errors[a].present? }
56
56
  end
57
-
57
+
58
58
  if invalid_attrs.present?
59
59
  # assert that no other than the invalid attributes have errors.
60
60
  record.errors.each do |a, error|
@@ -64,4 +64,15 @@ module CustomAssertions
64
64
  end
65
65
  end
66
66
 
67
+ # The method used to by Test::Unit to format arguments in
68
+ # #build_message. Prints ActiveRecord objects in a simpler format.
69
+ # Only works for Ruby 1.9
70
+ def mu_pp(obj)
71
+ if obj.is_a?(ActiveRecord::Base)
72
+ obj.to_s
73
+ else
74
+ super
75
+ end
76
+ end
77
+
67
78
  end
@@ -2,84 +2,84 @@
2
2
  # Simply implement the two methods :test_entry and :test_entry_attrs to test the basic
3
3
  # crud functionality. Override the test methods if you changed the behaviour in your subclass
4
4
  # controller.
5
- module CrudControllerTestHelper
6
-
5
+ module CrudControllerTestHelper
6
+
7
7
  def test_index
8
8
  get :index
9
9
  assert_response :success
10
10
  assert_template 'index'
11
11
  assert_present assigns(:entries)
12
12
  end
13
-
13
+
14
14
  def test_index_xml
15
15
  get :index, :format => 'xml'
16
16
  assert_response :success
17
17
  assert_present assigns(:entries)
18
18
  assert @response.body.starts_with?("<?xml")
19
19
  end
20
-
20
+
21
21
  def test_index_search
22
22
  field = @controller.search_columns.first
23
23
  val = field && test_entry[field].to_s
24
24
  return if val.blank? # does not support search or no value in this field
25
-
25
+
26
26
  get :index, :q => val[0..((val.size + 1)/ 2)]
27
27
  assert_response :success
28
28
  assert_present assigns(:entries)
29
29
  assert assigns(: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, :sort => col, :sort_dir => 'asc'
35
35
  assert_response :success
36
36
  assert_present assigns(:entries)
37
37
  sorted = assigns(:entries).sort_by &(col.to_sym)
38
- assert sorted, assigns(:entries)
38
+ assert_equal sorted, assigns(: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, :sort => col, :sort_dir => 'desc'
44
44
  assert_response :success
45
45
  assert_present assigns(:entries)
46
46
  sorted = assigns(:entries).sort_by &(col.to_sym)
47
- assert sorted.reverse, assigns(:entries)
47
+ assert_equal sorted.reverse, assigns(:entries)
48
48
  end
49
-
49
+
50
50
  def test_show
51
51
  get :show, :id => test_entry.id
52
52
  assert_response :success
53
53
  assert_template 'show'
54
54
  assert_equal test_entry, assigns(:entry)
55
55
  end
56
-
56
+
57
57
  def test_show_xml
58
58
  get :show, :id => test_entry.id, :format => 'xml'
59
59
  assert_response :success
60
60
  assert_equal test_entry, assigns(:entry)
61
61
  assert @response.body.starts_with?("<?xml")
62
62
  end
63
-
63
+
64
64
  def test_show_with_not_existing_id_raises_RecordNotFound
65
65
  assert_raise(ActiveRecord::RecordNotFound) do
66
66
  get :show, :id => 9999
67
67
  end
68
68
  end
69
-
69
+
70
70
  def test_show_without_id_redirects_to_index
71
71
  assert_raise(ActionController::RoutingError) do
72
72
  get :show
73
73
  end
74
74
  end
75
-
75
+
76
76
  def test_new
77
77
  get :new
78
78
  assert_response :success
79
79
  assert_template 'new'
80
80
  assert assigns(:entry).new_record?
81
81
  end
82
-
82
+
83
83
  def test_create
84
84
  assert_difference("#{model_class.name}.count") do
85
85
  post :create, model_identifier => test_entry_attrs
@@ -88,7 +88,7 @@ module CrudControllerTestHelper
88
88
  assert ! assigns(:entry).new_record?
89
89
  assert_test_attrs_equal
90
90
  end
91
-
91
+
92
92
  def test_create_xml
93
93
  assert_difference("#{model_class.name}.count") do
94
94
  post :create, model_identifier => test_entry_attrs, :format => 'xml'
@@ -96,20 +96,20 @@ module CrudControllerTestHelper
96
96
  assert_response :success
97
97
  assert @response.body.starts_with?("<?xml")
98
98
  end
99
-
99
+
100
100
  def test_edit
101
101
  get :edit, :id => test_entry.id
102
102
  assert_response :success
103
103
  assert_template 'edit'
104
104
  assert_equal test_entry, assigns(:entry)
105
105
  end
106
-
106
+
107
107
  def test_edit_without_id_raises_RecordNotFound
108
108
  assert_raise(ActionController::RoutingError) do
109
109
  get :edit
110
110
  end
111
111
  end
112
-
112
+
113
113
  def test_update
114
114
  assert_no_difference("#{model_class.name}.count") do
115
115
  put :update, :id => test_entry.id, model_identifier => test_entry_attrs
@@ -140,66 +140,36 @@ module CrudControllerTestHelper
140
140
  assert_response :success
141
141
  assert_equal "", @response.body.strip
142
142
  end
143
-
144
- # no need to test http methods for pure restfull controllers
145
- def ignore_test_create_with_wrong_http_method_redirects
146
- get :create, model_identifier => test_entry_attrs
147
- assert_redirected_to_index
148
-
149
- put :create, model_identifier => test_entry_attrs
150
- assert_redirected_to_index
151
-
152
- delete :create, model_identifier => test_entry_attrs
153
- assert_redirected_to_index
154
- end
155
143
 
156
- # no need to test http methods for pure restfull controllers
157
- def ignore_test_update_with_wrong_http_method_redirects
158
- get :update, :id => test_entry.id, model_identifier => test_entry_attrs
159
- assert_redirected_to_index
160
-
161
- delete :update, :id => test_entry.id, model_identifier => test_entry_attrs
162
- assert_redirected_to_index
163
- end
164
-
165
- # no need to test http methods for pure restfull controllers
166
- def ignore_test_destroy_with_wrong_http_method_redirects
167
- get :destroy, :id => test_entry.id
168
- assert_redirected_to_index
169
-
170
- put :destroy, :id => test_entry.id
171
- assert_redirected_to_index
172
- end
144
+ protected
173
145
 
174
- protected
175
-
176
146
  def assert_redirected_to_index
177
147
  assert_redirected_to :action => 'index', :returning => true
178
148
  end
179
-
149
+
180
150
  def assert_test_attrs_equal
181
151
  test_entry_attrs.each do |key, value|
182
152
  actual = assigns(:entry).send(key)
183
153
  assert_equal value, actual, "#{key} is expected to be <#{value.inspect}>, got <#{actual.inspect}>"
184
154
  end
185
155
  end
186
-
156
+
187
157
  def model_class
188
158
  @controller.model_class
189
159
  end
190
-
160
+
191
161
  def model_identifier
192
162
  @controller.model_identifier
193
163
  end
194
-
164
+
195
165
  # Test object used in several tests
196
166
  def test_entry
197
167
  raise "Implement this method in your test class"
198
168
  end
199
-
169
+
200
170
  # Attribute hash used in several tests
201
171
  def test_entry_attrs
202
172
  raise "Implement this method in your test class"
203
173
  end
204
-
174
+
205
175
  end
@@ -1,22 +1,22 @@
1
1
  require 'test_helper'
2
2
  require 'crud_test_model'
3
- require File.join(File.dirname(__FILE__), 'crud_controller_test_helper')
3
+ require File.join('functional', 'crud_controller_test_helper')
4
4
 
5
- # Tests all actions of the CrudController based on a dummy model
5
+ # Tests all actions of the CrudController based on a dummy model
6
6
  # (CrudTestModel). This is useful to test the general behavior
7
7
  # of CrudController.
8
8
  class CrudTestModelsControllerTest < ActionController::TestCase
9
-
9
+
10
10
  include CrudControllerTestHelper
11
11
  include CrudTestHelper
12
-
12
+
13
13
  attr_accessor :models
14
-
14
+
15
15
  setup :reset_db, :setup_db, :create_test_data, :special_routing
16
-
16
+
17
17
  teardown :reset_db
18
18
 
19
-
19
+
20
20
  def test_setup
21
21
  assert_equal 6, CrudTestModel.count
22
22
  assert_equal CrudTestModelsController, @controller.class
@@ -25,20 +25,20 @@ class CrudTestModelsControllerTest < ActionController::TestCase
25
25
  # no need to hide actions for pure restful controllers
26
26
  #assert_equal %w(index show new create edit update destroy).to_set, CrudTestModelsController.send(:action_methods)
27
27
  end
28
-
28
+
29
29
  def test_index
30
30
  super
31
31
  assert_equal 6, assigns(:entries).size
32
32
  assert_equal assigns(:entries).sort_by(&:name), assigns(:entries)
33
33
  assert_equal Hash.new, session[:list_params]
34
34
  end
35
-
35
+
36
36
  def test_index_search
37
- super
38
- assert_equal 1, assigns(:entries).size
37
+ super
38
+ assert_equal 1, assigns(:entries).size
39
39
  assert_equal({:q => 'AAAA'}, session[:list_params]['/crud_test_models'])
40
40
  end
41
-
41
+
42
42
  def test_index_with_custom_options
43
43
  get :index, :filter => true
44
44
  assert_response :success
@@ -47,7 +47,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
47
47
  assert_equal 2, assigns(:entries).size
48
48
  assert_equal assigns(:entries).sort_by(&:children).reverse, assigns(:entries)
49
49
  end
50
-
50
+
51
51
  def test_index_search_with_custom_options
52
52
  get :index, :q => 'DDD', :filter => true
53
53
  assert_response :success
@@ -57,9 +57,9 @@ class CrudTestModelsControllerTest < ActionController::TestCase
57
57
  assert_equal [CrudTestModel.find_by_name('BBBBB')], assigns(:entries)
58
58
  assert_equal({:q => 'DDD'}, session[:list_params]['/crud_test_models'])
59
59
  end
60
-
60
+
61
61
  def test_sort_given_column
62
- get :index, :sort => 'children', :sort_dir => 'asc'
62
+ get :index, :sort => 'children', :sort_dir => 'asc'
63
63
  assert_response :success
64
64
  assert_template 'index'
65
65
  assert_present assigns(:entries)
@@ -67,7 +67,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
67
67
  assert_equal CrudTestModel.all.sort_by(&:children), assigns(:entries)
68
68
  assert_equal({:sort => 'children', :sort_dir => 'asc'}, session[:list_params]['/crud_test_models'])
69
69
  end
70
-
70
+
71
71
  def test_sort_virtual_column
72
72
  get :index, :sort => 'chatty', :sort_dir => 'desc'
73
73
  assert_response :success
@@ -75,10 +75,10 @@ class CrudTestModelsControllerTest < ActionController::TestCase
75
75
  assert_present assigns(:entries)
76
76
  assert_equal 6, assigns(:entries).size
77
77
  assert_equal({:sort => 'chatty', :sort_dir => 'desc'}, session[:list_params]['/crud_test_models'])
78
-
78
+
79
79
  sorted = CrudTestModel.all.sort_by(&:chatty)
80
-
81
- # sort order is ambiguous, use index
80
+
81
+ # sort order is ambiguous, use index
82
82
  names = assigns(:entries).collect(&:name)
83
83
  assert names.index('BBBBB') < names.index('AAAAA')
84
84
  assert names.index('BBBBB') < names.index('DDDDD')
@@ -89,7 +89,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
89
89
  assert names.index('DDDDD') < names.index('CCCCC')
90
90
  assert names.index('DDDDD') < names.index('FFFFF')
91
91
  end
92
-
92
+
93
93
  def test_sort_with_search
94
94
  get :index, :q => 'DDD', :sort => 'chatty', :sort_dir => 'asc'
95
95
  assert_response :success
@@ -99,7 +99,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
99
99
  assert_equal ['CCCCC', 'DDDDD', 'BBBBB'], assigns(:entries).collect(&:name)
100
100
  assert_equal({:sort => 'chatty', :sort_dir => 'asc', :q => 'DDD'}, session[:list_params]['/crud_test_models'])
101
101
  end
102
-
102
+
103
103
  def test_index_returning
104
104
  session[:list_params] = {}
105
105
  session[:list_params]['/crud_test_models'] = {:q => 'DDD', :sort => 'chatty', :sort_dir => 'desc'}
@@ -113,33 +113,34 @@ class CrudTestModelsControllerTest < ActionController::TestCase
113
113
  assert_equal 'chatty', @controller.params[:sort]
114
114
  assert_equal 'desc', @controller.params[:sort_dir]
115
115
  end
116
-
116
+
117
117
  def test_new
118
118
  super
119
119
  assert assigns(:companions)
120
120
  assert_equal [:before_render_new, :before_render_form], @controller.called_callbacks
121
121
  end
122
-
122
+
123
123
  def test_create
124
124
  super
125
125
  assert_equal [:before_create, :before_save, :after_save, :after_create], @controller.called_callbacks
126
126
  end
127
-
127
+
128
128
  def test_edit
129
- super
129
+ super
130
130
  assert_equal [:before_render_edit, :before_render_form], @controller.called_callbacks
131
131
  end
132
-
132
+
133
133
  def test_update
134
134
  super
135
135
  assert_equal [:before_update, :before_save, :after_save, :after_update], @controller.called_callbacks
136
136
  end
137
-
137
+
138
138
  def test_destroy
139
139
  super
140
140
  assert_equal [:before_destroy, :after_destroy], @controller.called_callbacks
141
+ assert_equal 'model is gone', flash[:notice]
141
142
  end
142
-
143
+
143
144
  def test_create_with_before_callback
144
145
  assert_no_difference("#{model_class.name}.count") do
145
146
  post :create, :crud_test_model => {:name => 'illegal', :children => 2}
@@ -151,7 +152,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
151
152
  assert_equal 'illegal', assigns(:entry).name
152
153
  assert_equal [:before_render_new, :before_render_form], @controller.called_callbacks
153
154
  end
154
-
155
+
155
156
  def test_create_with_before_callback_redirect
156
157
  @controller.should_redirect = true
157
158
  assert_no_difference("#{model_class.name}.count") do
@@ -160,7 +161,7 @@ class CrudTestModelsControllerTest < ActionController::TestCase
160
161
  assert_redirected_to :action => 'index'
161
162
  assert_nil @controller.called_callbacks
162
163
  end
163
-
164
+
164
165
  def test_new_with_before_render_callback_redirect_does_not_set_companions
165
166
  @controller.should_redirect = true
166
167
  get :new
@@ -168,33 +169,38 @@ class CrudTestModelsControllerTest < ActionController::TestCase
168
169
  assert_nil assigns(:companions)
169
170
  end
170
171
 
171
- protected
172
-
172
+ def test_models_label
173
+ assert_equal 'Crud test models', @controller.models_label
174
+ assert_equal 'Crud test model', @controller.models_label(false)
175
+ end
176
+
177
+ protected
178
+
173
179
  def special_routing
174
180
  @routes = ActionDispatch::Routing::RouteSet.new
175
181
  _routes = @routes
176
-
182
+
177
183
  @controller.singleton_class.send(:include, _routes.url_helpers)
178
184
  @controller.view_context_class = Class.new(@controller.view_context_class) do
179
185
  include _routes.url_helpers
180
186
  end
181
-
187
+
182
188
  @routes.draw { resources :crud_test_models }
183
189
  end
184
-
190
+
185
191
  def test_entry
186
192
  crud_test_models(:AAAAA)
187
193
  end
188
-
194
+
189
195
  def test_entry_attrs
190
- {:name => 'foo',
191
- :children => 42,
196
+ {:name => 'foo',
197
+ :children => 42,
192
198
  :companion_id => 3,
193
- :rating => 8.5,
194
- :income => 2.42,
199
+ :rating => 8.5,
200
+ :income => 2.42,
195
201
  :birthdate => '31-12-1999'.to_date,
196
202
  :human => true,
197
203
  :remarks => "some custom\n\tremarks"}
198
204
  end
199
-
205
+
200
206
  end