Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update controller and it's spec templates

  • Loading branch information...
commit ffa67693ea6abba31541ebc1becd49649fd46bca 1 parent 5b5dd4d
Martin Emde authored
26 generators/rspecific_scaffold/templates/controller.rb
View
@@ -2,10 +2,10 @@ class <%= controller_class_name %>Controller < ApplicationController
before_filter :find_<%= file_name %>, :only => %w(show edit update destroy)
def index
- @<%= table_name %> = <%= class_name %>.find(:all)
+ @<%= table_name %> = <%= class_name %>.all
respond_to do |format|
- format.html # index.html.erb
+ format.html
format.xml { render :xml => @<%= table_name %> }
format.json { render :json => @<%= table_name %> }
end
@@ -13,7 +13,7 @@ def index
def show
respond_to do |format|
- format.html # show.html.erb
+ format.html
format.xml { render :xml => @<%= file_name %> }
format.json { render :json => @<%= file_name %> }
end
@@ -23,7 +23,7 @@ def new
@<%= file_name %> = <%= class_name %>.new
respond_to do |format|
- format.html # new.html.erb
+ format.html
format.xml { render :xml => @<%= file_name %> }
format.json { render :json => @<%= file_name %> }
end
@@ -34,12 +34,14 @@ def create
respond_to do |format|
if @<%= file_name %>.save
- flash[:notice] = '<%= class_name %> was successfully created.'
- format.html { redirect_to(@<%= file_name %>) }
+ format.html {
+ flash[:notice] = '<%= class_name %> was successfully created.'
+ redirect_to @<%= file_name %>
+ }
format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
format.json { render :json => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
else
- format.html { render :action => "new" }
+ format.html { render :new }
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
format.json { render :json => @<%= file_name %>.errors, :status => :unprocessable_entity }
end
@@ -52,12 +54,14 @@ def edit
def update
respond_to do |format|
if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
- flash[:notice] = '<%= class_name %> was successfully updated.'
- format.html { redirect_to(@<%= file_name %>) }
+ format.html {
+ flash[:notice] = '<%= class_name %> was successfully updated.'
+ redirect_to @<%= file_name %>
+ }
format.xml { head :ok }
format.json { head :ok }
else
- format.html { render :action => "edit" }
+ format.html { render :edit }
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
format.json { render :json => @<%= file_name %>.errors, :status => :unprocessable_entity }
end
@@ -68,7 +72,7 @@ def destroy
@<%= file_name %>.destroy
respond_to do |format|
- format.html { redirect_to(<%= table_name %>_url) }
+ format.html { redirect_to <%= table_name %>_url }
format.xml { head :ok }
format.json { head :ok }
end
309 generators/rspecific_scaffold/templates/functional_spec.rb
View
@@ -1,217 +1,172 @@
-require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper'
+require File.dirname(__FILE__) + '<%='/..' * class_nesting_depth%>/../spec_helper'
-describe <%= controller_class_name %>Controller, "GET #index" do
- before do
- @<%= table_name %> = []
- <%= class_name %>.stub!(:find).with(:all).and_return(@<%= table_name %>)
- end
-
- act! { get :index }
-
- it_assigns :<%= table_name %>
- it_renders :template, :index
-
-<% %w(xml json).each do |format|
-%> describe "(<%= format %>)" do
- act! { get :index, :format => '<%= format %>' }
-
- it_assigns :<%= table_name %>
- it_renders :<%= format %>, :<%= table_name %>
- end
-<% end %>
-end
+describe <%=controller_class_name%>Controller do
-describe <%= controller_class_name %>Controller, "GET #show" do
- before do
- @<%= file_name %> = mock_model <%= class_name %>
- <%= class_name %>.stub!(:find_by_param).with(@<%= file_name %>.to_param).and_return(@<%= file_name %>)
+ def mock_<%=file_name%>(stubs = {})
+ @<%=file_name%> ||= mock_model <%=class_name%>, {:to_param => '<%=file_name%>', :to_xml => 'xml', :to_json => 'json'}.merge(stubs)
+ <%=class_name%>.stub!(:find_by_param).with(@<%=filename%>.to_param).and_return(@<%=file_name%>)
end
- act! { get :show, :id => @<%= file_name %>.to_param }
-
- it_assigns :<%= file_name %>
- it_renders :template, :show
-
-<% %w(xml json).each do |format|
-%> describe "(<%= format %>)" do
+ describe "GET #index" do
before do
- @<%= file_name %>.stub!(:to_<%= format %>).and_return('<%= format %>')
+ @<%=table_name%> = [mock_<%=file_name%>]
+ <%=class_name%>.stub!(:all).and_return(@<%=table_name%>)
end
- act! { get :show, :id => @<%= file_name %>.to_param, :format => '<%= format %>' }
-
- it_renders :<%= format %>, :<%= file_name %>
- end
-
-<% end %>
-end
+ describe "(html)" do
+ act! { get :index }
+ it_assigns :<%=table_name%>
+ it_renders :template, :index
+ end
-describe <%= controller_class_name %>Controller, "GET #new" do
- before do
- @<%= file_name %> = <%= class_name %>.new
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ act! { get :index, :format => '<%=format%>' }
+ it_assigns :<%=table_name%>
+ it_renders :<%=format%>, :<%=table_name%>
+ end
+<% end -%>
end
- act! { get :new }
+ describe "GET #show" do
+ describe "(html)" do
+ act! { get :show, :id => @<%=file_name%>.to_param }
+ it_assigns :<%=file_name%>
+ it_renders :template, :show
+ end
- it "assigns @<%= file_name %>" do
- act!
- assigns[:<%= file_name %>].should be_new_record
- end
-
- it_renders :template, :new
-
-<% %w(xml json).each do |format|
-%> describe "(<%= format %>)" do
- act! { get :new, :format => '<%= format %>' }
-
- it_renders :<%= format %>, :<%= file_name %>
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ act! { get :show, :id => @<%=file_name%>.to_param, :format => '<%=format%>' }
+ it_renders :<%=format%>, :<%=file_name%>
+ end
+<% end -%>
end
-<% end %>
-end
+ describe "GET #new" do
+ before { @<%=file_name%> = <%=class_name%>.new }
-describe <%= controller_class_name %>Controller, "POST #create" do
- before do
- @attributes = {}
- @<%= file_name %> = mock_model <%= class_name %>, :new_record? => false, :errors => []
- <%= class_name %>.stub!(:new).with(@attributes).and_return(@<%= file_name %>)
- end
-
- describe "(successful creation)" do
- before do
- @<%= file_name %>.stub!(:save).and_return(true)
+ describe "(html)" do
+ act! { get :new }
+ it_assigns :<%=file_name%>
+ it_renders :template, :new
end
-
- act! { post :create, :<%= file_name %> => @attributes }
- it_assigns :<%= file_name %>, :flash => { :notice => :not_nil }
- it_redirects_to { <%= file_name %>_path(@<%= file_name %>) }
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ act! { get :new, :format => '<%=format%>' }
+ it_renders :<%=format%>, :<%=file_name%>
+ end
+<% end -%>
end
- describe "(unsuccessful creation)" do
+ describe "POST #create" do
before do
- @<%= file_name %>.stub!(:save).and_return(false)
+ @attributes = { 'attr' => 'value' }
+ @<%=file_name%> = mock_model <%=class_name%>, :new_record? => false, :errors => []
+ <%=class_name%>.stub!(:new).with(@attributes).and_return(@<%=file_name%>)
end
-
- act! { post :create, :<%= file_name %> => @attributes }
- it_assigns :<%= file_name %>
- it_renders :template, :new
- end
-
-<% %w(xml json).each do |format|
-%> describe "(successful creation, <%= format %>)" do
- before do
- @<%= file_name %>.stub!(:save).and_return(true)
- @<%= file_name %>.stub!(:to_<%= format %>).and_return("<%= format %>")
+ describe "(successful creation)" do
+ before { @<%=file_name%>.stub!(:save).and_return(true) }
+
+ describe "(html)" do
+ act! { post :create, :<%=file_name%> => @attributes }
+ it_assigns :<%=file_name%>, :flash => { :notice => :not_nil }
+ it_redirects_to { <%=file_name%>_path(@<%=file_name%>) }
+ end
+
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ act! { post :create, :<%=file_name%> => @attributes, :format => '<%=format%>' }
+ it_assigns :<%=file_name%>, :headers => { :Location => lambda { <%=file_name%>_url(@<%=file_name%>) } }
+ it_renders :<%=format%>, :<%=file_name%>, :status => :created
+ end
+<% end -%>
end
- act! { post :create, :<%= file_name %> => @attributes, :format => '<%= format %>' }
-
- it_assigns :<%= file_name %>, :headers => { :Location => lambda { <%= file_name %>_url(@<%= file_name %>) } }
- it_renders :<%= format %>, :<%= file_name %>, :status => :created
- end
-
- describe "(unsuccessful creation, <%= format %>)" do
- before do
- @<%= file_name %>.stub!(:save).and_return(false)
- @<%= file_name %>.errors.stub!(:to_<%= format %>).and_return('<%= file_name %>.errors')
+ describe "(unsuccessful creation)" do
+ before { @<%=file_name%>.stub!(:save).and_return(false) }
+
+ describe "(html)" do
+ act! { post :create, :<%=file_name%> => @attributes }
+ it_assigns :<%=file_name%>
+ it_renders :template, :new
+ end
+
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ before { mock_<%=file_name%>.errors.stub!(:to_<%=format%>).and_return('errors') }
+ act! { post :create, :<%=file_name%> => @attributes, :format => '<%=format%>' }
+ it_assigns :<%=file_name%>
+ it_renders :<%=format%>, "errors", :status => :unprocessable_entity
+ end
+<% end -%>
end
-
- act! { post :create, :<%= file_name %> => @attributes, :format => '<%= format %>' }
-
- it_assigns :<%= file_name %>
- it_renders :<%= format %>, "<%= file_name %>.errors", :status => :unprocessable_entity
end
-<% end %>end
-
-describe <%= controller_class_name %>Controller, "GET #edit" do
- before do
- @<%= file_name %> = mock_model <%= class_name %>
- <%= class_name %>.stub!(:find_by_param).with(@<%= file_name %>.to_param).and_return(@<%= file_name %>)
+ describe "GET #edit" do
+ act! { get :edit, :id => @<%=file_name%>.to_param }
+ it_assigns :<%=file_name%>
+ it_renders :template, :edit
end
- act! { get :edit, :id => @<%= file_name %>.to_param }
-
- it_assigns :<%= file_name %>
- it_renders :template, :edit
-end
+ describe "PUT #update" do
+ before { @attributes = { 'attr' => 'value' } }
-describe <%= controller_class_name %>Controller, "PUT #update" do
- before do
- @attributes = {}
- @<%= file_name %> = mock_model <%= class_name %>
- <%= class_name %>.stub!(:find_by_param).with(@<%= file_name %>.to_param).and_return(@<%= file_name %>)
- end
-
- describe "(successful save)" do
- before do
- @<%= file_name %>.stub!(:update_attributes).and_return(true)
- end
-
- act! { put :update, :id => @<%= file_name %>.to_param, :<%= file_name %> => @attributes }
+ describe "(successful save)" do
+ before { mock_<%=file_name%>.stub!(:update_attributes).with(@attributes).and_return(true) }
- it_assigns :<%= file_name %>, :flash => { :notice => :not_nil }
- it_redirects_to { <%= file_name %>_path(@<%= file_name %>) }
- end
-
- describe "(unsuccessful save)" do
- before do
- @<%= file_name %>.stub!(:update_attributes).and_return(false)
- end
-
- act! { put :update, :id => @<%= file_name %>.to_param, :<%= file_name %> => @attributes }
+ describe "(html)" do
+ act! { post :update, :id => mock_<%=file_name%>.to_param, :<%=file_name%> => @attributes }
+ it_assigns :<%=file_name%>, :flash => { :notice => :not_nil }
+ it_redirects_to { <%=file_name%>_path(@<%=file_name%>) }
+ end
- it_assigns :<%= file_name %>
- it_renders :template, :edit
- end
-
-<% %w(xml json).each do |format|
-%> describe "(successful save, <%= format %>)" do
- before do
- @<%= file_name %>.stub!(:update_attributes).and_return(true)
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ act! { post :update, :id => mock_<%=file_name%>.to_param, :<%=file_name%> => @attributes, :format => '<%=format%>' }
+ it_assigns :<%=file_name%>, :headers => { :Location => lambda { <%=file_name%>_url(@<%=file_name%>) } }
+ it_renders :<%=format%>, :<%=file_name%>, :status => :created
+ end
+<% end -%>
end
-
- act! { put :update, :id => @<%= file_name %>.to_param, :<%= file_name %> => @attributes, :format => '<%= format %>' }
- it_assigns :<%= file_name %>
- it_renders :blank
- end
-
- describe "(unsuccessful save, <%= format %>)" do
- before do
- @<%= file_name %>.stub!(:update_attributes).and_return(false)
- @<%= file_name %>.errors.stub!(:to_<%= format %>).and_return('<%= file_name %>.errors')
+ describe "(unsuccessful save)" do
+ before { mock_<%=file_name%>.stub!(:update_attributes).with(@attributes).and_return(false) }
+
+ describe "(html)" do
+ act! { post :update, :id => mock_<%=file_name%>.to_param, :<%=file_name%> => @attributes }
+ it_assigns :<%=file_name%>
+ it_renders :template, :new
+ end
+
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ before { mock_<%=file_name%>.errors.stub!(:to_<%=format%>).and_return('errors') }
+ act! { post :update, :id => mock_<%=file_name%>.to_param, :<%=file_name%> => @attributes, :format => '<%=format%>' }
+ it_assigns :<%=file_name%>
+ it_renders :<%=format%>, "errors", :status => :unprocessable_entity
+ end
+<% end -%>
end
-
- act! { put :update, :id => @<%= file_name %>.to_param, :<%= file_name %> => @attributes, :format => '<%= format %>' }
-
- it_assigns :<%= file_name %>
- it_renders :<%= format %>, "<%= file_name %>.errors", :status => :unprocessable_entity
end
-<% end %>end
+ describe "DELETE #destroy" do
+ before { mock_<%=file_name%>.stub!(:destroy) }
-describe <%= controller_class_name %>Controller, "DELETE #destroy" do
- before do
- @<%= file_name %> = mock_model <%= class_name %>
- @<%= file_name %>.stub!(:destroy)
- <%= class_name %>.stub!(:find_by_param).with(@<%= file_name %>.to_param).and_return(@<%= file_name %>)
- end
+ describe "(html)" do
+ act! { delete :destroy, :id => mock_<%=file_name%>.to_param }
+ it_assigns :<%=file_name%>
+ it_redirects_to { <%=table_name%>_path }
+ end
- act! { delete :destroy, :id => @<%= file_name %>.to_param }
-
- it_assigns :<%= file_name %>
- it_redirects_to { <%= table_name %>_path }
-
-<% %w(xml json).each do |format|
-%> describe "(<%= format %>)" do
- act! { delete :destroy, :id => @<%= file_name %>.to_param, :format => '<%= format %>' }
-
- it_assigns :<%= file_name %>
- it_renders :blank
+<% %w(xml json).each do |format| -%>
+ describe "(<%=format%>)" do
+ act! { delete :destroy, :id => mock_<%=file_name%>.to_param, :format => '<%=format%>' }
+ it_assigns :<%=file_name%>
+ it_renders :blank, :status => :ok
+ end
+<% end -%>
end
-
-<% end %>
end
Please sign in to comment.
Something went wrong with that request. Please try again.