view_mapper 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/Rakefile +5 -3
  2. data/VERSION +1 -1
  3. data/generators/view_for/view_for_generator.rb +4 -37
  4. data/lib/view_mapper/has_many_templates/controller.rb +85 -0
  5. data/lib/view_mapper/has_many_templates/fixtures.yml +19 -0
  6. data/lib/view_mapper/has_many_templates/functional_test.rb +45 -0
  7. data/lib/view_mapper/has_many_templates/helper.rb +18 -0
  8. data/lib/view_mapper/has_many_templates/helper_test.rb +4 -0
  9. data/lib/view_mapper/has_many_templates/layout.html.erb +19 -0
  10. data/lib/view_mapper/has_many_templates/migration.rb +16 -0
  11. data/lib/view_mapper/has_many_templates/model.rb +23 -0
  12. data/lib/view_mapper/has_many_templates/nested_attributes.js +13 -0
  13. data/lib/view_mapper/has_many_templates/style.css +58 -0
  14. data/lib/view_mapper/has_many_templates/unit_test.rb +8 -0
  15. data/lib/view_mapper/has_many_templates/view_child_form.html.erb +12 -0
  16. data/lib/view_mapper/has_many_templates/view_edit.html.erb +11 -0
  17. data/lib/view_mapper/has_many_templates/view_form.html.erb +20 -0
  18. data/lib/view_mapper/has_many_templates/view_index.html.erb +24 -0
  19. data/lib/view_mapper/has_many_templates/view_new.html.erb +10 -0
  20. data/lib/view_mapper/has_many_templates/view_show.html.erb +22 -0
  21. data/lib/view_mapper/has_many_view.rb +124 -0
  22. data/lib/view_mapper/model_info.rb +157 -0
  23. data/lib/view_mapper/paperclip_view.rb +5 -40
  24. data/lib/view_mapper/view_mapper.rb +2 -2
  25. data/lib/view_mapper.rb +2 -0
  26. data/test/expected_templates/has_many/_form.html.erb +26 -0
  27. data/test/expected_templates/has_many/_person.html.erb +18 -0
  28. data/test/expected_templates/has_many/create_parents.rb +15 -0
  29. data/test/expected_templates/has_many/edit.html.erb +11 -0
  30. data/test/expected_templates/has_many/index.html.erb +20 -0
  31. data/test/expected_templates/has_many/new.html.erb +10 -0
  32. data/test/expected_templates/has_many/parent.rb +14 -0
  33. data/test/expected_templates/has_many/show.html.erb +33 -0
  34. data/test/has_many_view_test.rb +405 -0
  35. data/test/model_info_test.rb +81 -0
  36. data/test/paperclip_view_test.rb +4 -27
  37. data/test/test_helper.rb +82 -9
  38. data/test/view_mapper_test.rb +2 -2
  39. data/view_mapper.gemspec +41 -5
  40. metadata +48 -5
