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
@@ -12,394 +12,436 @@ class HasManyViewTest < Test::Unit::TestCase
12
12
  attr_reader :table_name
13
13
  attr_reader :options
14
14
 
15
- context "A view_for generator instantiated for a test model" do
15
+ context "When a parent model that has two child models exists" do
16
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', 'some_flag' ], 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 [ 'name' ], child_models[1].columns
47
- assert_equal [ 'first_name', 'last_name', 'address', 'some_flag' ], child_models[0].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
17
+ has_two_child_models_code = <<SRC
18
+ has_many :child_models
19
+ has_many :second_child_models
20
+ def child_models_attributes=
21
+ 'fake'
22
+ end
23
+ def second_child_models_attributes=
24
+ 'fake'
25
+ end
26
+ SRC
27
+ ClassFactory :parent_model, :class_eval => has_two_child_models_code
28
+ ClassFactory :child_model
29
+ ClassFactory :second_child_model
30
+ end
31
+
32
+ context "A view_for generator instantiated for a test model" do
33
+
34
+ should "detect the existing child models when no child model is specified" do
35
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many'], 'parent_model')
36
+ child_models = gen.child_models
37
+ assert_equal 2, child_models.size
38
+ assert_equal 'ChildModel', child_models[0].name
39
+ assert_equal 'SecondChildModel', child_models[1].name
40
+ assert_equal [ 'name' ], child_models[0].columns
41
+ assert_equal [ 'first_name', 'last_name', 'address', 'some_flag' ], child_models[1].columns
42
+ end
74
43
 
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
44
+ should "use find the specified valid child model if provided" do
45
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many:child_models'], 'parent_model')
46
+ child_models = gen.child_models
47
+ assert_equal 'ChildModel', child_models[0].name
48
+ assert_equal 1, child_models.size
49
+ end
81
50
 
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
51
+ should "be able to parse two model names" do
52
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many:child_models,second_child_models'], 'parent_model')
53
+ child_models = gen.child_models
54
+ assert_equal 2, child_models.size
55
+ assert_equal 'ChildModel', child_models[0].name
56
+ assert_equal 'SecondChildModel', child_models[1].name
57
+ assert_equal [ 'name' ], child_models[0].columns
58
+ assert_equal [ 'first_name', 'last_name', 'address', 'some_flag' ], child_models[1].columns
59
+ end
89
60
 
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
61
+ should "return an error message with a bad child model param" do
62
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
63
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many:blah'], 'parent_model')
64
+ assert_equal [], gen.child_models
65
+ end
95
66
  end
96
67
 
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'))
68
+ context "A scaffold_for_view generator instantiated for a test model" do
69
+ should "return a warning when run with scaffold_for_view when no has_many is specified and not run any actions" do
70
+ expect_no_actions
71
+ Rails::Generator::Base.logger.expects('error').with('No has_many association specified.')
72
+ @generator_script = Rails::Generator::Scripts::Generate.new
73
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many'], 'parent_model'))
74
+ end
102
75
  end
103
76
 
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
77
+ context "A view_for generator instantiated for a test model with two has_many associations" do
78
+ setup do
79
+ @gen = new_generator_for_test_model('view_for', ['--view', 'has_many:child_models,second_child_models'], 'parent_model')
80
+ end
112
81
 
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
82
+ should "return the proper source root" do
83
+ assert_equal File.expand_path(File.dirname(__FILE__) + '/../../..//lib/view_mapper/views/has_many/templates'), ViewMapper::HasManyView.source_root
84
+ end
119
85
 
120
- should "return the proper source root" do
121
- assert_equal File.expand_path(File.dirname(__FILE__) + '/../../..//lib/view_mapper/views/has_many/templates'), ViewMapper::HasManyView.source_root
122
- end
86
+ view_for_templates = %w{ new edit show index }
87
+ view_for_templates.each do | template |
88
+ should "render the #{template} template as expected" do
89
+ @attributes = @gen.attributes
90
+ @singular_name = @gen.singular_name
91
+ @plural_name = @gen.plural_name
92
+ @child_models = @gen.child_models
93
+ template_file = File.open(@gen.source_path("view_#{template}.html.erb"))
94
+ result = ERB.new(template_file.read, nil, '-').result(binding)
95
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/#{template}.html.erb"))
96
+ assert_equal expected_file.read, result
97
+ end
98
+ end
123
99
 
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
100
+ should "render the form partial as expected" do
127
101
  @attributes = @gen.attributes
