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

Story4 #2596

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open

Story4 #2596

Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ gem 'jbuilder', '~> 2.5'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'bootstrap-sass'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
gem 'pry'
gem 'awesome_rails_console'
end

group :development do
Expand All @@ -46,3 +49,8 @@ end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'devise'
gem 'simple_form'
gem 'font-awesome-rails'
gem 'carrierwave'
gem 'mini_magick'
48 changes: 47 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.1)
autoprefixer-rails (6.7.7.2)
execjs
awesome_print (1.8.0)
awesome_rails_console (0.4.0)
awesome_print
pry-rails
railties
bcrypt (3.1.11)
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.1)
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
coderay (1.1.2)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.2.x)
Expand All @@ -50,9 +66,17 @@ GEM
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
devise (4.2.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0, < 5.1)
responders
warden (~> 1.2.3)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.14)
font-awesome-rails (4.7.0.2)
railties (>= 3.2, < 5.2)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
Expand All @@ -74,14 +98,21 @@ 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)
pry (0.11.0)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
pry-rails (0.3.6)
pry (>= 0.10.4)
puma (3.6.0)
rack (2.0.1)
rack-test (0.6.3)
Expand Down Expand Up @@ -113,13 +144,18 @@ GEM
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
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 (3.4.0)
actionpack (> 4, < 5.1)
activemodel (> 4, < 5.1)
spring (1.7.2)
spring-watcher-listen (2.0.0)
listen (>= 2.7, < 4.0)
Expand All @@ -142,6 +178,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 +193,22 @@ PLATFORMS
ruby

DEPENDENCIES
awesome_rails_console
bootstrap-sass
byebug
carrierwave
coffee-rails (~> 4.2)
devise
font-awesome-rails
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
mini_magick
pry
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 +218,4 @@ DEPENDENCIES
web-console

BUNDLED WITH
1.12.5
1.14.5
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/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/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/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/
43 changes: 43 additions & 0 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Admin::ProductsController < ApplicationController
before_action :authenticate_user!
before_action :admin_required
layout "admin"

def index
@products = Product.all
end

def new
@product = Product.new
end

def edit
@product = Product.new
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
29 changes: 29 additions & 0 deletions app/controllers/cart_items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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

def cart_item_params
params.require(:cart_item).permit(:quantity)
end
end
7 changes: 7 additions & 0 deletions app/controllers/carts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CartsController < ApplicationController
def clean
current_cart.clean!
flash[:warning] = "已清空购物车"
redirect_to carts_path
end
end
20 changes: 20 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ProductsController < ApplicationController
def index
@products = Product.all
end

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

def add_to_cart
@product = Product.find(params[:id])
if !current_cart.products.include? (@product)
current_cart.add_product_to_cart(@product)
flash[:notice] = "成功将#{@product.title} 加入购物车"
else
flash[:warning] = "你的购物车内已有此物品"
end
redirect_to :back
end
end
5 changes: 5 additions & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class WelcomeController < ApplicationController
def index
flash[:notice] = "Goodbye Loser"
end
end
2 changes: 2 additions & 0 deletions app/helpers/admin/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::ProductsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/cart_items_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CartItemsHelper
end
5 changes: 5 additions & 0 deletions app/helpers/carts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CartsHelper
def render_cart_total_price(cart)
cart.total_price
end
end
11 changes: 11 additions & 0 deletions app/helpers/flashes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module FlashesHelper
FLASH_CLASSES = { alert: "danger", notice: "success", warning: "warning"}.freeze

def flash_class(key)
FLASH_CLASSES.fetch key.to_sym, key
end

def user_facing_flashes
flash.to_hash.slice "alert", "notice","warning"
end
end
2 changes: 2 additions & 0 deletions app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProductsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/welcome_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WelcomeHelper
end
25 changes: 25 additions & 0 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Cart < ApplicationRecord
has_many :cart_items
has_many :products, through: :cart_items, source: :product

def add_product_to_cart(product)
ci = cart_items.build
ci.product = product
ci.quantity = 1
ci.save
end

def total_price
sum = 0
cart_items.each do |cart_item|
if cart_item.product.price.present?
sum += cart_item.product.price * cart_item.quantity
end
end
sum
end

def clean!
cart_items.destroy_all
end
end
Loading