repeated_auto_complete 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/.gitignore +21 -0
  2. data/README +102 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/init.rb +3 -0
  6. data/lib/auto_complete.rb +49 -0
  7. data/lib/auto_complete_form_builder_helper.rb +38 -0
  8. data/lib/auto_complete_macros_helper.rb +152 -0
  9. data/lib/repeated_auto_complete.rb +4 -0
  10. data/lib/view_mapper/README +4 -0
  11. data/lib/view_mapper/has_many_auto_complete_view.rb +88 -0
  12. data/lib/view_mapper/templates/controller.rb +90 -0
  13. data/lib/view_mapper/templates/layout.html.erb +19 -0
  14. data/lib/view_mapper/templates/style.css +82 -0
  15. data/lib/view_mapper/templates/view_child_form.html.erb +16 -0
  16. data/lib/view_mapper/templates/view_form.html.erb +24 -0
  17. data/rails/init.rb +3 -0
  18. data/repeated_auto_complete.gemspec +78 -0
  19. data/test/auto_complete_form_builder_helper_test.rb +123 -0
  20. data/test/auto_complete_nested_attributes_test.rb +143 -0
  21. data/test/auto_complete_test.rb +136 -0
  22. data/test/helper.rb +14 -0
  23. data/test/view_mapper/expected_templates/_form.html.erb +26 -0
  24. data/test/view_mapper/expected_templates/_person.html.erb +22 -0
  25. data/test/view_mapper/expected_templates/create_parents.rb +16 -0
  26. data/test/view_mapper/expected_templates/edit.html.erb +11 -0
  27. data/test/view_mapper/expected_templates/index.html.erb +20 -0
  28. data/test/view_mapper/expected_templates/new.html.erb +10 -0
  29. data/test/view_mapper/expected_templates/parent.rb +15 -0
  30. data/test/view_mapper/expected_templates/show.html.erb +37 -0
  31. data/test/view_mapper/expected_templates/testies_controller.rb +92 -0
  32. data/test/view_mapper/has_many_auto_complete_view_test.rb +587 -0
  33. metadata +93 -0