@@ -0,0 +1,405 @@
1
+ require 'test_helper'
2
+
3
+ class HasManyViewTest < Test::Unit::TestCase
4
+
5
+ attr_reader :singular_name
6
+ attr_reader :attributes
7
+ attr_reader :plural_name
8
+ attr_reader :child_models
9
+ attr_reader :child_model
10
+ attr_reader :class_name
11
+ attr_reader :migration_name
12
+ attr_reader :table_name
13
+ attr_reader :options
14
+
15
+ context "A view_for generator instantiated for a test model" do
16
+ setup do
17
+ setup_test_model
18
+ setup_parent_test_model
19
+ setup_test_model_without_nested_attributes
20
+ end
21
+
22
+ should "detect the existing child models when no child model is specified" do
23
+ Rails::Generator::Base.logger.expects('warning').with('Model Parent does not accept nested attributes for model ThirdModel.')
24
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many'], 'parent')
25
+ child_models = gen.child_models
26
+ assert_equal 2, child_models.size
27
+ assert_equal 'SomeOtherModel', child_models[0].name
28
+ assert_equal 'Testy', child_models[1].name
29
+ assert_equal [ 'name' ], child_models[0].columns
30
+ assert_equal [ 'first_name', 'last_name', 'address' ], child_models[1].columns
31
+ end
32
+
33
+ should "use find the specified valid child model if provided" do
34
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many:testies'], 'parent')
35
+ child_models = gen.child_models
36
+ assert_equal 'Testy', gen.child_models[0].name
37
+ assert_equal 1, gen.child_models.size
38
+ end
39
+
40
+ should "be able to parse two model names" do
41
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many:testies,some_other_models'], 'parent')
42
+ child_models = gen.child_models
43
+ assert_equal 2, gen.child_models.size
44
+ assert_equal 'Testy', child_models[0].name
45
+ assert_equal 'SomeOtherModel', child_models[1].name
46
+ assert_equal [ 'first_name', 'last_name', 'address' ], child_models[0].columns
47
+ assert_equal [ 'name' ], child_models[1].columns
48
+ end
49
+
50
+ should "return an error message with a bad child model param" do
51
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
52
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many:blah'], 'parent')
53
+ assert_equal [], gen.child_models
54
+ end
55
+ end
56
+
57
+ context "A scaffold_for_view generator instantiated for a test model" do
58
+ setup do
59
+ setup_test_model
60
+ end
61
+
62
+ should "return a warning when run with scaffold_for_view when no has_many is specified and not run any actions" do
63
+ expect_no_actions
64
+ Rails::Generator::Base.logger.expects('error').with('No has_many association specified.')
65
+ @generator_script = Rails::Generator::Scripts::Generate.new
66
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many'], 'parent'))
67
+ end
68
+ end
69
+
70
+ context "A test model with no has many associations" do
71
+ setup do
72
+ setup_test_model
73
+ end
74
+
75
+ should "return a error when run with view_for and not run any actions" do
76
+ expect_no_actions
77
+ Rails::Generator::Base.logger.expects('error').with('No has_many associations exist in class Testy.')
78
+ @generator_script = Rails::Generator::Scripts::Generate.new
79
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many']))
80
+ end
81
+
82
+ should "return a error when run with scaffold_for_view and not run any actions" do
83
+ expect_no_actions
84
+ Rails::Generator::Base.logger.expects('error').with('No has_many association specified.')
85
+ @generator_script = Rails::Generator::Scripts::Generate.new
86
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many']))
87
+ end
88
+ end
89
+
90
+ context "A test model with a has_many association for a model for which it does not accept nested attributes" do
91
+ setup do
92
+ setup_test_model
93
+ setup_parent_test_model
94
+ setup_test_model_without_nested_attributes
95
+ end
96
+
97
+ should "return a warning and stop when the problem model is specified" do
98
+ expect_no_actions
99
+ Rails::Generator::Base.logger.expects('warning').with('Model Parent does not accept nested attributes for model ThirdModel.')
100
+ @generator_script = Rails::Generator::Scripts::Generate.new
101
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:third_models'], 'parent'))
102
+ end
103
+
104
+ should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
105
+ stub_actions
106
+ Rails::Generator::Base.logger.expects('warning').with('Model Parent does not accept nested attributes for model ThirdModel.')
107
+ Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
108
+ @generator_script = Rails::Generator::Scripts::Generate.new
109
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many'], 'parent'))
110
+ end
111
+ end
112
+
113
+ context "A view_for generator instantiated for a test model with two has_many associations" do
114
+ setup do
115
+ setup_test_model
116
+ setup_parent_test_model
117
+ @gen = new_generator_for_test_model('view_for', ['--view', 'has_many'], 'parent')
118
+ end
119
+
120
+ should "return the proper source root folder" do
121
+ assert_equal './test/../lib/view_mapper/has_many_templates', @gen.source_root
122
+ end
123
+
124
+ view_for_templates = %w{ new edit show index }
125
+ view_for_templates.each do | template |
126
+ should "render the #{template} template as expected" do
127
+ @attributes = @gen.attributes
128
+ @singular_name = @gen.singular_name
129
+ @plural_name = @gen.plural_name
130
+ @child_models = @gen.child_models
131
+ template_file = File.open(File.join(File.dirname(__FILE__), "/../lib/view_mapper/has_many_templates/view_#{template}.html.erb"))
132
+ result = ERB.new(template_file.read, nil, '-').result(binding)
133
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/has_many/#{template}.html.erb"))
134
+ assert_equal expected_file.read, result
135
+ end
136
+ end
137
+
138
+ should "render the form partial as expected" do
139
+ @attributes = @gen.attributes
140
+ @singular_name = @gen.singular_name
141
+ @plural_name = @gen.plural_name
142
+ @child_models = @gen.child_models
143
+ template_file = File.open(File.join(File.dirname(__FILE__), "/../lib/view_mapper/has_many_templates/view_form.html.erb"))
144
+ result = ERB.new(template_file.read, nil, '-').result(binding)
145
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/has_many/_form.html.erb"))
146
+ assert_equal expected_file.read, result
147
+ end
148
+
149
+ should "render the person partial as expected" do
150
+ @child_model = @gen.child_models[1]
151
+ template_file = File.open(File.join(File.dirname(__FILE__), "/../lib/view_mapper/has_many_templates/view_child_form.html.erb"))
152
+ result = ERB.new(template_file.read, nil, '-').result(binding)
153
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/has_many/_person.html.erb"))
154
+ assert_equal expected_file.read, result
155
+ end
156
+ end
157
+
158
+ context "A scaffold_for_view generator instantiated for a test model with two has_many associations" do
159
+ setup do
160
+ setup_test_model
161
+ setup_parent_test_model
162
+ @gen = new_generator_for_test_model('scaffold_for_view', ['--view', 'has_many:some_other_models,testies'], 'parent')
163
+ end
164
+
165
+ should "render the model template as expected" do
166
+ @child_models = @gen.child_models
167
+ @class_name = @gen.class_name
168
+ @attributes = @gen.attributes
169
+ template_file = File.open(File.join(File.dirname(__FILE__), "/../lib/view_mapper/has_many_templates/model.rb"))
170
+ result = ERB.new(template_file.read, nil, '-').result(binding)
171
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/has_many/parent.rb"))
172
+ assert_equal expected_file.read, result
173
+ end
174
+
175
+ should "render the migration template as expected" do
176
+ @class_name = @gen.class_name
177
+ @attributes = @gen.attributes
178
+ @migration_name = 'CreateParents'
179
+ @table_name = @gen.table_name
180
+ @options = {}
181
+ template_file = File.open(File.join(File.dirname(__FILE__), "/../lib/view_mapper/has_many_templates/migration.rb"))
182
+ result = ERB.new(template_file.read, nil, '-').result(binding)
183
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/has_many/create_parents.rb"))
184
+ assert_equal expected_file.read, result
185
+ end
186
+ end
187
+
188
+ context "A Rails generator script" do
189
+ setup do
190
+ setup_test_model
191
+ setup_parent_test_model
192
+ @generator_script = Rails::Generator::Scripts::Generate.new
193
+ end
194
+
195
+ should "return a warning when run with view_for on an invalid child model and not run any actions" do
196
+ expect_no_actions
197
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
198
+ @generator_script = Rails::Generator::Scripts::Generate.new
199
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:blah']))
200
+ end
201
+
202
+ should "create the correct manifest when the view_for generator is run with a valid child model" do
203
+
204
+ expect_no_warnings
205
+
206
+ directories = [
207
+ 'app/controllers/',
208
+ 'app/helpers/',
209
+ 'app/views/parents',
210
+ 'app/views/layouts/',
211
+ 'test/functional/',
212
+ 'test/unit/',
213
+ 'test/unit/helpers/',
214
+ 'public/stylesheets/'
215
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
216
+
217
+ templates = {
218
+ 'view_index.html.erb' => 'app/views/parents/index.html.erb',
219
+ 'view_new.html.erb' => 'app/views/parents/new.html.erb',
220
+ 'view_edit.html.erb' => 'app/views/parents/edit.html.erb',
221
+ 'view_form.html.erb' => 'app/views/parents/_form.html.erb',
222
+ 'layout.html.erb' => 'app/views/layouts/parents.html.erb',
223
+ 'style.css' => 'public/stylesheets/scaffold.css',
224
+ 'controller.rb' => 'app/controllers/parents_controller.rb',
225
+ 'functional_test.rb' => 'test/functional/parents_controller_test.rb',
226
+ 'helper.rb' => 'app/helpers/parents_helper.rb',
227
+ 'helper_test.rb' => 'test/unit/helpers/parents_helper_test.rb'
228
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
229
+
230
+ testy_model_info = ViewMapper::ModelInfo.new('testy')
231
+ parent_model_info = ViewMapper::ModelInfo.new('parent')
232
+ ViewMapper::ModelInfo.stubs(:new).with('testy').returns(testy_model_info)
233
+ ViewMapper::ModelInfo.stubs(:new).with('parent').returns(parent_model_info)
234
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
235
+ 'view_show.html.erb',
236
+ 'app/views/parents/show.html.erb',
237
+ { :assigns => { :child_models => [ testy_model_info ] } }
238
+ )
239
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
240
+ 'view_child_form.html.erb',
241
+ 'app/views/parents/_testy.html.erb',
242
+ { :assigns => { :child_model => testy_model_info } }
243
+ )
244
+ Rails::Generator::Commands::Create.any_instance.expects(:file).with(
245
+ 'nested_attributes.js', 'public/javascripts/nested_attributes.js'
246
+ )
247
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('parents')
248
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
249
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
250
+
251
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
252
+ end
253
+
254
+ should "create the correct manifest when the scaffold_for_view generator is run with a valid child model" do
255
+
256
+ expect_no_warnings
257
+
258
+ directories = [
259
+ 'app/models/',
260
+ 'app/controllers/',
261
+ 'app/helpers/',
262
+ 'app/views/parents',
263
+ 'app/views/layouts/',
264
+ 'test/functional/',
265
+ 'test/unit/',
266
+ 'test/unit/helpers/',
267
+ 'test/fixtures/',
268
+ 'public/stylesheets/'
269
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
270
+
271
+ templates = {
272
+ 'view_index.html.erb' => 'app/views/parents/index.html.erb',
273
+ 'view_new.html.erb' => 'app/views/parents/new.html.erb',
274
+ 'view_edit.html.erb' => 'app/views/parents/edit.html.erb',
275
+ 'view_form.html.erb' => 'app/views/parents/_form.html.erb',
276
+ 'layout.html.erb' => 'app/views/layouts/parents.html.erb',
277
+ 'style.css' => 'public/stylesheets/scaffold.css',
278
+ 'controller.rb' => 'app/controllers/parents_controller.rb',
279
+ 'functional_test.rb' => 'test/functional/parents_controller_test.rb',
280
+ 'helper.rb' => 'app/helpers/parents_helper.rb',
281
+ 'helper_test.rb' => 'test/unit/helpers/parents_helper_test.rb',
282
+ 'model.rb' => 'app/models/parent.rb',
283
+ 'unit_test.rb' => 'test/unit/parent_test.rb',
284
+ 'fixtures.yml' => 'test/fixtures/parents.yml'
285
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
286
+
287
+ testy_model_info = ViewMapper::ModelInfo.new('testy')
288
+ parent_model_info = ViewMapper::ModelInfo.new('parent')
289
+ ViewMapper::ModelInfo.stubs(:new).with('testy').returns(testy_model_info)
290
+ ViewMapper::ModelInfo.stubs(:new).with('parent').returns(parent_model_info)
291
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
292
+ 'view_show.html.erb',
293
+ 'app/views/parents/show.html.erb',
294
+ { :assigns => { :child_models => [ testy_model_info ] } }
295
+ )
296
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
297
+ 'view_child_form.html.erb',
298
+ 'app/views/parents/_testy.html.erb',
299
+ { :assigns => { :child_model => testy_model_info } }
300
+ )
301
+ Rails::Generator::Commands::Create.any_instance.expects(:file).with(
302
+ 'nested_attributes.js', 'public/javascripts/nested_attributes.js'
303
+ )
304
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('parents')
305
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
306
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
307
+
308
+ Rails::Generator::Commands::Create.any_instance.expects(:migration_template).with(
309
+ 'migration.rb',
310
+ 'db/migrate',
311
+ :assigns => { :migration_name => "CreateParents" },
312
+ :migration_file_name => "create_parents"
313
+ )
314
+
315
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
316
+ end
317
+ end
318
+
319
+ context "A Rails generator script with a child model without a belongs_to association" do
320
+ setup do
321
+ setup_test_model
322
+ setup_parent_test_model(false, false)
323
+ @generator_script = Rails::Generator::Scripts::Generate.new
324
+ end
325
+
326
+ should "return a warning when run with view_for and not run any actions" do
327
+ expect_no_actions
328
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a belongs_to association for Parent.')
329
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
330
+ end
331
+
332
+ should "return a warning when run with scaffold_for_view and not run any actions" do
333
+ expect_no_actions
334
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a belongs_to association for Parent.')
335
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
336
+ end
337
+ end
338
+
339
+ context "A Rails generator script with a child model missing a foreign key" do
340
+ setup do
341
+ setup_test_model
342
+ setup_parent_test_model(false)
343
+ @generator_script = Rails::Generator::Scripts::Generate.new
344
+ end
345
+
346
+ should "return a warning when run with view_for and not run any actions" do
347
+ expect_no_actions
348
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a foreign key for Parent.')
349
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
350
+ end
351
+
352
+ should "return a warning when run with scaffold_for_view and not run any actions" do
353
+ expect_no_actions
354
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a foreign key for Parent.')
355
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
356
+ end
357
+ end
358
+
359
+ context "A Rails generator script with a child model that has a habtm association" do
360
+ setup do
361
+ setup_test_model
362
+ setup_parent_test_model(false, false)
363
+ Testy.class_eval do
364
+ has_and_belongs_to_many :parents
365
+ end
366
+ @generator_script = Rails::Generator::Scripts::Generate.new
367
+ end
368
+
369
+ should "not return a warning when run with view_for" do
370
+ stub_actions
371
+ expect_no_warnings
372
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
373
+ end
374
+
375
+ should "not return a warning when run with scaffold_for_view" do
376
+ stub_actions
377
+ expect_no_warnings
378
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
379
+ end
380
+ end
381
+
382
+ context "A Rails generator script with a child model that has_many (through) association" do
383
+ setup do
384
+ setup_test_model
385
+ setup_parent_test_model(false, false)
386
+ Testy.class_eval do
387
+ has_many :parents
388
+ end
389
+ @generator_script = Rails::Generator::Scripts::Generate.new
390
+ end
391
+
392
+ should "not return a warning when run with view_for" do
393
+ stub_actions
394
+ expect_no_warnings
395
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
396
+ end
397
+
398
+ should "not return a warning when run with scaffold_for_view" do
399
+ stub_actions
400
+ expect_no_warnings
401
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
402
+ end
403
+ end
404
+
405
+ end
@@ -0,0 +1,81 @@
1
+ require 'test_helper'
2
+
3
+ class ModelInfoTest < Test::Unit::TestCase
4
+
5
+ context "A model info object created for a test model" do
6
+ setup do
7
+ setup_test_model
8
+ @model_info = ViewMapper::ModelInfo.new('testy')
9
+ end
10
+
11
+ should "find the model" do
12
+ assert_equal Testy, @model_info.model
13
+ end
14
+
15
+ should "return the model's name" do
16
+ assert_equal 'Testy', @model_info.name
17
+ end
18
+
19
+ should "return the model's columns and not the primary key or time stamp columns" do
20
+ assert_equal [ 'first_name', 'last_name', 'address' ], @model_info.columns
21
+ end
22
+
23
+ should "return the model's Rails Generator attributes" do
24
+ attribs = @model_info.attributes
25
+ assert_kind_of Rails::Generator::GeneratedAttribute, attribs[0]
26
+ assert_kind_of Rails::Generator::GeneratedAttribute, attribs[1]
27
+ assert_kind_of Rails::Generator::GeneratedAttribute, attribs[2]
28
+ assert_equal 'first_name', attribs[0].name
29
+ assert_equal 'last_name', attribs[1].name
30
+ assert_equal 'address', attribs[2].name
31
+ end
32
+ end
33
+
34
+ context "Child and parent model info objects" do
35
+ setup do
36
+ setup_test_model
37
+ setup_parent_test_model
38
+ @child_model = ViewMapper::ModelInfo.new('testy')
39
+ @parent_model = ViewMapper::ModelInfo.new('parent')
40
+ end
41
+
42
+ should "not include the parent foreign key column in the child model's columns" do
43
+ assert_equal [ 'first_name', 'last_name', 'address' ], @child_model.columns
44
+ end
45
+
46
+ should "determine that the child model belongs to the parent model" do
47
+ assert_equal true, @child_model.belongs_to?('parent')
48
+ end
49
+
50
+ should "determine that the parent model has many child models" do
51
+ assert_equal true, @parent_model.has_many?('testies')
52
+ end
53
+ end
54
+
55
+ context "Two model info objects for models that in a habtm association" do
56
+ setup do
57
+ setup_test_model
58
+ setup_parent_test_model
59
+ Testy.class_eval do
60
+ has_and_belongs_to_many :parents
61
+ end
62
+ @child_model = ViewMapper::ModelInfo.new('testy')
63
+ @parent_model = ViewMapper::ModelInfo.new('parent')
64
+ end
65
+
66
+ should "determine that a habtm association exists" do
67
+ assert_equal true, @child_model.has_and_belongs_to_many?('parents')
68
+ end
69
+ end
70
+
71
+ context "A model info object created for a test model that has Paperclip attachments" do
72
+ setup do
73
+ setup_test_model(true)
74
+ @model_info = ViewMapper::ModelInfo.new('testy')
75
+ end
76
+
77
+ should "not include the Paperclip columns in the model's columns" do
78
+ assert_equal [ 'first_name', 'last_name', 'address' ], @model_info.columns
79
+ end
80
+ end
81
+ end
@@ -33,21 +33,14 @@ class PaperclipViewTest < Test::Unit::TestCase
33
33
  end
