From 441a86e7a6bd5ea8f78b5fd38304fbcfc7785c59 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Mon, 19 Jan 2009 09:53:47 -0800 Subject: [PATCH] Added generated RESTful RatesController. #1916 --- app/controllers/rates_controller.rb | 85 +++++++++++ app/views/rates/edit.html.erb | 28 ++++ app/views/rates/index.html.erb | 26 ++++ app/views/rates/new.html.erb | 27 ++++ app/views/rates/show.html.erb | 23 +++ routes.rb | 1 + spec/controllers/rates_controller_spec.rb | 173 ++++++++++++++++++++++ spec/controllers/rates_routing_spec.rb | 59 ++++++++ 8 files changed, 422 insertions(+) create mode 100644 app/controllers/rates_controller.rb create mode 100644 app/views/rates/edit.html.erb create mode 100644 app/views/rates/index.html.erb create mode 100644 app/views/rates/new.html.erb create mode 100644 app/views/rates/show.html.erb create mode 100644 routes.rb create mode 100644 spec/controllers/rates_controller_spec.rb create mode 100644 spec/controllers/rates_routing_spec.rb diff --git a/app/controllers/rates_controller.rb b/app/controllers/rates_controller.rb new file mode 100644 index 0000000..d67e828 --- /dev/null +++ b/app/controllers/rates_controller.rb @@ -0,0 +1,85 @@ +class RatesController < ApplicationController + # GET /rates + # GET /rates.xml + def index + @rates = Rate.find(:all) + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @rates } + end + end + + # GET /rates/1 + # GET /rates/1.xml + def show + @rate = Rate.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @rate } + end + end + + # GET /rates/new + # GET /rates/new.xml + def new + @rate = Rate.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @rate } + end + end + + # GET /rates/1/edit + def edit + @rate = Rate.find(params[:id]) + end + + # POST /rates + # POST /rates.xml + def create + @rate = Rate.new(params[:rate]) + + respond_to do |format| + if @rate.save + flash[:notice] = 'Rate was successfully created.' + format.html { redirect_to(@rate) } + format.xml { render :xml => @rate, :status => :created, :location => @rate } + else + format.html { render :action => "new" } + format.xml { render :xml => @rate.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /rates/1 + # PUT /rates/1.xml + def update + @rate = Rate.find(params[:id]) + + respond_to do |format| + if @rate.update_attributes(params[:rate]) + flash[:notice] = 'Rate was successfully updated.' + format.html { redirect_to(@rate) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @rate.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /rates/1 + # DELETE /rates/1.xml + def destroy + @rate = Rate.find(params[:id]) + @rate.destroy + + respond_to do |format| + format.html { redirect_to(rates_url) } + format.xml { head :ok } + end + end +end diff --git a/app/views/rates/edit.html.erb b/app/views/rates/edit.html.erb new file mode 100644 index 0000000..822d4bc --- /dev/null +++ b/app/views/rates/edit.html.erb @@ -0,0 +1,28 @@ +

Editing rate

+ +<% form_for(@rate) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :amount %>
+ <%= f.text_field :amount %> +

+

+ <%= f.label :user_id %>
+ <%= f.text_field :user_id %> +

+

+ <%= f.label :project_id %>
+ <%= f.text_field :project_id %> +

+

+ <%= f.label :date_in_effect %>
+ <%= f.date_select :date_in_effect %> +

+

+ <%= f.submit "Update" %> +

+<% end %> + +<%= link_to 'Show', @rate %> | +<%= link_to 'Back', rates_path %> diff --git a/app/views/rates/index.html.erb b/app/views/rates/index.html.erb new file mode 100644 index 0000000..2de155f --- /dev/null +++ b/app/views/rates/index.html.erb @@ -0,0 +1,26 @@ +

Listing rates

+ + + + + + + + + +<% for rate in @rates %> + + + + + + + + + +<% end %> +
AmountUserProjectDate in effect
<%=h rate.amount %><%=h rate.user_id %><%=h rate.project_id %><%=h rate.date_in_effect %><%= link_to 'Show', rate %><%= link_to 'Edit', edit_rate_path(rate) %><%= link_to 'Destroy', rate, :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New rate', new_rate_path %> diff --git a/app/views/rates/new.html.erb b/app/views/rates/new.html.erb new file mode 100644 index 0000000..0452d95 --- /dev/null +++ b/app/views/rates/new.html.erb @@ -0,0 +1,27 @@ +

