Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
shotengai
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
shotengai
Commits
81f27000
Commit
81f27000
authored
Aug 29, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Customer Order & Cart Controller
parent
16d637e5
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
182 additions
and
12 deletions
+182
-12
show.json.jbuilder
...nerators/templates/views/customer/cart/show.json.jbuilder
+1
-0
_cart.json.jbuilder
lib/generators/templates/views/share/_cart.json.jbuilder
+3
-0
cart.rb
lib/shotengai/cart.rb
+9
-4
cart_controller.rb
lib/shotengai/controllers/customer/cart_controller.rb
+59
-0
orders_controller.rb
lib/shotengai/controllers/customer/orders_controller.rb
+80
-0
product_snapshots_controller.rb
...ngai/controllers/customer/product_snapshots_controller.rb
+1
-1
product_snapshots_controller.rb
...ngai/controllers/merchant/product_snapshots_controller.rb
+1
-1
products_controller.rb
lib/shotengai/controllers/merchant/products_controller.rb
+1
-1
order.rb
lib/shotengai/order.rb
+27
-5
No files found.
lib/generators/templates/views/customer/cart/show.json.jbuilder
0 → 100644
View file @
81f27000
json.partial! "shotengai/share/cart", cart: @resource
lib/generators/templates/views/share/_cart.json.jbuilder
0 → 100644
View file @
81f27000
json.extract! cart, :id
json.snapshots cart.snapshots, partial: 'shotengai/share/snapshot_simple', as: :snapshot
\ No newline at end of file
lib/shotengai/cart.rb
View file @
81f27000
...
...
@@ -27,14 +27,19 @@ module Shotengai
class
Cart
<
::
ActiveRecord
::
Base
self
.
table_name
=
'shotengai_orders'
belongs_to
:buyer
,
polymorphic:
true
,
optional:
true
default_scope
{
where
(
status:
'cart'
)
}
#
# class Order < Shotengai::Order
# can_by 'Product'
# end
#
# Would let Product belongs to :order & order_cart
#
# Would let Product belongs to :order & order_cart.
# And get cart_class Order::Cart
#
class
<<
self
def
can_buy
*
good_class_names
good_classes
=
good_class_names
.
map
{
|
name
|
Object
.
const_get
(
name
)
}
...
...
@@ -44,8 +49,8 @@ module Shotengai
},
class_name:
'Shotengai::Snapshot'
,
foreign_key: :shotengai_order_id
good_classes
.
each
do
|
klass
|
# cart has many good_class.collection
has_many
klass
.
model_name
.
collection
.
to_sym
,
class_name:
klass
.
name
,
foreign_key: :shotengai_order_id
# cart has many good_class
_snapshot
.collection
has_many
"
#{
klass
.
model_name
.
singular
}
_snapshots"
.
to_sym
,
class_name:
"
#{
klass
.
snapshot_class
}
"
,
foreign_key: :shotengai_order_id
# belongs_to 本 Cart class
# optional: true 允许父对象不存在
klass
.
snapshot_class
.
belongs_to
(
...
...
lib/shotengai/controllers/customer/cart_controller.rb
0 → 100644
View file @
81f27000
module
Shotengai
module
Controller
module
Customer
class
OrdersController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Cart
self
.
template_dir
=
'shotengai/customer/orders/'
remove_actions
index_query
do
|
resource
,
params
|
resource
.
status_is
(
params
[
:status
])
end
def
add_to_cart
buyer_type
,
buyer_id
=
add_to_cart_params
.
values_at
(
:buyer_type
,
:buyer_id
)
buyer
=
buyer_type
.
constantize
.
find
(
buyer_id
)
@snapshot
=
buyer
.
order_cart
.
snapshots
.
create!
(
add_to_cart_params
)
head
201
end
def
create_by_snapshot
end
def
create_directly
end
def
pay
@resource
.
pay!
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
def
cancel
@resource
.
cancel!
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
def
get_it
@resource
.
get_it
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
private
def
resource_params
params
.
require
(
resource_key
).
permit
(
:address
,
:customer_remark
,
snapshot_ids:
[]
)
end
def
add_to_cart_params
params
.
require
(
:snapshot
).
permit
(
:series_id
,
:count
,
:buyer_type
,
:buyer_id
)
end
end
end
end
end
lib/shotengai/controllers/customer/orders_controller.rb
0 → 100644
View file @
81f27000
module
Shotengai
module
Controller
module
Customer
class
OrdersController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Order
self
.
template_dir
=
'shotengai/customer/orders/'
before_action
:buyer_auth
before_action
:edit_only_unpaid
,
only:
[
:update
]
# before_action would not keep the super methods' "only" condition
skip_before_action
:set_resource
,
only:
[
:cart
,
:add_to_cart
,
:create_directly
]
remove_actions
:destroy
index_query
do
|
resource
,
params
|
resource
.
status_is
(
params
[
:status
])
end
def
cart
@resource
=
@buyer
.
order_cart
respond_with
@resource
,
template:
"shotengai/customer/cart/show"
end
def
add_to_cart
snapshot
=
@buyer
.
order_cart
.
product_snapshots
.
create!
(
snapshot_params
)
respond_with
@resource
=
snapshot
,
template:
'shotengai/customer/snapshots/show'
,
status:
201
end
def
create_directly
# using :series_id & :count
ActiveRecord
::
Base
.
transaction
do
@resource
=
@buyer
.
orders
.
create!
(
resource_params
)
Shotengai
::
Series
.
find
(
snapshot_params
[
:shotengai_series_id
]).
snapshots
.
create!
(
count:
snapshot_params
[
:count
],
shotengai_order:
@resource
)
end
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
,
status:
201
end
def
pay
@resource
.
pay!
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
def
cancel
@resource
.
cancel!
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
def
get_it
@resource
.
get_it
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
end
private
def
buyer_auth
@buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
end
def
resource_params
params
.
require
(
resource_key
).
permit
(
:address
,
:customer_remark
,
incr_snapshot_ids:
[],
gone_snapshot_ids:
[]
)
end
def
snapshot_params
params
.
require
(
:snapshot
).
permit
(
:shotengai_series_id
,
:count
)
end
def
edit_only_unpaid
raise
Shotengai
::
WebError
.
new
(
'订单已支付,不可修改。'
,
'-1'
,
403
)
unless
@resource
.
unpaid?
end
end
end
end
end
lib/shotengai/controllers/customer/product_snapshots_controller.rb
View file @
81f27000
...
...
@@ -3,7 +3,7 @@ module Shotengai
module
Customer
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
ProductSnapshot
self
.
template_dir
=
'shotengai/
merchant
/snapshots/'
self
.
template_dir
=
'shotengai/
customer
/snapshots/'
before_action
:edit_only_unpaid
,
only:
[
:update
,
:destroy
]
...
...
lib/shotengai/controllers/merchant/product_snapshots_controller.rb
View file @
81f27000
...
...
@@ -28,7 +28,7 @@ module Shotengai
end
def
edit_only_unpaid
raise
Shotengai
::
WebError
.
new
(
'订单已支付,不可修改。'
,
'-1'
,
403
)
unless
@resource
.
order
.
unpaid?
raise
Shotengai
::
WebError
.
new
(
'订单已支付,不可修改
该快照
。'
,
'-1'
,
403
)
unless
@resource
.
order
.
unpaid?
end
end
end
...
...
lib/shotengai/controllers/merchant/products_controller.rb
View file @
81f27000
...
...
@@ -4,7 +4,7 @@ module Shotengai
class
ProductsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
Product
self
.
template_dir
=
'shotengai/merchant/products/'
index_query
do
|
resource
,
params
|
params
[
:catalog_list
]
?
resource
.
tagged_with
(
params
[:
catalog_list
],
on: :catalogs
)
:
...
...
lib/shotengai/order.rb
View file @
81f27000
...
...
@@ -38,8 +38,9 @@ module Shotengai
include
AASM_DLC
aasm
column: :status
do
state
:unpaid
,
initial:
true
state
:paid
,
:delivering
,
:received
,
:evaluated
event
:pay
,
after:
[
:fill_snapshot
,
:set_pay_time
]
{
state
:paid
,
:delivering
,
:received
,
:canceled
,
:evaluated
event
:pay
,
before:
[
:fill_snapshot
,
:set_pay_time
]
{
transitions
from: :unpaid
,
to: :paid
}
event
:cancel
{
...
...
@@ -51,9 +52,9 @@ module Shotengai
event
:get_it
,
after: :set_receipt_time
{
transitions
from: :delivering
,
to: :received
}
event
:evaluate
{
transitions
from: :received
,
to: :evaluated
}
#
event :evaluate {
#
transitions from: :received, to: :evaluated
#
}
# event :soft_delete
end
...
...
@@ -92,6 +93,27 @@ module Shotengai
def
total_original_price
snapshots
.
sum
(
&
:total_original_price
)
end
# into order
def
incr_snapshot_ids
=
ids
ActiveRecord
::
Base
.
transaction
do
ids
.
each
{
|
id
|
# using update(shotengai_order_id: id) can not get self.id before save
Shotengai
::
Snapshot
.
find
(
id
).
update!
(
shotengai_order:
self
)
}
end
end
# back to cart
def
gone_snapshot_ids
=
ids
ActiveRecord
::
Base
.
transaction
do
ids
.
each
{
|
id
|
Shotengai
::
Snapshot
.
find
(
id
).
update!
(
shotengai_order_id:
self
.
class
.
cart_class
.
where
(
buyer:
self
.
buyer
).
first
.
id
)
}
end
end
class
<<
self
def
inherited
subclass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment