Add JSON support for GET /my/pins with no pagination and tests.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user