Permalink
Browse files

Handle validation errors for Artefacts gracefully

  • Loading branch information...
1 parent 1651dc5 commit d36a2de3d5300221b6dc5a998b86642cebe47000 @jamiecobbett jamiecobbett committed Aug 22, 2012
Showing with 22 additions and 8 deletions.
  1. +12 −8 app/controllers/artefacts_controller.rb
  2. +10 −0 test/functional/artefacts_controller_test.rb
@@ -26,14 +26,7 @@ def new
end
def edit
- # Construct a list of actions, with embedded diffs
- # The reason for appending the nil is so that the initial action is
- # included: the DiffEnabledAction class handles the case where the previous
- # action does not exist
- reverse_actions = @artefact.actions.reverse
- @actions = (reverse_actions + [nil]).each_cons(2).map { |action, previous|
- DiffEnabledAction.new(action, previous)
- }
+ @actions = build_actions
end
def create
@@ -68,6 +61,7 @@ def update
if saved && params[:commit] == 'Save and continue editing'
redirect_to edit_artefact_path(@artefact)
else
+ @actions = build_actions
respond_with @artefact, status: status_to_use do |format|
format.json { render json: @artefact.to_json, status: status_to_use }
end
@@ -146,4 +140,14 @@ def action_user
action_user = current_user.is_a?(User) ? current_user : nil
end
+ def build_actions
+ # Construct a list of actions, with embedded diffs
+ # The reason for appending the nil is so that the initial action is
+ # included: the DiffEnabledAction class handles the case where the previous
+ # action does not exist
+ reverse_actions = @artefact.actions.reverse
+ (reverse_actions + [nil]).each_cons(2).map { |action, previous|
+ DiffEnabledAction.new(action, previous)
+ }
+ end
end
@@ -30,6 +30,16 @@ class ArtefactsControllerTest < ActionController::TestCase
end
end
+ context "PUT update" do
+ context "invalid artefact" do
+ should "be invalid with an empty title" do
+ artefact1 = FactoryGirl.create(:artefact)
+ artefact1.name = ""
+ put :update, id: artefact1.id, artefact: { name: "" }
+ assert_template :edit
+ end
+ end
+ end
end
context "accept JSON" do

0 comments on commit d36a2de

Please sign in to comment.