Skip to content

Commit 59ac214

Browse files
committed
Shop cart implementation 🚀
1 parent c29eef6 commit 59ac214

File tree

24 files changed

+261
-22
lines changed

24 files changed

+261
-22
lines changed

app/assets/stylesheets/app/base/_base.scss

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,28 @@ q {
134134
.pk-no-display {
135135
display: none !important;
136136
}
137+
138+
.shopping_cart-icon {
139+
display: inline-block;
140+
position: fixed;
141+
left: 1450px;
142+
opacity: 80%;
143+
144+
&:hover {
145+
opacity: 100%;
146+
}
147+
}
148+
149+
.cart_table {
150+
margin: 40px;
151+
152+
table {
153+
border-collapse: collapse;
154+
width: 100%;
155+
}
156+
157+
th, td {
158+
text-align: left;
159+
padding: 8px;
160+
}
161+
}

app/views/layouts/app/_header.slim

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,5 @@ header.pk-header class="#{header_classlist.compact.join(' ')}"
6767
li.pk-header__li-list-item = register_link
6868
li.pk-header__li-list-item = login_link
6969

70-
li.pk-list__unit
71-
= link_to supporters_path do
72-
i.fas.fa-shopping-cart.fa-2x
7370
li.pk-list__unit
7471
= support_us_link(class: 'btn pk-btn--green-bg donate-btn')

components/shop/Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ GEM
6565
rack-test (>= 0.6.3)
6666
regexp_parser (>= 1.5, < 3.0)
6767
xpath (~> 3.2)
68+
carrierwave (1.3.2)
69+
activemodel (>= 4.0.0)
70+
activesupport (>= 4.0.0)
71+
mime-types (>= 1.16)
72+
ssrf_filter (~> 1.0)
6873
coderay (1.1.3)
6974
concurrent-ruby (1.1.8)
7075
crass (1.0.6)
@@ -125,6 +130,9 @@ GEM
125130
mini_mime (>= 0.1.1)
126131
marcel (1.0.1)
127132
method_source (1.0.0)
133+
mime-types (3.3.1)
134+
mime-types-data (~> 3.2015)
135+
mime-types-data (3.2021.0225)
128136
mini_mime (1.1.0)
129137
minitest (5.14.4)
130138
nenv (0.3.0)
@@ -219,6 +227,7 @@ GEM
219227
activesupport (>= 4.0)
220228
sprockets (>= 3.0.0)
221229
sqlite3 (1.4.2)
230+
ssrf_filter (1.0.7)
222231
temple (0.8.2)
223232
thor (1.1.0)
224233
thread_safe (0.3.6)

components/shop/app/controllers/shop/base_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module Shop
44
class BaseController < ApplicationController
55
before_action :authenticate_user!
66

7+
def current_order
8+
session[:order_id] ? Shop::Order.find(session[:order_id]) : []
9+
end
10+
711
private
812

913
def render_form

components/shop/app/controllers/shop/items_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Shop
44
class ItemsController < BaseController
55
helper_method :items
6+
helper_method :current_order
67

78
# @deprecated
89
# def add_to_cart

components/shop/app/controllers/shop/order_items_controller.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,42 @@
33
module Shop
44
class OrderItemsController < BaseController
55
helper_method :item
6+
helper_method :order_items
7+
helper_method :current_order
68

7-
def index
9+
def create
10+
order_item = ::Shop::OrderItem.new(item_id: item.id, order_id: order_id, price: item.price)
11+
order_item.save
12+
13+
redirect_to shop_items_path
814
end
915

1016
def update
17+
order_item.increase_quantity!
18+
redirect_to shop_order_items_path
1119
end
1220

1321
def destroy
22+
order_item.reduce_quantity!
23+
redirect_to shop_order_items_path
1424
end
1525

1626
private
1727

28+
def order_items
29+
order_items = Shop::OrderItem.includes(:item)
30+
end
31+
32+
def order_item
33+
@order_items ||= Shop::OrderItem.find(params[:id])
34+
end
35+
1836
def item
19-
@items ||= Shop::Item.find(params[:id])
37+
@items ||= Shop::Item.find(params[:item_id])
38+
end
39+
40+
def order_id
41+
session[:order_id] ||= Shop::Order.create.id
2042
end
2143
end
2244
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
module Shop
4+
module ItemHelper
5+
end
6+
end

components/shop/app/models/shop/item.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Item < ApplicationRecord
55
self.table_name = 'shop_items'
66

77
has_many :item_images
8+
has_many :order_items
89
accepts_nested_attributes_for :item_images
910

1011
scope :active, -> { where(published: true) }

components/shop/app/models/shop/order.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ module Shop
99
class Order < ApplicationRecord
1010
self.table_name = 'shop_orders'
1111

12+
enum status: { shopping_cart: 0, placed: 1, processing: 2, shipping: 3, complete: 4, canceled: 5 }
13+
1214
has_many :order_items
15+
has_many :items, through: :order_items
16+
17+
def total_sum
18+
order_items.map(&:price).sum.to_f
19+
end
1320

14-
def total
15-
@totoal ||= 0
21+
def order_items_count
22+
order_items.count
1623
end
1724
end
1825
end
Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
11
# frozen_string_literal: true
22

3-
# item_id
4-
# qty
5-
# price
6-
73
module Shop
84
class OrderItem < ApplicationRecord
9-
self.table_name = 'shop_orders'
5+
self.table_name = 'shop_order_items'
6+
7+
validates_uniqueness_of :item_id
108

119
belongs_to :order
1210
belongs_to :item
11+
12+
def increase_quantity!
13+
self.quantity += 1
14+
update_price(quantity)
15+
save!
16+
end
17+
18+
def reduce_quantity!
19+
if self.quantity > 1
20+
self.quantity -= 1
21+
update_price(quantity)
22+
save!
23+
else
24+
delete_order_item
25+
end
26+
end
27+
28+
def delete_order_item
29+
destroy!
30+
end
31+
32+
def update_price(qty)
33+
self.price = item.price * qty
34+
end
1335
end
1436
end

0 commit comments

Comments
 (0)