34
34
  end
35
35
 
36
- context "A view_for generator instantiated for a test model" do
36
+ context "A view_for generator instantiated for a test model missing Paperclip columns" do
37
37
  setup do
38
38
  setup_test_model(false)
39
39
  end
40
40
 
41
- context "with missing paperclip columns" do
42
- setup do
43
- Struct.new("FakeColumn", :name)
44
- @fake_cols = [ Struct::FakeColumn.new("first_name"), Struct::FakeColumn.new("last_name"), Struct::FakeColumn.new("address") ]
45
- end
46
-
47
- should "return an error message with a bad paperclip param" do
48
- Rails::Generator::Base.logger.expects('error').with('Column \'avatar_file_name\' does not exist. First run script/generate paperclip testy avatar.')
49
- new_generator_for_test_model('view_for', ['--view', 'paperclip:avatar'])
50
- end
41
+ should "return an error message with a bad paperclip param" do
42
+ Rails::Generator::Base.logger.expects('error').with('Column \'avatar_file_name\' does not exist. First run script/generate paperclip testy avatar.')
43
+ new_generator_for_test_model('view_for', ['--view', 'paperclip:avatar'])
51
44
  end
52
45
  end
53
46
 
@@ -140,22 +133,6 @@ class PaperclipViewTest < Test::Unit::TestCase
140
133
  assert_equal './test/../lib/view_mapper/paperclip_templates', @gen.source_root
141
134
  end
142
135
 
143
- should "have the proper built in columns" do
144
- assert_equal [ 'id',
145
- 'created_at',
146
- 'updated_at',
147
- 'avatar_file_name',
148
- 'avatar_content_type',
149
- 'avatar_file_size',
150
- 'avatar_updated_at',
151
- 'avatar2_file_name',
152
- 'avatar2_content_type',
153
- 'avatar2_file_size',
154
- 'avatar2_updated_at'
155
- ].sort,
156
- @gen.built_in_columns.sort
157
- end
158
-
159
136
  view_for_templates = %w{ new edit index show }
160
137
  view_for_templates.each do | template |
161
138
  should "render the #{template} template as expected" do
data/test/test_helper.rb CHANGED
@@ -44,8 +44,8 @@ def setup_test_table(paperclip_columns = false)
44
44
  end
45
45
  end
46
46
 
47
- def setup_test_model(missing_columns = false)
48
- setup_test_table(missing_columns)
47
+ def setup_test_model(paperclip_columns = false)
48
+ setup_test_table(paperclip_columns)
49
49
  Object.send(:remove_const, "Testy") rescue nil
50
50
  Object.const_set("Testy", Class.new(ActiveRecord::Base))
51
51
  Testy.class_eval do
@@ -57,6 +57,49 @@ def setup_test_model(missing_columns = false)
57
57
  Object.const_get("Testy")
58
58
  end
59
59
 
60
+ def setup_parent_test_model(create_foreign_key = true, child_belongs_to_parent = true)
61
+ ActiveRecord::Base.connection.create_table :parents, :force => true do |table|
62
+ table.column :name, :string
63
+ end
64
+ ActiveRecord::Base.connection.create_table :some_other_models, :force => true do |table|
65
+ table.column :name, :string
66
+ table.column :parent_id, :integer
67
+ end
68
+ ActiveRecord::Base.connection.add_column :testies, :parent_id, :integer unless !create_foreign_key
69
+ Object.send(:remove_const, "Parent") rescue nil
70
+ Object.const_set("Parent", Class.new(ActiveRecord::Base))
71
+ Object.send(:remove_const, "SomeOtherModel") rescue nil
72
+ Object.const_set("SomeOtherModel", Class.new(ActiveRecord::Base))
73
+ Parent.class_eval do
74
+ has_many :testies
75
+ has_many :some_other_model
76
+ def testies_attributes=
77
+ 'fake'
78
+ end
79
+ def some_other_models_attributes=
80
+ 'fake'
81
+ end
82
+ end
83
+ Testy.class_eval do
84
+ belongs_to :parent unless !child_belongs_to_parent
85
+ end
86
+ SomeOtherModel.class_eval do
87
+ belongs_to :parent
88
+ end
89
+ Object.const_get("Parent")
90
+ end
91
+
92
+ def setup_test_model_without_nested_attributes
93
+ ActiveRecord::Base.connection.create_table :third_models, :force => true do |table|
94
+ table.column :name, :string
95
+ end
96
+ Object.send(:remove_const, "ThirdModel") rescue nil
97
+ Object.const_set("ThirdModel", Class.new(ActiveRecord::Base))
98
+ Parent.class_eval do
99
+ has_many :third_model
100
+ end
101
+ end
102
+
60
103
  module MockPaperclip
