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

Story5-完成购物网站 #2603

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9d7f9ca
add bootstrap to project
zhangyuwen12 Mar 14, 2019
019fcec
add bootstrap html
zhangyuwen12 Mar 14, 2019
968ae3e
add bootstrap flash function
zhangyuwen12 Mar 14, 2019
abc6f8b
user can login/logout/signup
zhangyuwen12 Mar 14, 2019
0bee2d8
install simpleform with bootstrap
zhangyuwen12 Mar 14, 2019
d9ddb5f
install font-awesome-rails
zhangyuwen12 Mar 14, 2019
e76f80a
implement backend CRUD
zhangyuwen12 Mar 14, 2019
4f4cec1
only admin can access backend panel
zhangyuwen12 Mar 15, 2019
2b11690
implement image upload
zhangyuwen12 Mar 15, 2019
85bb26c
implement product show
zhangyuwen12 Mar 15, 2019
7ecfdb6
change admin/product backend panel
zhangyuwen12 Mar 15, 2019
4d28ab3
add add_to_cart button
zhangyuwen12 Mar 15, 2019
34aa17f
implement add_product_to_cart function
zhangyuwen12 Mar 15, 2019
4d92690
implement current_cart function
zhangyuwen12 Mar 15, 2019
0285203
implement carts index function
zhangyuwen12 Mar 15, 2019
2de55a6
implement cart_total_price function
zhangyuwen12 Mar 15, 2019
09c8984
empty cart
zhangyuwen12 Mar 15, 2019
7067234
delete an item from cart
zhangyuwen12 Mar 15, 2019
9389e6f
cannot add same item in cart
zhangyuwen12 Mar 15, 2019
07176e2
change quantity in cart
zhangyuwen12 Mar 15, 2019
e325815
cannot add item with quantity 0
zhangyuwen12 Mar 15, 2019
1260215
cannot change quantity which exceed stock
zhangyuwen12 Mar 15, 2019
334e7e9
add checkout routing
zhangyuwen12 Mar 15, 2019
cbac94c
add order model
zhangyuwen12 Mar 15, 2019
407fee5
touch app/views/carts/checkout.html.erb
zhangyuwen12 Mar 15, 2019
7c4d1ff
add order controller
zhangyuwen12 Mar 15, 2019
dbd263d
add product_list model
zhangyuwen12 Mar 15, 2019
b307a04
add orders show function
zhangyuwen12 Mar 15, 2019
d823c6c
add token to order
zhangyuwen12 Mar 15, 2019
07d0c89
users can see their orders
zhangyuwen12 Mar 15, 2019
1172230
add payment_method function
zhangyuwen12 Mar 15, 2019
2d2050d
add OrderMailer function
zhangyuwen12 Mar 15, 2019
c87baea
add admin/orders
zhangyuwen12 Mar 15, 2019
f66f610
implement admin/orders show function
zhangyuwen12 Mar 15, 2019
a06e061
add ship/shipped/cancel/return action to admin/orders_controller
zhangyuwen12 Mar 15, 2019
0a4d918
add admin/orders/state_option button
zhangyuwen12 Mar 15, 2019
f7ae1f8
add apply_cancel function
zhangyuwen12 Mar 15, 2019
768ba08
add notify_cancel/ship function
zhangyuwen12 Mar 15, 2019
38747b2
stroy5
zhangyuwen12 Mar 20, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
# Ignore Byebug command history file.
.byebug_history
config/database.yml
public/uploads
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ gem 'jbuilder', '~> 2.5'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'bootstrap-sass'
gem 'devise'
gem 'simple_form'
gem 'font-awesome-rails'
gem 'carrierwave'

