Add API for updating and deactivating users
This commit is contained in:
@@ -14,7 +14,10 @@ class UsersController < ApplicationController
|
||||
|
||||
def update
|
||||
if @user.update(user_params)
|
||||
redirect_to @user
|
||||
respond_to do |format|
|
||||
format.html { redirect_to @user }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
else
|
||||
render :edit, status: :unprocessable_entity
|
||||
end
|
||||
@@ -22,7 +25,11 @@ class UsersController < ApplicationController
|
||||
|
||||
def destroy
|
||||
@user.deactivate
|
||||
redirect_to users_path
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to users_path }
|
||||
format.json { head :no_content }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -102,4 +102,23 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_response :success
|
||||
assert_equal users(:david).name, @response.parsed_body["name"]
|
||||
end
|
||||
|
||||
test "update as JSON" do
|
||||
sign_in_as :kevin
|
||||
|
||||
put user_path(users(:david)), params: { user: { name: "New David" } }, as: :json
|
||||
|
||||
assert_response :no_content
|
||||
assert_equal "New David", users(:david).reload.name
|
||||
end
|
||||
|
||||
test "destroy as JSON" do
|
||||
sign_in_as :kevin
|
||||
|
||||
assert_difference -> { User.active.count }, -1 do
|
||||
delete user_path(users(:david)), as: :json
|
||||
end
|
||||
|
||||
assert_response :no_content
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user