view_mapper 0.3.3 → 0.3.4

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 (81) hide show
  1. data/README.rdoc +3 -1
  2. data/VERSION +1 -1
  3. data/lib/view_mapper.rb +1 -0
  4. data/lib/view_mapper/belongs_to_parent_models.rb +12 -26
  5. data/lib/view_mapper/has_many_child_models.rb +9 -9
  6. data/lib/view_mapper/model_info.rb +56 -18
  7. data/lib/view_mapper/views/belongs_to/belongs_to_view.rb +14 -0
  8. data/lib/view_mapper/views/belongs_to_auto_complete/belongs_to_auto_complete_view.rb +9 -2
  9. data/lib/view_mapper/views/has_many/templates/helper.rb +1 -1
  10. data/lib/view_mapper/views/has_many/templates/view_child_form.html.erb +1 -1
  11. data/lib/view_mapper/views/has_many/templates/view_form.html.erb +1 -1
  12. data/lib/view_mapper/views/has_many_existing/has_many_existing_view.rb +126 -0
  13. data/lib/view_mapper/views/has_many_existing/templates/model.rb +21 -0
  14. data/lib/view_mapper/views/has_many_existing/templates/view_child_form.html.erb +16 -0
  15. data/lib/view_mapper/views/has_many_existing/templates/view_form.html.erb +20 -0
  16. data/lib/view_mapper/views/has_many_existing/templates/view_show.html.erb +26 -0
  17. data/test/generators/scaffold_for_view/scaffold_for_view_generator_test.rb +15 -15
  18. data/test/generators/view_for/view_for_generator_test.rb +18 -18
  19. data/test/model_info_test.rb +91 -20
  20. data/test/test_helper.rb +75 -105
  21. data/test/views/auto_complete/auto_complete_test.rb +104 -102
  22. data/test/views/auto_complete/expected_templates/edit.html.erb +6 -6
  23. data/test/views/auto_complete/expected_templates/expected_routes.rb +1 -1
  24. data/test/views/auto_complete/expected_templates/index.html.erb +10 -10
  25. data/test/views/auto_complete/expected_templates/new.html.erb +5 -5
  26. data/test/views/auto_complete/expected_templates/show.html.erb +6 -6
  27. data/test/views/auto_complete/expected_templates/{testies.html.erb → test_models.html.erb} +1 -1
  28. data/test/views/auto_complete/expected_templates/test_models_controller.rb +89 -0
  29. data/test/views/belongs_to/belongs_to_test.rb +238 -202
  30. data/test/views/belongs_to/expected_templates/_form.html.erb +4 -4
  31. data/test/views/belongs_to/expected_templates/child_model.rb +10 -0
  32. data/test/views/belongs_to/expected_templates/create_child_models.rb +18 -0
  33. data/test/views/belongs_to/expected_templates/edit.html.erb +4 -4
  34. data/test/views/belongs_to/expected_templates/index.html.erb +11 -11
  35. data/test/views/belongs_to/expected_templates/new.html.erb +3 -3
  36. data/test/views/belongs_to/expected_templates/show.html.erb +7 -7
  37. data/test/views/belongs_to_auto_complete/belongs_to_auto_complete_test.rb +440 -406
  38. data/test/views/belongs_to_auto_complete/expected_templates/_form.html.erb +4 -4
  39. data/test/views/belongs_to_auto_complete/expected_templates/child_model.rb +16 -0
  40. data/test/views/belongs_to_auto_complete/expected_templates/{some_other_models.html.erb → child_models.html.erb} +1 -1
  41. data/test/views/belongs_to_auto_complete/expected_templates/child_models_controller.rb +89 -0
  42. data/test/views/belongs_to_auto_complete/expected_templates/create_child_models.rb +18 -0
  43. data/test/views/belongs_to_auto_complete/expected_templates/edit.html.erb +4 -4
  44. data/test/views/belongs_to_auto_complete/expected_templates/expected_routes.rb +2 -2
  45. data/test/views/belongs_to_auto_complete/expected_templates/index.html.erb +11 -11
  46. data/test/views/belongs_to_auto_complete/expected_templates/new.html.erb +3 -3
  47. data/test/views/belongs_to_auto_complete/expected_templates/show.html.erb +7 -7
  48. data/test/views/has_many/expected_templates/_form.html.erb +8 -8
  49. data/test/views/has_many/expected_templates/{_person.html.erb → _second_child_model.html.erb} +5 -5
  50. data/test/views/has_many/expected_templates/create_parent_models.rb +16 -0
  51. data/test/views/has_many/expected_templates/edit.html.erb +4 -4
  52. data/test/views/has_many/expected_templates/index.html.erb +7 -7
  53. data/test/views/has_many/expected_templates/new.html.erb +3 -3
  54. data/test/views/has_many/expected_templates/{parent.rb → parent_model.rb} +5 -5
  55. data/test/views/has_many/expected_templates/show.html.erb +15 -15
  56. data/test/views/has_many/has_many_view_test.rb +334 -292
  57. data/test/views/has_many_existing/expected_templates/_assignment.html.erb +14 -0
  58. data/test/views/has_many_existing/expected_templates/_form.html.erb +16 -0
  59. data/test/views/{has_many/expected_templates/create_parents.rb → has_many_existing/expected_templates/create_programmers.rb} +3 -3
  60. data/test/views/has_many_existing/expected_templates/edit.html.erb +11 -0
  61. data/test/views/has_many_existing/expected_templates/index.html.erb +20 -0
  62. data/test/views/has_many_existing/expected_templates/new.html.erb +10 -0
  63. data/test/views/has_many_existing/expected_templates/programmer.rb +10 -0
  64. data/test/views/has_many_existing/expected_templates/show.html.erb +20 -0
  65. data/test/views/has_many_existing/has_many_existing_test.rb +495 -0
  66. data/test/views/paperclip/expected_templates/{create_testies.rb → create_test_models.rb} +3 -3
  67. data/test/views/paperclip/expected_templates/edit.html.erb +4 -4
  68. data/test/views/paperclip/expected_templates/index.html.erb +10 -10
  69. data/test/views/paperclip/expected_templates/new.html.erb +3 -3
  70. data/test/views/paperclip/expected_templates/show.html.erb +8 -8
  71. data/test/views/paperclip/expected_templates/{testy.rb → test_model.rb} +1 -1
  72. data/test/views/paperclip/paperclip_view_test.rb +254 -226
  73. data/view_mapper.gemspec +43 -27
  74. metadata +64 -37
  75. data/test/views/auto_complete/expected_templates/actual_index +0 -26
  76. data/test/views/auto_complete/expected_templates/testies_controller.rb +0 -89
  77. data/test/views/belongs_to/expected_templates/create_some_other_models.rb +0 -18
  78. data/test/views/belongs_to/expected_templates/some_other_model.rb +0 -10
  79. data/test/views/belongs_to_auto_complete/expected_templates/create_some_other_models.rb +0 -18
  80. data/test/views/belongs_to_auto_complete/expected_templates/some_other_model.rb +0 -16
  81. data/test/views/belongs_to_auto_complete/expected_templates/some_other_models_controller.rb +0 -89
@@ -13,35 +13,34 @@ class BelongsToViewTest < Test::Unit::TestCase
13
13
 
14
14
  context "A view_for generator instantiated for a test model" do
15
15
  setup do
16
- setup_test_model
17
- setup_parent_test_model
16
+ ClassFactory :parent_model
17
+ ClassFactory :second_parent_model
18
+ ClassFactory :child_model
18
19
  end
19
20
 
20
21
  should "detect the existing parents when no parent is specified" do
21
- gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to'], 'some_other_model')
22
+ gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to'], 'child_model')
22
23
  parent_models = gen.parent_models
23
- assert_equal 1, parent_models.size
24
- assert_equal 'Parent', parent_models[0].name
24
+ assert_equal 2, parent_models.size
25
+ assert_equal 'ParentModel', parent_models[0].name
26
+ assert_equal 'SecondParentModel', parent_models[1].name
25
27
  end
26
28
 
27
29
  should "use the specified parent if provided" do
28
- gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to:parent'], 'some_other_model')
30
+ gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to:parent_model'], 'child_model')
29
31
  parent_models = gen.parent_models
30
32
  assert_equal 1, parent_models.size
31
- assert_equal 'Parent', parent_models[0].name
33
+ assert_equal 'ParentModel', parent_models[0].name
32
34
  end
33
35
 
34
36
  should "return an error message with a bad parent param" do
37
+ ClassFactory :test_model
35
38
  Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
36
39
  new_generator_for_test_model('view_for', ['--view', 'belongs_to:blah'])
37
40
  end
38
41
  end
39
42
 
40
43
  context "A scaffold_for_view generator instantiated for a test model" do
41
- setup do
42
- setup_test_model
43
- setup_parent_test_model
44
- end
45
44
 
46
45
  should "return a warning when run with scaffold_for_view when no belongs_to is specified and not run any actions" do
47
46
  expect_no_actions
@@ -53,13 +52,12 @@ class BelongsToViewTest < Test::Unit::TestCase
53
52
 
54
53
  context "A test model with no belongs_to associations" do
55
54
  setup do
56
- setup_test_model
57
- setup_parent_test_model(true, false)
55
+ ClassFactory :test_model
58
56
  end
59
57
 
60
58
  should "return a error when run with view_for and not run any actions" do
61
59
  expect_no_actions
62
- Rails::Generator::Base.logger.expects('error').with('No belongs_to associations exist in class Testy.')
60
+ Rails::Generator::Base.logger.expects('error').with('No belongs_to associations exist in class TestModel.')
63
61
  @generator_script = Rails::Generator::Scripts::Generate.new
64
62
  @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to']))
65
63
  end
@@ -74,274 +72,312 @@ class BelongsToViewTest < Test::Unit::TestCase
74
72
 
75
73
  context "A test model with a belongs_to association for a model for which it does not have a name virtual attribute" do
76
74
  setup do
77
- setup_test_model
78
- setup_parent_test_model
79
- setup_second_parent_test_model(true, false)
75
+ ClassFactory :parent_model
76
+ ClassFactory :second_parent_model
77
+
78
+ child_model_code_missing_virtual_attribute = <<END
79
+ belongs_to :parent_model
80
+
81
+ belongs_to :second_parent_model
82
+ def second_parent_model_name
83
+ 'something else'
84
+ end
85
+ def second_parent_model_name=
86
+ end
87
+ END
88
+ ClassFactory :child_model, :class_eval => child_model_code_missing_virtual_attribute
80
89
  end
81
90
 
82
91
  should "return a warning and stop when the problem model is specified" do
83
92
  expect_no_actions
84
- Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method second_parent_name.')
93
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not have a method parent_model_name.')
85
94
  @generator_script = Rails::Generator::Scripts::Generate.new
86
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent'], 'some_other_model'))
95
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:parent_model'], 'child_model'))
87
96
  end
88
97
 
89
98
  should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
90
99
  stub_actions
91
- Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method second_parent_name.')
100
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not have a method parent_model_name.')
92
101
  Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
93
102
  @generator_script = Rails::Generator::Scripts::Generate.new
94
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to'], 'some_other_model'))
103
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to'], 'child_model'))
95
104
  end
96
105
  end
97
106
 
98
107
  context "A test model with a belongs_to association for a model which does not have a name method or column" do
99
108
  setup do
100
- setup_test_model
101
- setup_parent_test_model
102
- setup_second_parent_test_model(true, true, true, false, false)
109
+ ClassFactory :parent_model
110
+ ClassFactory :second_parent_model do |p|
111
+ p.string :not_the_name_column
112
+ end
113
+ ClassFactory :child_model
103
114
  end