gem 'mini_magick'
gem 'letter_opener', group: :development
gem 'aasm'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
47 changes: 46 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
GEM
remote: https://rubygems.org/
specs:
aasm (5.0.2)
concurrent-ruby (~> 1.0)
actioncable (5.0.0)
actionpack (= 5.0.0)
nio4r (~> 1.2)
Expand Down Expand Up @@ -38,9 +40,21 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
arel (7.1.1)
autoprefixer-rails (9.4.7)
execjs
bcrypt (3.1.12)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.2)
byebug (9.0.5)
carrierwave (1.2.2)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.2.x)
Expand All @@ -50,9 +64,17 @@ GEM
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
devise (4.6.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0, < 6.0)
responders
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.14)
font-awesome-rails (4.7.0.4)
railties (>= 3.2, < 6.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
Expand All @@ -63,6 +85,10 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.7.0)
launchy (~> 2.2)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -74,14 +100,17 @@ GEM
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_magick (4.8.0)
mini_portile2 (2.1.0)
minitest (5.9.0)
multi_json (1.12.1)
nio4r (1.2.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
orm_adapter (0.5.0)
pkg-config (1.1.7)
public_suffix (3.0.3)
puma (3.6.0)
rack (2.0.1)
rack-test (0.6.3)
Expand Down Expand Up @@ -113,13 +142,19 @@ GEM
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
responders (2.4.1)
actionpack (>= 4.2.0, < 6.0)
railties (>= 4.2.0, < 6.0)
sass (3.4.22)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
simple_form (4.1.0)
actionpack (>= 5.0)
activemodel (>= 5.0)
spring (1.7.2)
spring-watcher-listen (2.0.0)
listen (>= 2.7, < 4.0)
Expand All @@ -142,6 +177,8 @@ GEM
thread_safe (~> 0.1)
uglifier (3.0.1)
execjs (>= 0.3.0, < 3)
warden (1.2.7)
rack (>= 1.0)
web-console (3.3.1)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -155,14 +192,22 @@ PLATFORMS
ruby

DEPENDENCIES
aasm
bootstrap-sass
byebug
carrierwave
coffee-rails (~> 4.2)
devise
font-awesome-rails
jbuilder (~> 2.5)
jquery-rails
letter_opener
listen (~> 3.0.5)
mini_magick
puma (~> 3.0)
rails (~> 5.0.0)
sass-rails (~> 5.0)
simple_form
spring
spring-watcher-listen (~> 2.0.0)
sqlite3
Expand All @@ -172,4 +217,4 @@ DEPENDENCIES
web-console

BUNDLED WITH
1.12.5
1.15.4
3 changes: 3 additions & 0 deletions app/assets/javascripts/account/orders.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/orders.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/products.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap/alert
//= require bootstrap/dropdown
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/cart_items.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/carts.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/orders.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/products.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/welcome.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/account/orders.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the account::orders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/orders.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the admin::orders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/products.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the admin::products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
*= require_tree .
*= require_self
*/

@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome";
3 changes: 3 additions & 0 deletions app/assets/stylesheets/cart_items.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the cart_items controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/carts.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the carts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/orders.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the orders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/products.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/welcome.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the welcome controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
7 changes: 7 additions & 0 deletions app/controllers/account/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Account::OrdersController < ApplicationController
before_action :authenticate_user!

def index
@orders = current_user.orders.order("id DESC")
end
end
41 changes: 41 additions & 0 deletions app/controllers/admin/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Admin::OrdersController < ApplicationController
layout "admin"

before_action :authenticate_user!
before_action :admin_required

def index
@orders = Order.order("id DESC")
end

def show
@order = Order.find(params[:id])
@product_lists = @order.product_lists
end

def ship
@order = Order.find(params[:id])
@order.ship!
OrderMailer.notify_ship(@order).deliver!
redirect_to :back
end

def shipped
@order = Order.find(params[:id])
@order.deliver!
redirect_to :back
end

def cancel
@order = Order.find(params[:id])
@order.cancel_order!
OrderMailer.notify_cancel(@order).deliver!
redirect_to :back
end

def return
@order = Order.find(params[:id])
@order.return_good!
redirect_to :back
end
end
48 changes: 48 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Admin::ProductsController < ApplicationController
layout "admin"

before_action :authenticate_user!
before_action :admin_required

def index
@products = Product.all
end

def show
@product = Product.find(params[:id])
end

def new
@product = Product.new
end

def edit
@product = Product.find(params[:id])
end

def update
@product = Product.find(params[:id])

if @product.update(product_params)
redirect_to admin_products_path
else
render :edit
end
end

def create
@product = Product.new(product_params)

if @product.save
redirect_to admin_products_path
else
render :new
end
end

private

def product_params
params.require(:product).permit(:title, :description, :quantity, :price, :image)
end
end
23 changes: 23 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def admin_required
if !current_user.admin?
redirect_to "/", alert: "You are not admin."
end
end

helper_method :current_cart

def current_cart
@current_cart ||= find_cart
end

private

def find_cart
cart = Cart.find_by(id: session[:cart_id])
if cart.blank?
cart = Cart.create
end
session[:cart_id] = cart.id
return cart
end
end
32 changes: 32 additions & 0 deletions app/controllers/cart_items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class CartItemsController < ApplicationController
before_action :authenticate_user!

def destroy
@cart = current_cart
@cart_item = @cart.cart_items.find_by(product_id: params[:id])
@product = @cart_item.product
@cart_item.destroy

flash[:warning] = "成功将 #{@product.title} 从购物车删除!"
redirect_to :back
end

def update
@cart = current_cart
@cart_item = @cart.cart_items.find_by(product_id: params[:id])
if @cart_item.product.quantity >= cart_item_params[:quantity].to_i
@cart_item.update(cart_item_params)
flash[:notice] = "成功变更数量"
else
flash[:warning] = "数量不足以加入购物车"
end

redirect_to carts_path
end

private

def cart_item_params
params.require(:cart_item).permit(:quantity)
end
end
Loading