@@ -0,0 +1,92 @@
1
+ class TestiesController < ApplicationController
2
+
3
+ auto_complete_for :testy, :first_name
4
+ auto_complete_for :testy, :last_name
5
+ auto_complete_for :testy, :address
6
+ auto_complete_for :child1, :field1
7
+ auto_complete_for :child1, :field2
8
+
9
+ # GET /testies
10
+ # GET /testies.xml
11
+ def index
12
+ @testies = Testy.all
13
+
14
+ respond_to do |format|
15
+ format.html # index.html.erb
16
+ format.xml { render :xml => @testies }
17
+ end
18
+ end
19
+
20
+ # GET /testies/1
21
+ # GET /testies/1.xml
22
+ def show
23
+ @testy = Testy.find(params[:id])
24
+
25
+ respond_to do |format|
26
+ format.html # show.html.erb
27
+ format.xml { render :xml => @testy }
28
+ end
29
+ end
30
+
31
+ # GET /testies/new
32
+ # GET /testies/new.xml
33
+ def new
34
+ @testy = Testy.new
35
+
36
+ respond_to do |format|
37
+ format.html # new.html.erb
38
+ format.xml { render :xml => @testy }
39
+ end
40
+ end
41
+
42
+ # GET /testies/1/edit
43
+ def edit
44
+ @testy = Testy.find(params[:id])
45
+ end
46
+
47
+ # POST /testies
48
+ # POST /testies.xml
49
+ def create
50
+ @testy = Testy.new(params[:testy])
51
+
52
+ respond_to do |format|
53
+ if @testy.save
54
+ flash[:notice] = 'Testy was successfully created.'
55
+ format.html { redirect_to(@testy) }
56
+ format.xml { render :xml => @testy, :status => :created, :location => @testy }
57
+ else
58
+ format.html { render :action => "new" }
59
+ format.xml { render :xml => @testy.errors, :status => :unprocessable_entity }
60
+ end
61
+ end
62
+ end
63
+
64
+ # PUT /testies/1
65
+ # PUT /testies/1.xml
66
+ def update
67
+ @testy = Testy.find(params[:id])
68
+
69
+ respond_to do |format|
70
+ if @testy.update_attributes(params[:testy])
71
+ flash[:notice] = 'Testy was successfully updated.'
72
+ format.html { redirect_to(@testy) }
73
+ format.xml { head :ok }
74
+ else
75
+ format.html { render :action => "edit" }
76
+ format.xml { render :xml => @testy.errors, :status => :unprocessable_entity }
77
+ end
78
+ end
79
+ end
80
+
81
+ # DELETE /testies/1
82
+ # DELETE /testies/1.xml
83
+ def destroy
84
+ @testy = Testy.find(params[:id])
85
+ @testy.destroy
86
+
87
+ respond_to do |format|
88
+ format.html { redirect_to(testies_url) }
89
+ format.xml { head :ok }
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,587 @@
1
+ require 'helper'
2
+ require 'shoulda'
3
+ require 'mocha'
4
+ require 'rails_generator'
5
+ require 'rails_generator/scripts'
6
+ require 'rails_generator/scripts/generate'
7
+ require 'view_mapper'
8
+
9
+ def setup_test_table
10
+ ActiveRecord::Base.connection.create_table :testies, :force => true do |table|
11
+ table.column :first_name, :string
12
+ table.column :last_name, :string
13
+ table.column :address, :string
14
+ table.column :some_flag, :boolean
15
+ end
16
+ end
17
+
18
+ def setup_test_model
19
+ setup_test_table
20
+ Object.send(:remove_const, "Testy") rescue nil
21
+ Object.const_set("Testy", Class.new(ActiveRecord::Base))
22
+ Object.const_get("Testy")
23
+ end
24
+
25
+ def setup_parent_test_model(create_foreign_key = true, child_belongs_to_parent = true)
26
+ ActiveRecord::Base.connection.create_table :parents, :force => true do |table|
27
+ table.column :name, :string
28
+ end
29
+ ActiveRecord::Base.connection.create_table :some_other_models, :force => true do |table|
30
+ table.column :name, :string
31
+ table.column :parent_id, :integer
32
+ end
33
+ ActiveRecord::Base.connection.add_column :testies, :parent_id, :integer unless !create_foreign_key
34
+ Object.send(:remove_const, "Parent") rescue nil
35
+ Object.const_set("Parent", Class.new(ActiveRecord::Base))
36
+ Object.send(:remove_const, "SomeOtherModel") rescue nil
37
+ Object.const_set("SomeOtherModel", Class.new(ActiveRecord::Base))
38
+ Parent.class_eval do
39
+ has_many :testies
40
+ has_many :some_other_model
41
+ def testies_attributes=
42
+ 'fake'
43
+ end
44
+ def some_other_models_attributes=
45
+ 'fake'
46
+ end
47
+ end
48
+ Testy.class_eval do
49
+ belongs_to :parent unless !child_belongs_to_parent
50
+ end
51
+ SomeOtherModel.class_eval do
52
+ belongs_to :parent
53
+ end
54
+ Object.const_get("Parent")
55
+ end
56
+
57
+ def setup_test_model_without_nested_attributes
58
+ ActiveRecord::Base.connection.create_table :third_models, :force => true do |table|
59
+ table.column :name, :string
60
+ end
61
+ Object.send(:remove_const, "ThirdModel") rescue nil
62
+ Object.const_set("ThirdModel", Class.new(ActiveRecord::Base))
63
+ Parent.class_eval do
64
+ has_many :third_model
65
+ end
66
+ end
67
+
68
+ class Rails::Generator::NamedBase
69
+ public :attributes
70
+ end
71
+
72
+ def generator_cmd_line(gen, args, model)
73
+ if gen == 'view_for'
74
+ cmd_line = [model]
75
+ else
76
+ cmd_line = [model, 'first_name:string', 'last_name:string', 'address:string', 'some_flag:boolean']
77
+ end
78
+ (cmd_line << args).flatten
79
+ end
80
+
81
+ def generator_script_cmd_line(gen, args, model = 'testy')
82
+ ([gen] << generator_cmd_line(gen, args, model)).flatten
83
+ end
84
+
85
+ def new_generator_for_test_model(gen, args, model = 'testy')
86
+ Rails::Generator::Base.instance(gen, generator_cmd_line(gen, args, model))
87
+ end
88
+
89
+ def expect_no_actions
90
+ Rails::Generator::Commands::Create.any_instance.expects(:directory).never
91
+ Rails::Generator::Commands::Create.any_instance.expects(:template).never
92
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).never
93
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
94
+ Rails::Generator::Commands::Create.any_instance.expects(:route).never
95
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
96
+ end
97
+
98
+ def expect_no_warnings
99
+ Rails::Generator::Base.logger.expects(:error).never
100
+ Rails::Generator::Base.logger.expects(:warning).never
101
+ Rails::Generator::Base.logger.expects(:route).never
102
+ end
103
+
104
+ def stub_actions
105
+ Rails::Generator::Commands::Create.any_instance.stubs(:directory)
106
+ Rails::Generator::Commands::Create.any_instance.stubs(:template)
107
+ Rails::Generator::Commands::Create.any_instance.stubs(:route_resources)
108
+ Rails::Generator::Commands::Create.any_instance.stubs(:file)
109
+ Rails::Generator::Commands::Create.any_instance.stubs(:route)
110
+ Rails::Generator::Commands::Create.any_instance.stubs(:dependency)
111
+ end
112
+
113
+ def stub_warnings
114
+ Rails::Generator::Base.logger.stubs(:error)
115
+ Rails::Generator::Base.logger.stubs(:warning)
116
+ Rails::Generator::Base.logger.stubs(:route)
117
+ end
118
+
119
+ def is_auto_complete_attribute?(model_name, text_field)
120
+ (model_name == 'parent' && text_field == 'name') ||
121
+ (model_name == 'testy' && (text_field == 'first_name' || text_field == 'last_name' || text_field == 'address'))
122
+ end
123
+
124
+ class HasManyAutoCompleteViewTest < Test::Unit::TestCase
125
+
126
+ attr_reader :singular_name
127
+ attr_reader :attributes
128
+ attr_reader :plural_name
129
+ attr_reader :child_models
130
+ attr_reader :child_model
131
+ attr_reader :class_name
132
+ attr_reader :migration_name
133
+ attr_reader :table_name
134
+ attr_reader :options
135
+
136
+ context "A view_for generator instantiated for a test model" do
137
+ setup do
138
+ setup_test_model
139
+ setup_parent_test_model
140
+ setup_test_model_without_nested_attributes
141
+ end
142
+
143
+ should "detect the existing child models when no child model is specified" do
144
+ Rails::Generator::Base.logger.expects('warning').with('Model Parent does not accept nested attributes for model ThirdModel.')
145
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many_auto_complete'], 'parent')
146
+ child_models = gen.child_models
147
+ assert_equal 2, child_models.size
148
+ assert_equal 'SomeOtherModel', child_models[0].name
149
+ assert_equal 'Testy', child_models[1].name
150
+ assert_equal [ 'name' ], child_models[0].columns
151
+ assert_equal [ 'first_name', 'last_name', 'address', 'some_flag' ], child_models[1].columns
152
+ end
153
+
154
+ should "find the specified valid child model if provided" do
155
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many_auto_complete:testies'], 'parent')
156
+ child_models = gen.child_models
157
+ assert_equal 'Testy', gen.child_models[0].name
158
+ assert_equal 1, gen.child_models.size
159
+ end
160
+
161
+ should "be able to parse two model names" do
162
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many_auto_complete:testies,some_other_models'], 'parent')
163
+ child_models = gen.child_models
164
+ assert_equal 2, gen.child_models.size
165
+ assert_equal 'Testy', child_models[0].name
166
+ assert_equal 'SomeOtherModel', child_models[1].name
167
+ assert_equal [ 'name' ], child_models[1].columns
168
+ assert_equal [ 'first_name', 'last_name', 'address', 'some_flag' ], child_models[0].columns
169
+ end
170
+
171
+ should "return an error message with a bad child model param" do
172
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
173
+ gen = new_generator_for_test_model('view_for', ['--view', 'has_many_auto_complete:blah'], 'parent')
174
+ assert_equal [], gen.child_models
175
+ end
176
+ end
177
+
178
+ context "A scaffold_for_view generator instantiated for a test model" do
179
+ setup do
180
+ setup_test_model
181
+ end
182
+
183
+ should "return a warning when run with scaffold_for_view when no has_many is specified and not run any actions" do
184
+ expect_no_actions
185
+ Rails::Generator::Base.logger.expects('error').with('No has_many association specified.')
186
+ @generator_script = Rails::Generator::Scripts::Generate.new
187
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete'], 'parent'))
188
+ end
189
+ end
190
+
191
+ context "A test model with no has many associations" do
192
+ setup do
193
+ setup_test_model
194
+ end
195
+
196
+ should "return a error when run with view_for and not run any actions" do
197
+ expect_no_actions
198
+ Rails::Generator::Base.logger.expects('error').with('No has_many associations exist in class Testy.')
199
+ @generator_script = Rails::Generator::Scripts::Generate.new
200
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete']))
201
+ end
202
+
203
+ should "return a error when run with scaffold_for_view and not run any actions" do
204
+ expect_no_actions
205
+ Rails::Generator::Base.logger.expects('error').with('No has_many association specified.')
206
+ @generator_script = Rails::Generator::Scripts::Generate.new
207
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete']))
208
+ end
209
+ end
210
+
211
+ context "A test model with a has_many association for a model for which it does not accept nested attributes" do
212
+ setup do
213
+ setup_test_model
214
+ setup_parent_test_model
215
+ setup_test_model_without_nested_attributes
216
+ end
217
+
218
+ should "return a warning and stop when the problem model is specified" do
219
+ expect_no_actions
220
+ Rails::Generator::Base.logger.expects('warning').with('Model Parent does not accept nested attributes for model ThirdModel.')
221
+ @generator_script = Rails::Generator::Scripts::Generate.new
222
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:third_models'], 'parent'))
223
+ end
224
+
225
+ should "return a warning and not include the problem model when run with view_for but continue to run for other models" do
226
+ stub_actions
227
+ Rails::Generator::Base.logger.expects('warning').with('Model Parent does not accept nested attributes for model ThirdModel.')
228
+ Rails::Generator::Commands::Create.any_instance.expects(:directory).with('app/controllers/')
229
+ @generator_script = Rails::Generator::Scripts::Generate.new
230
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete'], 'parent'))
231
+ end
232
+ end
233
+
234
+ context "A view_for generator instantiated for a test model with two has_many associations" do
235
+ setup do
236
+ setup_test_model
237
+ setup_parent_test_model
238
+ @gen = new_generator_for_test_model('view_for', ['--view', 'has_many_auto_complete'], 'parent')
239
+ end
240
+
241
+ should "return the proper source root" do
242
+ assert_equal File.expand_path(File.dirname(__FILE__) + '/../../lib/view_mapper/templates'), ViewMapper::HasManyAutoCompleteView.source_root
243
+ end
244
+
245
+ view_for_templates = %w{ new edit show index }
246
+ view_for_templates.each do | template |
247
+ should "render the #{template} template as expected" do
248
+ @attributes = @gen.attributes
249
+ @singular_name = @gen.singular_name
250
+ @plural_name = @gen.plural_name
251
+ @child_models = @gen.child_models
252
+ template_file = File.open(@gen.source_path("view_#{template}.html.erb"))
253
+ result = ERB.new(template_file.read, nil, '-').result(binding)
254
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/#{template}.html.erb"))
255
+ assert_equal expected_file.read, result
256
+ end
257
+ end
258
+
259
+ should "render the form partial as expected" do
260
+ @attributes = @gen.attributes
261
+ @singular_name = @gen.singular_name
262
+ @plural_name = @gen.plural_name
263
+ @child_models = @gen.child_models
264
+ template_file = File.open(@gen.source_path("view_form.html.erb"))
265
+ result = ERB.new(template_file.read, nil, '-').result(binding)
266
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_form.html.erb"))
267
+ assert_equal expected_file.read, result
268
+ end
269
+
270
+ should "render the person partial as expected" do
271
+ @child_model = @gen.child_models[1]
272
+ template_file = File.open(@gen.source_path("view_child_form.html.erb"))
273
+ result = ERB.new(template_file.read, nil, '-').result(binding)
274
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/_person.html.erb"))
275
+ assert_equal expected_file.read, result
276
+ end
277
+ end
278
+
279
+ context "A scaffold_for_view generator instantiated for a test model with two has_many associations" do
280
+ setup do
281
+ setup_test_model
282
+ setup_parent_test_model
283
+ @gen = new_generator_for_test_model('scaffold_for_view', ['--view', 'has_many_auto_complete:some_other_models,testies'], 'parent')
284
+ end
285
+
286
+ should "find the parent model text field attributes" do
287
+ assert_equal [
288
+ {:model_name=>"parent", :text_field=>"first_name" },
289
+ {:model_name=>"parent", :text_field=>"last_name" },
290
+ {:model_name=>"parent", :text_field=>"address" }
291
+ ], @gen.auto_complete_attributes_from_command_line
292
+ end
293
+
294
+ should "render the model template as expected" do
295
+ @child_models = @gen.child_models
296
+ @class_name = @gen.class_name
297
+ @attributes = @gen.attributes
298
+ template_file = File.open(@gen.source_path("model.rb"))
299
+ result = ERB.new(template_file.read, nil, '-').result(binding)
300
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/parent.rb"))
301
+ assert_equal expected_file.read, result
302
+ end
303
+
304
+ should "render the migration template as expected" do
305
+ @class_name = @gen.class_name
306
+ @attributes = @gen.attributes
307
+ @migration_name = 'CreateParents'
308
+ @table_name = @gen.table_name
309
+ @options = {}
310
+ template_file = File.open(@gen.source_path("migration.rb"))
311
+ result = ERB.new(template_file.read, nil, '-').result(binding)
312
+ expected_file = File.open(File.join(File.dirname(__FILE__), "expected_templates/create_parents.rb"))
313
+ assert_equal expected_file.read, result
314
+ end
315
+ end
316
+
317
+ context "A Rails generator script" do
318
+ setup do
319
+ setup_test_model
320
+ setup_parent_test_model
321
+ @generator_script = Rails::Generator::Scripts::Generate.new
322
+ end
323
+
324
+ should "return a warning when run with view_for on an invalid child model and not run any actions" do
325
+ expect_no_actions
326
+ Rails::Generator::Base.logger.expects('error').with('Class \'blah\' does not exist or contains a syntax error and could not be loaded.')
327
+ @generator_script = Rails::Generator::Scripts::Generate.new
328
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:blah']))
329
+ end
330
+
331
+ should "create the correct manifest when the view_for generator is run with a valid child model" do
332
+
333
+ expect_no_warnings
334
+
335
+ directories = [
336
+ 'app/controllers/',
337
+ 'app/helpers/',
338
+ 'app/views/parents',
339
+ 'app/views/layouts/',
340
+ 'test/functional/',
341
+ 'test/unit/',
342
+ 'test/unit/helpers/',
343
+ 'public/stylesheets/'
344
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
345
+
346
+ templates = {
347
+ 'view_index.html.erb' => 'app/views/parents/index.html.erb',
348
+ 'view_new.html.erb' => 'app/views/parents/new.html.erb',
349
+ 'view_edit.html.erb' => 'app/views/parents/edit.html.erb',
350
+ 'view_form.html.erb' => 'app/views/parents/_form.html.erb',
351
+ 'layout.html.erb' => 'app/views/layouts/parents.html.erb',
352
+ 'style.css' => 'public/stylesheets/scaffold.css',
353
+ 'controller.rb' => 'app/controllers/parents_controller.rb',
354
+ 'functional_test.rb' => 'test/functional/parents_controller_test.rb',
355
+ 'helper.rb' => 'app/helpers/parents_helper.rb',
356
+ 'helper_test.rb' => 'test/unit/helpers/parents_helper_test.rb'
357
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
358
+
359
+ testy_model_info = ViewMapper::ModelInfo.new('testy')
360
+ parent_model_info = ViewMapper::ModelInfo.new('parent')
361
+ ViewMapper::ModelInfo.stubs(:new).with('testy').returns(testy_model_info)
362
+ ViewMapper::ModelInfo.stubs(:new).with('parent').returns(parent_model_info)
363
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
364
+ 'view_show.html.erb',
365
+ 'app/views/parents/show.html.erb',
366
+ { :assigns => { :child_models => [ testy_model_info ] } }
367
+ )
368
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
369
+ 'view_child_form.html.erb',
370
+ 'app/views/parents/_testy.html.erb',
371
+ { :assigns => { :child_model => testy_model_info } }
372
+ )
373
+ Rails::Generator::Commands::Create.any_instance.expects(:file).with(
374
+ 'nested_attributes.js', 'public/javascripts/nested_attributes.js'
375
+ )
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 => 'parents')
382
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
383
+ :path => 'auto_complete_for_testy_first_name',
384
+ :name => 'connect',
385
+ :action => 'auto_complete_for_testy_first_name',
386
+ :controller => 'parents')
387
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
388
+ :path => 'auto_complete_for_testy_last_name',
389
+ :name => 'connect',
390
+ :action => 'auto_complete_for_testy_last_name',
391
+ :controller => 'parents')
392
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
393
+ :path => 'auto_complete_for_testy_address',
394
+ :name => 'connect',
395
+ :action => 'auto_complete_for_testy_address',
396
+ :controller => 'parents')
397
+
398
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('parents')
399
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
400
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
401
+
402
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:testies'], 'parent'))
403
+ end
404
+
405
+ should "create the correct manifest when the scaffold_for_view generator is run with a valid child model" do
406
+
407
+ expect_no_warnings
408
+
409
+ directories = [
410
+ 'app/models/',
411
+ 'app/controllers/',
412
+ 'app/helpers/',
413
+ 'app/views/parents',
414
+ 'app/views/layouts/',
415
+ 'test/functional/',
416
+ 'test/unit/',
417
+ 'test/unit/helpers/',
418
+ 'test/fixtures/',
419
+ 'public/stylesheets/'
420
+ ].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
421
+
422
+ templates = {
423
+ 'view_index.html.erb' => 'app/views/parents/index.html.erb',
424
+ 'view_new.html.erb' => 'app/views/parents/new.html.erb',
425
+ 'view_edit.html.erb' => 'app/views/parents/edit.html.erb',
426
+ 'view_form.html.erb' => 'app/views/parents/_form.html.erb',
427
+ 'layout.html.erb' => 'app/views/layouts/parents.html.erb',
428
+ 'style.css' => 'public/stylesheets/scaffold.css',
429
+ 'controller.rb' => 'app/controllers/parents_controller.rb',
430
+ 'functional_test.rb' => 'test/functional/parents_controller_test.rb',
431
+ 'helper.rb' => 'app/helpers/parents_helper.rb',
432
+ 'helper_test.rb' => 'test/unit/helpers/parents_helper_test.rb',
433
+ 'model.rb' => 'app/models/parent.rb',
434
+ 'unit_test.rb' => 'test/unit/parent_test.rb',
435
+ 'fixtures.yml' => 'test/fixtures/parents.yml'
436
+ }.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
437
+
438
+ testy_model_info = ViewMapper::ModelInfo.new('testy')
439
+ parent_model_info = ViewMapper::ModelInfo.new('parent')
440
+ ViewMapper::ModelInfo.stubs(:new).with('testy').returns(testy_model_info)
441
+ ViewMapper::ModelInfo.stubs(:new).with('parent').returns(parent_model_info)
442
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
443
+ 'view_show.html.erb',
444
+ 'app/views/parents/show.html.erb',
445
+ { :assigns => { :child_models => [ testy_model_info ] } }
446
+ )
447
+ Rails::Generator::Commands::Create.any_instance.expects(:template).with(
448
+ 'view_child_form.html.erb',
449
+ 'app/views/parents/_testy.html.erb',
450
+ { :assigns => { :child_model => testy_model_info } }
451
+ )
452
+ Rails::Generator::Commands::Create.any_instance.expects(:file).with(
453
+ 'nested_attributes.js', 'public/javascripts/nested_attributes.js'
454
+ )
455
+ Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('parents')
456
+ Rails::Generator::Commands::Create.any_instance.expects(:file).never
457
+ Rails::Generator::Commands::Create.any_instance.expects(:dependency).never
458
+
459
+ Rails::Generator::Commands::Create.any_instance.expects(:migration_template).with(
460
+ 'migration.rb',
461
+ 'db/migrate',
462
+ :assigns => { :migration_name => "CreateParents" },
463
+ :migration_file_name => "create_parents"
464
+ )
465
+
466
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
467
+ :path => 'auto_complete_for_parent_first_name',
468
+ :name => 'connect',
469
+ :action => 'auto_complete_for_parent_first_name',
470
+ :controller => 'parents')
471
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
472
+ :path => 'auto_complete_for_parent_last_name',
473
+ :name => 'connect',
474
+ :action => 'auto_complete_for_parent_last_name',
475
+ :controller => 'parents')
476
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
477
+ :path => 'auto_complete_for_parent_address',
478
+ :name => 'connect',
479
+ :action => 'auto_complete_for_parent_address',
480
+ :controller => 'parents')
481
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
482
+ :path => 'auto_complete_for_testy_first_name',
483
+ :name => 'connect',
484
+ :action => 'auto_complete_for_testy_first_name',
485
+ :controller => 'parents')
486
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
487
+ :path => 'auto_complete_for_testy_last_name',
488
+ :name => 'connect',
489
+ :action => 'auto_complete_for_testy_last_name',
490
+ :controller => 'parents')
491
+ Rails::Generator::Commands::Create.any_instance.expects(:route).with(
492
+ :path => 'auto_complete_for_testy_address',
493
+ :name => 'connect',
494
+ :action => 'auto_complete_for_testy_address',
495
+ :controller => 'parents')
496
+
497
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete:testies'], 'parent'))
498
+ end
499
+ end
500
+
501
+ context "A Rails generator script with a child model without a belongs_to association" do
502
+ setup do
503
+ setup_test_model
504
+ setup_parent_test_model(false, false)
505
+ @generator_script = Rails::Generator::Scripts::Generate.new
506
+ end
507
+
508
+ should "return a warning when run with view_for and not run any actions" do
509
+ expect_no_actions
510
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a belongs_to association for Parent.')
511
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:testies'], 'parent'))
512
+ end
513
+
514
+ should "return a warning when run with scaffold_for_view and not run any actions" do
515
+ expect_no_actions
516
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a belongs_to association for Parent.')
517
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete:testies'], 'parent'))
518
+ end
519
+ end
520
+
521
+ context "A Rails generator script with a child model missing a foreign key" do
522
+ setup do
523
+ setup_test_model
524
+ setup_parent_test_model(false)
525
+ @generator_script = Rails::Generator::Scripts::Generate.new
526
+ end
527
+
528
+ should "return a warning when run with view_for and not run any actions" do
529
+ expect_no_actions
530
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a foreign key for Parent.')
531
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:testies'], 'parent'))
532
+ end
533
+
534
+ should "return a warning when run with scaffold_for_view and not run any actions" do
535
+ expect_no_actions
536
+ Rails::Generator::Base.logger.expects('warning').with('Model Testy does not contain a foreign key for Parent.')
537
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete:testies'], 'parent'))
538
+ end
539
+ end
540
+
541
+ context "A Rails generator script with a child model that has a habtm association" do
542
+ setup do
543
+ setup_test_model
544
+ setup_parent_test_model(false, false)
545
+ Testy.class_eval do
546
+ has_and_belongs_to_many :parents
547
+ end
548
+ @generator_script = Rails::Generator::Scripts::Generate.new
549
+ end
550
+
551
+ should "not return a warning when run with view_for" do
552
+ stub_actions
553
+ expect_no_warnings
554
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:testies'], 'parent'))
555
+ end
556
+
557
+ should "not return a warning when run with scaffold_for_view" do
558
+ stub_actions
559
+ expect_no_warnings
560
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete:testies'], 'parent'))
561
+ end
562
+ end
563
+
564
+ context "A Rails generator script with a child model that has_many (through) association" do
565
+ setup do
566
+ setup_test_model
567
+ setup_parent_test_model(false, false)
568
+ Testy.class_eval do
569
+ has_many :parents
570
+ end
571
+ @generator_script = Rails::Generator::Scripts::Generate.new
572
+ end
573
+
574
+ should "not return a warning when run with view_for" do
575
+ stub_actions
576
+ expect_no_warnings
577
+ @generator_script.run(generator_script_cmd_line('view_for', ['--view', 'has_many_auto_complete:testies'], 'parent'))
578
+ end
579
+
580
+ should "not return a warning when run with scaffold_for_view" do
581
+ stub_actions
582
+ expect_no_warnings
583
+ @generator_script.run(generator_script_cmd_line('scaffold_for_view', ['--view', 'has_many_auto_complete:testies'], 'parent'))
584
+ end
585
+ end
586
+
587
+ end