Skip to content

Commit dedd285

Browse files
committed
Support tag_ids on JSON card create/update
1 parent 56f4736 commit dedd285

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

app/controllers/cards_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class CardsController < ApplicationController
2-
wrap_parameters :card, include: %i[ title description image created_at last_active_at ]
2+
wrap_parameters :card, include: %i[ title description image created_at last_active_at tag_ids ]
33

44
include FilterScoped
55

@@ -68,6 +68,6 @@ def ensure_permission_to_administer_card
6868
end
6969

7070
def card_params
71-
params.expect(card: [ :title, :description, :image, :created_at, :last_active_at ])
71+
params.expect(card: [ :title, :description, :image, :created_at, :last_active_at, tag_ids: [] ])
7272
end
7373
end

test/controllers/api/flat_json_params_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ class FlatJsonParamsTest < ActionDispatch::IntegrationTest
7575
assert_equal "Flat description", card.description.to_plain_text
7676
end
7777

78+
test "create card with flat JSON and tag_ids" do
79+
tag = tags(:mobile)
80+
81+
assert_difference -> { Card.count }, +1 do
82+
post board_cards_path(boards(:writebook)),
83+
params: { title: "Flat tagged card", tag_ids: [ tag.id ] },
84+
as: :json
85+
end
86+
87+
assert_response :created
88+
card = Card.last
89+
assert_equal [ tag ], card.reload.tags
90+
assert_equal [ tag.title ], @response.parsed_body["tags"]
91+
end
92+
7893
test "update card with flat JSON" do
7994
card = cards(:logo)
8095

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

106+
test "update card with flat JSON and tag_ids" do
107+
card = cards(:logo)
108+
tag = tags(:mobile)
109+
110+
put card_path(card),
111+
params: { tag_ids: [ tag.id ] },
112+
as: :json
113+
114+
assert_response :success
115+
assert_equal [ tag ], card.reload.tags
116+
assert_equal [ tag.title ], @response.parsed_body["tags"]
117+
end
118+
91119
test "create board with flat JSON" do
92120
assert_difference -> { Board.count }, +1 do
93121
post boards_path, params: { name: "Flat board" }, as: :json

test/controllers/cards_controller_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@ class CardsControllerTest < ActionDispatch::IntegrationTest
207207
assert_equal "Big if true", card.description.to_plain_text
208208
end
209209

210+
test "create as JSON with tag_ids applies tags to the created card" do
211+
tag = tags(:mobile)
212+
213+
assert_difference -> { Card.count }, +1 do
214+
post board_cards_path(boards(:writebook)),
215+
params: { card: { title: "Tagged card", tag_ids: [ tag.id ] } },
216+
as: :json
217+
assert_response :created
218+
end
219+
220+
card = Card.last
221+
assert_equal [ tag ], card.reload.tags
222+
assert_equal [ tag.title ], @response.parsed_body["tags"]
223+
end
224+
210225
test "create as JSON with custom created_at" do
211226
custom_time = Time.utc(2024, 1, 15, 10, 30, 0)
212227

@@ -293,6 +308,17 @@ class CardsControllerTest < ActionDispatch::IntegrationTest
293308
assert_equal "Update test", card.reload.title
294309
end
295310

311+
test "update as JSON with tag_ids updates tags on the card" do
312+
card = cards(:logo)
313+
tag = tags(:mobile)
314+
315+
put card_path(card, format: :json), params: { card: { tag_ids: [ tag.id ] } }
316+
assert_response :success
317+
318+
assert_equal [ tag ], card.reload.tags
319+
assert_equal [ tag.title ], @response.parsed_body["tags"]
320+
end
321+
296322
test "delete as JSON" do
297323
card = cards(:logo)
298324

0 commit comments

Comments
 (0)