Last active
December 12, 2019 04:00
-
-
Save dwhite96/d9b5ffb2efe2d6bf13af88c4df7ba4d9 to your computer and use it in GitHub Desktop.
aBox shopping cart feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= link_to "My cart: #{current_order.quantity} items", | |
current_order_path, | |
id: "cart", class: "link item" | |
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#nav #cart").replaceWith("<%= j render partial: 'cart' %>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderItem < ApplicationRecord | |
validates :quantity, presence: true | |
belongs_to :artwork | |
belongs_to :order | |
def artwork_subtotal | |
artwork.price * quantity | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrdersController < ApplicationController | |
def show | |
@order = current_order | |
end | |
private | |
def order_params | |
params.require(:order).permit(:status) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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