128
102
  @singular_name = @gen.singular_name
129
103
  @plural_name = @gen.plural_name
130
104
  @child_models = @gen.child_models
131
- template_file = File.open(@gen.source_path("view_#{template}.html.erb"))
105
+ template_file = File.open(@gen.source_path("view_form.html.erb"))
132
106
  result = ERB.new(template_file.read, nil, '-').result(binding)
133
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/#{template}.html.erb"))
107
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_form.html.erb"))
134
108
  assert_equal expected_file.read, result
135
109
  end
136
- end
137
110
 
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(@gen.source_path("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/_form.html.erb"))
146
- assert_equal expected_file.read, result
111
+ should "render the child model partial as expected" do
112
+ @child_model = @gen.child_models[1]
113
+ template_file = File.open(@gen.source_path("view_child_form.html.erb"))
114
+ result = ERB.new(template_file.read, nil, '-').result(binding)
115
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_second_child_model.html.erb"))
116
+ assert_equal expected_file.read, result
117
+ end
147
118
  end
148
119
 
149
- should "render the person partial as expected" do
150
- @child_model = @gen.child_models[1]
151
- template_file = File.open(@gen.source_path("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/_person.html.erb"))
154
- assert_equal expected_file.read, result
155
- end
156
- end
120
+ context "A scaffold_for_view generator instantiated for a test model with two has_many associations" do
121
+ setup do
122
+ @gen = new_generator_for_test_model('scaffold_for_view', ['--view', 'has_many:child_models,second_child_models'], 'parent_model')
123
+ end
157
124
 
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
125
+ should "render the model template as expected" do
126
+ @child_models = @gen.child_models
127
+ @class_name = @gen.class_name
128
+ @attributes = @gen.attributes
129
+ template_file = File.open(@gen.source_path("model.rb"))
130
+ result = ERB.new(template_file.read, nil, '-').result(binding)
131
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/parent_model.rb"))
132
+ assert_equal expected_file.read, result
133
+ end
164
134
 
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(@gen.source_path("model.rb"))
170
- result = ERB.new(template_file.read, nil, '-').result(binding)
171
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/parent.rb"))
172
- assert_equal expected_file.read, result
135
+ should "render the migration template as expected" do
136
+ @class_name = @gen.class_name
137
+ @attributes = @gen.attributes
138
+ @migration_name = 'CreateParentModels'
139
+ @table_name = @gen.table_name
140
+ @options = {}
141
+ template_file = File.open(@gen.source_path("migration.rb"))
142
+ result = ERB.new(template_file.read, nil, '-').result(binding)
143
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/create_parent_models.rb"))
144
+ assert_equal expected_file.read, result
145
+ end
173
146
  end
