Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/controllers/cards/pins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ def create
@pin = @card.pin_by Current.user

broadcast_add_pin_to_tray
render_pin_button_replacement

respond_to do |format|
format.turbo_stream { render_pin_button_replacement }
format.json { head :no_content }
end
end

def destroy
@pin = @card.unpin_by Current.user

broadcast_remove_pin_from_tray
render_pin_button_replacement

respond_to do |format|
format.turbo_stream { render_pin_button_replacement }
format.json { head :no_content }
end
end

private
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/my/pins_controller.rb
Original file line number Diff line number Diff line change
@@ -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)
Comment on lines +10 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving Current.user.pins.includes(:card).ordered to a variable so it isn't repeated, then conditionally limiting for web requests? This endpoint isn't paginated. Should we limit to 100 like for unread notifications to be safe?

end
end
end
3 changes: 3 additions & 0 deletions app/views/my/pins/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.array! @pins do |pin|
json.partial! "cards/card", card: pin.card
end
22 changes: 22 additions & 0 deletions test/controllers/cards/pins_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class Cards::PinsControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "create as JSON" do
card = cards(:layout)

assert_not card.pinned_by?(users(:kevin))

post card_pin_path(card), as: :json

assert_response :no_content
assert card.reload.pinned_by?(users(:kevin))
end

test "destroy" do
assert_changes -> { cards(:shipping).pinned_by?(users(:kevin)) }, from: true, to: false do
perform_enqueued_jobs do
Expand All @@ -28,4 +39,15 @@ class Cards::PinsControllerTest < ActionDispatch::IntegrationTest

assert_response :success
end

test "destroy as JSON" do
card = cards(:shipping)

assert card.pinned_by?(users(:kevin))

delete card_pin_path(card), as: :json

assert_response :no_content
assert_not card.reload.pinned_by?(users(:kevin))
end
end
10 changes: 10 additions & 0 deletions test/controllers/my/pins_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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