From e82dc08f6cfe7e1a114489283139fb06ae124157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=A0vara?= Date: Wed, 14 Jan 2026 17:43:16 +0100 Subject: [PATCH] Add JSON support for GET `/my/pins` with no pagination and tests. --- app/controllers/my/pins_controller.rb | 11 ++++++++++- app/views/my/pins/index.json.jbuilder | 3 +++ test/controllers/my/pins_controller_test.rb | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 app/views/my/pins/index.json.jbuilder diff --git a/app/controllers/my/pins_controller.rb b/app/controllers/my/pins_controller.rb index 4468c75d5..93b77df79 100644 --- a/app/controllers/my/pins_controller.rb +++ b/app/controllers/my/pins_controller.rb @@ -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 diff --git a/app/views/my/pins/index.json.jbuilder b/app/views/my/pins/index.json.jbuilder new file mode 100644 index 000000000..f3a39ae84 --- /dev/null +++ b/app/views/my/pins/index.json.jbuilder @@ -0,0 +1,3 @@ +json.array! @pins do |pin| + json.partial! "cards/card", card: pin.card +end diff --git a/test/controllers/my/pins_controller_test.rb b/test/controllers/my/pins_controller_test.rb index 3c8ca33e2..daff96354 100644 --- a/test/controllers/my/pins_controller_test.rb +++ b/test/controllers/my/pins_controller_test.rb @@ -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