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.
- data/README.rdoc +3 -1
- data/VERSION +1 -1
- data/lib/view_mapper.rb +1 -0
- data/lib/view_mapper/belongs_to_parent_models.rb +12 -26
- data/lib/view_mapper/has_many_child_models.rb +9 -9
- data/lib/view_mapper/model_info.rb +56 -18
- data/lib/view_mapper/views/belongs_to/belongs_to_view.rb +14 -0
- data/lib/view_mapper/views/belongs_to_auto_complete/belongs_to_auto_complete_view.rb +9 -2
- data/lib/view_mapper/views/has_many/templates/helper.rb +1 -1
- data/lib/view_mapper/views/has_many/templates/view_child_form.html.erb +1 -1
- data/lib/view_mapper/views/has_many/templates/view_form.html.erb +1 -1
- data/lib/view_mapper/views/has_many_existing/has_many_existing_view.rb +126 -0
- data/lib/view_mapper/views/has_many_existing/templates/model.rb +21 -0
- data/lib/view_mapper/views/has_many_existing/templates/view_child_form.html.erb +16 -0
- data/lib/view_mapper/views/has_many_existing/templates/view_form.html.erb +20 -0
- data/lib/view_mapper/views/has_many_existing/templates/view_show.html.erb +26 -0
- data/test/generators/scaffold_for_view/scaffold_for_view_generator_test.rb +15 -15
- data/test/generators/view_for/view_for_generator_test.rb +18 -18
- data/test/model_info_test.rb +91 -20
- data/test/test_helper.rb +75 -105
- data/test/views/auto_complete/auto_complete_test.rb +104 -102
- data/test/views/auto_complete/expected_templates/edit.html.erb +6 -6
- data/test/views/auto_complete/expected_templates/expected_routes.rb +1 -1
- data/test/views/auto_complete/expected_templates/index.html.erb +10 -10
- data/test/views/auto_complete/expected_templates/new.html.erb +5 -5
- data/test/views/auto_complete/expected_templates/show.html.erb +6 -6
- data/test/views/auto_complete/expected_templates/{testies.html.erb → test_models.html.erb} +1 -1
- data/test/views/auto_complete/expected_templates/test_models_controller.rb +89 -0
- data/test/views/belongs_to/belongs_to_test.rb +238 -202
- data/test/views/belongs_to/expected_templates/_form.html.erb +4 -4
- data/test/views/belongs_to/expected_templates/child_model.rb +10 -0
- data/test/views/belongs_to/expected_templates/create_child_models.rb +18 -0
- data/test/views/belongs_to/expected_templates/edit.html.erb +4 -4
- data/test/views/belongs_to/expected_templates/index.html.erb +11 -11
- data/test/views/belongs_to/expected_templates/new.html.erb +3 -3
- data/test/views/belongs_to/expected_templates/show.html.erb +7 -7
- data/test/views/belongs_to_auto_complete/belongs_to_auto_complete_test.rb +440 -406
- data/test/views/belongs_to_auto_complete/expected_templates/_form.html.erb +4 -4
- data/test/views/belongs_to_auto_complete/expected_templates/child_model.rb +16 -0
- data/test/views/belongs_to_auto_complete/expected_templates/{some_other_models.html.erb → child_models.html.erb} +1 -1
- data/test/views/belongs_to_auto_complete/expected_templates/child_models_controller.rb +89 -0
- data/test/views/belongs_to_auto_complete/expected_templates/create_child_models.rb +18 -0
- data/test/views/belongs_to_auto_complete/expected_templates/edit.html.erb +4 -4
- data/test/views/belongs_to_auto_complete/expected_templates/expected_routes.rb +2 -2
- data/test/views/belongs_to_auto_complete/expected_templates/index.html.erb +11 -11
- data/test/views/belongs_to_auto_complete/expected_templates/new.html.erb +3 -3
- data/test/views/belongs_to_auto_complete/expected_templates/show.html.erb +7 -7
- data/test/views/has_many/expected_templates/_form.html.erb +8 -8
- data/test/views/has_many/expected_templates/{_person.html.erb → _second_child_model.html.erb} +5 -5
- data/test/views/has_many/expected_templates/create_parent_models.rb +16 -0
- data/test/views/has_many/expected_templates/edit.html.erb +4 -4
- data/test/views/has_many/expected_templates/index.html.erb +7 -7
- data/test/views/has_many/expected_templates/new.html.erb +3 -3
- data/test/views/has_many/expected_templates/{parent.rb → parent_model.rb} +5 -5
- data/test/views/has_many/expected_templates/show.html.erb +15 -15
- data/test/views/has_many/has_many_view_test.rb +334 -292
- data/test/views/has_many_existing/expected_templates/_assignment.html.erb +14 -0
- data/test/views/has_many_existing/expected_templates/_form.html.erb +16 -0
- data/test/views/{has_many/expected_templates/create_parents.rb → has_many_existing/expected_templates/create_programmers.rb} +3 -3
- data/test/views/has_many_existing/expected_templates/edit.html.erb +11 -0
- data/test/views/has_many_existing/expected_templates/index.html.erb +20 -0
- data/test/views/has_many_existing/expected_templates/new.html.erb +10 -0
- data/test/views/has_many_existing/expected_templates/programmer.rb +10 -0
- data/test/views/has_many_existing/expected_templates/show.html.erb +20 -0
- data/test/views/has_many_existing/has_many_existing_test.rb +495 -0
- data/test/views/paperclip/expected_templates/{create_testies.rb → create_test_models.rb} +3 -3
- data/test/views/paperclip/expected_templates/edit.html.erb +4 -4
- data/test/views/paperclip/expected_templates/index.html.erb +10 -10
- data/test/views/paperclip/expected_templates/new.html.erb +3 -3
- data/test/views/paperclip/expected_templates/show.html.erb +8 -8
- data/test/views/paperclip/expected_templates/{testy.rb → test_model.rb} +1 -1
- data/test/views/paperclip/paperclip_view_test.rb +254 -226
- data/view_mapper.gemspec +43 -27
- metadata +64 -37
- data/test/views/auto_complete/expected_templates/actual_index +0 -26
- data/test/views/auto_complete/expected_templates/testies_controller.rb +0 -89
- data/test/views/belongs_to/expected_templates/create_some_other_models.rb +0 -18
- data/test/views/belongs_to/expected_templates/some_other_model.rb +0 -10
- data/test/views/belongs_to_auto_complete/expected_templates/create_some_other_models.rb +0 -18
- data/test/views/belongs_to_auto_complete/expected_templates/some_other_model.rb +0 -16
- 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(:
|
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(:
|
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
|
1
|
+
<h1>Editing child_model</h1>
|
2
2
|
|
3
|
-
<% form_for(@
|
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', @
|
11
|
-
<%= link_to 'Back',
|
10
|
+
<%= link_to 'Show', @child_model %> |
|
11
|
+
<%= link_to 'Back', child_models_path %>
|
@@ -1,24 +1,24 @@
|
|
1
|
-
<h1>Listing
|
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
|
-
<% @
|
10
|
+
<% @child_models.each do |child_model| %>
|
11
11
|
<tr>
|
12
|
-
<td><%=h
|
13
|
-
<td><%=h
|
14
|
-
<td><%=h
|
15
|
-
<td><%= link_to 'Show',
|
16
|
-
<td><%= link_to 'Edit',
|
17
|
-
<td><%= link_to 'Destroy',
|
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
|
24
|
+
<%= link_to 'New child_model', new_child_model_path %>
|
@@ -1,10 +1,10 @@
|
|
1
|
-
<h1>New
|
1
|
+
<h1>New child_model</h1>
|
2
2
|
|
3
|
-
<% form_for(@
|
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',
|
10
|
+
<%= link_to 'Back', child_models_path %>
|
@@ -1,17 +1,17 @@
|
|
1
1
|
<p>
|
2
2
|
<b>Name:</b>
|
3
|
-
<%=h @
|
3
|
+
<%=h @child_model.name %>
|
4
4
|
</p>
|
5
5
|
|
6
6
|
<p>
|
7
|
-
<b>Parent:</b>
|
8
|
-
<%=h @
|
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 @
|
12
|
+
<b>Second Parent Model:</b>
|
13
|
+
<%=h @child_model.second_parent_model_other_field %>
|
14
14
|
</p>
|
15
15
|
|
16
|
-
<%= link_to 'Edit',
|
17
|
-
<%= link_to 'Back',
|
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 "
|
16
|
+
context "When the auto_complete plugin is installed" do
|
17
17
|
setup do
|
18
|
-
|
19
|
-
setup_parent_test_model
|
18
|
+
ActionController::Base.send(:include, MockAutoComplete)
|
20
19
|
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
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
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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
|
-
|
149
|
-
|
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
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
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
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
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
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
-
|
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
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
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
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
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
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
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
|
-
|
218
|
-
|
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
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
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
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
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
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
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
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
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
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
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
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
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
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
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
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
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
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
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
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
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
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
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
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
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
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
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
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
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
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
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
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
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
|
|