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:
1 parent
1cd24e451b
commit
a52afb7a2b
2 files changed
+48
-1
No files matched your search
@@ -79,6 +79,7 @@ class RatesController < ApplicationController
|
|||||||
@rate = Rate.find(params[:id])
|
@rate = Rate.find(params[:id])
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
# Locked rates will fail saving here.
|
||||||
if @rate.update_attributes(params[:rate])
|
if @rate.update_attributes(params[:rate])
|
||||||
flash[:notice] = 'Rate was successfully updated.'
|
flash[:notice] = 'Rate was successfully updated.'
|
||||||
format.html {
|
format.html {
|
||||||
@@ -90,6 +91,10 @@ class RatesController < ApplicationController
|
|||||||
}
|
}
|
||||||
format.xml { head :ok }
|
format.xml { head :ok }
|
||||||
else
|
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.html { render :action => "edit" }
|
||||||
format.xml { render :xml => @rate.errors, :status => :unprocessable_entity }
|
format.xml { render :xml => @rate.errors, :status => :unprocessable_entity }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -123,7 +123,8 @@ describe RatesController, "as an administrator" do
|
|||||||
:amount => 100.0,
|
:amount => 100.0,
|
||||||
:user => @user,
|
:user => @user,
|
||||||
:user_id => @user.id,
|
:user_id => @user.id,
|
||||||
:unlocked? => true
|
:unlocked? => true,
|
||||||
|
:locked? => false
|
||||||
}.merge(stubs)
|
}.merge(stubs)
|
||||||
@mock_rate ||= mock_model(Rate, stubs)
|
@mock_rate ||= mock_model(Rate, stubs)
|
||||||
end
|
end
|
||||||
@@ -353,6 +354,47 @@ describe RatesController, "as an administrator" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user