Skip to content

Instantly share code, notes, and snippets.

@dwhite96
Last active December 12, 2019 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwhite96/d9b5ffb2efe2d6bf13af88c4df7ba4d9 to your computer and use it in GitHub Desktop.
Save dwhite96/d9b5ffb2efe2d6bf13af88c4df7ba4d9 to your computer and use it in GitHub Desktop.
aBox shopping cart feature
<%= link_to "My cart: #{current_order.quantity} items",
current_order_path,
id: "cart", class: "link item"
%>
<div id="nav" class="ui top stackable fixed menu">
<%= link_to "aBox", root_path, class: "link item" %>
<%= form_tag search_path, method: :get, class: "item", style: "width:400px" do %>
<div class="ui action input">
<%=
text_field_tag :search,
params[:search],
placeholder: "Search for artists, artwork, or orders"
%>
<%= button_tag "Search", type: "submit", name: nil, class: "ui teal basic button" %>
</div>
<% end %>
<div class="right menu">
<%= render partial: "order_items/cart" %>
<% if user_signed_in? %>
<%= link_to "Post artwork", new_artwork_path, class: "link item" %>
<%= link_to "My profile", profile_path, class: "link item" %>
<%= link_to "Edit profile", edit_user_registration_path, class: "link item" %>
<%= link_to "Logout", destroy_user_session_path, method: :delete, class: "link item" %>
<% else %>
<%= link_to "Sign up", new_user_registration_path, class: "link item" %>
<%= link_to "Login", new_user_session_path, class: "link item" %>
<% end %>
</div>
</div>
$("#nav #cart").replaceWith("<%= j render partial: 'cart' %>");
class Order < ApplicationRecord
multisearchable against: %i[id]
validates :status, presence: true
has_many :order_items, dependent: :destroy
has_many :artworks, through: :order_items
def quantity
sum(:quantity)
end
def subtotal
sum(:artwork_subtotal)
end
private
def sum(method)
order_items.reduce(0) { |sum, order_item| sum + order_item.public_send(method) }
end
end
class OrderItem < ApplicationRecord
validates :quantity, presence: true
belongs_to :artwork
belongs_to :order
def artwork_subtotal
artwork.price * quantity
end
end
class OrderItemsController < ApplicationController
def create
@order = current_order
@order_item = @order.order_items.new(order_item_params)
respond_to do |format|
if @order_item.save
session[:order_id] = @order.id
format.js { render file: '/order_items/add_to_cart.js.erb' }
else
format.html { render :back }
format.json { render json: @order_item.errors, status: :unprocessable_entity }
end
format.js { render file: '/order_items/add_to_cart.js.erb' }
end
end
private
def order_item_params
params.require(:order_item).permit(:quantity, :artwork_id, :order_id)
end
end
class OrdersController < ApplicationController
def show
@order = current_order
end
private
def order_params
params.require(:order).permit(:status)
end
end
<p id="notice"><%= notice %></p>
<div class="ui stackable two column grid container">
<div class="nine wide column">
<%= image_tag(@artwork.photo, class: "ui large fluid image") %>
</div>
<div class="seven wide column">
<p>
<strong>Title:</strong>
<%= @artwork.title %>
</p>
<p>
<strong>Price:</strong>
<%= number_to_currency @artwork.price %>
</p>
<div class="ui divider"></div>
<%= form_with(model: @order_item) do |form| %>
<% if @order_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order_item.errors.count, "error") %> prohibited this order item from being saved:</h2>
<ul>
<% @order_item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form.hidden_field :artwork_id, value: @artwork.id %>
<%= form.label :quantity %>
<%= form.select(
:quantity,
(1..10).collect { |i| i }
) %><br><br>
<%= form.submit "Add to cart", class: "ui teal button" %>
<% end %>
</div>
</div>
<div class="ui hidden divider"></div>
<% if current_user == @artwork.gallery.user %>
<%= link_to 'Edit', edit_artwork_path(@artwork) %> |
<% end %>
<%= link_to 'Back', artworks_path %>
<p id="notice"><%= notice %></p>
<% if @order.order_items.empty? %>
<h1>
Your Cart is empty.
</h1>
<% else %>
<h1>
Your Cart
</h1>
<table class="ui very basic table">
<thead>
<tr>
<th>Artwork Item</th>
<th>Title</th>
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<% @order.order_items.each do |item| %>
<tr>
<td>
<%= link_to item.artwork do %>
<%= image_tag(item.artwork.photo, display: "inline", class: "ui tiny image") %>
<% end %>
</td>
<td>
<%= item.artwork.title %><br>
</td>
<td>
Price: <%= number_to_currency(item.artwork.price) %><br>
</td>
<td>
Quantity: <%= item.quantity %>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="ui divider"></div>
<div class="ui stackable four column grid container">
<div class="ui four wide column">
<strong>Subtotal (<%= @order.quantity %> items):</strong>
<strong><%= number_to_currency(@order.subtotal) %></strong>
</div>
<div class="ui twelve wide column">
<div class="ui mini button">Proceed to checkout</div><br>
</div>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment