Skip to content

Commit

Permalink
add namespaced-resource support to scaffold generator
Browse files Browse the repository at this point in the history
- Closes rspec#331.

e.g. `rails g scaffold admin/user name:string order:integer`
  • Loading branch information
tjmcewan authored and dchelimsky committed Mar 29, 2011
1 parent ab18f4b commit 85f54b0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 55 deletions.
22 changes: 20 additions & 2 deletions lib/generators/rspec/scaffold/scaffold_generator.rb
Expand Up @@ -65,6 +65,24 @@ def params
"{'these' => 'params'}"
end

# support for namespaced-resources
def ns_file_name
if $ARGV[0].match(/(\w+)\/(\w+)/)
"#{$1.underscore}_#{$2.singularize.underscore}"
else
file_name
end
end

# support for namespaced-resources
def ns_table_name
if $ARGV[0].match(/(\w+)\/(\w+)/)
"#{$1.underscore}/#{$2.tableize}"
else
table_name
end
end

# Returns the name of the mock. For example, if the file name is user,
# it returns mock_user.
#
Expand All @@ -81,9 +99,9 @@ def mock_file_name(hash=nil)
if hash
method, and_return = hash.to_a.first
method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
"mock_#{file_name}(:#{method} => #{and_return})"
"mock_#{ns_file_name}(:#{method} => #{and_return})"
else
"mock_#{file_name}"
"mock_#{ns_file_name}"
end
end

Expand Down
46 changes: 23 additions & 23 deletions lib/generators/rspec/scaffold/templates/controller_spec.rb
Expand Up @@ -21,85 +21,85 @@ def <%= mock_file_name %>(stubs={})
<% end -%>
describe "GET show" do
it "assigns the requested <%= file_name %> as @<%= file_name %>" do
it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
get :show, :id => "37"
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
end
describe "GET new" do
it "assigns a new <%= file_name %> as @<%= file_name %>" do
it "assigns a new <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.build(class_name) %> { <%= mock_file_name %> }
get :new
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
end
describe "GET edit" do
it "assigns the requested <%= file_name %> as @<%= file_name %>" do
it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
get :edit, :id => "37"
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
end
describe "POST create" do
describe "with valid params" do
it "assigns a newly created <%= file_name %> as @<%= file_name %>" do
it "assigns a newly created <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.build(class_name, params) %> { <%= mock_file_name(:save => true) %> }
post :create, :<%= file_name %> => <%= params %>
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
post :create, :<%= ns_file_name %> => <%= params %>
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
it "redirects to the created <%= file_name %>" do
it "redirects to the created <%= ns_file_name %>" do
<%= stub orm_class.build(class_name) %> { <%= mock_file_name(:save => true) %> }
post :create, :<%= file_name %> => {}
post :create, :<%= ns_file_name %> => {}
response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
it "assigns a newly created but unsaved <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.build(class_name, params) %> { <%= mock_file_name(:save => false) %> }
post :create, :<%= file_name %> => <%= params %>
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
post :create, :<%= ns_file_name %> => <%= params %>
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
it "re-renders the 'new' template" do
<%= stub orm_class.build(class_name) %> { <%= mock_file_name(:save => false) %> }
post :create, :<%= file_name %> => {}
post :create, :<%= ns_file_name %> => {}
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested <%= file_name %>" do
it "updates the requested <%= ns_file_name %>" do
<%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
mock_<%= should_receive orm_instance.update_attributes(params) %>
put :update, :id => "37", :<%= file_name %> => <%= params %>
put :update, :id => "37", :<%= ns_file_name %> => <%= params %>
end
it "assigns the requested <%= file_name %> as @<%= file_name %>" do
it "assigns the requested <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
put :update, :id => "1"
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
it "redirects to the <%= file_name %>" do
it "redirects to the <%= ns_file_name %>" do
<%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => true) %> }
put :update, :id => "1"
response.should redirect_to(<%= table_name.singularize %>_url(<%= mock_file_name %>))
end
end
describe "with invalid params" do
it "assigns the <%= file_name %> as @<%= file_name %>" do
it "assigns the <%= ns_file_name %> as @<%= ns_file_name %>" do
<%= stub orm_class.find(class_name) %> { <%= mock_file_name(:update_attributes => false) %> }
put :update, :id => "1"
assigns(:<%= file_name %>).should be(<%= mock_file_name %>)
assigns(:<%= ns_file_name %>).should be(<%= mock_file_name %>)
end
it "re-renders the 'edit' template" do
Expand All @@ -111,7 +111,7 @@ def <%= mock_file_name %>(stubs={})
end
describe "DELETE destroy" do
it "destroys the requested <%= file_name %>" do
it "destroys the requested <%= ns_file_name %>" do
<%= stub orm_class.find(class_name, "37".inspect) %> { <%= mock_file_name %> }
mock_<%= should_receive orm_instance.destroy %>
delete :destroy, :id => "37"
Expand Down
14 changes: 7 additions & 7 deletions lib/generators/rspec/scaffold/templates/edit_spec.rb
@@ -1,29 +1,29 @@
require 'spec_helper'

<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= table_name %>/edit.html.<%= options[:template_engine] %>" do
describe "<%= ns_table_name %>/edit.html.<%= options[:template_engine] %>" do
before(:each) do
@<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
@<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
<%= output_attributes.empty? ? "" : " ))\n" -%>
end
it "renders the edit <%= file_name %> form" do
it "renders the edit <%= ns_file_name %> form" do
render
<% if webrat? -%>
rendered.should have_selector("form", :action => <%= file_name %>_path(@<%= file_name %>), :method => "post") do |form|
rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= ns_file_name %>), :method => "post") do |form|
<% for attribute in output_attributes -%>
form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
<% end -%>
end
<% else -%>
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => <%= index_helper %>_path(@<%= file_name %>), :method => "post" do
assert_select "form", :action => <%= index_helper %>_path(@<%= ns_file_name %>), :method => "post" do
<% for attribute in output_attributes -%>
assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]"
<% end -%>
end
<% end -%>
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/rspec/scaffold/templates/index_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= table_name %>/index.html.<%= options[:template_engine] %>" do
describe "<%= ns_table_name %>/index.html.<%= options[:template_engine] %>" do
before(:each) do
assign(:<%= table_name %>, [
<% [1,2].each_with_index do |id, model_index| -%>
Expand All @@ -16,7 +16,7 @@
])
end
it "renders a list of <%= table_name %>" do
it "renders a list of <%= ns_table_name %>" do
render
<% for attribute in output_attributes -%>
<% if webrat? -%>
Expand Down
10 changes: 5 additions & 5 deletions lib/generators/rspec/scaffold/templates/new_spec.rb
@@ -1,28 +1,28 @@
require 'spec_helper'

<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= table_name %>/new.html.<%= options[:template_engine] %>" do
describe "<%= ns_table_name %>/new.html.<%= options[:template_engine] %>" do
before(:each) do
assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
<%= !output_attributes.empty? ? " ).as_new_record)\n end" : " end" %>
it "renders new <%= file_name %> form" do
it "renders new <%= ns_file_name %> form" do
render
<% if webrat? -%>
rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
<% for attribute in output_attributes -%>
form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
form.should have_selector("<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]")
<% end -%>
end
<% else -%>
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => <%= index_helper %>_path, :method => "post" do
<% for attribute in output_attributes -%>
assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= attribute.name %>", :name => "<%= ns_file_name %>[<%= attribute.name %>]"
<% end -%>
end
<% end -%>
Expand Down
28 changes: 14 additions & 14 deletions lib/generators/rspec/scaffold/templates/routing_spec.rb
Expand Up @@ -4,33 +4,33 @@
describe "routing" do

<% unless options[:singleton] -%>
it "recognizes and generates #index" do
get("/<%= table_name %>").should route_to("<%= table_name %>#index")
it "routes to #index" do
get("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#index")
end

<% end -%>
it "recognizes and generates #new" do
get("/<%= table_name %>/new").should route_to("<%= table_name %>#new")
it "routes to #new" do
get("/<%= ns_table_name %>/new").should route_to("<%= ns_table_name %>#new")
end

it "recognizes and generates #show" do
get("/<%= table_name %>/1").should route_to("<%= table_name %>#show", :id => "1")
it "routes to #show" do
get("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#show", :id => "1")
end

it "recognizes and generates #edit" do
get("/<%= table_name %>/1/edit").should route_to("<%= table_name %>#edit", :id => "1")
it "routes to #edit" do
get("/<%= ns_table_name %>/1/edit").should route_to("<%= ns_table_name %>#edit", :id => "1")
end

it "recognizes and generates #create" do
post("/<%= table_name %>").should route_to("<%= table_name %>#create")
it "routes to #create" do
post("/<%= ns_table_name %>").should route_to("<%= ns_table_name %>#create")
end

it "recognizes and generates #update" do
put("/<%= table_name %>/1").should route_to("<%= table_name %>#update", :id => "1")
it "routes to #update" do
put("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#update", :id => "1")
end

it "recognizes and generates #destroy" do
delete("/<%= table_name %>/1").should route_to("<%= table_name %>#destroy", :id => "1")
it "routes to #destroy" do
delete("/<%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#destroy", :id => "1")
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/rspec/scaffold/templates/show_spec.rb
@@ -1,9 +1,9 @@
require 'spec_helper'

<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= table_name %>/show.html.<%= options[:template_engine] %>" do
describe "<%= ns_table_name %>/show.html.<%= options[:template_engine] %>" do
before(:each) do
@<%= file_name %> = assign(:<%= file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
@<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
Expand Down

0 comments on commit 85f54b0

Please sign in to comment.