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