Add API for CRUD actions on steps

This commit is contained in:
Stanko K.R.
2025-12-05 15:30:15 +01:00
parent 8a61ca2a79
commit c517ef6063
4 changed files with 60 additions and 0 deletions
+15
View File
@@ -5,6 +5,11 @@ class Cards::StepsController < ApplicationController
def create
@step = @card.steps.create!(step_params)
respond_to do |format|
format.turbo_stream
format.json { head :created, location: card_step_path(@card, @step, format: :json) }
end
end
def show
@@ -15,10 +20,20 @@ class Cards::StepsController < ApplicationController
def update
@step.update!(step_params)
respond_to do |format|
format.turbo_stream
format.json { render :show }
end
end
def destroy
@step.destroy!
respond_to do |format|
format.turbo_stream
format.json { head :no_content }
end
end
private
@@ -0,0 +1 @@
json.(step, :id, :content, :completed)
+1
View File
@@ -0,0 +1 @@
json.partial! "cards/steps/step", step: @step
@@ -52,4 +52,47 @@ class Cards::StepsControllerTest < ActionDispatch::IntegrationTest
assert_turbo_stream action: :replace, target: dom_id(step)
end
end
test "create as JSON" do
card = cards(:logo)
assert_difference -> { card.steps.count }, +1 do
post card_steps_path(card), params: { step: { content: "New step" } }, as: :json
end
assert_response :created
assert_equal card_step_path(card, Step.last, format: :json), @response.headers["Location"]
end
test "show as JSON" do
card = cards(:logo)
step = card.steps.create!(content: "Test step")
get card_step_path(card, step), as: :json
assert_response :success
assert_equal step.id, @response.parsed_body["id"]
assert_equal "Test step", @response.parsed_body["content"]
end
test "update as JSON" do
card = cards(:logo)
step = card.steps.create!(content: "Original")
put card_step_path(card, step), params: { step: { content: "Updated" } }, as: :json
assert_response :success
assert_equal "Updated", step.reload.content
assert_equal "Updated", @response.parsed_body["content"]
end
test "destroy as JSON" do
card = cards(:logo)
step = card.steps.create!(content: "To delete")
delete card_step_path(card, step), as: :json
assert_response :no_content
assert_not Step.exists?(step.id)
end
end