view_mapper 0.3.3 → 0.3.4

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