Add JSON support for GET /my/pins with no pagination and tests.

This commit is contained in:
Denis Švara
2026-01-14 17:43:16 +01:00
parent 1d8b765bab
commit e82dc08f6c
3 changed files with 23 additions and 1 deletions
+10 -1
View File
@@ -1,6 +1,15 @@
class My::PinsController < ApplicationController
def index
@pins = Current.user.pins.includes(:card).ordered.limit(20)
@pins = pins_for_request
fresh_when etag: [ @pins, @pins.collect(&:card) ]
end
private
def pins_for_request
if request.format.json?
Current.user.pins.includes(:card).ordered
else
Current.user.pins.includes(:card).ordered.limit(20)
end
end
end
+3
View File
@@ -0,0 +1,3 @@
json.array! @pins do |pin|
json.partial! "cards/card", card: pin.card
end
@@ -11,4 +11,14 @@ class My::PinsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_select "div", text: /#{users(:kevin).pins.first.card.title}/
end
test "index as JSON" do
expected_ids = users(:kevin).pins.ordered.pluck(:card_id)
get my_pins_path(format: :json)
assert_response :success
assert_equal expected_ids.count, @response.parsed_body.count
assert_equal expected_ids, @response.parsed_body.map { |card| card["id"] }
end
end