has_many_through_generator 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+ def index
4
+ end
5
+
6
+ def create
7
+ begin
8
+ @<%= singular_name %> = <%= class_name %>.new(
9
+ params[:<%= singular_name %>].merge(
10
+ :<%= first_id_column %> => params[:<%= first_id_column %>],
11
+ :<%= second_id_column %> => params[:<%= second_id_column %>]
12
+ ))
13
+ @successful = @<%= singular_name %>.save
14
+ rescue
15
+ flash[:error], @successful = $!.to_s, false
16
+ end
17
+
18
+ respond_to do |type|
19
+ type.html {return redirect_to_main}
20
+ end
21
+ end
22
+
23
+ def update
24
+ begin
25
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
26
+ @successful = @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
27
+ rescue
28
+ flash[:error], @successful = $!.to_s, false
29
+ end
30
+
31
+ respond_to do |type|
32
+ type.html {return redirect_to_main}
33
+ end
34
+ end
35
+
36
+ def destroy
37
+ begin
38
+ @successful = <%= class_name %>.find(params[:id]).destroy
39
+ rescue
40
+ flash[:error], @successful = $!.to_s, false
41
+ end
42
+
43
+ respond_to do |type|
44
+ type.html {return redirect_to_main}
45
+ end
46
+ end
47
+
48
+ protected
49
+ def redirect_to_main
50
+ redirect_to common_redirection
51
+ end
52
+
53
+ def common_redirection
54
+ { :action => 'index' }
55
+ end
56
+ end
@@ -0,0 +1,8 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ first:
3
+ id: 1
4
+ second:
5
+ id: 2
6
+ another:
7
+ id: 3
8
+
@@ -0,0 +1,13 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ <%= first_singular_name %>1_<%= second_singular_name %>1:
3
+ id: 1
4
+ <%= first_singular_name %>_id: 1
5
+ <%= second_singular_name %>_id: 1
6
+ <%= first_singular_name %>1_<%= second_singular_name %>2:
7
+ id: 2
8
+ <%= first_singular_name %>_id: 1
9
+ <%= second_singular_name %>_id: 2
10
+ <%= first_singular_name %>2_<%= second_singular_name %>2:
11
+ id: 3
12
+ <%= first_singular_name %>_id: 2
13
+ <%= second_singular_name %>_id: 2
@@ -0,0 +1,78 @@
1
+ require File.dirname(__FILE__) + '<%= "/.." * controller_class_nesting_depth %>/../test_helper'
2
+ require '<%= controller_file_path %>_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
+ fixtures :<%= plural_name %>, :<%= other_plural_name %>, :<%= manytomany_plural_name %>
9
+
10
+ NEW_<%= singular_name.upcase %> = { } # e.g. {:name => 'Test <%= class_name %>', :description => 'Dummy'}
11
+ REDIRECT_TO_MAIN = { :action => 'index' } # put hash or string redirection that you normally expect
12
+
13
+ def setup
14
+ @controller = <%= controller_class_name %>Controller.new
15
+ @request = ActionController::TestRequest.new
16
+ @response = ActionController::TestResponse.new
17
+ # Retrieve fixtures via their name
18
+ # @first = <%= plural_name %>(:first)
19
+ @first = <%= class_name %>.find_first
20
+ end
21
+
22
+ def test_index
23
+ post :index
24
+ assert_response :success
25
+ assert_template '<%= plural_name %>/index'
26
+ end
27
+
28
+ def test_create
29
+ <%= singular_name %>_count = <%= class_name %>.find(:all).length
30
+ post :create, {:<%= singular_name %> => NEW_<%= singular_name.upcase %>}
31
+ <%= singular_name %>, successful = check_attrs(%w(<%= singular_name %> successful))
32
+ assert <%= singular_name %>.errors.blank?, "Should be no flash error messages, found #{<%= singular_name %>.errors.inspect}"
33
+ assert_equal "<%= class_name %> created", flash[:info], "Flash message incorrect"
34
+ assert successful, "Should be successful"
35
+ assert_response :redirect
36
+ assert_redirected_to REDIRECT_TO_MAIN
37
+ assert_equal <%= singular_name %>_count + 1, <%= class_name %>.find(:all).length, "Expected an additional <%= class_name %>"
38
+ end
39
+
40
+ def test_update
41
+ <%= singular_name %>_count = <%= class_name %>.find(:all).length
42
+ post :update, {:id => @first.id, :<%= singular_name %> => NEW_<%= singular_name.upcase %>}
43
+ <%= singular_name %>, successful = check_attrs(%w(<%= singular_name %> successful))
44
+ assert <%= singular_name %>.errors.blank?, "Should be no flash error messages, found #{<%= singular_name %>.errors.inspect}"
45
+ assert_equal "<%= class_name %> updated", flash[:info], "Flash message incorrect"
46
+ assert successful, "Should be successful"
47
+ <%= singular_name %>.reload
48
+ NEW_<%= singular_name.upcase %>.each do |attr_name|
49
+ assert_equal NEW_<%= singular_name.upcase %>[attr_name], <%= singular_name %>.attributes[attr_name], "@<%= singular_name %>.#{attr_name.to_s} incorrect"
50
+ end
51
+ assert_equal <%= singular_name %>_count, <%= class_name %>.find(:all).length, "Number of <%= class_name %>s should be the same"
52
+ assert_response :redirect
53
+ assert_redirected_to REDIRECT_TO_MAIN
54
+ end
55
+
56
+ def test_destroy
57
+ <%= singular_name %>_count = <%= class_name %>.find(:all).length
58
+ post :destroy, {:id => @first.id}
59
+ assert flash[:error].blank?, "Should be no flash error messages, found #{flash[:error].inspect}"
60
+ assert_equal "<%= class_name %> deleted", flash[:info], "Flash message incorrect"
61
+ assert_response :redirect
62
+ assert_equal <%= singular_name %>_count - 1, <%= class_name %>.find(:all).length, "Number of <%= class_name %>s should be one less"
63
+ assert_redirected_to REDIRECT_TO_MAIN
64
+ end
65
+
66
+ protected
67
+ # Could be put in a Helper library and included at top of test class
68
+ def check_attrs(attr_list)
69
+ attrs = []
70
+ attr_list.each do |attr_sym|
71
+ attr = assigns(attr_sym.to_sym)
72
+ assert_not_nil attr, "Attribute @#{attr_sym} should not be nil"
73
+ assert !attr.new_record?, "Should have saved the @#{attr_sym} obj" if attr.class == ActiveRecord
74
+ attrs << attr
75
+ end
76
+ attrs.length > 1 ? attrs : attrs[0]
77
+ end
78
+ end
@@ -0,0 +1,75 @@
1
+ require File.dirname(__FILE__) + '<%= "/.." * controller_class_nesting_depth %>/../test_helper'
2
+ require '<%= controller_file_path %>_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
8
+ fixtures :<%= first_table_name %>, :<%= second_table_name %>, :<%= table_name %>
9
+
10
+ UPDATE_<%= singular_name.upcase %> = { } # Put data for the <%= class_name %> model here
11
+ NEW_<%= singular_name.upcase %> = { :<%= first_id_column %> => 3, :<%= second_id_column %> => 3,
12
+ :<%= singular_name %> => UPDATE_<%= singular_name.upcase %>
13
+ }
14
+ REDIRECT_TO_MAIN = { :action => 'index' } # put hash or string redirection that you normally expect
15
+
16
+ def setup
17
+ @controller = <%= controller_class_name %>Controller.new
18
+ @request = ActionController::TestRequest.new
19
+ @response = ActionController::TestResponse.new
20
+ # Retrieve fixtures via their name
21
+ # @first = <%= plural_name %>(:first)
22
+ @first = <%= class_name %>.find_first
23
+ end
24
+
25
+ def test_index
26
+ post :index
27
+ assert_response :success
28
+ assert_template '<%= plural_name %>/index'
29
+ end
30
+
31
+ def test_create
32
+ <%= singular_name %>_count = <%= class_name %>.find(:all).length
33
+ post :create, NEW_<%= singular_name.upcase %>
34
+ <%= singular_name %>, successful = check_attrs(%w(<%= singular_name %> successful))
35
+ assert successful, "Should be successful"
36
+ assert_response :redirect
37
+ assert_redirected_to REDIRECT_TO_MAIN
38
+ assert_equal <%= singular_name %>_count + 1, <%= class_name %>.find(:all).length, "Expected an additional <%= class_name %>"
39
+ end
40
+
41
+ def test_update
42
+ <%= singular_name %>_count = <%= class_name %>.find(:all).length
43
+ post :update, {:id => @first.id, :<%= singular_name %> => UPDATE_<%= singular_name.upcase %>}
44
+ <%= singular_name %>, successful = check_attrs(%w(<%= singular_name %> successful))
45
+ assert successful, "Should be successful"
46
+ <%= singular_name %>.reload
47
+ UPDATE_<%= singular_name.upcase %>.each do |attr_name|
48
+ assert_equal NEW_<%= singular_name.upcase %>[attr_name], <%= singular_name %>.attributes[attr_name], "@<%= singular_name %>.#{attr_name.to_s} incorrect"
49
+ end
50
+ assert_equal <%= singular_name %>_count, <%= class_name %>.find(:all).length, "Number of <%= class_name %>s should be the same"
51
+ assert_response :redirect
52
+ assert_redirected_to REDIRECT_TO_MAIN
53
+ end
54
+
55
+ def test_destroy
56
+ <%= singular_name %>_count = <%= class_name %>.find(:all).length
57
+ post :destroy, {:id => @first.id}
58
+ assert_response :redirect
59
+ assert_equal <%= singular_name %>_count - 1, <%= class_name %>.find(:all).length, "Number of <%= class_name %>s should be one less"
60
+ assert_redirected_to REDIRECT_TO_MAIN
61
+ end
62
+
63
+ protected
64
+ # Could be put in a Helper library and included at top of test class
65
+ def check_attrs(attr_list)
66
+ attrs = []
67
+ attr_list.each do |attr_sym|
68
+ attr = assigns(attr_sym.to_sym)
69
+ assert_not_nil attr, "Attribute @#{attr_sym} should not be nil"
70
+ assert !attr.new_record?, "Should have saved the @#{attr_sym} obj" if attr.class == ActiveRecord
71
+ attrs << attr
72
+ end
73
+ attrs.length > 1 ? attrs : attrs[0]
74
+ end
75
+ end
@@ -0,0 +1,2 @@
1
+ module <%= controller_class_name %>Helper
2
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html
2
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3
+ "DTD/xhtml1-strict.dtd">
4
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
5
+ <head>
6
+ <title><%= Inflector.titleize(plural_name) %></title>
7
+ <%%= stylesheet_link_tag '<%= plural_name %>', :media => 'all' %>
8
+ <%%= javascript_include_tag :defaults %>
9
+ </head>
10
+ <body>
11
+
12
+ <%%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,11 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ # t.column :name, :string
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ drop_table :<%= table_name %>
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ t.column :<%= first_id_column %>, :integer
5
+ t.column :<%= second_id_column %>, :integer
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :<%= table_name %>
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ has_many :<%= manytomany_table_name %>
3
+ has_many :<%= other_table_name %>, :through => :<%= manytomany_table_name %>
4
+ end
@@ -0,0 +1,8 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ belongs_to :<%= first_singular_name %>
3
+ belongs_to :<%= second_singular_name %>
4
+
5
+ validates_presence_of :<%= first_id_column %>, :<%= second_singular_name %>_id
6
+ validates_uniqueness_of :<%= first_singular_name %>_id, :scope => :<%= second_id_column %>
7
+ validates_uniqueness_of :<%= second_id_column %>, :scope => :<%= first_id_column %>
8
+ end
@@ -0,0 +1,3 @@
1
+ <fieldset>
2
+ <!-- Place your <%= class_name %> new/edit form here -->
3
+ </fieldset>
@@ -0,0 +1,5 @@
1
+ .clickable {
2
+ cursor: pointer;
3
+ cursor: hand;
4
+ }
5
+
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
2
+
3
+ class <%= class_name %>Test < Test::Unit::TestCase
4
+ fixtures :<%= plural_name %>, :<%= other_plural_name %>, :<%= manytomany_plural_name %>
5
+
6
+ NEW_<%= singular_name.upcase %> = { } # e.g. {:name => 'Test <%= class_name %>', :description => 'Dummy'}
7
+ REQ_ATTR_NAMES = %w( ) # name of fields that must be present, e.g. %(name description)
8
+ DUPLICATE_ATTR_NAMES = %w( ) # name of fields that cannot be a duplicate, e.g. %(name description)
9
+
10
+ def setup
11
+ # Retrieve fixtures via their name
12
+ # @first = <%= plural_name %>(:first)
13
+ end
14
+
15
+ def test_raw_validation
16
+ <%= singular_name %> = <%= class_name %>.new
17
+ if REQ_ATTR_NAMES.blank?
18
+ assert <%= singular_name %>.valid?, "<%= class_name %> should be valid without initialisation parameters"
19
+ else
20
+ # If <%= class_name %> has validation, then use the following:
21
+ assert !<%= singular_name %>.valid?, "<%= class_name %> should not be valid without initialisation parameters"
22
+ REQ_ATTR_NAMES.each {|attr_name| assert <%= singular_name %>.errors.invalid?(attr_name.to_sym), "Should be an error message for :#{attr_name}"}
23
+ end
24
+ end
25
+
26
+ def test_new
27
+ <%= singular_name %> = <%= class_name %>.new(NEW_<%= singular_name.upcase %>)
28
+ assert <%= singular_name %>.valid?, "<%= class_name %> should be valid"
29
+ NEW_<%= singular_name.upcase %>.each do |attr_name|
30
+ assert_equal NEW_<%= singular_name.upcase %>[attr_name], <%= singular_name %>.attributes[attr_name], "<%= class_name %>.@#{attr_name.to_s} incorrect"
31
+ end
32
+ end
33
+
34
+ def test_validates_presence_of
35
+ REQ_ATTR_NAMES.each do |attr_name|
36
+ tmp_<%= singular_name %> = NEW_<%= singular_name.upcase %>.clone
37
+ tmp_<%= singular_name %>.delete attr_name.to_sym
38
+ <%= singular_name %> = <%= class_name %>.new(tmp_<%= singular_name %>)
39
+ assert !<%= singular_name %>.valid?, "<%= class_name %> should be invalid, as @#{attr_name} is invalid"
40
+ assert <%= singular_name %>.errors.invalid?(attr_name.to_sym), "Should be an error message for :#{attr_name}"
41
+ end
42
+ end
43
+
44
+ def test_duplicate
45
+ current_<%= singular_name %> = <%= class_name %>.find_first
46
+ DUPLICATE_ATTR_NAMES.each do |attr_name|
47
+ <%= singular_name %> = <%= class_name %>.new(NEW_<%= singular_name.upcase %>.merge(attr_name.to_sym => current_<%= singular_name %>[attr_name]))
48
+ assert !<%= singular_name %>.valid?, "<%= class_name %> should be invalid, as @#{attr_name} is a duplicate"
49
+ assert <%= singular_name %>.errors.invalid?(attr_name.to_sym), "Should be an error message for :#{attr_name}"
50
+ end
51
+ end
52
+
53
+ def test_<%= manytomany_plural_name %>_to_<%= singular_name %>
54
+ assert_equal <%= plural_name %>(:first), <%= manytomany_class_name %>.find_first.<%= singular_name %>, "<%= manytomany_class_name %>.<%= singular_name %> should be a <%= class_name %>"
55
+ end
56
+
57
+ def test_<%= singular_name %>_to_<%= manytomany_plural_name %>
58
+ assert_not_nil <%= plural_name %>(:first).<%= manytomany_plural_name %>, "<%= class_name %>.<%= plural_name %> should not be nil"
59
+ assert_equal <%= manytomany_class_name %>, <%= plural_name %>(:first).<%= manytomany_plural_name %>[0].class, "<%= class_name %>.<%= manytomany_plural_name %> should be an array of <%= manytomany_class_name %>"
60
+ end
61
+
62
+ def test_<%= other_plural_name %>
63
+ assert_not_nil <%= plural_name %>(:first).<%= other_plural_name %>, "<%= class_name %>.<%= other_plural_name %> should not be nil"
64
+ assert_equal <%= other_class_name %>, <%= plural_name %>(:first).<%= other_plural_name %>[0].class, "<%= class_name %>.<%= other_plural_name %> should be an array of <%= other_class_name %>"
65
+ end
66
+
67
+ end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper'
2
+
3
+ class <%= class_name %>Test < Test::Unit::TestCase
4
+ fixtures :<%= first_table_name %>, :<%= second_table_name %>, :<%= table_name %>
5
+
6
+ NEW_<%= singular_name.upcase %> = { :<%= first_id_column %> => 3, :<%= second_id_column %> => 3 }
7
+ REQ_ATTR_NAMES = %w( <%= first_id_column %> <%= second_id_column %> ) # name of fields that must be present, e.g. %(name description)
8
+
9
+ def setup
10
+ # Retrieve fixtures via their name
11
+ # @<%= first_singular_name %>1_and_<%= second_singular_name %>1 = <%= table_name %>(:<%= first_singular_name %>1_and_<%= second_singular_name %>1)
12
+ end
13
+
14
+ def test_raw_validation
15
+ <%= singular_name %> = <%= class_name %>.new
16
+ if REQ_ATTR_NAMES.blank?
17
+ assert <%= singular_name %>.valid?, "<%= class_name %> should be valid without initialisation parameters"
18
+ else
19
+ # If <%= class_name %> has validation, then use the following:
20
+ assert !<%= singular_name %>.valid?, "<%= class_name %> should not be valid without initialisation parameters"
21
+ REQ_ATTR_NAMES.each {|attr_name| assert <%= singular_name %>.errors.invalid?(attr_name.to_sym), "Should be an error message for :#{attr_name}"}
22
+ end
23
+ end
24
+
25
+ def test_new
26
+ <%= singular_name %> = <%= class_name %>.new(NEW_<%= singular_name.upcase %>)
27
+ assert <%= singular_name %>.valid?, "<%= class_name %> should be valid"
28
+ NEW_<%= singular_name.upcase %>.each do |attr_name|
29
+ assert_equal NEW_<%= singular_name.upcase %>[attr_name], <%= singular_name %>.attributes[attr_name], "<%= class_name %>.@#{attr_name.to_s} incorrect"
30
+ end
31
+ end
32
+
33
+ def test_validates_presence_of
34
+ REQ_ATTR_NAMES.each do |attr_name|
35
+ tmp_<%= singular_name %> = NEW_<%= singular_name.upcase %>.clone
36
+ tmp_<%= singular_name %>.delete attr_name.to_sym
37
+ <%= singular_name %> = <%= class_name %>.new(tmp_<%= singular_name %>)
38
+ assert !<%= singular_name %>.valid?, "<%= class_name %> should be invalid, as @#{attr_name} is invalid"
39
+ assert <%= singular_name %>.errors.invalid?(attr_name.to_sym), "Should be an error message for :#{attr_name}"
40
+ end
41
+ end
42
+
43
+ def test_duplicate
44
+ current_<%= singular_name %> = <%= class_name %>.find_first
45
+ <%= singular_name %> = <%= class_name %>.new(current_<%= singular_name %>.attributes)
46
+ assert !<%= singular_name %>.valid?, "<%= class_name %> should be invalid, as <%= class_name %> is a duplicate"
47
+ end
48
+
49
+ def test_<%= plural_name %>_to_<%= first_singular_name %>
50
+ assert_equal <%= first_table_name %>(:first), <%= table_name %>(:<%= first_singular_name %>1_<%= second_singular_name %>1).<%= first_singular_name %>, "<%= class_name %>.<%= first_singular_name %> should be a <%= first_class_name %>"
51
+ end
52
+
53
+ def test_<%= first_singular_name %>_to_<%= plural_name %>
54
+ assert_not_nil <%= first_table_name %>(:first).<%= plural_name %>, "<%= first_class_name %>.<%= plural_name %> should not be nil"
55
+ assert_equal <%= class_name %>, <%= first_table_name %>(:first).<%= plural_name %>[0].class, "<%= first_class_name %>.<%= plural_name %> should be an array of <%= class_name %>"
56
+ assert_equal 2, <%= first_table_name %>(:first).<%= plural_name %>.length, "Incorrect number of <%= class_name %>"
57
+ end
58
+
59
+ def test_<%= plural_name %>_to_<%= second_singular_name %>
60
+ assert_equal <%= second_table_name %>(:first), <%= table_name %>(:<%= first_singular_name %>1_<%= second_singular_name %>1).<%= second_singular_name %>, "<%= class_name %>.<%= second_singular_name %> should be a <%= second_class_name %>"
61
+ end
62
+
63
+ def test_<%= second_singular_name %>_to_<%= plural_name %>
64
+ assert_not_nil <%= second_table_name %>(:first).<%= plural_name %>, "<%= second_class_name %>.<%= plural_name %> should not be nil"
65
+ assert_equal <%= class_name %>, <%= second_table_name %>(:first).<%= plural_name %>[0].class, "<%= second_class_name %>.<%= plural_name %> should be an array of <%= class_name %>"
66
+ assert_equal 1, <%= second_table_name %>(:first).<%= plural_name %>.length, "Incorrect number of <%= class_name %>"
67
+ end
68
+ end