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

Shopper may add Notes to a Product in their Cart on checkout #2663

Merged
merged 2 commits into from
Oct 17, 2024
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
8 changes: 8 additions & 0 deletions app/furniture/marketplace/cart_product/notes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= form_with(model: cart_product,
url: polymorphic_path(cart_product.location(child: :note))) do |form| %>

<%= form.label :note %>
<%= form.text_area :note %>

<%= form.submit %>
<%- end %>
6 changes: 6 additions & 0 deletions app/furniture/marketplace/cart_product/notes/_note.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%- if cart_product.note.present? %>
<p><%= cart_product.note %></p>
<%= link_to("Edit Note", cart_product.location(:edit, child: :note)) %>
<%- else %>
<%= link_to("Add Note", cart_product.location(:new, child: :note)) %>
<%- end %>
3 changes: 3 additions & 0 deletions app/furniture/marketplace/cart_product/notes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag(cart_product, :note) do %>
<%= render "form", cart_product: %>
<%- end %>
3 changes: 3 additions & 0 deletions app/furniture/marketplace/cart_product/notes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag(cart_product, :note) do %>
<%= render "form", cart_product: %>
<%- end %>
3 changes: 3 additions & 0 deletions app/furniture/marketplace/cart_product/notes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= turbo_frame_tag(cart_product, :note) do %>
<%= render "note", cart_product: %>
<%- end %>
33 changes: 33 additions & 0 deletions app/furniture/marketplace/cart_product/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Marketplace
class CartProduct::NotesController < Controller
expose :cart, scope: -> { policy_scope(marketplace.carts) }, model: Cart
expose :cart_product, scope: -> { policy_scope(cart.cart_products) }, model: CartProduct

def new
authorize(cart_product, :update?)
end

def show
authorize(cart_product)
end

def edit
authorize(cart_product)
end

def update
authorize(cart_product)
cart_product.update(cart_product_params)

if cart_product.errors.present?
render :new, status: :unprocessable_entity
else
redirect_to cart_product.location(child: :note)
end
end

def cart_product_params
params.require(:cart_product).permit(:note)
end
end
end
8 changes: 7 additions & 1 deletion app/furniture/marketplace/checkouts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
<dl class="grid grid-cols-2 gap-3 w-96">
<%- cart.cart_products.each do |cart_product| %>
<dt class="text-right"><%= cart_product.name %><br />
(<%=cart_product.quantity%>) x <%= humanized_money_with_symbol(cart_product.price) %></dt>
(<%=cart_product.quantity%>) x <%= humanized_money_with_symbol(cart_product.price) %><br />
<div>
<%= turbo_frame_tag(cart_product, :note) do %>
<%= render "marketplace/cart_product/notes/note", cart_product: %>
<%- end %>
</div>
</dt>
<dd><%= humanized_money_with_symbol(cart_product.price_total) %></dd>
<%- end %>
<dt class="font-bold text-right">Sub Total</dt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
<td class="text-center">x <%= ordered_product.quantity %></td>
<td><%= humanized_money_with_symbol(ordered_product.price) %></td>
</tr>

<%- if ordered_product.note.present? %>
<tr>
<td></td>
<td colspan="2"><em><%= ordered_product.note %></em></td>
</tr>
<%- end %>
<%- end %>
<tr>
<th class="text-right" colspan="1">Subtotal</th>
Expand Down
5 changes: 4 additions & 1 deletion app/furniture/marketplace/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ class Routes
def self.append_routes(router)
router.resources :marketplaces, only: [:show, :edit, :update], module: "marketplace" do
router.resources :carts, only: [] do
router.resources :cart_products
router.resources :cart_products do
router.resource :note, controller: "cart_product/notes"
end

router.resource :checkout, only: [:show, :create]
router.resource :delivery, controller: "cart/deliveries"
router.resource :delivery_area, controller: "cart/delivery_areas"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MarketplaceAddNoteToCartProduct < ActiveRecord::Migration[7.1]
def change
add_column :marketplace_cart_products, :note, :string, null: false, default: ""
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_10_10_004915) do
ActiveRecord::Schema[7.1].define(version: 2024_10_17_003307) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -145,6 +145,7 @@
t.integer "quantity"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "note", default: "", null: false
t.index ["cart_id"], name: "index_marketplace_cart_products_on_cart_id"
t.index ["product_id"], name: "index_marketplace_cart_products_on_product_id"
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/furniture/marketplace/marketplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@

product { association(:marketplace_product, marketplace: marketplace) }
order { association(:marketplace_order, marketplace: marketplace) }
note { "No #{Faker::Food.allergen} Please!" }
quantity { 1 }
end

Expand Down
4 changes: 4 additions & 0 deletions spec/furniture/marketplace/buying_products_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def url_options
click_link("Checkout")
expect(page).to have_current_path(polymorphic_path(marketplace.carts.first.location(child: :checkout)))

click_link("Add Note")
fill_in("Note", with: "No bananas!")
click_button("Save changes")

set_delivery_details(delivery_address: "123 N West St Oakland, CA",
contact_email: "[email protected]",
contact_phone_number: "1234567890")
Expand Down