61
104
  class << self
62
105
  def included(base)
@@ -73,19 +116,49 @@ class Rails::Generator::NamedBase
73
116
  public :attributes
74
117
  end
75
118
 
76
- def generator_cmd_line(gen, args)
119
+ def generator_cmd_line(gen, args, model)
77
120
  if gen == 'view_for'
78
- cmd_line = ['testy']
121
+ cmd_line = [model]
79
122
  else
80
- cmd_line = ['testy', 'first_name:string', 'last_name:string', 'address:string']
123
+ cmd_line = [model, 'first_name:string', 'last_name:string', 'address:string']
81
124
  end
82
125
  (cmd_line << args).flatten
83
126
  end
84
127
 
85
- def generator_script_cmd_line(gen, args)
86
- ([gen] << generator_cmd_line(gen, args)).flatten
128
+ def generator_script_cmd_line(gen, args, model = 'testy')
129
+ ([gen] << generator_cmd_line(gen, args, model)).flatten
130
+ end
131
+
132
+ def new_generator_for_test_model(gen, args, model = 'testy')
133
+ Rails::Generator::Base.instance(gen, generator_cmd_line(gen, args, model))
134
+ end
135
+
136
+ def expect_no_actions
137
+ Rails::Generator::Commands::Create.any_instance.expects(:directory).never
138
+ Rails::Generator::Commands::Create.any_instance.expects(:template).never
139
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).never
140
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
141
+ Rails::Generator::Commands::Create.any_instance.expects(:route).never
142
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
143
+ end
144
+
145
+ def expect_no_warnings
146
+ Rails::Generator::Base.logger.expects(:error).never
147
+ Rails::Generator::Base.logger.expects(:warning).never
148
+ Rails::Generator::Base.logger.expects(:route).never
149
+ end
150
+
151
+ def stub_actions
152
+ Rails::Generator::Commands::Create.any_instance.stubs(:directory)
153
+ Rails::Generator::Commands::Create.any_instance.stubs(:template)
154
+ Rails::Generator::Commands::Create.any_instance.stubs(:route_resources)
155
+ Rails::Generator::Commands::Create.any_instance.stubs(:file)
156
+ Rails::Generator::Commands::Create.any_instance.stubs(:route)
157
+ Rails::Generator::Commands::Create.any_instance.stubs(:dependency)
87
158
  end
