view_mapper 0.3.2 → 0.3.3
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.
- data/README.rdoc +3 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/view_mapper/belongs_to_parent_models.rb +130 -0
- data/lib/view_mapper/model_info.rb +2 -6
- data/lib/view_mapper/views/auto_complete/auto_complete_view.rb +2 -1
- data/lib/view_mapper/views/belongs_to/belongs_to_view.rb +1 -86
- data/lib/view_mapper/views/belongs_to/templates/{model.rb → model.erb} +2 -2
- data/lib/view_mapper/views/belongs_to/templates/view_form.html.erb +1 -1
- data/lib/view_mapper/views/belongs_to/templates/view_index.html.erb +1 -1
- data/lib/view_mapper/views/belongs_to/templates/view_show.html.erb +1 -1
- data/lib/view_mapper/views/belongs_to_auto_complete/belongs_to_auto_complete_view.rb +60 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/controller.rb +90 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/layout.html.erb +18 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/migration.rb +19 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/model.erb +16 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/view_edit.html.erb +11 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/view_form.html.erb +13 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/view_index.html.erb +30 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/view_new.html.erb +10 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/templates/view_show.html.erb +16 -0
- data/lib/view_mapper/views/paperclip/paperclip_view.rb +2 -1
- data/lib/view_mapper.rb +2 -0
- data/test/test_helper.rb +29 -7
- data/test/views/auto_complete/auto_complete_test.rb +1 -1
- data/test/views/belongs_to/belongs_to_test.rb +13 -10
- data/test/views/belongs_to/expected_templates/_form.html.erb +1 -1
- data/test/views/belongs_to/expected_templates/index.html.erb +1 -1
- data/test/views/belongs_to/expected_templates/show.html.erb +1 -1
- data/test/views/belongs_to/expected_templates/some_other_model.rb +2 -2
- data/test/views/belongs_to_auto_complete/belongs_to_auto_complete_test.rb +489 -0
- data/test/views/belongs_to_auto_complete/expected_templates/_form.html.erb +13 -0
- data/test/views/belongs_to_auto_complete/expected_templates/create_some_other_models.rb +18 -0
- data/test/views/belongs_to_auto_complete/expected_templates/edit.html.erb +11 -0
- data/test/views/belongs_to_auto_complete/expected_templates/expected_routes.rb +47 -0
- data/test/views/belongs_to_auto_complete/expected_templates/index.html.erb +24 -0
- data/test/views/belongs_to_auto_complete/expected_templates/new.html.erb +10 -0
- data/test/views/belongs_to_auto_complete/expected_templates/show.html.erb +17 -0
- data/test/views/belongs_to_auto_complete/expected_templates/some_other_model.rb +16 -0
- data/test/views/belongs_to_auto_complete/expected_templates/some_other_models.html.erb +18 -0
- data/test/views/belongs_to_auto_complete/expected_templates/some_other_models_controller.rb +89 -0
- data/test/views/belongs_to_auto_complete/expected_templates/standard_routes.rb +43 -0
- data/view_mapper.gemspec +35 -6
- metadata +33 -4
@@ -0,0 +1,489 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper'
|
2
|
+
|
3
|
+
class BelongsToAutoCompleteViewTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
attr_reader :singular_name
|
6
|
+
attr_reader :attributes
|
7
|
+
attr_reader :plural_name
|
8
|
+
attr_reader :parent_models
|
9
|
+
attr_reader :class_name
|
10
|
+
attr_reader :migration_name
|
11
|
+
attr_reader :table_name
|
12
|
+
attr_reader :options
|
13
|
+
attr_reader :controller_class_name
|
14
|
+
attr_reader :file_name
|
15
|
+
|
16
|
+
context "A view_for generator instantiated for a test model" do
|
17
|
+
setup do
|
18
|
+
setup_test_model
|
19
|
+
setup_parent_test_model
|
20
|
+
end
|
21
|
+
|
22
|
+
should "detect the existing parents when no parent is specified" do
|
23
|
+
gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to_auto_complete'], 'some_other_model')
|
24
|
+
parent_models = gen.parent_models
|
25
|
+
assert_equal 1, parent_models.size
|
26
|
+
assert_equal 'Parent', parent_models[0].name
|
27
|
+
end
|
28
|
+
|
29
|
+
should "use the specified parent if provided" do
|
30
|
+
gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to_auto_complete:parent'], 'some_other_model')
|
31
|
+
parent_models = gen.parent_models
|
32
|
+
assert_equal 1, parent_models.size
|
33
|
+
assert_equal 'Parent', parent_models[0].name
|
34
|
+
end
|
35
|
+
|
36
|
+
should "return an error message with a bad parent param" do
|
37
|
+
Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
|
38
|
+
new_generator_for_test_model('view_for', ['--view', 'belongs_to_auto_complete:blah'])
|
39
|
+
end
|
40
|
+
|
41
|
+
should "use 'name' as the default parent field" do
|
42
|
+
gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to_auto_complete:parent'], 'some_other_model')
|
43
|
+
assert_equal 'name', gen.field_for(ViewMapper::ModelInfo.new('parent'))
|
44
|
+
end
|
45
|
+
|
46
|
+
should "parse the parent model field" do
|
47
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method parent_first_name.')
|
48
|
+
gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to_auto_complete:parent[first_name]'], 'some_other_model')
|
49
|
+
assert_equal 'first_name', gen.field_for(ViewMapper::ModelInfo.new('parent'))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "A scaffold_for_view generator instantiated for a test model" do
|
54
|
+
setup do
|
55
|
+
setup_test_model
|
56
|
+
setup_parent_test_model
|
57
|
+
end
|
58
|
+
|
59
|
+
should "return a warning when run with scaffold_for_view when no belongs_to_auto_complete is specified and not run any actions" do
|
60
|
+
expect_no_actions
|
61
|
+
Rails::Generator::Base.logger.expects('error').with('No belongs_to association specified.')
|
62
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
63
|
+
@generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to_auto_complete']))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "A test model with no belongs_to associations" do
|
68
|
+
setup do
|
69
|
+
setup_test_model
|
70
|
+
setup_parent_test_model(true, false)
|
71
|
+
end
|
72
|
+
|
73
|
+
should "return a error when run with view_for and not run any actions" do
|
74
|
+
expect_no_actions
|
75
|
+
Rails::Generator::Base.logger.expects('error').with('No belongs_to associations exist in class Testy.')
|
76
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
77
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete']))
|
78
|
+
end
|
79
|
+
|
80
|
+
should "return a error when run with scaffold_for_view and not run any actions" do
|
81
|
+
expect_no_actions
|
82
|
+
Rails::Generator::Base.logger.expects('error').with('No belongs_to association specified.')
|
83
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
84
|
+
@generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to_auto_complete']))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "A test model with a belongs_to association for a model for which it does not have a name virtual attribute" do
|
89
|
+
setup do
|
90
|
+
setup_test_model
|
91
|
+
setup_parent_test_model
|
92
|
+
setup_second_parent_test_model(true, false)
|
93
|
+
end
|
94
|
+
|
95
|
+
should "return a warning and stop when the problem model is specified" do
|
96
|
+
expect_no_actions
|
97
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method second_parent_name.')
|
98
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
99
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:second_parent'], 'some_other_model'))
|
100
|
+
end
|
101
|
+
|
102
|
+
should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
|
103
|
+
stub_actions
|
104
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method second_parent_name.')
|
105
|
+
Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
|
106
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
107
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete'], 'some_other_model'))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "A test model with a belongs_to association for a model for which it does not have a name virtual attribute setter" do
|
112
|
+
setup do
|
113
|
+
setup_test_model
|
114
|
+
setup_parent_test_model
|
115
|
+
setup_second_parent_test_model(false, true)
|
116
|
+
end
|
117
|
+
|
118
|
+
should "return a warning and stop when the problem model is specified" do
|
119
|
+
expect_no_actions
|
120
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method second_parent_name=.')
|
121
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
122
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:second_parent'], 'some_other_model'))
|
123
|
+
end
|
124
|
+
|
125
|
+
should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
|
126
|
+
stub_actions
|
127
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method second_parent_name=.')
|
128
|
+
Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
|
129
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
130
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete'], 'some_other_model'))
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "A test model with a belongs_to association for a model that has some other virtual attribute missing" do
|
135
|
+
setup do
|
136
|
+
setup_test_model
|
137
|
+
setup_parent_test_model
|
138
|
+
end
|
139
|
+
|
140
|
+
should "return a warning and stop when the problem model is specified" do
|
141
|
+
expect_no_actions
|
142
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not have a method parent_first_name.')
|
143
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
144
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:parent[first_name]'], 'some_other_model'))
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "A test model with a belongs_to association for a model which does not have a name method or column" do
|
149
|
+
setup do
|
150
|
+
setup_test_model
|
151
|
+
setup_parent_test_model
|
152
|
+
setup_second_parent_test_model(true, true, true, false, false)
|
153
|
+
end
|
154
|
+
|
155
|
+
should "return a warning and stop when the problem model is specified" do
|
156
|
+
expect_no_actions
|
157
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SecondParent does not have a name attribute.')
|
158
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
159
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:second_parent'], 'some_other_model'))
|
160
|
+
end
|
161
|
+
|
162
|
+
should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
|
163
|
+
stub_actions
|
164
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SecondParent does not have a name attribute.')
|
165
|
+
Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
|
166
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
167
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete'], 'some_other_model'))
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
context "A test model with a belongs_to association for a model which does not have some other attribute or column" do
|
172
|
+
setup do
|
173
|
+
setup_test_model
|
174
|
+
setup_parent_test_model
|
175
|
+
ViewMapper::ModelInfo.any_instance.stubs(:has_method?).returns(:true)
|
176
|
+
end
|
177
|
+
|
178
|
+
should "return a warning and stop when the problem model is specified" do
|
179
|
+
expect_no_actions
|
180
|
+
Rails::Generator::Base.logger.expects('warning').with('Model Parent does not have a missing_field column.')
|
181
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
182
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:parent[missing_field]'], 'some_other_model'))
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context "A test model with a belongs_to association for a model for which it does not have a foreign key" do
|
187
|
+
setup do
|
188
|
+
setup_test_model
|
189
|
+
setup_parent_test_model
|
190
|
+
setup_second_parent_test_model(true, true, false)
|
191
|
+
end
|
192
|
+
|
193
|
+
should "return a warning and stop when the problem model is specified" do
|
194
|
+
expect_no_actions
|
195
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not contain a foreign key for SecondParent.')
|
196
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
197
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:second_parent'], 'some_other_model'))
|
198
|
+
end
|
199
|
+
|
200
|
+
should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
|
201
|
+
stub_actions
|
202
|
+
Rails::Generator::Base.logger.expects('warning').with('Model SomeOtherModel does not contain a foreign key for SecondParent.')
|
203
|
+
Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
|
204
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
205
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete'], 'some_other_model'))
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
context "A view_for generator instantiated for a test model with two belongs_to associations" do
|
210
|
+
setup do
|
211
|
+
setup_test_model
|
212
|
+
setup_parent_test_model
|
213
|
+
setup_second_parent_test_model
|
214
|
+
@gen = new_generator_for_test_model('view_for', ['--view', 'belongs_to_auto_complete:parent,second_parent[other_field]'], 'some_other_model')
|
215
|
+
end
|
216
|
+
|
217
|
+
should "return the proper source root" do
|
218
|
+
assert_equal File.expand_path(File.dirname(__FILE__) + '/../../..//lib/view_mapper/views/belongs_to_auto_complete/templates'), ViewMapper::BelongsToAutoCompleteView.source_root
|
219
|
+
end
|
220
|
+
|
221
|
+
view_for_templates = %w{ new edit index show }
|
222
|
+
view_for_templates.each do | template |
|
223
|
+
should "render the #{template} template as expected" do
|
224
|
+
@attributes = @gen.attributes
|
225
|
+
@singular_name = @gen.singular_name
|
226
|
+
@plural_name = @gen.plural_name
|
227
|
+
@parent_models = @gen.parent_models
|
228
|
+
template_file = File.open(@gen.source_path("view_#{template}.html.erb"))
|
229
|
+
result = ERB.new(template_file.read, nil, '-').result(binding)
|
230
|
+
expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/#{template}.html.erb"))
|
231
|
+
assert_equal expected_file.read, result
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
should "render the form template as expected" do
|
236
|
+
@attributes = @gen.attributes
|
237
|
+
@singular_name = @gen.singular_name
|
238
|
+
@plural_name = @gen.plural_name
|
239
|
+
@parent_models = @gen.parent_models
|
240
|
+
template_file = File.open(@gen.source_path("view_form.html.erb"))
|
241
|
+
result = ERB.new(template_file.read, nil, '-').result(binding)
|
242
|
+
expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_form.html.erb"))
|
243
|
+
assert_equal expected_file.read, result
|
244
|
+
end
|
245
|
+
|
246
|
+
should "render the layout template as expected" do
|
247
|
+
@controller_class_name = @gen.controller_class_name
|
248
|
+
template_file = File.open(@gen.source_path("layout.html.erb"))
|
249
|
+
result = ERB.new(template_file.read, nil, '-').result(binding)
|
250
|
+
expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/some_other_models.html.erb"))
|
251
|
+
assert_equal expected_file.read, result
|
252
|
+
end
|
253
|
+
|
254
|
+
should "render the controller template as expected" do
|
255
|
+
@controller_class_name = @gen.controller_class_name
|
256
|
+
@table_name = @gen.table_name
|
257
|
+
@class_name = @gen.class_name
|
258
|
+
@file_name = @gen.file_name
|
259
|
+
@controller_class_name = @gen.controller_class_name
|
260
|
+
@parent_models = @gen.parent_models
|
261
|
+
template_file = File.open(@gen.source_path("controller.rb"))
|
262
|
+
result = ERB.new(template_file.read, nil, '-').result(binding)
|
263
|
+
expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/some_other_models_controller.rb"))
|
264
|
+
assert_equal expected_file.read, result
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
context "A scaffold_for_view generator instantiated for a test model with two belongs_to associations" do
|
269
|
+
setup do
|
270
|
+
setup_test_model
|
271
|
+
setup_parent_test_model
|
272
|
+
setup_second_parent_test_model
|
273
|
+
@gen = new_generator_for_test_model('scaffold_for_view', ['--view', 'belongs_to_auto_complete:parent,second_parent[other_field]'], 'some_other_model')
|
274
|
+
end
|
275
|
+
|
276
|
+
should "render the model template as expected" do
|
277
|
+
@parent_models = @gen.parent_models
|
278
|
+
@class_name = @gen.class_name
|
279
|
+
@attributes = @gen.attributes
|
280
|
+
template_file = File.open(@gen.source_path("model.erb"))
|
281
|
+
result = ERB.new(template_file.read, nil, '-').result(binding)
|
282
|
+
expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/some_other_model.rb"))
|
283
|
+
assert_equal expected_file.read, result
|
284
|
+
end
|
285
|
+
|
286
|
+
should "render the migration template as expected" do
|
287
|
+
@class_name = @gen.class_name
|
288
|
+
@attributes = @gen.attributes
|
289
|
+
@migration_name = 'CreateSomeOtherModels'
|
290
|
+
@table_name = @gen.table_name
|
291
|
+
@parent_models = @gen.parent_models
|
292
|
+
@options = {}
|
293
|
+
template_file = File.open(@gen.source_path("migration.rb"))
|
294
|
+
result = ERB.new(template_file.read, nil, '-').result(binding)
|
295
|
+
expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/create_some_other_models.rb"))
|
296
|
+
assert_equal expected_file.read, result
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
context "A Rails generator script" do
|
301
|
+
setup do
|
302
|
+
setup_test_model
|
303
|
+
setup_parent_test_model
|
304
|
+
setup_second_parent_test_model
|
305
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
306
|
+
end
|
307
|
+
|
308
|
+
should "add the proper auto_complete route to routes.rb" do
|
309
|
+
Rails::Generator::Commands::Create.any_instance.stubs(:directory)
|
310
|
+
Rails::Generator::Commands::Create.any_instance.stubs(:template)
|
311
|
+
Rails::Generator::Commands::Create.any_instance.stubs(:route_resources)
|
312
|
+
Rails::Generator::Commands::Create.any_instance.stubs(:file)
|
313
|
+
Rails::Generator::Commands::Create.any_instance.stubs(:dependency)
|
314
|
+
Rails::Generator::Base.logger.stubs(:route)
|
315
|
+
|
316
|
+
expected_path = File.dirname(__FILE__) + '/expected_templates'
|
317
|
+
standard_routes_file = expected_path + '/standard_routes.rb'
|
318
|
+
expected_routes_file = expected_path + '/expected_routes.rb'
|
319
|
+
test_routes_file = expected_path + '/routes.rb'
|
320
|
+
ViewForGenerator.any_instance.stubs(:destination_path).returns test_routes_file
|
321
|
+
FileUtils.copy(standard_routes_file, test_routes_file)
|
322
|
+
Rails::Generator::Commands::Create.any_instance.stubs(:route_file).returns(test_routes_file)
|
323
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:parent,second_parent[other_field]'], 'some_other_model'))
|
324
|
+
assert_equal File.open(expected_routes_file).read, File.open(test_routes_file).read
|
325
|
+
File.delete(test_routes_file)
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
context "A Rails generator script" do
|
330
|
+
setup do
|
331
|
+
setup_test_model
|
332
|
+
setup_parent_test_model
|
333
|
+
setup_second_parent_test_model
|
334
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
335
|
+
end
|
336
|
+
|
337
|
+
should "return a warning when run with view_for on an invalid parent model and not run any actions" do
|
338
|
+
expect_no_actions
|
339
|
+
Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
|
340
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
341
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:blah'], 'some_other_model'))
|
342
|
+
end
|
343
|
+
|
344
|
+
should "create the correct manifest when the view_for generator is run with a valid parent model" do
|
345
|
+
|
346
|
+
expect_no_warnings
|
347
|
+
|
348
|
+
directories = [
|
349
|
+
'app/controllers/',
|
350
|
+
'app/helpers/',
|
351
|
+
'app/views/some_other_models',
|
352
|
+
'app/views/layouts/',
|
353
|
+
'test/functional/',
|
354
|
+
'test/unit/',
|
355
|
+
'test/unit/helpers/',
|
356
|
+
'public/stylesheets/'
|
357
|
+
].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
|
358
|
+
|
359
|
+
templates = {
|
360
|
+
'view_index.html.erb' => 'app/views/some_other_models/index.html.erb',
|
361
|
+
'view_new.html.erb' => 'app/views/some_other_models/new.html.erb',
|
362
|
+
'view_edit.html.erb' => 'app/views/some_other_models/edit.html.erb',
|
363
|
+
'view_show.html.erb' => 'app/views/some_other_models/show.html.erb',
|
364
|
+
'view_form.html.erb' => 'app/views/some_other_models/_form.html.erb',
|
365
|
+
'layout.html.erb' => 'app/views/layouts/some_other_models.html.erb',
|
366
|
+
'style.css' => 'public/stylesheets/scaffold.css',
|
367
|
+
'controller.rb' => 'app/controllers/some_other_models_controller.rb',
|
368
|
+
'functional_test.rb' => 'test/functional/some_other_models_controller_test.rb',
|
369
|
+
'helper.rb' => 'app/helpers/some_other_models_helper.rb',
|
370
|
+
'helper_test.rb' => 'test/unit/helpers/some_other_models_helper_test.rb'
|
371
|
+
}.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
|
372
|
+
|
373
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('some_other_models')
|
374
|
+
Rails::Generator::Commands::Create.any_instance.expects(:file).never
|
375
|
+
Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
|
376
|
+
|
377
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route).with(
|
378
|
+
:path => 'auto_complete_for_parent_name',
|
379
|
+
:name => 'connect',
|
380
|
+
:action => 'auto_complete_for_parent_name',
|
381
|
+
:controller => 'some_other_models')
|
382
|
+
|
383
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route).with(
|
384
|
+
:path => 'auto_complete_for_second_parent_other_field',
|
385
|
+
:name => 'connect',
|
386
|
+
:action => 'auto_complete_for_second_parent_other_field',
|
387
|
+
:controller => 'some_other_models')
|
388
|
+
|
389
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:parent,second_parent[other_field]'], 'some_other_model'))
|
390
|
+
end
|
391
|
+
|
392
|
+
should "create the correct manifest when the scaffold_for_view generator is run with a valid parent model" do
|
393
|
+
|
394
|
+
expect_no_warnings
|
395
|
+
|
396
|
+
directories = [
|
397
|
+
'app/models/',
|
398
|
+
'app/controllers/',
|
399
|
+
'app/helpers/',
|
400
|
+
'app/views/some_other_models',
|
401
|
+
'app/views/layouts/',
|
402
|
+
'test/functional/',
|
403
|
+
'test/unit/',
|
404
|
+
'test/unit/helpers/',
|
405
|
+
'test/fixtures/',
|
406
|
+
'public/stylesheets/'
|
407
|
+
].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
|
408
|
+
|
409
|
+
templates = {
|
410
|
+
'view_index.html.erb' => 'app/views/some_other_models/index.html.erb',
|
411
|
+
'view_new.html.erb' => 'app/views/some_other_models/new.html.erb',
|
412
|
+
'view_edit.html.erb' => 'app/views/some_other_models/edit.html.erb',
|
413
|
+
'view_show.html.erb' => 'app/views/some_other_models/show.html.erb',
|
414
|
+
'view_form.html.erb' => 'app/views/some_other_models/_form.html.erb',
|
415
|
+
'layout.html.erb' => 'app/views/layouts/some_other_models.html.erb',
|
416
|
+
'style.css' => 'public/stylesheets/scaffold.css',
|
417
|
+
'controller.rb' => 'app/controllers/some_other_models_controller.rb',
|
418
|
+
'functional_test.rb' => 'test/functional/some_other_models_controller_test.rb',
|
419
|
+
'helper.rb' => 'app/helpers/some_other_models_helper.rb',
|
420
|
+
'helper_test.rb' => 'test/unit/helpers/some_other_models_helper_test.rb',
|
421
|
+
'model.erb' => 'app/models/some_other_model.rb',
|
422
|
+
'unit_test.rb' => 'test/unit/some_other_model_test.rb',
|
423
|
+
'fixtures.yml' => 'test/fixtures/some_other_models.yml'
|
424
|
+
}.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
|
425
|
+
|
426
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('some_other_models')
|
427
|
+
Rails::Generator::Commands::Create.any_instance.expects(:file).never
|
428
|
+
Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
|
429
|
+
|
430
|
+
Rails::Generator::Commands::Create.any_instance.expects(:migration_template).with(
|
431
|
+
'migration.rb',
|
432
|
+
'db/migrate',
|
433
|
+
:assigns => { :migration_name => "CreateSomeOtherModels" },
|
434
|
+
:migration_file_name => "create_some_other_models"
|
435
|
+
)
|
436
|
+
|
437
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route).with(
|
438
|
+
:path => 'auto_complete_for_parent_name',
|
439
|
+
:name => 'connect',
|
440
|
+
:action => 'auto_complete_for_parent_name',
|
441
|
+
:controller => 'some_other_models')
|
442
|
+
|
443
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route).with(
|
444
|
+
:path => 'auto_complete_for_second_parent_other_field',
|
445
|
+
:name => 'connect',
|
446
|
+
:action => 'auto_complete_for_second_parent_other_field',
|
447
|
+
:controller => 'some_other_models')
|
448
|
+
|
449
|
+
@generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to_auto_complete:parent,second_parent[other_field]'], 'some_other_model'))
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
context "A Rails generator script with a parent model without a has_many association" do
|
454
|
+
setup do
|
455
|
+
setup_test_model
|
456
|
+
setup_parent_test_model(true, true, false)
|
457
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
458
|
+
end
|
459
|
+
|
460
|
+
should "return a warning when run with view_for and not run any actions" do
|
461
|
+
expect_no_actions
|
462
|
+
Rails::Generator::Base.logger.expects('warning').with('Model Parent does not contain a has_many association for SomeOtherModel.')
|
463
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:parent'], 'some_other_model'))
|
464
|
+
end
|
465
|
+
|
466
|
+
should "return a warning when run with scaffold_for_view and not run any actions" do
|
467
|
+
expect_no_actions
|
468
|
+
Rails::Generator::Base.logger.expects('warning').with('Model Parent does not contain a has_many association for SomeOtherModel.')
|
469
|
+
@generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'belongs_to_auto_complete:parent'], 'some_other_model'))
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
context "A Rails generator script" do
|
474
|
+
setup do
|
475
|
+
@generator_script = Rails::Generator::Scripts::Generate.new
|
476
|
+
end
|
477
|
+
|
478
|
+
should "return an error when run when the auto_complete plugin is not installed" do
|
479
|
+
expect_no_actions
|
480
|
+
Rails::Generator::Base.logger.expects('error').with('The auto_complete plugin does not appear to be installed.')
|
481
|
+
ActionController::Base.stubs(:methods).returns([])
|
482
|
+
@generator_script.run(generator_script_cmd_line('view_for', ['--view', 'belongs_to_auto_complete:parent'], 'some_other_model'))
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
486
|
+
def field_for(parent_model)
|
487
|
+
@gen.field_for(parent_model)
|
488
|
+
end
|
489
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<%= f.error_messages %>
|
2
|
+
<p>
|
3
|
+
<%= f.label :name %><br />
|
4
|
+
<%= f.text_field :name %>
|
5
|
+
</p>
|
6
|
+
<p>
|
7
|
+
Parent:<br />
|
8
|
+
<%= text_field_with_auto_complete :some_other_model, :parent_name, {}, { :method => :get, :url => '/auto_complete_for_parent_name', :param_name => 'parent[name]' } %>
|
9
|
+
</p>
|
10
|
+
<p>
|
11
|
+
Second Parent:<br />
|
12
|
+
<%= text_field_with_auto_complete :some_other_model, :second_parent_other_field, {}, { :method => :get, :url => '/auto_complete_for_second_parent_other_field', :param_name => 'second_parent[other_field]' } %>
|
13
|
+
</p>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CreateSomeOtherModels < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :some_other_models do |t|
|
4
|
+
t.string :first_name
|
5
|
+
t.string :last_name
|
6
|
+
t.string :address
|
7
|
+
t.boolean :some_flag
|
8
|
+
t.integer :parent_id
|
9
|
+
t.integer :second_parent_id
|
10
|
+
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :some_other_models
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<h1>Editing some_other_model</h1>
|
2
|
+
|
3
|
+
<% form_for(@some_other_model) do |f| %>
|
4
|
+
<%= render :partial => 'form', :locals => { :f => f } %>
|
5
|
+
<p>
|
6
|
+
<%= f.submit 'Update' %>
|
7
|
+
</p>
|
8
|
+
<% end %>
|
9
|
+
|
10
|
+
<%= link_to 'Show', @some_other_model %> |
|
11
|
+
<%= link_to 'Back', some_other_models_path %>
|
@@ -0,0 +1,47 @@
|
|
1
|
+
ActionController::Routing::Routes.draw do |map|
|
2
|
+
map.connect 'auto_complete_for_parent_name', :controller => 'some_other_models', :action => 'auto_complete_for_parent_name'
|
3
|
+
|
4
|
+
map.connect 'auto_complete_for_second_parent_other_field', :controller => 'some_other_models', :action => 'auto_complete_for_second_parent_other_field'
|
5
|
+
|
6
|
+
# The priority is based upon order of creation: first created -> highest priority.
|
7
|
+
|
8
|
+
# Sample of regular route:
|
9
|
+
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
|
10
|
+
# Keep in mind you can assign values other than :controller and :action
|
11
|
+
|
12
|
+
# Sample of named route:
|
13
|
+
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
|
14
|
+
# This route can be invoked with purchase_url(:id => product.id)
|
15
|
+
|
16
|
+
# Sample resource route (maps HTTP verbs to controller actions automatically):
|
17
|
+
# map.resources :products
|
18
|
+
|
19
|
+
# Sample resource route with options:
|
20
|
+
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
|
21
|
+
|
22
|
+
# Sample resource route with sub-resources:
|
23
|
+
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
|
24
|
+
|
25
|
+
# Sample resource route with more complex sub-resources
|
26
|
+
# map.resources :products do |products|
|
27
|
+
# products.resources :comments
|
28
|
+
# products.resources :sales, :collection => { :recent => :get }
|
29
|
+
# end
|
30
|
+
|
31
|
+
# Sample resource route within a namespace:
|
32
|
+
# map.namespace :admin do |admin|
|
33
|
+
# # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
|
34
|
+
# admin.resources :products
|
35
|
+
# end
|
36
|
+
|
37
|
+
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
|
38
|
+
# map.root :controller => "welcome"
|
39
|
+
|
40
|
+
# See how all your routes lay out with "rake routes"
|
41
|
+
|
42
|
+
# Install the default routes as the lowest priority.
|
43
|
+
# Note: These default routes make all actions in every controller accessible via GET requests. You should
|
44
|
+
# consider removing the them or commenting them out if you're using named routes and resources.
|
45
|
+
map.connect ':controller/:action/:id'
|
46
|
+
map.connect ':controller/:action/:id.:format'
|
47
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
<h1>Listing some_other_models</h1>
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<tr>
|
5
|
+
<th>Name</th>
|
6
|
+
<th>Parent</th>
|
7
|
+
<th>Second Parent</th>
|
8
|
+
</tr>
|
9
|
+
|
10
|
+
<% @some_other_models.each do |some_other_model| %>
|
11
|
+
<tr>
|
12
|
+
<td><%=h some_other_model.name %></td>
|
13
|
+
<td><%=h some_other_model.parent_name %></td>
|
14
|
+
<td><%=h some_other_model.second_parent_other_field %></td>
|
15
|
+
<td><%= link_to 'Show', some_other_model %></td>
|
16
|
+
<td><%= link_to 'Edit', edit_some_other_model_path(some_other_model) %></td>
|
17
|
+
<td><%= link_to 'Destroy', some_other_model, :confirm => 'Are you sure?', :method => :delete %></td>
|
18
|
+
</tr>
|
19
|
+
<% end %>
|
20
|
+
</table>
|
21
|
+
|
22
|
+
<br />
|
23
|
+
|
24
|
+
<%= link_to 'New some_other_model', new_some_other_model_path %>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<p>
|
2
|
+
<b>Name:</b>
|
3
|
+
<%=h @some_other_model.name %>
|
4
|
+
</p>
|
5
|
+
|
6
|
+
<p>
|
7
|
+
<b>Parent:</b>
|
8
|
+
<%=h @some_other_model.parent_name %>
|
9
|
+
</p>
|
10
|
+
|
11
|
+
<p>
|
12
|
+
<b>Second Parent:</b>
|
13
|
+
<%=h @some_other_model.second_parent_other_field %>
|
14
|
+
</p>
|
15
|
+
|
16
|
+
<%= link_to 'Edit', edit_some_other_model_path(@some_other_model) %> |
|
17
|
+
<%= link_to 'Back', some_other_models_path %>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class SomeOtherModel < ActiveRecord::Base
|
2
|
+
belongs_to :parent
|
3
|
+
belongs_to :second_parent
|
4
|
+
def parent_name
|
5
|
+
parent.name if parent
|
6
|
+
end
|
7
|
+
def parent_name=(name)
|
8
|
+
self.parent = Parent.find_by_name(name)
|
9
|
+
end
|
10
|
+
def second_parent_other_field
|
11
|
+
second_parent.other_field if second_parent
|
12
|
+
end
|
13
|
+
def second_parent_other_field=(other_field)
|
14
|
+
self.second_parent = SecondParent.find_by_other_field(other_field)
|
15
|
+
end
|
16
|
+
end
|