104
115
 
105
116
  should "return a warning and stop when the problem model is specified" do
106
117
  expect_no_actions
107
- Rails::Generator::Base.logger.expects('warning').with('Model SecondParent does not have a name attribute.')
118
+ Rails::Generator::Base.logger.expects('warning').with('Model SecondParentModel does not have a name attribute.')
108
119
  @generator_script = Rails::Generator::Scripts::Generate.new
109
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent'], 'some_other_model'))
120
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent_model'], 'child_model'))
110
121
  end
111
122
 
112
123
  should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
113
124
  stub_actions
114
- Rails::Generator::Base.logger.expects('warning').with('Model SecondParent does not have a name attribute.')
125
+ Rails::Generator::Base.logger.expects('warning').with('Model SecondParentModel does not have a name attribute.')
115
126
  Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
116
127
  @generator_script = Rails::Generator::Scripts::Generate.new
117
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to'], 'some_other_model'))
128
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to'], 'child_model'))
118
129
  end
119
130
  end
120
131
 
121
132
  context "A test model with a belongs_to association for a model which has a name method but not a name column" do
122
133
  setup do
123
- setup_test_model
124
- setup_parent_test_model
125
- setup_second_parent_test_model(true, true, true, true, false)
134
+ ClassFactory :parent_model
135
+ parent_code_with_name_method = <<SRC
136
+ has_many :child_models
137
+ def name
138
+ 'the name'
139
+ end
140
+ SRC
141
+ ClassFactory :second_parent_model, :class_eval => parent_code_with_name_method do |p|
142
+ p.string :not_the_name_column
143
+ end
144
+ ClassFactory :child_model
126
145
  end
127
146
 
128
147
  should "continue to generate as usual" do
129
148
  stub_actions
130
149
  Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
131
150
  @generator_script = Rails::Generator::Scripts::Generate.new
132
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent'], 'some_other_model'))
151
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent_model'], 'child_model'))
133
152
  end
134
153
  end
135
154
 
136
155
  context "A test model with a belongs_to association for a model for which it does not have a foreign key" do
137
156
  setup do
138
- setup_test_model
139
- setup_parent_test_model
140
- setup_second_parent_test_model(true, true, false)
157
+ ClassFactory :parent_model
158
+ ClassFactory :second_parent_model
159
+ ClassFactory :child_model do |child|
160
+ child.string :name
161
+ child.integer :parent_model_id
162
+ end
141
163
  end
142
164
 
143
165
  should "return a warning and stop when the problem model is specified" do
144
166
  expect_no_actions
145
- Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not contain a foreign key for SecondParent.')
167
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not contain a foreign key for SecondParentModel.')
146
168
  @generator_script = Rails::Generator::Scripts::Generate.new
147
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent'], 'some_other_model'))
169
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:second_parent_model'], 'child_model'))
148
170
  end
149
171
 
150
172
  should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
151
173
  stub_actions
152
- Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not contain a foreign key for SecondParent.')
174
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not contain a foreign key for SecondParentModel.')
153
175
  Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
154
176
  @generator_script = Rails::Generator::Scripts::Generate.new
155
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to'], 'some_other_model'))
177
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to'], 'child_model'))
156
178
  end
157
179
  end
158
180
 
159
- context "A view_for generator instantiated for a test model with two belongs_to associations" do
181
+ context "A parent model, a child model and a second parent model" do
160
182
  setup do
161
- setup_test_model
162
- setup_parent_test_model
163
- setup_second_parent_test_model
164
- @gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to:parent,second_parent[other_field]'], 'some_other_model')
165
- end
183
+ ClassFactory :parent_model
184
+ ClassFactory :second_parent_model
185
+ ClassFactory :child_model, :class_eval => <<END
186
+ belongs_to :parent_model
187
+ def parent_model_name
188
+ 'something'
189
+ end
190
+ def parent_model_name=
191
+ end
192
+
193
+ belongs_to :second_parent_model
194
+ def second_parent_model_other_field
195
+ 'something else'
196
+ end
197
+ def second_parent_model_other_field=
198
+ end
199
+ END
200
+ end
201
+
202
+ context "A view_for generator instantiated for a test model with two belongs_to associations" do
203
+ setup do
204
+ @gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to:parent_model,second_parent_model[other_field]'], 'child_model')
205
+ end
166
206
 