174
147
 
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(@gen.source_path("migration.rb"))
182
- result = ERB.new(template_file.read, nil, '-').result(binding)
183
- expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/create_parents.rb"))
184
- assert_equal expected_file.read, result
148
+ context "A Rails generator script" do
149
+ setup do
150
+ @generator_script = Rails::Generator::Scripts::Generate.new
151
+ end
152
+
153
+ should "return a warning when run with view_for on an invalid child model and not run any actions" do
154
+ expect_no_actions
155
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
156
+ @generator_script = Rails::Generator::Scripts::Generate.new
157
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:blah'], 'parent_model'))
158
+ end
159
+
160
+ should "create the correct manifest when the view_for generator is run with a valid child model" do
161
+
162
+ expect_no_warnings
163
+
164
+ directories = [
165
+ 'app/controllers/',
166
+ 'app/helpers/',
167
+ 'app/views/parent_models',
168
+ 'app/views/layouts/',
169
+ 'test/functional/',
170
+ 'test/unit/',
171
+ 'test/unit/helpers/',
172
+ 'public/stylesheets/'
173
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
174
+
175
+ templates = {
176
+ 'view_index.html.erb' => 'app/views/parent_models/index.html.erb',
177
+ 'view_new.html.erb' => 'app/views/parent_models/new.html.erb',
178
+ 'view_edit.html.erb' => 'app/views/parent_models/edit.html.erb',
179
+ 'view_form.html.erb' => 'app/views/parent_models/_form.html.erb',
180
+ 'layout.html.erb' => 'app/views/layouts/parent_models.html.erb',
181
+ 'style.css' => 'public/stylesheets/scaffold.css',
182
+ 'controller.rb' => 'app/controllers/parent_models_controller.rb',
183
+ 'functional_test.rb' => 'test/functional/parent_models_controller_test.rb',
184
+ 'helper.rb' => 'app/helpers/parent_models_helper.rb',
185
+ 'helper_test.rb' => 'test/unit/helpers/parent_models_helper_test.rb'
186
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
187
+
188
+ child_model_model_info = ViewMapper::ModelInfo.new('child_model')
189
+ parent_model_info = ViewMapper::ModelInfo.new('parent_model')
190
+ ViewMapper::ModelInfo.stubs(:new).with('child_model').returns(child_model_model_info)
191
+ ViewMapper::ModelInfo.stubs(:new).with('parent_model').returns(parent_model_info)
192
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
193
+ 'view_show.html.erb',
194
+ 'app/views/parent_models/show.html.erb',
195
+ { :assigns => { :child_models => [ child_model_model_info ] } }
196
+ )
197
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
198
+ 'view_child_form.html.erb',
199
+ 'app/views/parent_models/_child_model.html.erb',
200
+ { :assigns => { :child_model => child_model_model_info } }
201
+ )
202
+ Rails::Generator::Commands::Create.any_instance.expects(:file).with(
203
+ 'nested_attributes.js', 'public/javascripts/nested_attributes.js'
204
+ )
205
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('parent_models')
206
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
207
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
208
+
209
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:child_models'], 'parent_model'))
210
+ end
211
+
212
+ should "create the correct manifest when the scaffold_for_view generator is run with a valid child model" do
213
+
214
+ expect_no_warnings
215
+
216
+ directories = [
217
+ 'app/models/',
218
+ 'app/controllers/',
219
+ 'app/helpers/',
220
+ 'app/views/parent_models',
221
+ 'app/views/layouts/',
222
+ 'test/functional/',
223
+ 'test/unit/',
224
+ 'test/unit/helpers/',
225
+ 'test/fixtures/',
226
+ 'public/stylesheets/'
227
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
228
+
229
+ templates = {
230
+ 'view_index.html.erb' => 'app/views/parent_models/index.html.erb',
231
+ 'view_new.html.erb' => 'app/views/parent_models/new.html.erb',
232
+ 'view_edit.html.erb' => 'app/views/parent_models/edit.html.erb',
233
+ 'view_form.html.erb' => 'app/views/parent_models/_form.html.erb',
234
+ 'layout.html.erb' => 'app/views/layouts/parent_models.html.erb',
235
+ 'style.css' => 'public/stylesheets/scaffold.css',
236
+ 'controller.rb' => 'app/controllers/parent_models_controller.rb',
237
+ 'functional_test.rb' => 'test/functional/parent_models_controller_test.rb',
238
+ 'helper.rb' => 'app/helpers/parent_models_helper.rb',
239
+ 'helper_test.rb' => 'test/unit/helpers/parent_models_helper_test.rb',
240
+ 'model.rb' => 'app/models/parent_model.rb',
241
+ 'unit_test.rb' => 'test/unit/parent_model_test.rb',
242
+ 'fixtures.yml' => 'test/fixtures/parent_models.yml'
243
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
244
+
245
+ child_model_model_info = ViewMapper::ModelInfo.new('child_model')
246
+ parent_model_model_info = ViewMapper::ModelInfo.new('parent_model')
247
+ ViewMapper::ModelInfo.stubs(:new).with('child_model').returns(child_model_model_info)
248
+ ViewMapper::ModelInfo.stubs(:new).with('parent_model').returns(parent_model_model_info)
249
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
250
+ 'view_show.html.erb',
251
+ 'app/views/parent_models/show.html.erb',
252
+ { :assigns => { :child_models => [ child_model_model_info ] } }
253
+ )
254
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
255
+ 'view_child_form.html.erb',
256
+ 'app/views/parent_models/_child_model.html.erb',
257
+ { :assigns => { :child_model => child_model_model_info } }
258
+ )
259
+ Rails::Generator::Commands::Create.any_instance.expects(:file).with(
260
+ 'nested_attributes.js', 'public/javascripts/nested_attributes.js'
261
+ )
262
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('parent_models')
263
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
264
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
265
+
266
+ Rails::Generator::Commands::Create.any_instance.expects(:migration_template).with(
267
+ 'migration.rb',
268
+ 'db/migrate',
269
+ :assigns => { :migration_name => "CreateParentModels" },
270
+ :migration_file_name => "create_parent_models"
271
+ )
272
+
273
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:child_models'], 'parent_model'))
274
+ end
185
275
  end
