Skip to content
Draft
4 changes: 2 additions & 2 deletions app/controllers/cards_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CardsController < ApplicationController
wrap_parameters :card, include: %i[ title description image created_at last_active_at ]
wrap_parameters :card, include: %i[ title description image created_at last_active_at tag_ids ]

include FilterScoped

Expand Down Expand Up @@ -68,6 +68,6 @@ def ensure_permission_to_administer_card
end

def card_params
params.expect(card: [ :title, :description, :image, :created_at, :last_active_at ])
params.expect(card: [ :title, :description, :image, :created_at, :last_active_at, tag_ids: [] ])
end
end
28 changes: 28 additions & 0 deletions test/controllers/api/flat_json_params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ class FlatJsonParamsTest < ActionDispatch::IntegrationTest
assert_equal "Flat description", card.description.to_plain_text
end

test "create card with flat JSON and tag_ids" do
tag = tags(:mobile)

assert_difference -> { Card.count }, +1 do
post board_cards_path(boards(:writebook)),
params: { title: "Flat tagged card", tag_ids: [ tag.id ] },
as: :json
end

assert_response :created
card = Card.last
assert_equal [ tag ], card.reload.tags
assert_equal [ tag.title ], @response.parsed_body["tags"]
end

test "update card with flat JSON" do
card = cards(:logo)

Expand All @@ -88,6 +103,19 @@ class FlatJsonParamsTest < ActionDispatch::IntegrationTest
assert_equal "Updated flat", card.description.to_plain_text
end

test "update card with flat JSON and tag_ids" do
card = cards(:logo)
tag = tags(:mobile)

put card_path(card),
params: { tag_ids: [ tag.id ] },
as: :json

assert_response :success
assert_equal [ tag ], card.reload.tags
assert_equal [ tag.title ], @response.parsed_body["tags"]
end

test "create board with flat JSON" do
assert_difference -> { Board.count }, +1 do
post boards_path, params: { name: "Flat board" }, as: :json
Expand Down
26 changes: 26 additions & 0 deletions test/controllers/cards_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ class CardsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Big if true", card.description.to_plain_text
end

test "create as JSON with tag_ids applies tags to the created card" do
tag = tags(:mobile)

assert_difference -> { Card.count }, +1 do
post board_cards_path(boards(:writebook)),
params: { card: { title: "Tagged card", tag_ids: [ tag.id ] } },
as: :json
assert_response :created
end

card = Card.last
assert_equal [ tag ], card.reload.tags
assert_equal [ tag.title ], @response.parsed_body["tags"]
end

test "create as JSON with custom created_at" do
custom_time = Time.utc(2024, 1, 15, 10, 30, 0)

Expand Down Expand Up @@ -293,6 +308,17 @@ class CardsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Update test", card.reload.title
end

test "update as JSON with tag_ids updates tags on the card" do
card = cards(:logo)
tag = tags(:mobile)

put card_path(card, format: :json), params: { card: { tag_ids: [ tag.id ] } }
assert_response :success

assert_equal [ tag ], card.reload.tags
assert_equal [ tag.title ], @response.parsed_body["tags"]
end

test "delete as JSON" do
card = cards(:logo)

Expand Down
Loading