my_generators 0.0.1

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.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +17 -0
  5. data/Rakefile +52 -0
  6. data/VERSION +1 -0
  7. data/lib/generators/my_generators/controller/controller_generator.rb +18 -0
  8. data/lib/generators/my_generators/controller/templates/view.html.haml +2 -0
  9. data/lib/generators/my_generators/model/model_generator.rb +28 -0
  10. data/lib/generators/my_generators/model/templates/model.rb +26 -0
  11. data/lib/generators/my_generators/scaffold/scaffold_generator.rb +49 -0
  12. data/lib/generators/my_generators/scaffold/templates/_form.html.haml +15 -0
  13. data/lib/generators/my_generators/scaffold/templates/edit.html.haml +7 -0
  14. data/lib/generators/my_generators/scaffold/templates/index.html.haml +23 -0
  15. data/lib/generators/my_generators/scaffold/templates/new.html.haml +5 -0
  16. data/lib/generators/my_generators/scaffold/templates/show.html.haml +11 -0
  17. data/lib/generators/my_scaffold_generator.rb +161 -0
  18. data/lib/generators/my_spec/controller/controller_generator.rb +33 -0
  19. data/lib/generators/my_spec/controller/templates/controller_spec.rb +14 -0
  20. data/lib/generators/my_spec/controller/templates/view_spec.rb +5 -0
  21. data/lib/generators/my_spec/helper/helper_generator.rb +20 -0
  22. data/lib/generators/my_spec/helper/templates/helper_spec.rb +15 -0
  23. data/lib/generators/my_spec/integration/integration_generator.rb +20 -0
  24. data/lib/generators/my_spec/integration/templates/request_spec.rb +14 -0
  25. data/lib/generators/my_spec/model/model_generator.rb +33 -0
  26. data/lib/generators/my_spec/model/templates/fixtures.yml +19 -0
  27. data/lib/generators/my_spec/model/templates/model_spec.rb +20 -0
  28. data/lib/generators/my_spec/model/templates/mongoid.rb +4 -0
  29. data/lib/generators/my_spec/observer/observer_generator.rb +16 -0
  30. data/lib/generators/my_spec/observer/templates/observer_spec.rb +5 -0
  31. data/lib/generators/my_spec/scaffold/scaffold_generator.rb +93 -0
  32. data/lib/generators/my_spec/scaffold/templates/controller_spec.rb +140 -0
  33. data/lib/generators/my_spec/scaffold/templates/edit_spec.rb +38 -0
  34. data/lib/generators/my_spec/scaffold/templates/index_spec.rb +37 -0
  35. data/lib/generators/my_spec/scaffold/templates/inherited_resources_stubs.rb +28 -0
  36. data/lib/generators/my_spec/scaffold/templates/new_spec.rb +37 -0
  37. data/lib/generators/my_spec/scaffold/templates/routing_spec.rb +37 -0
  38. data/lib/generators/my_spec/scaffold/templates/show_spec.rb +33 -0
  39. data/lib/generators/my_spec/view/templates/view_spec.rb +5 -0
  40. data/lib/generators/my_spec/view/view_generator.rb +24 -0
  41. data/lib/generators/rails/USAGE +10 -0
  42. data/lib/generators/rails/my_controller_generator.rb +33 -0
  43. data/lib/generators/rails/templates/controller.rb +13 -0
  44. data/lib/my_generators.rb +16 -0
  45. data/my_generators.gemspec +88 -0
  46. data/test/helper.rb +10 -0
  47. data/test/test_my_generators.rb +7 -0
  48. metadata +112 -0
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name %> do
4
+ <% for attribute in attributes -%>
5
+ it { should have_field(:<%= attribute.name %>).of_type(<%= attribute.type_class %>) }
6
+ <% end -%>
7
+ <% if child_model? -%>
8
+ it {should <%= association_matcher %>(:<%=parent_name%>).as_inverse_of(:<%=parent_association%>)}
9
+ <% end -%>
10
+ <% if references_one -%> it {should reference_one(:<%=references_one%>)} <% end %>
11
+ <% if references_many -%> it {should reference_many(:<%=references_many%>)} <% end %>
12
+ <% if embeds_one -%> it {should embed_one(:<%=embeds_one%>)} <% end %>
13
+ <% if embeds_many -%> it {should embed_many(:<%=embeds_many%>)} <% end %>
14
+ end
15
+
16
+ <% if child_model? -%>
17
+ describe <%= parent_class %> do
18
+ it {should <%= parent_association_matcher %>(:<%=parent_association%>)}
19
+ end
20
+ <% end -%>
@@ -0,0 +1,4 @@
1
+ RSpec.configure do |configuration|
2
+ configuration.include Mongoid::Matchers
3
+ end
4
+
@@ -0,0 +1,16 @@
1
+ require 'generators/my_scaffold_generator'
2
+
3
+ module MySpec
4
+ module Generators
5
+ class ObserverGenerator < Rails::Generators::NamedBase
6
+ include MyGenerators::Generators::MyScaffoldGenerator
7
+ def generate_observer_spec
8
+ template 'observer_spec.rb',
9
+ File.join('spec', 'models', class_path, "#{file_name}_observer_spec.rb")
10
+ end
11
+ def self.source_root
12
+ @source_root ||= File.expand_path("templates", File.dirname(__FILE__))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name %>Observer do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
@@ -0,0 +1,93 @@
1
+ require 'generators/rspec'
2
+ require 'generators/my_scaffold_generator'
3
+ require 'rails/generators/resource_helpers'
4
+
5
+ module MySpec
6
+ module Generators
7
+ class ScaffoldGenerator < Rails::Generators::NamedBase
8
+ include Rails::Generators::ResourceHelpers
9
+ include MyGenerators::Generators::MyScaffoldGenerator
10
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
11
+
12
+ def generate_controller_spec
13
+ return unless options[:controller_specs]
14
+ template 'inherited_resources_stubs.rb', 'spec/support/inherited_resources_stubs.rb'
15
+
16
+ template 'controller_spec.rb',
17
+ File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
18
+ end
19
+
20
+ def generate_view_specs
21
+ return unless options[:view_specs]
22
+
23
+ copy_view :edit
24
+ copy_view :index unless options[:singleton]
25
+ copy_view :new
26
+ copy_view :show
27
+ end
28
+
29
+ # Invoke the helper using the controller name (pluralized)
30
+ hook_for :helper, :as => :scaffold do |invoked|
31
+ invoke invoked, [ controller_name ]
32
+ end
33
+
34
+ def generate_routing_spec
35
+ return unless options[:routing_specs]
36
+
37
+ template 'routing_spec.rb',
38
+ File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
39
+ end
40
+
41
+ hook_for :integration_tool, :as => :integration
42
+
43
+
44
+ def banner
45
+ self.class.banner
46
+ end
47
+ def self.source_root
48
+ @source_root ||= File.expand_path("templates", File.dirname(__FILE__))
49
+ end
50
+ protected
51
+ def redirect_url_helper(str=nil)
52
+ if str
53
+ belongs_to ?
54
+ "#{parent_name}_#{model_name}_url(john_q(:#{parent_name})#{', '+str})" :
55
+ "#{model_name}_url(#{str})"
56
+ else
57
+ belongs_to ?
58
+ "#{parent_name}_#{table_name}_url(john_q(:#{parent_name}))" :
59
+ "#{table_name}_url"
60
+ end
61
+ end
62
+ def mock(str=nil)
63
+ "john_q(:#{str || model_name})"
64
+ end
65
+ def stub_finder(idstr, retstr, method="stub")
66
+ if child_model?
67
+ "john_q(:#{parent_name}).#{table_name}.#{method}(:find).with(#{idstr}) { #{retstr} }"
68
+ else
69
+ "#{class_name}.#{method}(:find).with(#{idstr}) { retstr }"
70
+ end
71
+ end
72
+ def stub_index_finder(retstr, method="stub")
73
+ if child_model?
74
+ "john_q(:#{parent_name}).#{table_name}.#{method}(:all) { #{retstr} }"
75
+ else
76
+ "#{class_name}.#{method}(:find).with(:all) { retstr }"
77
+ end
78
+ end
79
+ def base_url
80
+ (belongs_to ? "/#{parents_name}/24" : "") + "/#{singleton ? model_name : table_name}"
81
+ end
82
+
83
+ def stub_builder(retstr)
84
+ if child_model?
85
+ "john_q(:#{parent_name}).#{table_name}.stub(:build) { #{retstr} }"
86
+ else
87
+ "#{class_name}.stub(:new) { retstr }"
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= controller_class_name %>Controller do
4
+ before :each do
5
+ <% if child_model? %>
6
+ <%=parent_class%>.stub(:find).with(anything()) {john_q :<%=parent_name%>}
7
+ <% unless belongs_to %>
8
+ controller.stub("<%=parent_name%>_<%=model_name%>_url".to_sym).with(any_args()) {"/some/url"}
9
+ <% end %>
10
+ <% end %>
11
+ end
12
+
13
+ <% unless options[:singleton] -%>
14
+ describe "GET index" do
15
+ it "assigns all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
16
+ <%= stub_index_finder("[john_q(:#{model_name})]") %>
17
+ get :index<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
18
+ <% if child_model? %>
19
+ assigns(:<%= parent_name %>).should eq(john_q(:<%= parent_name %>))
20
+ <% else %>
21
+ assigns(:<%= table_name %>).should eq([john_q(:<%= model_name %>)])
22
+ <% end %>
23
+ end
24
+ end
25
+
26
+ <% end -%>
27
+ describe "GET show" do
28
+ it "assigns the requested <%= file_name %> as @<%= file_name %>" do
29
+ <%= stub_finder("37".inspect, "john_q(:#{model_name})") %>
30
+ get :show, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
31
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
32
+ end
33
+ end
34
+
35
+ describe "GET new" do
36
+ it "assigns a new <%= file_name %> as @<%= file_name %>" do
37
+ <%= stub_builder "john_q :#{model_name}" %>
38
+ get :new<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
39
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
40
+ end
41
+ end
42
+
43
+ describe "GET edit" do
44
+ it "assigns the requested <%= file_name %> as @<%= file_name %>" do
45
+ <%= stub_finder("37".inspect, "john_q(:#{model_name})") %>
46
+ get :edit, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
47
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
48
+ end
49
+ end
50
+
51
+ describe "POST create" do
52
+
53
+ describe "with valid params" do
54
+ it "assigns a newly created <%= file_name %> as @<%= file_name %>" do
55
+ <%= stub_builder "john_q :#{class_name}, :save => true" %>
56
+ post :create, :<%= file_name %> => <%= params %><%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
57
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
58
+ end
59
+
60
+ it "redirects to the created <%= file_name %>" do
61
+ <%= stub_builder "john_q :#{class_name}, :save => true" %>
62
+ post :create, :<%= file_name %> => {}<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
63
+ response.should redirect_to(<%= redirect_url_helper("john_q(:#{model_name})") %>)
64
+ end
65
+ end
66
+
67
+ describe "with invalid params" do
68
+ it "assigns a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
69
+ <%= stub_builder "john_q :#{class_name}, :save => false" %>
70
+ post :create, :<%= file_name %> => <%= params %><%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
71
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
72
+ end
73
+
74
+ it "re-renders the 'new' template" do
75
+ <%= stub_builder "john_q :#{class_name}, :save => false" %>
76
+ john_q(:<%= model_name %>).stub_chain("errors.empty?") {false}
77
+ post :create, :<%= file_name %> => {}<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
78
+ response.should render_template("")
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ describe "PUT update" do
85
+
86
+ describe "with valid params" do
87
+ before :each do
88
+ <%= stub_finder("37".inspect, "john_q(:#{model_name}, :update_attributes => true)", "should_receive") %>
89
+ end
90
+ it "updates the requested <%= file_name %>" do
91
+ john_q(:<%=model_name%>).should_receive(:update_attributes).with(<%=params%>) {true}
92
+ put :update, :id => "37", :<%= file_name %> => <%= params %><%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
93
+ end
94
+
95
+ it "assigns the requested <%= file_name %> as @<%= file_name %>" do
96
+ put :update, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
97
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
98
+ end
99
+
100
+ it "redirects to the <%= file_name %>" do
101
+ put :update, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
102
+ response.should redirect_to(<%= redirect_url_helper("john_q(:#{model_name})") %>)
103
+ end
104
+ end
105
+
106
+ describe "with invalid params" do
107
+ before :each do
108
+ <%= stub_finder("37".inspect, "john_q(:#{model_name}, :update_attributes => false)", "should_receive") %>
109
+ john_q(:<%= model_name %>).stub_chain("errors.empty?") {false}
110
+ end
111
+ it "assigns the <%= file_name %> as @<%= file_name %>" do
112
+ put :update, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
113
+ assigns(:<%= file_name %>).should be(john_q(:<%= model_name %>))
114
+ end
115
+
116
+ it "re-renders the 'edit' template" do
117
+ put :update, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
118
+ response.should render_template("")
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ describe "DELETE destroy" do
125
+ before :each do
126
+ <%= stub_finder("37".inspect, "john_q(:#{model_name})", "should_receive") %>
127
+ john_q(:<%= model_name %>).stub_chain("errors.empty?") {false}
128
+ end
129
+ it "destroys the requested <%= file_name %>" do
130
+ john_q(:<%= model_name %>).should_receive(:destroy)
131
+ delete :destroy, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
132
+ end
133
+
134
+ it "redirects to the <%= table_name %> list" do
135
+ delete :destroy, :id => "37"<%= ", :#{parent_id} => john_q(:#{parent_name}).id.to_s" if belongs_to %>
136
+ response.should redirect_to(<%= redirect_url_helper %>)
137
+ end
138
+ end
139
+
140
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
+ describe "<%= table_name %>/edit.html.haml" do
5
+ before(:each) do
6
+ @<%= file_name %> = assign(:<%= file_name %>, john_q(:<%=model_name%>, :new_record? => false<%= output_attributes.empty? ? '' : ',' %>
7
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
+ :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
+ <% end -%>
10
+ ))
11
+ <% if child_model? %>
12
+ <%=parent_class%>.stub(:find).with(anything()) {john_q :<%=parent_name%>}
13
+ <% end %>
14
+ <% if belongs_to %>
15
+ params[:<%=parent_id%>] = john_q(:<%=parent_name%>).id
16
+ @<%= parent_name %> = assign(:<%=parent_name%>, john_q(:<%=parent_name%>))
17
+ <% end %>
18
+
19
+ end
20
+ it "renders the edit <%= file_name %> form" do
21
+ render
22
+
23
+ <% if webrat? -%>
24
+ rendered.should have_selector("form", :action => <%= resource_url_helper() %>, :method => "post") do |form|
25
+ <% for attribute in output_attributes -%>
26
+ form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
27
+ <% end -%>
28
+ end
29
+ <% else -%>
30
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
31
+ assert_select "form", :action => <%= file_name %>_path(@<%= file_name %>), :method => "post" do
32
+ <% for attribute in output_attributes -%>
33
+ assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
34
+ <% end -%>
35
+ end
36
+ <% end -%>
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
+ describe "<%= table_name %>/index.html.haml" do
5
+ before(:each) do
6
+ assign(:<%= table_name %>, [
7
+ <% [1,2].each_with_index do |id, model_index| -%>
8
+ stub_model(<%= class_name %><%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : ',' %>
9
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
10
+ :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
11
+ <% end -%>
12
+ <% if !output_attributes.empty? -%>
13
+ <%= model_index == 1 ? ')' : '),' %>
14
+ <% end -%>
15
+ <% end -%>
16
+ ])
17
+ <% if child_model? %>
18
+ <%=parent_class%>.stub(:find).with(anything()) {john_q :<%=parent_name%>}
19
+ <% end %>
20
+ end
21
+
22
+ it "renders a list of <%= table_name %>" do
23
+ <% if belongs_to %>
24
+ params[:<%=parent_id%>] = john_q(:<%=parent_name%>).id
25
+ @<%= parent_name %> = assign(:<%=parent_name%>, john_q(:<%=parent_name%>))
26
+ <% end %>
27
+ render
28
+ <% for attribute in output_attributes -%>
29
+ <% if webrat? -%>
30
+ rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
31
+ <% else -%>
32
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
33
+ assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
34
+ <% end -%>
35
+ <% end -%>
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+
2
+ def john_q(type, stubs={})
3
+ any_old type, stubs
4
+ end
5
+
6
+ def any_old(type, stubs={})
7
+ type = type.to_s.camelize.constantize
8
+ ivar = '@mock_' + type.name.downcase
9
+ (mock_obj = instance_variable_get(ivar) || mock_model(type).as_null_object).tap do |node|
10
+ node.stub(stubs) unless stubs.empty?
11
+ end
12
+ instance_variable_set(ivar, mock_obj)
13
+ mock_obj
14
+ end
15
+
16
+ def stub_inherited_resources_helpers(view, resource=nil)
17
+ view.stub(:resource_url) {"/foobar"}
18
+ view.stub(:edit_resource_url) {"/foobar"}
19
+ view.stub(:resource_url) {"/projects/#{resource.try(:id) || '1'}/tags/1}"}
20
+ view.stub(:resource_url).with(resource) {"/projects/#{resource.try(:id) || '1'}/tags/1"}
21
+ view.stub(:new_resource_url) {"/projects/#{resource.try(:id) || '1'}/tags/new"}
22
+ view.stub(:edit_resource_url) {"/projects/#{resource.try(:id) || '1'}/tags/#{resource.try(:id) || '1'}/edit"}
23
+ view.stub(:edit_resource_url).with(resource) {"/projects/1/tags/#{resource.try(:id) || '1'}/edit"}
24
+ view.stub(:collection_url) {"/projects/1/tags"}
25
+ view.stub(:parent_url) {"/projects/1"}
26
+ end
27
+
28
+
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
+ describe "<%= table_name %>/new.html.haml" do
5
+ before(:each) do
6
+ <% if child_model? %>
7
+ <%=parent_class%>.stub(:find).with(anything()) {john_q :<%=parent_name%>}
8
+ <% end %>
9
+ <% if belongs_to %>
10
+ params[:<%=parent_id%>] = john_q(:<%=parent_name%>).id
11
+ @<%= parent_name %> = assign(:<%=parent_name%>, john_q(:<%=parent_name%>))
12
+ <% end %>
13
+ @<%=model_name%> = assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
14
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
15
+ :<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
16
+ <% end -%>
17
+ <%= !output_attributes.empty? ? " ).as_new_record)\n end" : " end" %>
18
+
19
+ it "renders new <%= file_name %> form" do
20
+ render
21
+
22
+ <% if webrat? -%>
23
+ rendered.should have_selector("form", :action => <%= index_url_helper %>, :method => "post") do |form|
24
+ <% for attribute in output_attributes -%>
25
+ form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
26
+ <% end -%>
27
+ end
28
+ <% else -%>
29
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
30
+ assert_select "form", :action => <%= table_name %>_path, :method => "post" do
31
+ <% for attribute in output_attributes -%>
32
+ assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
33
+ <% end -%>
34
+ end
35
+ <% end -%>
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe <%= controller_class_name %>Controller do
4
+ describe "routing" do
5
+
6
+ <% unless options[:singleton] -%>
7
+ it "recognizes and generates #index" do
8
+ { :get => "<%= base_url %>" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "index")
9
+ end
10
+
11
+ <% end -%>
12
+ it "recognizes and generates #new" do
13
+ { :get => "<%= base_url %>/new" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "new")
14
+ end
15
+
16
+ it "recognizes and generates #show" do
17
+ { :get => "<%= base_url %>/1" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "show", :id => "1")
18
+ end
19
+
20
+ it "recognizes and generates #edit" do
21
+ { :get => "<%= base_url %>/1/edit" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "edit", :id => "1")
22
+ end
23
+
24
+ it "recognizes and generates #create" do
25
+ { :post => "<%= base_url %>" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "create")
26
+ end
27
+
28
+ it "recognizes and generates #update" do
29
+ { :put => "<%= base_url %>/1" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "update", :id => "1")
30
+ end
31
+
32
+ it "recognizes and generates #destroy" do
33
+ { :delete => "<%= base_url %>/1" }.should route_to(:controller => "<%= table_name %>", <%= ":#{parent_id} => '24', " if belongs_to %>:action => "destroy", :id => "1")
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
+ describe "<%= table_name %>/show.html.haml" do
5
+ before(:each) do
6
+ @<%= file_name %> = assign(:<%= file_name %>, john_q(:<%=model_name%>, :new_record? => true<%= output_attributes.empty? ? '' : ',' %>
7
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
8
+ :<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
9
+ <% end -%>
10
+ <% if !output_attributes.empty? -%>
11
+ ))
12
+ <% end -%>
13
+ <% if child_model? %>
14
+ <%=parent_class%>.stub(:find).with(anything()) {john_q :<%=parent_name%>}
15
+ <% end %>
16
+ <% if belongs_to %>
17
+ params[:<%=parent_id%>] = john_q(:<%=parent_name%>).id
18
+ @<%= parent_name %> = assign(:<%=parent_name%>, john_q(:<%=parent_name%>))
19
+ <% end %>
20
+ end
21
+
22
+ it "renders attributes in <p>" do
23
+ render
24
+ <% for attribute in output_attributes -%>
25
+ <% if webrat? -%>
26
+ rendered.should contain(<%= value_for(attribute) %>.to_s)
27
+ <% else -%>
28
+ # Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
29
+ rendered.should match(/<%= eval(value_for(attribute)) %>/)
30
+ <% end -%>
31
+ <% end -%>
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe "<%= file_name %>/<%= @action %>.html.<%= options[:template_engine] %>" do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
@@ -0,0 +1,24 @@
1
+ require 'generators/my_scaffold_generator'
2
+
3
+ module MySpec
4
+ module Generators
5
+ class ViewGenerator < Rails::Generators::NamedBase
6
+ include MyGenerators::Generators::MyScaffoldGenerator
7
+ argument :actions, :type => :array, :default => [], :banner => "action action"
8
+ def create_view_specs
9
+ empty_directory File.join("spec", "views", file_path)
10
+
11
+ actions.each do |action|
12
+ @action = action
13
+ template 'view_spec.rb',
14
+ File.join("spec", "views", file_path, "#{@action}.html.#{options[:template_engine]}_spec.rb")
15
+ end
16
+ end
17
+
18
+ def self.source_root
19
+ @source_root ||= File.expand_path("templates", File.dirname(__FILE__))
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Stubs out a scaffolded controller and its views using InheritedResources.
3
+ Pass the model name, either CamelCased or under_scored. The controller
4
+ name is retrieved as a pluralized version of the model name.
5
+
6
+ To create a controller within a module, specify the model name as a
7
+ path like 'parent_module/controller_name'.
8
+
9
+ This generates a controller class in app/controllers and invokes helper,
10
+ template engine and test framework generators.
@@ -0,0 +1,33 @@
1
+ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
2
+ require 'generators/my_scaffold_generator'
3
+
4
+ module Rails
5
+ module Generators
6
+ class MyControllerGenerator < ScaffoldControllerGenerator
7
+ include MyGenerators::Generators::MyScaffoldGenerator
8
+ class_option :belongs_to, :desc => "This controller should be nested", :default=>nil, :required=>false
9
+ class_option :embedded_in, :desc => "This controller is embedded but not nested.", :default=>nil, :required=>false
10
+
11
+ protected
12
+
13
+ def embedded?
14
+ options[:embedded_in]
15
+ end
16
+ def belongs_to?
17
+ options[:belongs_to]
18
+ end
19
+ def parent_name
20
+ options[:embedded_in] || options[:belongs_to]
21
+ end
22
+ def parent_class
23
+ parent_name.camelize
24
+ end
25
+ def parent_id
26
+ parent_name + "_id"
27
+ end
28
+ def self.source_root
29
+ @source_root ||= File.expand_path("templates", File.dirname(__FILE__))
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ class <%= controller_class_name %>Controller < InheritedResources::Base
2
+ <% if options[:singleton] -%>
3
+ defaults :singleton => true
4
+ <% end -%>
5
+ <% if belongs_to -%>
6
+ belongs_to :<%= parent_name %>
7
+ <% end -%>
8
+ <% if child_model? && !belongs_to -%>
9
+ def begin_of_association_chain
10
+ @<%= parent_name %> ||= <%= parent_class %>.find(params[:<%=parent_id%>])
11
+ end
12
+ <% end -%>
13
+ end
@@ -0,0 +1,16 @@
1
+ module MyGenerators
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ config.app_generators do |g|
5
+ g.orm :my_generators
6
+ g.integration_tool :my_spec
7
+ g.test_framework :my_spec
8
+ g.scaffold_controller :my_controller
9
+ g.template_engine :my_generators
10
+ end
11
+ # rake_tasks do
12
+ # load "rspec/rails/tasks/rspec.rake"
13
+ # end
14
+ end
15
+ end
16
+ end