186
276
  end
187
277
 
188
- context "A Rails generator script" do
278
+ context "A test model with no has many associations" do
189
279
  setup do
190
- setup_test_model
191
- setup_parent_test_model
192
- @generator_script = Rails::Generator::Scripts::Generate.new
280
+ ClassFactory :test_model
193
281
  end
194
282
 
195
- should "return a warning when run with view_for on an invalid child model and not run any actions" do
283
+ should "return a error when run with view_for and not run any actions" do
196
284
  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.')
285
+ Rails::Generator::Base.logger.expects('error').with('No has_many associations exist in class TestModel.')
198
286
  @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'))
287
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many']))
252
288
  end
253
289
 
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'))
290
+ should "return a error when run with scaffold_for_view and not run any actions" do
291
+ expect_no_actions
292
+ Rails::Generator::Base.logger.expects('error').with('No has_many association specified.')
293
+ @generator_script = Rails::Generator::Scripts::Generate.new
294
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many']))
316
295
  end
317
296
  end
318
297
 
319
298
  context "A Rails generator script with a child model without a belongs_to association" do
320
299
  setup do
321
- setup_test_model
322
- setup_parent_test_model(false, false)
300
+ has_child_model_code = <<SRC
301
+ has_many :child_models
302
+ def child_models_attributes=
303
+ 'fake'
304
+ end
305
+ SRC
306
+ ClassFactory :parent_model, :class_eval => has_child_model_code
307
+
308
+ missing_belongs_to_code = <<END
309
+ def parent_model_name
310
+ 'something'
311
+ end
312
+ def parent_model_name=
313
+ end
314
+ END
315
+ ClassFactory :child_model, :class_eval => missing_belongs_to_code do |child|
316
+ child.string :name
317
+ child.integer :parent_model_id
318
+ end
319
+
323
320
  @generator_script = Rails::Generator::Scripts::Generate.new
324
321
  end
325
322
 
326
323
  should "return a warning when run with view_for and not run any actions" do
327
324
  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'))
325
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not contain a belongs_to association for ParentModel.')
326
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:child_models'], 'parent_model'))
330
327
  end
331
328
 
332
329
  should "return a warning when run with scaffold_for_view and not run any actions" do
333
330
  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'))
331
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not contain a belongs_to association for ParentModel.')
332
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:child_models'], 'parent_model'))
336
333
  end
337
334
  end
338
335
 
339
336
  context "A Rails generator script with a child model missing a foreign key" do
340
337
  setup do
341
- setup_test_model
342
- setup_parent_test_model(false)
338
+ has_child_model_code = <<SRC
339
+ has_many :child_models
340
+ def child_models_attributes=
341
+ 'fake'
342
+ end
343
+ SRC
344
+ ClassFactory :parent_model, :class_eval => has_child_model_code
345
+ ClassFactory :child_model, :class_eval => 'belongs_to :parent_model' do |child|
346
+ child.string :name
347
+ end
348
+
343
349
  @generator_script = Rails::Generator::Scripts::Generate.new