New rate

+ +<% form_for(@rate) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :amount %>
+ <%= f.text_field :amount %> +

+

+ <%= f.label :user_id %>
+ <%= f.text_field :user_id %> +

+

+ <%= f.label :project_id %>
+ <%= f.text_field :project_id %> +

+

+ <%= f.label :date_in_effect %>
+ <%= f.date_select :date_in_effect %> +

+

+ <%= f.submit "Create" %> +

+<% end %> + +<%= link_to 'Back', rates_path %> diff --git a/app/views/rates/show.html.erb b/app/views/rates/show.html.erb new file mode 100644 index 0000000..b4a30f1 --- /dev/null +++ b/app/views/rates/show.html.erb @@ -0,0 +1,23 @@ +

+ Amount: + <%=h @rate.amount %> +

+ +

+ User: + <%=h @rate.user_id %> +

+ +

+ Project: + <%=h @rate.project_id %> +

+ +

+ Date in effect: + <%=h @rate.date_in_effect %> +

+ + +<%= link_to 'Edit', edit_rate_path(@rate) %> | +<%= link_to 'Back', rates_path %> diff --git a/routes.rb b/routes.rb new file mode 100644 index 0000000..ec623c3 --- /dev/null +++ b/routes.rb @@ -0,0 +1 @@ +resources :rates diff --git a/spec/controllers/rates_controller_spec.rb b/spec/controllers/rates_controller_spec.rb new file mode 100644 index 0000000..d867c46 --- /dev/null +++ b/spec/controllers/rates_controller_spec.rb @@ -0,0 +1,173 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe RatesController do + + def mock_rate(stubs={}) + @mock_rate ||= mock_model(Rate, stubs) + end + + describe "responding to GET index" do + + it "should expose all rates as @rates" do + Rate.should_receive(:find).with(:all).and_return([mock_rate]) + get :index + assigns[:rates].should == [mock_rate] + end + + describe "with mime type of xml" do + + it "should render all rates as xml" do + request.env["HTTP_ACCEPT"] = "application/xml" + Rate.should_receive(:find).with(:all).and_return(rates = mock("Array of Rates")) + rates.should_receive(:to_xml).and_return("generated XML") + get :index + response.body.should == "generated XML" + end + + end + + end + + describe "responding to GET show" do + + it "should expose the requested rate as @rate" do + Rate.should_receive(:find).with("37").and_return(mock_rate) + get :show, :id => "37" + assigns[:rate].should equal(mock_rate) + end + + describe "with mime type of xml" do + + it "should render the requested rate as xml" do + request.env["HTTP_ACCEPT"] = "application/xml" + Rate.should_receive(:find).with("37").and_return(mock_rate) + mock_rate.should_receive(:to_xml).and_return("generated XML") + get :show, :id => "37" + response.body.should == "generated XML" + end + + end + + end + + describe "responding to GET new" do + + it "should expose a new rate as @rate" do + Rate.should_receive(:new).and_return(mock_rate) + get :new + assigns[:rate].should equal(mock_rate) + end + + end + + describe "responding to GET edit" do + + it "should expose the requested rate as @rate" do + Rate.should_receive(:find).with("37").and_return(mock_rate) + get :edit, :id => "37" + assigns[:rate].should equal(mock_rate) + end + + end + + describe "responding to POST create" do + + describe "with valid params" do + + it "should expose a newly created rate as @rate" do + Rate.should_receive(:new).with({'these' => 'params'}).and_return(mock_rate(:save => true)) + post :create, :rate => {:these => 'params'} + assigns(:rate).should equal(mock_rate) + end + + it "should redirect to the created rate" do + Rate.stub!(:new).and_return(mock_rate(:save => true)) + post :create, :rate => {} + response.should redirect_to(rate_url(mock_rate)) + end + + end + + describe "with invalid params" do + + it "should expose a newly created but unsaved rate as @rate" do + Rate.stub!(:new).with({'these' => 'params'}).and_return(mock_rate(:save => false)) + post :create, :rate => {:these => 'params'} + assigns(:rate).should equal(mock_rate) + end + + it "should re-render the 'new' template" do + Rate.stub!(:new).and_return(mock_rate(:save => false)) + post :create, :rate => {} + response.should render_template('new') + end + + end + + end + + describe "responding to PUT udpate" do + + describe "with valid params" do + + it "should update the requested rate" do + Rate.should_receive(:find).with("37").and_return(mock_rate) + mock_rate.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => "37", :rate => {:these => 'params'} + end + + it "should expose the requested rate as @rate" do + Rate.stub!(:find).and_return(mock_rate(:update_attributes => true)) + put :update, :id => "1" + assigns(:rate).should equal(mock_rate) + end + + it "should redirect to the rate" do + Rate.stub!(:find).and_return(mock_rate(:update_attributes => true)) + put :update, :id => "1" + response.should redirect_to(rate_url(mock_rate)) + end + + end + + describe "with invalid params" do + + it "should update the requested rate" do + Rate.should_receive(:find).with("37").and_return(mock_rate) + mock_rate.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => "37", :rate => {:these => 'params'} + end + + it "should expose the rate as @rate" do + Rate.stub!(:find).and_return(mock_rate(:update_attributes => false)) + put :update, :id => "1" + assigns(:rate).should equal(mock_rate) + end + + it "should re-render the 'edit' template" do + Rate.stub!(:find).and_return(mock_rate(:update_attributes => false)) + put :update, :id => "1" + response.should render_template('edit') + end + + end + + end + + describe "responding to DELETE destroy" do + + it "should destroy the requested rate" do + Rate.should_receive(:find).with("37").and_return(mock_rate) + mock_rate.should_receive(:destroy) + delete :destroy, :id => "37" + end + + it "should redirect to the rates list" do + Rate.stub!(:find).and_return(mock_rate(:destroy => true)) + delete :destroy, :id => "1" + response.should redirect_to(rates_url) + end + + end + +end diff --git a/spec/controllers/rates_routing_spec.rb b/spec/controllers/rates_routing_spec.rb new file mode 100644 index 0000000..be5c81d --- /dev/null +++ b/spec/controllers/rates_routing_spec.rb @@ -0,0 +1,59 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe RatesController do + describe "route generation" do + it "should map #index" do + route_for(:controller => "rates", :action => "index").should == "/rates" + end + + it "should map #new" do + route_for(:controller => "rates", :action => "new").should == "/rates/new" + end + + it "should map #show" do + route_for(:controller => "rates", :action => "show", :id => 1).should == "/rates/1" + end + + it "should map #edit" do + route_for(:controller => "rates", :action => "edit", :id => 1).should == "/rates/1/edit" + end + + it "should map #update" do + route_for(:controller => "rates", :action => "update", :id => 1).should == "/rates/1" + end + + it "should map #destroy" do + route_for(:controller => "rates", :action => "destroy", :id => 1).should == "/rates/1" + end + end + + describe "route recognition" do + it "should generate params for #index" do + params_from(:get, "/rates").should == {:controller => "rates", :action => "index"} + end + + it "should generate params for #new" do + params_from(:get, "/rates/new").should == {:controller => "rates", :action => "new"} + end + + it "should generate params for #create" do + params_from(:post, "/rates").should == {:controller => "rates", :action => "create"} + end + + it "should generate params for #show" do + params_from(:get, "/rates/1").should == {:controller => "rates", :action => "show", :id => "1"} + end + + it "should generate params for #edit" do + params_from(:get, "/rates/1/edit").should == {:controller => "rates", :action => "edit", :id => "1"} + end + + it "should generate params for #update" do + params_from(:put, "/rates/1").should == {:controller => "rates", :action => "update", :id => "1"} + end + + it "should generate params for #destroy" do + params_from(:delete, "/rates/1").should == {:controller => "rates", :action => "destroy", :id => "1"} + end + end +end