167
- should "return the proper source root" do
168
- assert_equal File.expand_path(File.dirname(__FILE__) + '/../../..//lib/view_mapper/views/belongs_to/templates'), ViewMapper::BelongsToView.source_root
169
- end
207
+ should "return the proper source root" do
208
+ assert_equal File.expand_path(File.dirname(__FILE__) + '/../../..//lib/view_mapper/views/belongs_to/templates'), ViewMapper::BelongsToView.source_root
209
+ end
210
+
211
+ view_for_templates = %w{ new edit index show }
212
+ view_for_templates.each do | template |
213
+ should "render the #{template} template as expected" do
214
+ @attributes = @gen.attributes
215
+ @singular_name = @gen.singular_name
216
+ @plural_name = @gen.plural_name
217
+ @parent_models = @gen.parent_models
218
+ template_file = File.open(@gen.source_path("view_#{template}.html.erb"))
219
+ result = ERB.new(template_file.read, nil, '-').result(binding)
220
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/#{template}.html.erb"))
221
+ assert_equal expected_file.read, result
222
+ end
223
+ end
170
224
 
171
- view_for_templates = %w{ new edit index show }
172
- view_for_templates.each do | template |
173
- should "render the #{template} template as expected" do
225
+ should "render the form template as expected" do
174
226
  @attributes = @gen.attributes
175
227
  @singular_name = @gen.singular_name
176
228
  @plural_name = @gen.plural_name
177
229
  @parent_models = @gen.parent_models
178
- template_file = File.open(@gen.source_path("view_#{template}.html.erb"))
230
+ template_file = File.open(@gen.source_path("view_form.html.erb"))
179
231
  result = ERB.new(template_file.read, nil, '-').result(binding)
180
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/#{template}.html.erb"))
232
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_form.html.erb"))
181
233
  assert_equal expected_file.read, result
182
234
  end
183
235
  end
184
236
 
185
- should "render the form template as expected" do
186
- @attributes = @gen.attributes
187
- @singular_name = @gen.singular_name
188
- @plural_name = @gen.plural_name
189
- @parent_models = @gen.parent_models
190
- template_file = File.open(@gen.source_path("view_form.html.erb"))
191
- result = ERB.new(template_file.read, nil, '-').result(binding)
192
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_form.html.erb"))
193
- assert_equal expected_file.read, result
194
- end
195
- end
196
-
197
- context "A scaffold_for_view generator instantiated for a test model with two belongs_to associations" do
198
- setup do
199
- setup_test_model
200
- setup_parent_test_model
201
- setup_second_parent_test_model
202
- @gen = new_generator_for_test_model('scaffold_for_view', ['--view', 'belongs_to:parent,second_parent[other_field]'], 'some_other_model')
203
- end
237
+ context "A scaffold_for_view generator instantiated for a test model with two belongs_to associations" do
238
+ setup do
239
+ @gen = new_generator_for_test_model('scaffold_for_view', ['--view', 'belongs_to:parent_model,second_parent_model[other_field]'], 'child_model')
240
+ end
204
241
 
205
- should "render the model template as expected" do
206
- @parent_models = @gen.parent_models
207
- @class_name = @gen.class_name
208
- @attributes = @gen.attributes
209
- template_file = File.open(@gen.source_path("model.erb"))
210
- result = ERB.new(template_file.read, nil, '-').result(binding)
211
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/some_other_model.rb"))
212
- assert_equal expected_file.read, result
213
- end
242
+ should "render the model template as expected" do
243
+ @parent_models = @gen.parent_models
244
+ @class_name = @gen.class_name
245
+ @attributes = @gen.attributes
246
+ template_file = File.open(@gen.source_path("model.erb"))
247
+ result = ERB.new(template_file.read, nil, '-').result(binding)
248
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/child_model.rb"))
249
+ assert_equal expected_file.read, result
250
+ end
214
251
 