344
350
  end
345
351
 
346
352
  should "return a warning when run with view_for and not run any actions" do
347
353
  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'))
354
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not contain a foreign key for ParentModel.')
355
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:child_models'], 'parent_model'))
350
356
  end
351
357
 
352
358
  should "return a warning when run with scaffold_for_view and not run any actions" do
353
359
  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'))
360
+ Rails::Generator::Base.logger.expects('warning').with('Model ChildModel does not contain a foreign key for ParentModel.')
361
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:child_models'], 'parent_model'))
356
362
  end
357
363
  end
358
364
 
359
365
  context "A Rails generator script with a child model that has a habtm association" do
360
366
  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
367
+ has_two_child_models_code = <<SRC
368
+ has_many :child_models
369
+ def child_models_attributes=
370
+ 'fake'
371
+ end
372
+ SRC
373
+ ClassFactory :parent_model, :class_eval => has_two_child_models_code
374
+ ClassFactory :child_model, :class_eval => 'has_and_belongs_to_many :parent_models'
366
375
  @generator_script = Rails::Generator::Scripts::Generate.new
367
376
  end
368
377
 
369
378
  should "not return a warning when run with view_for" do
370
379
  stub_actions
371
380
  expect_no_warnings
372
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
381
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:child_models'], 'parent_model'))
373
382
  end
374
383
 
375
384
  should "not return a warning when run with scaffold_for_view" do
376
385
  stub_actions
377
386
  expect_no_warnings
378
- @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
387
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:child_models'], 'parent_model'))
379
388
  end
380
389
  end
381
390
 
382
391
  context "A Rails generator script with a child model that has_many (through) association" do
383
392
  setup do
384
- setup_test_model
385
- setup_parent_test_model(false, false)
386
- Testy.class_eval do
387
- has_many :parents
388
- end
393
+ has_two_child_models_code = <<SRC
394
+ has_many :child_models
395
+ def child_models_attributes=
396
+ 'fake'
397
+ end
398
+ SRC
399
+ ClassFactory :parent_model, :class_eval => has_two_child_models_code
400
+ ClassFactory :child_model, :class_eval => 'has_many :parent_models'
389
401
  @generator_script = Rails::Generator::Scripts::Generate.new
390
402
  end
391
403
 
392
404
  should "not return a warning when run with view_for" do
393
405
  stub_actions
394
406
  expect_no_warnings
395
- @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:testies'], 'parent'))
407
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:child_models'], 'parent_model'))
396
408
  end
397
409
 
398
410
  should "not return a warning when run with scaffold_for_view" do
399
411
  stub_actions
400
412
  expect_no_warnings
401
- @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:testies'], 'parent'))
413
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many:child_models'], 'parent_model'))
402
414
  end
403
415
  end
404
416
 
417
+ context "A test model with a has_many association for a model for which it does not accept nested attributes" do
418
+ setup do
419
+ missing_nested_attribute_code = <<SRC
420
+ has_many :child_models
421
+ has_many :second_child_models
422
+ def second_child_models_attributes=
423
+ 'fake'
405
424
  end
425
+ SRC
426
+ ClassFactory :parent_model, :class_eval => missing_nested_attribute_code
427
+ ClassFactory :child_model
428
+ ClassFactory :second_child_model
429
+ end
430
+
431
+ should "return a warning and stop when the problem model is specified" do
432
+ expect_no_actions
433
+ Rails::Generator::Base.logger.expects('warning').with('Model ParentModel does not accept nested attributes for model ChildModel.')
434
+ @generator_script = Rails::Generator::Scripts::Generate.new
435
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many:child_models'], 'parent_model'))
436
+ end
437
+
438
+ should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
439
+ stub_actions
440
+ Rails::Generator::Base.logger.expects('warning').with('Model ParentModel does not accept nested attributes for model ChildModel.')
441
+ Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
442
+ @generator_script = Rails::Generator::Scripts::Generate.new
443
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many'], 'parent_model'))
444
+ end
445
+ end
446
+
447
+ end