Protect locked rates from being updated.

* Locked rates will fail to save
* Attempting to save a Locked rates will display an error and reload the
  Rate from the database so the update parameters are thrown away

  #1919
This commit is contained in:
Eric Davis
2009-01-20 11:05:19 -08:00
parent 1cd24e451b
commit a52afb7a2b
2 changed files with 48 additions and 1 deletions
+5
View File
@@ -79,6 +79,7 @@ class RatesController < ApplicationController
@rate = Rate.find(params[:id])
respond_to do |format|
# Locked rates will fail saving here.
if @rate.update_attributes(params[:rate])
flash[:notice] = 'Rate was successfully updated.'
format.html {
@@ -90,6 +91,10 @@ class RatesController < ApplicationController
}
format.xml { head :ok }
else
if @rate.locked?
flash[:error] = "Rate is locked and cannot be edited"
@rate.reload # Removes attribute changes
end
format.html { render :action => "edit" }
format.xml { render :xml => @rate.errors, :status => :unprocessable_entity }
end
+43 -1
View File
@@ -123,7 +123,8 @@ describe RatesController, "as an administrator" do
:amount => 100.0,
:user => @user,
:user_id => @user.id,
:unlocked? => true
:unlocked? => true,
:locked? => false
}.merge(stubs)
@mock_rate ||= mock_model(Rate, stubs)
end
@@ -353,6 +354,47 @@ describe RatesController, "as an administrator" do
end
end
describe "on a locked rate" do
def mock_locked_rate(stubs = { })
mock_rate(stubs.merge(:locked? => true,
:unlocked? => false,
:update_attributes => false,
:reload => nil
))
end
it "should try to update the requested rate" do
Rate.should_receive(:find).with("37").and_return(mock_locked_rate)
mock_locked_rate.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => "37", :rate => {:these => 'params'}
end
it "should not save the rate" do
Rate.should_receive(:find).with("37").and_return(mock_locked_rate)
mock_locked_rate.should_receive(:update_attributes).and_return(false)
put :update, :id => "37", :rate => {:these => 'params'}
end
it "should reload the locked rate as @rate" do
Rate.stub!(:find).and_return(mock_locked_rate(:id => 37))
mock_locked_rate.should_receive(:reload).and_return(mock_locked_rate(:id => 37))
put :update, :id => "37", :rate => { :amount => 200.0 }
assigns(:rate).should equal(mock_locked_rate)
end
it "should re-render the 'edit' template" do
Rate.stub!(:find).and_return(mock_locked_rate)
put :update, :id => "1"
response.should render_template('edit')
end
it "should render an error message" do
Rate.stub!(:find).and_return(mock_locked_rate)
put :update, :id => "1"
flash[:error].should match(/locked/)
end
end
end