Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marketplace: Sprout Checkout #994

Merged
merged 5 commits into from
Dec 18, 2022
Merged
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
1 change: 1 addition & 0 deletions app/furniture/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def self.append_routes(router)
router.resources :carts do
router.resources :cart_products
end
router.resources :checkouts
end
end

Expand Down
5 changes: 5 additions & 0 deletions app/furniture/marketplace/breadcrumbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
link 'Marketplace', marketplace.location
end

crumb :marketplace_checkout do |checkout|
parent :marketplace, checkout.cart.marketplace
link 'Checkout', url_for([space, room, checkout])
end

crumb :marketplace_products do |marketplace|
parent :marketplace, marketplace
link 'Products', marketplace.location(:products)
Expand Down
7 changes: 5 additions & 2 deletions app/furniture/marketplace/cart_products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def create
turbo_stream.replace("cart-product-#{cart_product.product_id}", cart_product),
turbo_stream.replace("cart-footer-#{cart.id}",
partial: "marketplace/carts/footer", locals: {cart: cart}),
turbo_stream.replace("cart-total-#{cart.id}", partial: "marketplace/carts/total", locals: {cart: cart})
Copy link
Member

Choose a reason for hiding this comment

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

I notice we make this particular change 3 different times in this controller; what if we gathered these three replace calls into a method so they aren't duplicated everywhere? What arguments would the method need to take to work appropriately across it's different calls?

]
end
end
Expand Down Expand Up @@ -51,7 +52,8 @@ def update
turbo_stream.replace("cart-product-#{cart_product.product_id}", cart_product),
turbo_stream.replace("cart-footer-#{cart.id}",
partial: "marketplace/carts/footer", locals: {cart: cart}),
]
turbo_stream.replace("cart-total-#{cart.id}", partial: "marketplace/carts/total", locals: {cart: cart})
]
end
end
end
Expand All @@ -77,7 +79,8 @@ def destroy
render turbo_stream: [
turbo_stream.replace("cart-product-#{cart_product.product_id}", cart.cart_products.new(product: cart_product.product)),
turbo_stream.replace("cart-footer-#{cart.id}",
partial: "marketplace/carts/footer", locals: {cart: cart})
partial: "marketplace/carts/footer", locals: {cart: cart}),
turbo_stream.replace("cart-total-#{cart.id}", partial: "marketplace/carts/total", locals: {cart: cart})
]
end
end
Expand Down
9 changes: 8 additions & 1 deletion app/furniture/marketplace/carts/_footer.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<%
marketplace = cart.marketplace
room = marketplace.room
space = room.space
%>

<tfoot id="cart-footer-<%= cart.id%>" class="bg-gray-50">
<tr>
<td></td>
<th> <%= link_to("checkout", new_space_room_marketplace_checkout_path(space, room, marketplace), html_options = {}) %> </th>
Copy link
Member

Choose a reason for hiding this comment

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

We may be able to use [:new, space, room, marketplace, :checkout] in place of new_space_room_marketplace_checkout_path(space, room, marketplace)

<th scope="row" class="text-right px-1 py-3.5">Total: </th>
<td class="text-left px-3 py-3.5 font-bold">
<%= humanized_money_with_symbol(cart.price_total) %>
<%= render "marketplace/carts/total", cart: cart %>
</td>
</tr>
</tfoot>
3 changes: 3 additions & 0 deletions app/furniture/marketplace/carts/_total.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<span id="cart-total-<%= cart.id%>">
<%= humanized_money_with_symbol(cart.price_total) %>
</span>
11 changes: 11 additions & 0 deletions app/furniture/marketplace/checkout_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Marketplace
class CheckoutPolicy < ApplicationPolicy
alias checkout object

def create?
checkout.shopper.person == current_person
end
end
end
30 changes: 30 additions & 0 deletions app/furniture/marketplace/checkouts/_checkout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="-mx-4 mt-8 overflow-hidden shadow ring-1 ring-black ring-opacity-5 sm:-mx-6 md:mx-0 md:rounded-lg">
<table class="min-w-full divide-y divide-gray-300 table-fixed">
<thead class="bg-gray-50">
<tr>
<th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6">
<%= Marketplace::Product.human_attribute_name(:name) %>
</th>
<th scope="col" class="hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 lg:table-cell">
<%= Marketplace::Product.human_attribute_name(:description) %>
</th>
<th scope="col" class="hidden px-3 py-3.5 text-left text-sm font-semibold text-gray-900 sm:table-cell">
<%= Marketplace::Product.human_attribute_name(:price) %>
</th>
<th scope="col" class="w-48">&nbsp;</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200 bg-white">
<%= render checkout.cart.cart_products %>
</tbody>
<tfoot id="checkout-footer-<%= checkout.id%>" class="bg-gray-50">
<tr>
<td></td>
<th scope="row" class="text-right px-1 py-3.5">Total: </th>
<td class="text-left px-3 py-3.5 font-bold">
<%= render "marketplace/carts/total" %>
</td>
</tr>
</tfoot>
</table>
</div>
2 changes: 2 additions & 0 deletions app/furniture/marketplace/checkouts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%- breadcrumb :marketplace_checkout, checkout%>
<%= render checkout %>
27 changes: 27 additions & 0 deletions app/furniture/marketplace/checkouts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Marketplace
class CheckoutsController < FurnitureController
def new
authorize(checkout)
end

helper_method def checkout
@checkout ||= cart.build_checkout(shopper: shopper)
end

helper_method def shopper
@shopper ||= if current_person.is_a?(Guest)
Shopper.find_or_create_by(id: session[:current_cart] ||= SecureRandom.uuid)
else
Shopper.find_or_create_by(person: current_person)
end
end

helper_method def cart
@cart ||= marketplace.carts.find_or_create_by(shopper: shopper)
end

helper_method def marketplace
@marketplace ||= policy_scope(Marketplace).find(params[:marketplace_id])
end
end
end
13 changes: 13 additions & 0 deletions spec/furniture/marketplace/checkouts_controller_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails_helper"

RSpec.describe Marketplace::Checkout, type: :request do
let(:marketplace) { create(:marketplace) }
let(:space) { marketplace.space }
let(:room) { marketplace.room }

describe "#create" do
it "Create Checkout from a Cart" do
# TODO: insert test
end
end
end