Skip to content

Commit

Permalink
add aasm_state to order
Browse files Browse the repository at this point in the history
  • Loading branch information
anndoko committed May 17, 2017
1 parent f3bd2e3 commit 622af4c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -37,6 +37,7 @@ 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
3 changes: 3 additions & 0 deletions Gemfile.lock
@@ -1,6 +1,8 @@
GEM
remote: https://rubygems.org/
specs:
aasm (4.12.0)
concurrent-ruby (~> 1.0)
actioncable (5.0.0)
actionpack (= 5.0.0)
nio4r (~> 1.2)
Expand Down Expand Up @@ -189,6 +191,7 @@ PLATFORMS
ruby

DEPENDENCIES
aasm
bootstrap-sass
byebug
carrierwave
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/orders_controller.rb
Expand Up @@ -16,7 +16,7 @@ def create
product_list.quantity = cart_item.quantity
product_list.save
end

current_cart.clean!
OrderMailer.notify_order_placed(@order).deliver!

Expand All @@ -35,15 +35,16 @@ def show
def pay_with_alipay
@order = Order.find_by_token(params[:id])
@order.set_payment_with!("alipay")
@order.pay!
@order.make_payment!


redirect_to order_path(@order.token), notice: "使用支付宝成功完成付款"
end

def pay_with_wechat
@order = Order.find_by_token(params[:id])
@order.set_payment_with!("wechat")
@order.pay!
@order.make_payment!

redirect_to order_path(@order.token), notice: "使用微信成功完成付款"
end
Expand Down
35 changes: 35 additions & 0 deletions app/models/order.rb
Expand Up @@ -20,4 +20,39 @@ def pay!

belongs_to :user
has_many :product_lists

include AASM

aasm do
state :order_placed, initial: true
state :paid
state :shipping
state :shipped
state :order_cancelled
state :good_returned

event :make_payment, after_commit: :pay! do
transitions from: :order_placed, to: :paid
end

event :ship do
transitions from: :paid, to: :shipping
end

event :deliver do
transitions from: :shipping, to: :shipped
end

event :return_good do
transitions from: :shipped, to: :good_returned
end

event :cancel_order do
transitions from: [:order_placed, :paid], to: :order_cancelled
end
end




end
6 changes: 6 additions & 0 deletions db/migrate/20170517043732_add_aasm_state_to_order.rb
@@ -0,0 +1,6 @@
class AddAasmStateToOrder < ActiveRecord::Migration[5.0]
def change
add_column :orders, :aasm_state, :string, default: "order_placed"
add_index :orders, :aasm_state
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170517033724) do
ActiveRecord::Schema.define(version: 20170517043732) do

create_table "cart_items", force: :cascade do |t|
t.integer "cart_id"
Expand All @@ -32,11 +32,13 @@
t.string "billing_address"
t.string "shipping_name"
t.string "shipping_address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "token"
t.boolean "is_paid", default: false
t.string "payment_method"
t.string "aasm_state", default: "order_placed"
t.index ["aasm_state"], name: "index_orders_on_aasm_state"
end

create_table "product_lists", force: :cascade do |t|
Expand Down

0 comments on commit 622af4c

Please sign in to comment.