-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
277 additions
and
145 deletions.
There are no files selected for viewing
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
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
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
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 | ||
|
||
* ... | ||
实作购物网站 | ||
添加优惠券功能 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ | |
@import "bootstrap-sprockets"; | ||
@import "bootstrap"; | ||
@import "font-awesome"; | ||
@import "bootstrap-datepicker3"; |
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
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
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 |
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
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
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
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 |
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
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
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
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> |
Oops, something went wrong.