215
- should "render the migration template as expected" do
216
- @class_name = @gen.class_name
217
- @attributes = @gen.attributes
218
- @migration_name = 'CreateSomeOtherModels'
219
- @table_name = @gen.table_name
220
- @parent_models = @gen.parent_models
221
- @options = {}
222
- template_file = File.open(@gen.source_path("migration.rb"))
223
- result = ERB.new(template_file.read, nil, '-').result(binding)
224
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/create_some_other_models.rb"))
225
- assert_equal expected_file.read, result
252
+ should "render the migration template as expected" do
253
+ @class_name = @gen.class_name
254
+ @attributes = @gen.attributes
255
+ @migration_name = 'CreateChildModels'
256
+ @table_name = @gen.table_name
257
+ @parent_models = @gen.parent_models
258
+ @options = {}
259
+ template_file = File.open(@gen.source_path("migration.rb"))
260
+ result = ERB.new(template_file.read, nil, '-').result(binding)
261
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/create_child_models.rb"))
262
+ assert_equal expected_file.read, result
263
+ end
226
264
  end
227
- end
228
265
 
229
- context "A Rails generator script" do
230
- setup do
231
- setup_test_model
232
- setup_parent_test_model
233
- @generator_script = Rails::Generator::Scripts::Generate.new
234
- end
266
+ context "A Rails generator script" do
267
+ setup do
268
+ @generator_script = Rails::Generator::Scripts::Generate.new
269
+ end
235
270
 
236
- should "return a warning when run with view_for on an invalid parent model and not run any actions" do
237
- expect_no_actions
238
- Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
239
- @generator_script = Rails::Generator::Scripts::Generate.new
240
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:blah'], 'some_other_model'))
241
- end
271
+ should "return a warning when run with view_for on an invalid parent model and not run any actions" do
272
+ expect_no_actions
273
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
274
+ @generator_script = Rails::Generator::Scripts::Generate.new
275
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:blah'], 'child_model'))
276
+ end
242
277
 
