Skip to content

Commit

Permalink
add json response to workflow_actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Gum committed Mar 24, 2017
1 parent 7767cc6 commit 8d01732
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 14 additions & 4 deletions app/controllers/hyrax/workflow_actions_controller.rb
Expand Up @@ -3,16 +3,19 @@ class WorkflowActionsController < ApplicationController
before_action :authenticate_user!

def update
work = ActiveFedora::Base.find(params[:id])
@curation_concern = ActiveFedora::Base.find(params[:id])
workflow_action_form = Hyrax::Forms::WorkflowActionForm.new(
current_ability: current_ability,
work: work,
work: @curation_concern,
attributes: workflow_action_params
)
if workflow_action_form.save
redirect_to [main_app, work], notice: "The #{work.human_readable_type} has been updated."
after_update_response
else
render 'hyrax/base/unauthorized', status: :unauthorized
respond_to do |wants|
wants.html { render 'hyrax/base/unauthorized', status: :unauthorized }
wants.json { render_json_response(response_type: :unprocessable_entity, options: { errors: @curation_concern.errors }) }
end
end
end

Expand All @@ -21,5 +24,12 @@ def update
def workflow_action_params
params.require(:workflow_action).permit(:name, :comment)
end

def after_update_response
respond_to do |wants|
wants.html { redirect_to [main_app, @curation_concern], notice: "The #{@curation_concern.human_readable_type} has been updated." }
wants.json { render 'hyrax/base/show', status: :ok, location: polymorphic_path([main_app, @curation_concern]) }
end
end
end
end
18 changes: 18 additions & 0 deletions spec/controllers/hyrax/workflow_actions_controller_spec.rb
Expand Up @@ -33,5 +33,23 @@
put :update, params: { id: generic_work.to_param, workflow_action: { name: 'advance', comment: '' } }
expect(response).to redirect_to(main_app.hyrax_generic_work_path(generic_work, locale: 'en'))
end

context 'when responding to json' do
it 'will render :ok when the form is successfully saved' do
expect(form).to receive(:save).and_return(true)
sign_in(user)

put :update, params: { id: generic_work.to_param, workflow_action: { name: 'advance', comment: '' } }, format: :json
expect(response.status).to eq 200
end

it 'will render :unprocessable_entity when the form fails to save' do
expect(form).to receive(:save).and_return(false)
sign_in(user)

put :update, params: { id: generic_work.to_param, workflow_action: { name: 'advance', comment: '' } }, format: :json
expect(response.status).to eq 422
end
end
end
end

0 comments on commit 8d01732

Please sign in to comment.