88
159
 
89
- def new_generator_for_test_model(gen, args)
90
- Rails::Generator::Base.instance(gen, generator_cmd_line(gen, args))
160
+ def stub_warnings
161
+ Rails::Generator::Base.logger.stubs(:error)
162
+ Rails::Generator::Base.logger.stubs(:warning)
163
+ Rails::Generator::Base.logger.stubs(:route)
91
164
  end
@@ -4,7 +4,7 @@ class ViewMapperTest < Test::Unit::TestCase
4
4
 
5
5
  context "A rails generator script with a view option specified" do
6
6
  setup do
7
- @gen = Rails::Generator::Base.instance('fake', ['testy', 'name:string', '--view', 'fake'])
7
+ @gen = new_generator_for_test_model('fake', ['testy', 'name:string', '--view', 'fake'])
8
8
  end
9
9
 
10
10
  should "use the specified view" do
@@ -18,7 +18,7 @@ class ViewMapperTest < Test::Unit::TestCase
18
18
 
19
19
  context "A rails generator script with a view option and parameter specified" do
20
20
  setup do
21
- @gen = Rails::Generator::Base.instance('fake', ['testy', 'name:string', '--view', 'fake:value'])
21
+ @gen = new_generator_for_test_model('fake', ['testy', 'name:string', '--view', 'fake:value'])
22
22
  end
23
23
 
24
24
  should "pass the view parameter to the specified view" do