Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
xueshaojie1 committed Mar 15, 2019
2 parents b3ac5df + d5b83cb commit 2c79eff
Show file tree
Hide file tree
Showing 40 changed files with 277 additions and 145 deletions.
16 changes: 0 additions & 16 deletions .tags

This file was deleted.

8 changes: 0 additions & 8 deletions .tags1

This file was deleted.

2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ gem 'carrierwave'
gem 'mini_magick'
gem 'letter_opener', group: :development
gem 'aasm'
gem 'bootstrap-datepicker-rails'
gem 'pry'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ GEM
execjs
bcrypt (3.1.12)
bindex (0.5.0)
bootstrap-datepicker-rails (1.8.0.1)
railties (>= 3.0)
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
Expand All @@ -56,6 +58,7 @@ GEM
activemodel (>= 4.0.0)
activesupport (>= 4.0.0)
mime-types (>= 1.16)
coderay (1.1.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand Down Expand Up @@ -113,6 +116,9 @@ GEM
mini_portile2 (~> 2.4.0)
orm_adapter (0.5.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.6)
Expand Down Expand Up @@ -201,6 +207,7 @@ PLATFORMS

DEPENDENCIES
aasm
bootstrap-datepicker-rails
bootstrap-sass
byebug
carrierwave
Expand All @@ -213,6 +220,7 @@ DEPENDENCIES
listen (~> 3.0.5)
mini_magick
pg
pry
puma (~> 3.0)
rails (~> 5.0.0)
sass-rails (~> 5.0)
Expand Down
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
# README

This README would normally document whatever steps are necessary to get the
application up and running.

Things you may want to cover:

* Ruby version

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...
实作购物网站
添加优惠券功能
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
//= require turbolinks
//= require bootstrap/alert
//= require bootstrap/dropdown
//= require bootstrap-datepicker/core
//= require bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN
//= require_tree .
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome";
@import "bootstrap-datepicker3";
8 changes: 6 additions & 2 deletions app/controllers/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def show
def ship
@order = Order.find(params[:id])
@order.ship!
# OrderMailer.notify_ship(@order).deliver!
#OrderMailer.notify_ship(@order).deliver!
redirect_to :back
end

Expand All @@ -33,7 +33,11 @@ def cancel
@order.product_lists.each do |order_item|
order_item.product.update(quantity: order_item.product.quantity + order_item.quantity)
end
# OrderMailer.notify_cancel(@order).deliver!

@voucher = Voucher.find_by_code(@order.voucher_code)
@voucher.cancel! if @voucher.present?

#OrderMailer.notify_cancel(@order).deliver!
redirect_to :back
end

Expand Down
46 changes: 46 additions & 0 deletions app/controllers/admin/vouchers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Admin::VouchersController < ApplicationController

layout "admin"

before_action :authenticate_user!
before_action :admin_required

def index
@vouchers = Voucher.order("id DESC")
end

def new
@voucher = Voucher.new
end

def create
@voucher = Voucher.new(voucher_params)
if @voucher.save
redirect_to admin_vouchers_path
else
render :new
end
end

def update
@voucher = Voucher.find(params[:id])

if voucher_params["aasm_state"] == "invalid"
@voucher.admin_valid!
redirect_to admin_vouchers_path
elsif voucher_params["aasm_state"] == "valid_no_used"
@voucher.admin_invalid!
redirect_to admin_vouchers_path
else
render :back
end
end


private

def voucher_params
params.require(:voucher).permit(:start_at, :end_at, :amount, :aasm_state)
end

end
8 changes: 4 additions & 4 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class OrdersController < ApplicationController

def create
@order = Order.new(order_params)

@order.user = current_user
@order.total = current_cart.total_price

if @order.save

current_cart.cart_items.each do |cart_item|
product_list = ProductList.new
product_list.order = @order
Expand All @@ -20,7 +20,7 @@ def create
cart_item.product.update(quantity: cart_item.product.quantity - cart_item.quantity)
end
current_cart.clean!
# OrderMailer.notify_order_placed(@order).deliver!
#OrderMailer.notify_order_placed(@order).deliver!

redirect_to order_path(@order.token)
else
Expand Down Expand Up @@ -51,7 +51,7 @@ def pay_with_wechat

def apply_to_cancel
@order = Order.find_by_token(params[:id])
# OrderMailer.apply_cancel(@order).deliver!
#OrderMailer.apply_cancel(@order).deliver!
flash[:notice] = "已提交申请"
redirect_to :back
end
Expand All @@ -60,7 +60,7 @@ def apply_to_cancel
private

def order_params
params.require(:order).permit(:billing_name, :billing_address, :shipping_name, :shipping_address)
params.require(:order).permit(:billing_name, :billing_address, :shipping_name, :shipping_address, :voucher_code)
end

end
13 changes: 13 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ class Order < ApplicationRecord
validates :billing_name, :billing_address, :shipping_name, :shipping_address, presence: true

belongs_to :user
has_one :voucher
has_many :product_lists

before_create :generate_token

after_create :update_voucher

aasm do
state :order_placed, initial: true
state :paid
Expand Down Expand Up @@ -50,4 +53,14 @@ def set_payment_with!(method)
def pay!
self.update_columns(is_paid: true )
end

def update_voucher
@voucher = Voucher.where(code: self.voucher_code, aasm_state: "valid_no_used").first rescue nil
self.update(voucher_amount: @voucher.amount) if @voucher.present? && @voucher.start_at <= created_at && @voucher.end_at >= created_at && total > @voucher.amount
self.update(total: self.voucher_amount.present? ? ( self.total - self.voucher_amount) : self.total)
if self.voucher_amount.present?
@voucher.used!
@voucher.update(order_id: self.id)
end
end
end
46 changes: 46 additions & 0 deletions app/models/voucher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Voucher < ApplicationRecord

include AASM

validates :start_at, :end_at, :amount, presence: true

belongs_to :order, optional: true

before_create :generate_code

aasm do
state :valid_no_used, initial: true
state :valid_used
state :invalid

event :used do
transitions form: :valid_no_used, to: :valid_used
end

event :cancel, after_commit: :update_order_id! do
transitions from: :valid_used, to: :valid_no_used
end

event :admin_valid do
transitions from: :valid_no_used, to: :invalid
end

event :admin_invalid do
transitions from: :invalid, to: :valid_no_used
end
end


def generate_code
self.code = SecureRandom.hex(4)
end

def update_order_id!
self.update_columns(order_id: "")
end

def is_valid?()

end

end
5 changes: 4 additions & 1 deletion app/views/admin/orders/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

<div class="total clearfix">
<span class="pull-right">
总计 <%= @order.total %> CNY
总计 <%= @order.total %> CNY
<% if @order.voucher_amount.present? %>
优惠 <%= @order.voucher_amount %> CNY
<% end %>
</span>
</div>

Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<%= image_tag(@product.image.thumb.url) %>
<% end %>

<div class="group">
<%= f.input :image, as: :file %>
</div>

<%= f.submit "Submit", data: { disable_with: "Submitting..."} %>
<% end %>
47 changes: 47 additions & 0 deletions app/views/admin/vouchers/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<h2> Voucher List</h2>

<div class="pull-right" style="padding-bottom: 20px;">
<%= link_to("新增优惠券", new_admin_voucher_path, class: "btn btn-primary btn-sm") %>
</div>

<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>开始时间</th>
<th>结束时间</th>
<th>优惠金额</th>
<th>状态</th>
<th>Code</th>
<th width="150"> Options</th>
</tr>
</thead>
<tbody>
<% @vouchers.each do |voucher| %>
<tr>
<td>
<%= voucher.id %>
</td>
<td>
<%= voucher.start_at %>
</td>
<td>
<%= voucher.end_at %>
</td>
<td>
<%= voucher.amount %>
</td>
<td>
<%= voucher.aasm_state %>
</td>
<td>
<%= voucher.code %>
</td>
<td>
<%= link_to("设为有效未使用", admin_voucher_path(voucher, "voucher[aasm_state]": "valid_no_used"), method: :patch, class: "btn btn-info") if voucher.aasm_state == "invalid" %>
<%= link_to("设为无效", admin_voucher_path(voucher, "voucher[aasm_state]": "invalid"), method: :patch, class: "btn btn-info") if voucher.aasm_state == "valid_no_used" %>
</td>
</tr>
<% end %>
</tbody>
</table>
Loading

0 comments on commit 2c79eff

Please sign in to comment.