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,16 @@
|
|
1
|
+
<div class="child">
|
2
|
+
<% for attribute in hmt_model.through_model.attributes -%>
|
3
|
+
<p>
|
4
|
+
<%= hmt_model.through_model.name %> <%%= f.label :<%= attribute.name %> %><br />
|
5
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
6
|
+
</p>
|
7
|
+
<% end -%>
|
8
|
+
<p>
|
9
|
+
<%= hmt_model.name.titleize %>:<br />
|
10
|
+
<%%= f.collection_select(:<%= hmt_model.name.underscore %>_id, <%= hmt_model.name %>.all, :id, :<%= field_for(hmt_model) %>, { :prompt => true }) %>
|
11
|
+
</p>
|
12
|
+
<p>
|
13
|
+
<%%= f.hidden_field :_destroy, :class => 'delete' %>
|
14
|
+
<%%= remove_child_link 'remove', f %>
|
15
|
+
</p>
|
16
|
+
</div>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
<%%= f.error_messages %>
|
2
|
+
<% for attribute in attributes -%>
|
3
|
+
|
4
|
+
<p>
|
5
|
+
<%%= f.label :<%= attribute.name %> %><br />
|
6
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
7
|
+
</p>
|
8
|
+
<% end -%>
|
9
|
+
<% has_many_through_models.each do |hmt_model| -%>
|
10
|
+
|
11
|
+
<div id='<%= hmt_model.through_model.name.underscore %>_children'>
|
12
|
+
<%% f.fields_for :<%= hmt_model.through_model.name.underscore.pluralize %> do |<%= hmt_model.through_model.name.underscore %>_form| %>
|
13
|
+
<%%= render :partial => '<%= hmt_model.through_model.name.underscore %>', :locals => { :f => <%= hmt_model.through_model.name.underscore %>_form } %>
|
14
|
+
<%% end %>
|
15
|
+
</div>
|
16
|
+
|
17
|
+
<p>
|
18
|
+
<%%= add_child_link 'Add <%= hmt_model.through_model.name %>', '<%= hmt_model.through_model.name.underscore %>', f %>
|
19
|
+
</p>
|
20
|
+
<% end -%>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<% for attribute in attributes -%>
|
2
|
+
<p>
|
3
|
+
<b><%= attribute.column.human_name %>:</b>
|
4
|
+
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
|
5
|
+
</p>
|
6
|
+
|
7
|
+
<% end -%>
|
8
|
+
<% has_many_through_models.each do |hmt_model| -%>
|
9
|
+
<%% @<%= singular_name %>.<%= hmt_model.through_model.name.underscore.pluralize %>.each do |<%= hmt_model.through_model.name.underscore %>| %>
|
10
|
+
<div class="child">
|
11
|
+
<% for attribute in hmt_model.through_model.attributes -%>
|
12
|
+
<p>
|
13
|
+
<b><%= hmt_model.through_model.name %> <%= attribute.column.human_name %>:</b>
|
14
|
+
<%%=h <%= hmt_model.through_model.name.underscore%>.<%= attribute.name %> %>
|
15
|
+
</p>
|
16
|
+
<% end -%>
|
17
|
+
<p>
|
18
|
+
<b><%= hmt_model.name %>:</b>
|
19
|
+
<%%=h <%= hmt_model.through_model.name.underscore%>.<%= hmt_model.name.underscore%>_<%= field_for(hmt_model) %> %>
|
20
|
+
</p>
|
21
|
+
</div>
|
22
|
+
<%% end %>
|
23
|
+
|
24
|
+
<% end -%>
|
25
|
+
<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
|
26
|
+
<%%= link_to 'Back', <%= plural_name %>_path %>
|
@@ -15,14 +15,14 @@ class ScaffoldForViewGeneratorTest < Test::Unit::TestCase
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
context "run on a
|
19
|
-
should "create a manifest = scaffold for
|
18
|
+
context "run on a TestModel" do
|
19
|
+
should "create a manifest = scaffold for TestModel" do
|
20
20
|
|
21
21
|
directories = [
|
22
22
|
'app/models/',
|
23
23
|
'app/controllers/',
|
24
24
|
'app/helpers/',
|
25
|
-
'app/views/
|
25
|
+
'app/views/test_models',
|
26
26
|
'app/views/layouts/',
|
27
27
|
'test/functional/',
|
28
28
|
'test/unit/',
|
@@ -31,31 +31,31 @@ class ScaffoldForViewGeneratorTest < Test::Unit::TestCase
|
|
31
31
|
].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
|
32
32
|
|
33
33
|
templates = {
|
34
|
-
'view_index.html.erb' => 'app/views/
|
35
|
-
'view_show.html.erb' => 'app/views/
|
36
|
-
'view_new.html.erb' => 'app/views/
|
37
|
-
'view_edit.html.erb' => 'app/views/
|
38
|
-
'layout.html.erb' => 'app/views/layouts/
|
34
|
+
'view_index.html.erb' => 'app/views/test_models/index.html.erb',
|
35
|
+
'view_show.html.erb' => 'app/views/test_models/show.html.erb',
|
36
|
+
'view_new.html.erb' => 'app/views/test_models/new.html.erb',
|
37
|
+
'view_edit.html.erb' => 'app/views/test_models/edit.html.erb',
|
38
|
+
'layout.html.erb' => 'app/views/layouts/test_models.html.erb',
|
39
39
|
'style.css' => 'public/stylesheets/scaffold.css',
|
40
|
-
'controller.rb' => 'app/controllers/
|
41
|
-
'functional_test.rb' => 'test/functional/
|
42
|
-
'helper.rb' => 'app/helpers/
|
43
|
-
'helper_test.rb' => 'test/unit/helpers/
|
40
|
+
'controller.rb' => 'app/controllers/test_models_controller.rb',
|
41
|
+
'functional_test.rb' => 'test/functional/test_models_controller_test.rb',
|
42
|
+
'helper.rb' => 'app/helpers/test_models_helper.rb',
|
43
|
+
'helper_test.rb' => 'test/unit/helpers/test_models_helper_test.rb'
|
44
44
|
}.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
|
45
45
|
|
46
|
-
Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('
|
46
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('test_models')
|
47
47
|
Rails::Generator::Commands::Create.any_instance.expects(:file).never
|
48
48
|
|
49
49
|
Rails::Generator::Commands::Create.any_instance.stubs(:dependency)
|
50
50
|
|
51
|
-
@generator_script.run(['scaffold_for_view', '
|
51
|
+
@generator_script.run(['scaffold_for_view', 'test_model'])
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
context "A scaffold_for_view generator" do
|
57
57
|
setup do
|
58
|
-
@scaffold_for_view_gen = Rails::Generator::Base.instance('scaffold_for_view', ['
|
58
|
+
@scaffold_for_view_gen = Rails::Generator::Base.instance('scaffold_for_view', ['test_model'] )
|
59
59
|
end
|
60
60
|
|
61
61
|
should "not call any actions when invalid" do
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'test_helper'
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper'
|
2
2
|
|
3
3
|
class ViewForGeneratorTest < Test::Unit::TestCase
|
4
4
|
|
@@ -33,17 +33,17 @@ class ViewForGeneratorTest < Test::Unit::TestCase
|
|
33
33
|
@generator_script.run(['view_for', 'blah'])
|
34
34
|
end
|
35
35
|
|
36
|
-
context "run on a
|
36
|
+
context "run on a TestModel" do
|
37
37
|
setup do
|
38
|
-
|
38
|
+
ClassFactory :test_model
|
39
39
|
end
|
40
40
|
|
41
|
-
should "create a manifest = (scaffold for
|
41
|
+
should "create a manifest = (scaffold for TestModel) - (model template)" do
|
42
42
|
|
43
43
|
directories = [
|
44
44
|
'app/controllers/',
|
45
45
|
'app/helpers/',
|
46
|
-
'app/views/
|
46
|
+
'app/views/test_models',
|
47
47
|
'app/views/layouts/',
|
48
48
|
'test/functional/',
|
49
49
|
'test/unit/',
|
@@ -52,30 +52,30 @@ class ViewForGeneratorTest < Test::Unit::TestCase
|
|
52
52
|
].each { |path| Rails::Generator::Commands::Create.any_instance.expects(:directory).with(path) }
|
53
53
|
|
54
54
|
templates = {
|
55
|
-
'view_index.html.erb' => 'app/views/
|
56
|
-
'view_show.html.erb' => 'app/views/
|
57
|
-
'view_new.html.erb' => 'app/views/
|
58
|
-
'view_edit.html.erb' => 'app/views/
|
59
|
-
'layout.html.erb' => 'app/views/layouts/
|
55
|
+
'view_index.html.erb' => 'app/views/test_models/index.html.erb',
|
56
|
+
'view_show.html.erb' => 'app/views/test_models/show.html.erb',
|
57
|
+
'view_new.html.erb' => 'app/views/test_models/new.html.erb',
|
58
|
+
'view_edit.html.erb' => 'app/views/test_models/edit.html.erb',
|
59
|
+
'layout.html.erb' => 'app/views/layouts/test_models.html.erb',
|
60
60
|
'style.css' => 'public/stylesheets/scaffold.css',
|
61
|
-
'controller.rb' => 'app/controllers/
|
62
|
-
'functional_test.rb' => 'test/functional/
|
63
|
-
'helper.rb' => 'app/helpers/
|
64
|
-
'helper_test.rb' => 'test/unit/helpers/
|
61
|
+
'controller.rb' => 'app/controllers/test_models_controller.rb',
|
62
|
+
'functional_test.rb' => 'test/functional/test_models_controller_test.rb',
|
63
|
+
'helper.rb' => 'app/helpers/test_models_helper.rb',
|
64
|
+
'helper_test.rb' => 'test/unit/helpers/test_models_helper_test.rb'
|
65
65
|
}.each { |template, target| Rails::Generator::Commands::Create.any_instance.expects(:template).with(template, target) }
|
66
66
|
|
67
|
-
Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('
|
67
|
+
Rails::Generator::Commands::Create.any_instance.expects(:route_resources).with('test_models')
|
68
68
|
Rails::Generator::Commands::Create.any_instance.expects(:file).never
|
69
69
|
|
70
|
-
@generator_script.run(['view_for', '
|
70
|
+
@generator_script.run(['view_for', 'test_model'])
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
context "A view_for generator" do
|
76
76
|
setup do
|
77
|
-
@view_for_gen = Rails::Generator::Base.instance('view_for', ['
|
78
|
-
@model =
|
77
|
+
@view_for_gen = Rails::Generator::Base.instance('view_for', ['test_model'] )
|
78
|
+
@model = ClassFactory :test_model
|
79
79
|
end
|
80
80
|
|
81
81
|
should "have the proper model name" do
|
data/test/model_info_test.rb
CHANGED
@@ -4,16 +4,16 @@ class ModelInfoTest < Test::Unit::TestCase
|
|
4
4
|
|
5
5
|
context "A model info object created for a test model" do
|
6
6
|
setup do
|
7
|
-
|
8
|
-
@model_info = ViewMapper::ModelInfo.new('
|
7
|
+
ClassFactory :test_model
|
8
|
+
@model_info = ViewMapper::ModelInfo.new('test_model')
|
9
9
|
end
|
10
10
|
|
11
11
|
should "find the model" do
|
12
|
-
assert_equal
|
12
|
+
assert_equal TestModel, @model_info.model
|
13
13
|
end
|
14
14
|
|
15
15
|
should "return the model's name" do
|
16
|
-
assert_equal '
|
16
|
+
assert_equal 'TestModel', @model_info.name
|
17
17
|
end
|
18
18
|
|
19
19
|
should "return the model's columns and not the primary key or time stamp columns" do
|
@@ -42,10 +42,10 @@ class ModelInfoTest < Test::Unit::TestCase
|
|
42
42
|
|
43
43
|
context "Child and parent model info objects" do
|
44
44
|
setup do
|
45
|
-
|
46
|
-
|
47
|
-
@child_model = ViewMapper::ModelInfo.new('
|
48
|
-
@parent_model = ViewMapper::ModelInfo.new('
|
45
|
+
ClassFactory :parent_model, :class_eval => 'has_many :second_child_models'
|
46
|
+
ClassFactory :second_child_model
|
47
|
+
@child_model = ViewMapper::ModelInfo.new('second_child_model')
|
48
|
+
@parent_model = ViewMapper::ModelInfo.new('parent_model')
|
49
49
|
end
|
50
50
|
|
51
51
|
should "not include the parent foreign key column in the child model's columns" do
|
@@ -53,38 +53,109 @@ class ModelInfoTest < Test::Unit::TestCase
|
|
53
53
|
end
|
54
54
|
|
55
55
|
should "determine that the child model belongs to the parent model" do
|
56
|
-
assert_equal true, @child_model.belongs_to?('
|
56
|
+
assert_equal true, @child_model.belongs_to?('parent_model')
|
57
57
|
end
|
58
58
|
|
59
59
|
should "determine that the parent model has many child models" do
|
60
|
-
assert_equal true, @parent_model.has_many?('
|
60
|
+
assert_equal true, @parent_model.has_many?('second_child_models')
|
61
|
+
end
|
62
|
+
|
63
|
+
should "return the child's foreign key for the parent" do
|
64
|
+
assert_equal 'parent_model_id', @child_model.foreign_key_for('parent_model').name
|
65
|
+
assert_equal true, @child_model.has_foreign_key_for?('parent_model')
|
66
|
+
assert_equal nil, @child_model.foreign_key_for('invalid_model')
|
67
|
+
assert_equal false, @child_model.has_foreign_key_for?('invalid_model')
|
61
68
|
end
|
62
69
|
end
|
63
70
|
|
64
71
|
context "Two model info objects for models that in a habtm association" do
|
65
72
|
setup do
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
@child_model = ViewMapper::ModelInfo.new('testy')
|
72
|
-
@parent_model = ViewMapper::ModelInfo.new('parent')
|
73
|
+
ClassFactory :parent_model
|
74
|
+
ClassFactory :child_model, :class_eval => 'has_and_belongs_to_many :parent_models'
|
75
|
+
@child_model = ViewMapper::ModelInfo.new('child_model')
|
76
|
+
@parent_model = ViewMapper::ModelInfo.new('parent_model')
|
73
77
|
end
|
74
78
|
|
75
79
|
should "determine that a habtm association exists" do
|
76
|
-
assert_equal true, @child_model.has_and_belongs_to_many?('
|
80
|
+
assert_equal true, @child_model.has_and_belongs_to_many?('parent_models')
|
77
81
|
end
|
78
82
|
end
|
79
83
|
|
80
84
|
context "A model info object created for a test model that has Paperclip attachments" do
|
81
85
|
setup do
|
82
|
-
|
83
|
-
@model_info = ViewMapper::ModelInfo.new('
|
86
|
+
ClassFactory :test_model
|
87
|
+
@model_info = ViewMapper::ModelInfo.new('test_model')
|
84
88
|
end
|
85
89
|
|
86
90
|
should "not include the Paperclip columns in the model's columns" do
|
87
91
|
assert_equal [ 'first_name', 'last_name', 'address', 'some_flag' ], @model_info.columns
|
88
92
|
end
|
89
93
|
end
|
94
|
+
|
95
|
+
context "A model info object created with two models in a has_many/belongs_to setup" do
|
96
|
+
setup do
|
97
|
+
ClassFactory :parent_model
|
98
|
+
ClassFactory :second_parent_model
|
99
|
+
ClassFactory :child_model
|
100
|
+
end
|
101
|
+
|
102
|
+
should "return the proper child model" do
|
103
|
+
parent_model_info = ViewMapper::ModelInfo.new('parent_model')
|
104
|
+
child_models = parent_model_info.child_models
|
105
|
+
assert_equal 1, child_models.size
|
106
|
+
assert_equal 'ChildModel', child_models[0].name
|
107
|
+
assert_equal ChildModel, child_models[0].model
|
108
|
+
assert_equal nil, child_models[0].through_model
|
109
|
+
end
|
110
|
+
|
111
|
+
should "return the proper parent model" do
|
112
|
+
child_model_info = ViewMapper::ModelInfo.new('child_model')
|
113
|
+
parent_models = child_model_info.parent_models
|
114
|
+
assert_equal 2, parent_models.size
|
115
|
+
assert_equal 'ParentModel', parent_models[0].name
|
116
|
+
assert_equal ParentModel, parent_models[0].model
|
117
|
+
assert_equal nil, parent_models[0].through_model
|
118
|
+
assert_equal 'SecondParentModel', parent_models[1].name
|
119
|
+
assert_equal SecondParentModel, parent_models[1].model
|
120
|
+
assert_equal nil, parent_models[1].through_model
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "A model info object created with three models in a has_many through setup" do
|
125
|
+
setup do
|
126
|
+
ClassFactory :programmer
|
127
|
+
ClassFactory :project
|
128
|
+
ClassFactory :assignment
|
129
|
+
@programmer_info = ViewMapper::ModelInfo.new('programmer')
|
130
|
+
end
|
131
|
+
|
132
|
+
should "return the proper through reflection model" do
|
133
|
+
hmt_models = @programmer_info.has_many_through_models
|
134
|
+
assert_equal 1, hmt_models.size
|
135
|
+
assert_equal 'Project', hmt_models[0].name
|
136
|
+
assert_equal Project, hmt_models[0].model
|
137
|
+
assert_equal 'Assignment', hmt_models[0].through_model.name
|
138
|
+
assert_equal Assignment, hmt_models[0].through_model.model
|
139
|
+
end
|
140
|
+
|
141
|
+
should "return the both the target and through models as child models" do
|
142
|
+
child_models = @programmer_info.child_models
|
143
|
+
assert_equal 2, child_models.size
|
144
|
+
assert_equal 'Assignment', child_models[0].name
|
145
|
+
assert_equal Assignment, child_models[0].model
|
146
|
+
assert_equal 'Project', child_models[1].name
|
147
|
+
assert_equal Project, child_models[1].model
|
148
|
+
end
|
149
|
+
|
150
|
+
should "determine that a has_many_through association exists" do
|
151
|
+
assert_equal true, @programmer_info.has_many_through?('projects')
|
152
|
+
end
|
153
|
+
|
154
|
+
should "find the through model later" do
|
155
|
+
hmt_model = ViewMapper::ModelInfo.new('project')
|
156
|
+
through_model = hmt_model.find_through_model('Programmer')
|
157
|
+
assert_equal 'Assignment', through_model.name
|
158
|
+
assert_equal Assignment, through_model.model
|
159
|
+
end
|
160
|
+
end
|
90
161
|
end
|
data/test/test_helper.rb
CHANGED
@@ -3,6 +3,7 @@ require 'test/unit'
|
|
3
3
|
require 'shoulda'
|
4
4
|
require 'mocha'
|
5
5
|
require 'active_record'
|
6
|
+
require 'class_factory'
|
6
7
|
|
7
8
|
ActiveRecord::Base.establish_connection({ :adapter => 'sqlite3', :database => ':memory:' })
|
8
9
|
|
@@ -21,123 +22,92 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
|
|
21
22
|
require 'view_mapper'
|
22
23
|
require 'views/fake/fake_view'
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
table.column :some_flag, :boolean
|
30
|
-
if paperclip_columns
|
31
|
-
table.column :avatar_file_name, :string
|
32
|
-
table.column :avatar_content_type, :string
|
33
|
-
table.column :avatar_file_size, :integer
|
34
|
-
table.column :avatar_updated_at, :datetime
|
35
|
-
table.column :avatar2_file_name, :string
|
36
|
-
table.column :avatar2_content_type, :string
|
37
|
-
table.column :avatar2_file_size, :integer
|
38
|
-
table.column :avatar2_updated_at, :datetime
|
39
|
-
end
|
40
|
-
end
|
25
|
+
ClassFactory.define :test_model do |t|
|
26
|
+
t.string :first_name
|
27
|
+
t.string :last_name
|
28
|
+
t.string :address
|
29
|
+
t.boolean :some_flag
|
41
30
|
end
|
42
31
|
|
43
|
-
|
44
|
-
|
45
|
-
Object.send(:remove_const, "Testy") rescue nil
|
46
|
-
Object.const_set("Testy", Class.new(ActiveRecord::Base))
|
47
|
-
Testy.class_eval do
|
48
|
-
def self.attachment_definitions
|
49
|
-
{ :avatar => {:validations => []}, :avatar2 => {:validations => []} }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
ActiveRecord::Base.send(:include, MockPaperclip)
|
53
|
-
ActionController::Base.send(:include, MockAutoComplete)
|
54
|
-
Object.const_get("Testy")
|
32
|
+
ClassFactory.define :parent_model, :class_eval => 'has_many :child_models' do |parent|
|
33
|
+
parent.string :name
|
55
34
|
end
|
56
35
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
36
|
+
ClassFactory.define :second_parent_model, :class_eval => 'has_many :child_models' do |parent|
|
37
|
+
parent.string :name
|
38
|
+
parent.string :other_field
|
39
|
+
end
|
40
|
+
|
41
|
+
child_model_code = <<END
|
42
|
+
belongs_to :parent_model
|
43
|
+
def parent_model_name
|
44
|
+
'something'
|
64
45
|
end
|
65
|
-
|
66
|
-
Object.send(:remove_const, "Parent") rescue nil
|
67
|
-
Object.const_set("Parent", Class.new(ActiveRecord::Base))
|
68
|
-
Object.send(:remove_const, "SomeOtherModel") rescue nil
|
69
|
-
Object.const_set("SomeOtherModel", Class.new(ActiveRecord::Base))
|
70
|
-
Parent.class_eval do
|
71
|
-
has_many :testies
|
72
|
-
has_many :some_other_models unless !parent_has_many_children
|
73
|
-
def testies_attributes=
|
74
|
-
'fake'
|
75
|
-
end
|
76
|
-
def some_other_models_attributes=
|
77
|
-
'fake'
|
78
|
-
end
|
46
|
+
def parent_model_name=
|
79
47
|
end
|
80
|
-
|
81
|
-
|
48
|
+
|
49
|
+
belongs_to :second_parent_model
|
50
|
+
def second_parent_model_name
|
51
|
+
'something else'
|
82
52
|
end
|
83
|
-
|
84
|
-
belongs_to :parent
|
85
|
-
def parent_name
|
86
|
-
'something'
|
87
|
-
end
|
88
|
-
def parent_name=
|
89
|
-
end
|
53
|
+
def second_parent_model_name=
|
90
54
|
end
|
91
|
-
|
55
|
+
END
|
56
|
+
|
57
|
+
ClassFactory.define :child_model, :class_eval => child_model_code do |child|
|
58
|
+
child.string :name
|
59
|
+
child.integer :parent_model_id
|
60
|
+
child.integer :second_parent_model_id
|
92
61
|
end
|
93
62
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
63
|
+
second_child_model_code = <<END
|
64
|
+
belongs_to :parent_model
|
65
|
+
def parent_model_name
|
66
|
+
'something'
|
98
67
|
end
|
99
|
-
|
100
|
-
Object.send(:remove_const, "SecondParent") rescue nil
|
101
|
-
Object.const_set("SecondParent", Class.new(ActiveRecord::Base))
|
102
|
-
SecondParent.class_eval do
|
103
|
-
has_many :some_other_models
|
104
|
-
def some_other_models_attributes=
|
105
|
-
'fake'
|
106
|
-
end
|
107
|
-
def name
|
108
|
-
'fake'
|
109
|
-
end unless !parent_has_name_method
|
110
|
-
end
|
111
|
-
SomeOtherModel.class_eval do
|
112
|
-
belongs_to :second_parent
|
113
|
-
if has_virtual_attribute
|
114
|
-
def second_parent_name
|
115
|
-
'something'
|
116
|
-
end
|
117
|
-
def second_parent_other_field
|
118
|
-
'something'
|
119
|
-
end
|
120
|
-
end
|
121
|
-
if has_virtual_attribute_setter
|
122
|
-
def second_parent_name=
|
123
|
-
'something'
|
124
|
-
end
|
125
|
-
def second_parent_other_field=
|
126
|
-
'something'
|
127
|
-
end
|
128
|
-
end
|
68
|
+
def parent_model_name=
|
129
69
|
end
|
70
|
+
END
|
71
|
+
|
72
|
+
ClassFactory.define :second_child_model, :class_eval => second_child_model_code do |child|
|
73
|
+
child.integer :parent_model_id
|
74
|
+
child.string :first_name
|
75
|
+
child.string :last_name
|
76
|
+
child.string :address
|
77
|
+
child.boolean :some_flag
|
130
78
|
end
|
131
79
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
80
|
+
has_many_through_code = <<SRC
|
81
|
+
has_many :programmers, :through => :assignments
|
82
|
+
has_many :assignments
|
83
|
+
SRC
|
84
|
+
ClassFactory.define :project, :class_eval => has_many_through_code do |t|
|
85
|
+
t.string :name
|
86
|
+
end
|
87
|
+
|
88
|
+
has_many_through_code = <<SRC
|
89
|
+
has_many :projects, :through => :assignments
|
90
|
+
has_many :assignments
|
91
|
+
def assignments_attributes=
|
92
|
+
'fake'
|
93
|
+
end
|
94
|
+
SRC
|
95
|
+
ClassFactory.define :programmer, :class_eval => has_many_through_code do |t|
|
96
|
+
t.string :name
|
97
|
+
end
|
98
|
+
|
99
|
+
through_model_code = <<SRC
|
100
|
+
belongs_to :project
|
101
|
+
belongs_to :programmer
|
102
|
+
def project_name
|
103
|
+
'something'
|
104
|
+
end
|
105
|
+
def project_name=
|
106
|
+
end
|
107
|
+
SRC
|
108
|
+
ClassFactory.define :assignment, :class_eval => through_model_code do |a|
|
109
|
+
a.integer :project_id
|
110
|
+
a.integer :programmer_id
|
141
111
|
end
|
142
112
|
|
143
113
|
module MockPaperclip
|
@@ -179,11 +149,11 @@ def generator_cmd_line(gen, args, model)
|
|
179
149
|
(cmd_line << args).flatten
|
180
150
|
end
|
181
151
|
|
182
|
-
def generator_script_cmd_line(gen, args, model = '
|
152
|
+
def generator_script_cmd_line(gen, args, model = 'test_model')
|
183
153
|
([gen] << generator_cmd_line(gen, args, model)).flatten
|
184
154
|
end
|
185
155
|
|
186
|
-
def new_generator_for_test_model(gen, args, model = '
|
156
|
+
def new_generator_for_test_model(gen, args, model = 'test_model')
|
187
157
|
Rails::Generator::Base.instance(gen, generator_cmd_line(gen, args, model))
|
188
158
|
end
|
189
159
|
|