243
- should "create the correct manifest when the view_for generator is run with a valid parent model" do
244
-
245
- expect_no_warnings
246
-
247
- directories = [
248
- 'app/controllers/',
249
- 'app/helpers/',
250
- 'app/views/some_other_models',
251
- 'app/views/layouts/',
252
- 'test/functional/',
253
- 'test/unit/',
254
- 'test/unit/helpers/',
255
- 'public/stylesheets/'
256
- ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
257
-
258
- templates = {
259
- 'view_index.html.erb' => 'app/views/some_other_models/index.html.erb',
260
- 'view_new.html.erb' => 'app/views/some_other_models/new.html.erb',
261
- 'view_edit.html.erb' => 'app/views/some_other_models/edit.html.erb',
262
- 'view_show.html.erb' => 'app/views/some_other_models/show.html.erb',
263
- 'view_form.html.erb' => 'app/views/some_other_models/_form.html.erb',
264
- 'layout.html.erb' => 'app/views/layouts/some_other_models.html.erb',
265
- 'style.css' => 'public/stylesheets/scaffold.css',
266
- 'controller.rb' => 'app/controllers/some_other_models_controller.rb',
267
- 'functional_test.rb' => 'test/functional/some_other_models_controller_test.rb',
268
- 'helper.rb' => 'app/helpers/some_other_models_helper.rb',
269
- 'helper_test.rb' => 'test/unit/helpers/some_other_models_helper_test.rb'
270
- }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
271
-
272
- Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('some_other_models')
273
- Rails::Generator::Commands::Create.any_instance.expects(:file).never
274
- Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
275
-
276
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:parent'], 'some_other_model'))
277
- end
278
+ should "create the correct manifest when the view_for generator is run with a valid parent model" do
279
+
280
+ expect_no_warnings
281
+
282
+ directories = [
283
+ 'app/controllers/',
284
+ 'app/helpers/',
285
+ 'app/views/child_models',
286
+ 'app/views/layouts/',
287
+ 'test/functional/',
288
+ 'test/unit/',
289
+ 'test/unit/helpers/',
290
+ 'public/stylesheets/'
291
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
292
+
293
+ templates = {
294
+ 'view_index.html.erb' => 'app/views/child_models/index.html.erb',
295
+ 'view_new.html.erb' => 'app/views/child_models/new.html.erb',
296
+ 'view_edit.html.erb' => 'app/views/child_models/edit.html.erb',
297
+ 'view_show.html.erb' => 'app/views/child_models/show.html.erb',
298
+ 'view_form.html.erb' => 'app/views/child_models/_form.html.erb',
299
+ 'layout.html.erb' => 'app/views/layouts/child_models.html.erb',
300
+ 'style.css' => 'public/stylesheets/scaffold.css',
301
+ 'controller.rb' => 'app/controllers/child_models_controller.rb',
302
+ 'functional_test.rb' => 'test/functional/child_models_controller_test.rb',
303
+ 'helper.rb' => 'app/helpers/child_models_helper.rb',
304
+ 'helper_test.rb' => 'test/unit/helpers/child_models_helper_test.rb'
305
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
306
+
307
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('child_models')
308
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
309
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
310
+
311
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:parent_model'], 'child_model'))
312
+ end
278
313
 
279
- should "create the correct manifest when the scaffold_for_view generator is run with a valid parent model" do
280
-
281
- expect_no_warnings
282
-
283
- directories = [
284
- 'app/models/',
285
- 'app/controllers/',
286
- 'app/helpers/',
287
- 'app/views/some_other_models',
288
- 'app/views/layouts/',
289
- 'test/functional/',
290
- 'test/unit/',
291
- 'test/unit/helpers/',
292
- 'test/fixtures/',
293
- 'public/stylesheets/'
294
- ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
295
-
296
- templates = {
297
- 'view_index.html.erb' => 'app/views/some_other_models/index.html.erb',
298
- 'view_new.html.erb' => 'app/views/some_other_models/new.html.erb',
299
- 'view_edit.html.erb' => 'app/views/some_other_models/edit.html.erb',
300
- 'view_show.html.erb' => 'app/views/some_other_models/show.html.erb',
301
- 'view_form.html.erb' => 'app/views/some_other_models/_form.html.erb',
302
- 'layout.html.erb' => 'app/views/layouts/some_other_models.html.erb',
303
- 'style.css' => 'public/stylesheets/scaffold.css',
304
- 'controller.rb' => 'app/controllers/some_other_models_controller.rb',
305
- 'functional_test.rb' => 'test/functional/some_other_models_controller_test.rb',
306
- 'helper.rb' => 'app/helpers/some_other_models_helper.rb',
307
- 'helper_test.rb' => 'test/unit/helpers/some_other_models_helper_test.rb',
308
- 'model.erb' => 'app/models/some_other_model.rb',
309
- 'unit_test.rb' => 'test/unit/some_other_model_test.rb',
310
- 'fixtures.yml' => 'test/fixtures/some_other_models.yml'
311
- }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
312
-
313
- Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('some_other_models')
314
- Rails::Generator::Commands::Create.any_instance.expects(:file).never
315
- Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
316
-
317
- Rails::Generator::Commands::Create.any_instance.expects(:migration_template).with(
318
- 'migration.rb',
319
- 'db/migrate',
320
- :assigns => { :migration_name => "CreateSomeOtherModels" },
321
- :migration_file_name => "create_some_other_models"
322
- )
323
-
324
- @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to:parent'], 'some_other_model'))
314
+ should "create the correct manifest when the scaffold_for_view generator is run with a valid parent model" do
315
+
316
+ expect_no_warnings
317
+
318
+ directories = [
319
+ 'app/models/',
320
+ 'app/controllers/',
321
+ 'app/helpers/',
322
+ 'app/views/child_models',
323
+ 'app/views/layouts/',
324
+ 'test/functional/',
325
+ 'test/unit/',
326
+ 'test/unit/helpers/',
327
+ 'test/fixtures/',
328
+ 'public/stylesheets/'
329
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
330
+
331
+ templates = {
332
+ 'view_index.html.erb' => 'app/views/child_models/index.html.erb',
333
+ 'view_new.html.erb' => 'app/views/child_models/new.html.erb',
334
+ 'view_edit.html.erb' => 'app/views/child_models/edit.html.erb',
335
+ 'view_show.html.erb' => 'app/views/child_models/show.html.erb',
336
+ 'view_form.html.erb' => 'app/views/child_models/_form.html.erb',
337
+ 'layout.html.erb' => 'app/views/layouts/child_models.html.erb',
338
+ 'style.css' => 'public/stylesheets/scaffold.css',
339
+ 'controller.rb' => 'app/controllers/child_models_controller.rb',
340
+ 'functional_test.rb' => 'test/functional/child_models_controller_test.rb',
341
+ 'helper.rb' => 'app/helpers/child_models_helper.rb',
342
+ 'helper_test.rb' => 'test/unit/helpers/child_models_helper_test.rb',
343
+ 'model.erb' => 'app/models/child_model.rb',
344
+ 'unit_test.rb' => 'test/unit/child_model_test.rb',
345
+ 'fixtures.yml' => 'test/fixtures/child_models.yml'
346
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
347
+
348
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('child_models')
349
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
350
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
351
+
352
+ Rails::Generator::Commands::Create.any_instance.expects(:migration_template).with(
353
+ 'migration.rb',
354
+ 'db/migrate',
355
+ :assigns => { :migration_name => "CreateChildModels" },
356
+ :migration_file_name => "create_child_models"
357
+ )
358
+
359
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to:parent_model'], 'child_model'))
360
+ end
325
361
  end
326
- end
327
362
 
328
- context "A Rails generator script with a parent model without a has_many association" do
329
- setup do
330
- setup_test_model
331
- setup_parent_test_model(true, true, false)
332
- @generator_script = Rails::Generator::Scripts::Generate.new
333
- end
363
+ context "A Rails generator script with a parent model without a has_many association" do
364
+ setup do
365
+ @generator_script = Rails::Generator::Scripts::Generate.new
366
+ ClassFactory :parent_model, :class_eval => ''
367
+ ClassFactory :child_model
368
+ end
334
369
 
335
- should "return a warning when run with view_for and not run any actions" do
336
- expect_no_actions
337
- Rails::Generator::Base.logger.expects('warning').with('Model Parent does not contain a has_many association for SomeOtherModel.')
338
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:parent'], 'some_other_model'))
339
- end
370
+ should "return a warning when run with view_for and not run any actions" do
371
+ expect_no_actions
372
+ Rails::Generator::Base.logger.expects('warning').with('Model ParentModel does not contain a has_many association for ChildModel.')
373
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to:parent_model'], 'child_model'))
374
+ end
340
375
 
341
- should "return a warning when run with scaffold_for_view and not run any actions" do
342
- expect_no_actions
343
- Rails::Generator::Base.logger.expects('warning').with('Model Parent does not contain a has_many association for SomeOtherModel.')
344
- @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to:parent'], 'some_other_model'))
376
+ should "return a warning when run with scaffold_for_view and not run any actions" do
377
+ expect_no_actions
378
+ Rails::Generator::Base.logger.expects('warning').with('Model ParentModel does not contain a has_many association for ChildModel.')
379
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to:parent_model'], 'child_model'))
380
+ end
345
381
  end
346
382
  end
347
383