Unselect a Seat from the <dialog>
When viewing the details for a Seat
that is already included in the
visitor’s Cart
, present them with a button to remove it.
To determine whether or not the visitor has added a seat, check if the
Seat record’s id is in the list of the Cart record’s selected seats
(accessed via the has_many :seats-generated
seats_id
method ).
DELETE
request routing
In order to support the removal of a Seat without looking up the
intermediary SeatSelection
record, utilize the previously-declared
singular resource :seat_selection
route
declaration .
As previously stated, instead of routing DELETE
/seats/:seat_id/selections/:id
requests to the selections#destroy
controller action, route DELETE /seats/:seat_id/selections
requests.
In the controller action, the correct SeatSelection
record can be
determined based on the context provided by the Current.cart
and the
params[:seat_id]
.
Desktop
Collapse app/models/cart.rb
Expand app/models/cart.rb
app/models/cart.rb
diff --git a/app/models/cart.rb b/app/models/cart.rb
index 62b78c9..9e146a3 100644
--- a/app/models/cart.rb
+++ b/app/models/cart.rb
@@ -3,4 +3,8 @@ class Cart < ApplicationRecord
has_many :seat_selections
has_many :seats, through: :seat_selections
+
+ def include?(seat)
+ seat_ids.include?(seat.id)
+ end
end
Collapse app/views/seats/show.html.erb
Expand app/views/seats/show.html.erb
app/views/seats/show.html.erb
diff --git a/app/views/seats/show.html.erb b/app/views/seats/show.html.erb
index bfdd3d9..2ef93c4 100644
--- a/app/views/seats/show.html.erb
+++ b/app/views/seats/show.html.erb
@@ -59,11 +59,20 @@
</div>
<div class="syos-inline-stack__item">
- <%= button_to(
- "Select",
- seat_selections_path(seat),
- class: "syos-button",
- ) %>
+ <% if Current.cart.include?(seat) %>
+ <%= button_to(
+ "Remove",
+ seat_selection_path(seat),
+ method: :delete,
+ class: "syos-button",
+ ) %>
+ <% else %>
+ <%= button_to(
+ "Select",
+ seat_selections_path(seat),
+ class: "syos-button",
+ ) %>
+ <% end %>
</div>
</div>
</footer>
Collapse test/application_system_test_case.rb
Expand test/application_system_test_case.rb
test/application_system_test_case.rb
diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb
index d19212a..6d85d7a 100644
--- a/test/application_system_test_case.rb
+++ b/test/application_system_test_case.rb
@@ -2,4 +2,8 @@ require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
+
+ def click_on_seat(row_number)
+ find(%{[aria-label*="#{row_number}"]}).click
+ end
end
Collapse test/controllers/selections_controller_test.rb
Expand test/controllers/selections_controller_test.rb
test/controllers/selections_controller_test.rb
diff --git a/test/controllers/selections_controller_test.rb b/test/controllers/selections_controller_test.rb
index 76b31c9..2f59574 100644
--- a/test/controllers/selections_controller_test.rb
+++ b/test/controllers/selections_controller_test.rb
@@ -26,7 +26,7 @@ class SelectionsControllerTest < ActionDispatch::IntegrationTest
seat = seat_selection.seat
cookies[:cart_token] = cart.token
- delete seat_selection_path(seat, seat_selection)
+ delete seat_selection_path(seat)
assert_equal cart.seats.ids, []
end
Collapse test/models/cart_test.rb
Expand test/models/cart_test.rb
test/models/cart_test.rb
diff --git a/test/models/cart_test.rb b/test/models/cart_test.rb
new file mode 100644
index 0000000..e98e76f
--- /dev/null
+++ b/test/models/cart_test.rb
@@ -0,0 +1,22 @@
+require "test_helper"
+
+class CartTest < ActiveSupport::TestCase
+ test "#include? returns true when a seat is selected" do
+ seat_selection = create(:seat_selection)
+ seat = seat_selection.seat
+ cart = seat_selection.cart
+
+ included = cart.include?(seat)
+
+ assert included, "cart includes selected seat"
+ end
+
+ test "#include? returns false when a seat is not yet selected" do
+ seat = create(:seat)
+ cart = create(:cart)
+
+ included = cart.include?(seat)
+
+ refute included, "cart includes selected seat"
+ end
+end
Collapse test/system/visitor_unselects_seat_test.rb
Expand test/system/visitor_unselects_seat_test.rb
test/system/visitor_unselects_seat_test.rb
diff --git a/test/system/visitor_unselects_seat_test.rb b/test/system/visitor_unselects_seat_test.rb
index bfb6a23..439bbb5 100644
--- a/test/system/visitor_unselects_seat_test.rb
+++ b/test/system/visitor_unselects_seat_test.rb
@@ -15,4 +15,21 @@ class VisitorUnselectsSeatTest < ApplicationSystemTestCase
assert_no_text "$10.00"
end
end
+
+ test "from the details dialog of an already selected seat" do
+ venue = create(:benedum_center)
+ floor = create(:orchestra, venue: venue)
+ section = create(:section, floor: floor, price: 10_00)
+ seat = create(:seat, row: "AA", number: "101", section: section)
+
+ visit "/venues/benedum_center/floors/orchestra/seats"
+ click_on_seat "AA-101"
+ click_on "Select"
+ click_on_seat "AA-101"
+ within("dialog") { click_on "Remove" }
+
+ within "#cart-summary" do
+ assert_no_text "$10.00"
+ end
+ end
end
Collapse test/system/visitor_views_seats_test.rb
Expand test/system/visitor_views_seats_test.rb
test/system/visitor_views_seats_test.rb
diff --git a/test/system/visitor_views_seats_test.rb b/test/system/visitor_views_seats_test.rb
index 6678b42..4461a43 100644
--- a/test/system/visitor_views_seats_test.rb
+++ b/test/system/visitor_views_seats_test.rb
@@ -12,8 +12,4 @@ class VisitorViewsSeatsTest < ApplicationSystemTestCase
assert_text("$10.00")
end
-
- def click_on_seat(row_number)
- find(%{[aria-label*="#{row_number}"]}).click
- end
end