Added generated RESTful RatesController. #1916
This commit is contained in:
85
app/controllers/rates_controller.rb
Normal file
85
app/controllers/rates_controller.rb
Normal file
@@ -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
|
||||||
28
app/views/rates/edit.html.erb
Normal file
28
app/views/rates/edit.html.erb
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<h1>Editing rate</h1>
|
||||||
|
|
||||||
|
<% form_for(@rate) do |f| %>
|
||||||
|
<%= f.error_messages %>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<%= f.label :amount %><br />
|
||||||
|
<%= f.text_field :amount %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.label :user_id %><br />
|
||||||
|
<%= f.text_field :user_id %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.label :project_id %><br />
|
||||||
|
<%= f.text_field :project_id %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.label :date_in_effect %><br />
|
||||||
|
<%= f.date_select :date_in_effect %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.submit "Update" %>
|
||||||
|
</p>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= link_to 'Show', @rate %> |
|
||||||
|
<%= link_to 'Back', rates_path %>
|
||||||
26
app/views/rates/index.html.erb
Normal file
26
app/views/rates/index.html.erb
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<h1>Listing rates</h1>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Amount</th>
|
||||||
|
<th>User</th>
|
||||||
|
<th>Project</th>
|
||||||
|
<th>Date in effect</th>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<% for rate in @rates %>
|
||||||
|
<tr>
|
||||||
|
<td><%=h rate.amount %></td>
|
||||||
|
<td><%=h rate.user_id %></td>
|
||||||
|
<td><%=h rate.project_id %></td>
|
||||||
|
<td><%=h rate.date_in_effect %></td>
|
||||||
|
<td><%= link_to 'Show', rate %></td>
|
||||||
|
<td><%= link_to 'Edit', edit_rate_path(rate) %></td>
|
||||||
|
<td><%= link_to 'Destroy', rate, :confirm => 'Are you sure?', :method => :delete %></td>
|
||||||
|
</tr>
|
||||||
|
<% end %>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<br />
|
||||||
|
|
||||||
|
<%= link_to 'New rate', new_rate_path %>
|
||||||
27
app/views/rates/new.html.erb
Normal file
27
app/views/rates/new.html.erb
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<h1>New rate</h1>
|
||||||
|
|
||||||
|
<% form_for(@rate) do |f| %>
|
||||||
|
<%= f.error_messages %>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<%= f.label :amount %><br />
|
||||||
|
<%= f.text_field :amount %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.label :user_id %><br />
|
||||||
|
<%= f.text_field :user_id %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.label :project_id %><br />
|
||||||
|
<%= f.text_field :project_id %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.label :date_in_effect %><br />
|
||||||
|
<%= f.date_select :date_in_effect %>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<%= f.submit "Create" %>
|
||||||
|
</p>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= link_to 'Back', rates_path %>
|
||||||
23
app/views/rates/show.html.erb
Normal file
23
app/views/rates/show.html.erb
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<p>
|
||||||
|
<b>Amount:</b>
|
||||||
|
<%=h @rate.amount %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<b>User:</b>
|
||||||
|
<%=h @rate.user_id %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<b>Project:</b>
|
||||||
|
<%=h @rate.project_id %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<b>Date in effect:</b>
|
||||||
|
<%=h @rate.date_in_effect %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
|
||||||
|
<%= link_to 'Edit', edit_rate_path(@rate) %> |
|
||||||
|
<%= link_to 'Back', rates_path %>
|
||||||
173
spec/controllers/rates_controller_spec.rb
Normal file
173
spec/controllers/rates_controller_spec.rb
Normal file
@@ -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
|
||||||
59
spec/controllers/rates_routing_spec.rb
Normal file
59
spec/controllers/rates_routing_spec.rb
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user