diff --git a/app/furniture/marketplace/cart_product/notes/_form.html.erb b/app/furniture/marketplace/cart_product/notes/_form.html.erb new file mode 100644 index 000000000..4bc21a468 --- /dev/null +++ b/app/furniture/marketplace/cart_product/notes/_form.html.erb @@ -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 %> diff --git a/app/furniture/marketplace/cart_product/notes/_note.html.erb b/app/furniture/marketplace/cart_product/notes/_note.html.erb new file mode 100644 index 000000000..3e5fce2e1 --- /dev/null +++ b/app/furniture/marketplace/cart_product/notes/_note.html.erb @@ -0,0 +1,6 @@ +<%- if cart_product.note.present? %> +

<%= cart_product.note %>

+ <%= link_to("Edit Note", cart_product.location(:edit, child: :note)) %> +<%- else %> + <%= link_to("Add Note", cart_product.location(:new, child: :note)) %> +<%- end %> diff --git a/app/furniture/marketplace/cart_product/notes/edit.html.erb b/app/furniture/marketplace/cart_product/notes/edit.html.erb new file mode 100644 index 000000000..377eeeaca --- /dev/null +++ b/app/furniture/marketplace/cart_product/notes/edit.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag(cart_product, :note) do %> + <%= render "form", cart_product: %> +<%- end %> diff --git a/app/furniture/marketplace/cart_product/notes/new.html.erb b/app/furniture/marketplace/cart_product/notes/new.html.erb new file mode 100644 index 000000000..377eeeaca --- /dev/null +++ b/app/furniture/marketplace/cart_product/notes/new.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag(cart_product, :note) do %> + <%= render "form", cart_product: %> +<%- end %> diff --git a/app/furniture/marketplace/cart_product/notes/show.html.erb b/app/furniture/marketplace/cart_product/notes/show.html.erb new file mode 100644 index 000000000..7f67763bf --- /dev/null +++ b/app/furniture/marketplace/cart_product/notes/show.html.erb @@ -0,0 +1,3 @@ +<%= turbo_frame_tag(cart_product, :note) do %> + <%= render "note", cart_product: %> +<%- end %> diff --git a/app/furniture/marketplace/cart_product/notes_controller.rb b/app/furniture/marketplace/cart_product/notes_controller.rb new file mode 100644 index 000000000..f5bf65d53 --- /dev/null +++ b/app/furniture/marketplace/cart_product/notes_controller.rb @@ -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 diff --git a/app/furniture/marketplace/checkouts/show.html.erb b/app/furniture/marketplace/checkouts/show.html.erb index c810d3cf8..91fdcab50 100644 --- a/app/furniture/marketplace/checkouts/show.html.erb +++ b/app/furniture/marketplace/checkouts/show.html.erb @@ -6,7 +6,13 @@
<%- cart.cart_products.each do |cart_product| %>
<%= cart_product.name %>
- (<%=cart_product.quantity%>) x <%= humanized_money_with_symbol(cart_product.price) %>
+ (<%=cart_product.quantity%>) x <%= humanized_money_with_symbol(cart_product.price) %>
+
+ <%= turbo_frame_tag(cart_product, :note) do %> + <%= render "marketplace/cart_product/notes/note", cart_product: %> + <%- end %> +
+
<%= humanized_money_with_symbol(cart_product.price_total) %>
<%- end %>
Sub Total
diff --git a/app/furniture/marketplace/order/email_receipt_component.html.erb b/app/furniture/marketplace/order/email_receipt_component.html.erb index 146e71a6a..ff5ca1cb2 100644 --- a/app/furniture/marketplace/order/email_receipt_component.html.erb +++ b/app/furniture/marketplace/order/email_receipt_component.html.erb @@ -22,6 +22,13 @@ x <%= ordered_product.quantity %> <%= humanized_money_with_symbol(ordered_product.price) %> + + <%- if ordered_product.note.present? %> + + + <%= ordered_product.note %> + + <%- end %> <%- end %> Subtotal diff --git a/app/furniture/marketplace/routes.rb b/app/furniture/marketplace/routes.rb index 52ae8e6e0..5c72df5ac 100644 --- a/app/furniture/marketplace/routes.rb +++ b/app/furniture/marketplace/routes.rb @@ -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" diff --git a/db/migrate/20241017003307_marketplace_add_note_to_cart_product.rb b/db/migrate/20241017003307_marketplace_add_note_to_cart_product.rb new file mode 100644 index 000000000..c9ffca8a3 --- /dev/null +++ b/db/migrate/20241017003307_marketplace_add_note_to_cart_product.rb @@ -0,0 +1,5 @@ +class MarketplaceAddNoteToCartProduct < ActiveRecord::Migration[7.1] + def change + add_column :marketplace_cart_products, :note, :string, null: false, default: "" + end +end diff --git a/db/schema.rb b/db/schema.rb index 8115dc3ca..7ff190fbb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/spec/factories/furniture/marketplace/marketplace.rb b/spec/factories/furniture/marketplace/marketplace.rb index deb3cc5cf..9ad21491c 100644 --- a/spec/factories/furniture/marketplace/marketplace.rb +++ b/spec/factories/furniture/marketplace/marketplace.rb @@ -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 diff --git a/spec/furniture/marketplace/buying_products_system_spec.rb b/spec/furniture/marketplace/buying_products_system_spec.rb index 119362bfb..37cf156f6 100644 --- a/spec/furniture/marketplace/buying_products_system_spec.rb +++ b/spec/furniture/marketplace/buying_products_system_spec.rb @@ -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: "AhsokaTano@example.com", contact_phone_number: "1234567890")