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
73f9c6f4
Commit
73f9c6f4
authored
Sep 11, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add code about stock & Add validation uniq of buyer in Cart
parent
843c69a7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
86 additions
and
53 deletions
+86
-53
cart.rb
lib/shotengai/cart.rb
+2
-1
product_snapshots_controller.rb
...ngai/controllers/customer/product_snapshots_controller.rb
+36
-35
order.rb
lib/shotengai/order.rb
+7
-1
series.rb
lib/shotengai/series.rb
+12
-8
snapshot.rb
lib/shotengai/snapshot.rb
+5
-0
shotengai_series.rb
spec/factories/shotengai_series.rb
+3
-3
models_spec.rb
spec/shotengai/models_spec.rb
+21
-5
No files found.
lib/shotengai/cart.rb
View file @
73f9c6f4
...
...
@@ -30,7 +30,8 @@ module Shotengai
belongs_to
:buyer
,
polymorphic:
true
,
optional:
true
default_scope
{
where
(
status:
'cart'
)
}
validates_uniqueness_of
:buyer_id
,
scope:
[
:buyer_type
]
#
# class Order < Shotengai::Order
# can_by 'Product'
...
...
lib/shotengai/controllers/customer/product_snapshots_controller.rb
View file @
73f9c6f4
...
...
@@ -2,47 +2,48 @@ module Shotengai
module
Controller
module
Customer
class
ProductSnapshotsController
<
Shotengai
::
Controller
::
Base
self
.
resources
=
ProductSnapshot
self
.
template_dir
=
'shotengai/customer/snapshots/'
#
self.resources = ProductSnapshot
#
self.template_dir = 'shotengai/customer/snapshots/'
before_action
:buyer_auth
before_action
:edit_only_unpaid
,
except:
[
:index
,
:show
,
:create
]
default_query
do
|
resource
,
params
|
# /orders/:order_id/snapshots
# /series/:series_id/snapshots
resource
.
where
(
params
[
:order_id
]
&&
{
shotengai_order_id:
params
[
:order_id
]
}
).
where
(
params
[
:series_id
]
&&
{
shotengai_series_id:
params
[
:series_id
]
}
)
end
# before_action :buyer_auth
# before_action :edit_only_unpaid, except: [:index, :show, :create]
# default_query do |resource, params|
# # /orders/:order_id/snapshots
# # /series/:series_id/snapshots
# p 'come here'
# resource.where(
# params[:order_id] && { shotengai_order_id: params[:order_id] }
# ).where(
# params[:series_id] && { shotengai_series_id: params[:series_id] }
# )
# end
index_query
do
|
resource
,
params
|
params
[
:in_cart
]
?
resource
.
in_cart
:
resource
.
in_order
end
#
index_query do |resource, params|
#
params[:in_cart] ? resource.in_cart : resource.in_order
#
end
# 不指定 order 时,默认创建在 cart 中
# TODO: WARNING: snapshots
def
create
order_or_cart
=
Shotengai
::
Order
.
find_by_id
(
params
[
:order_id
])
||
@buyer
.
order_cart
@resource
=
order_or_cart
.
product_snapshots
.
create!
(
resource_params
)
respond_with
@resource
,
template:
"
#{
@@template_dir
}
/show"
,
status:
201
end
#
#
不指定 order 时,默认创建在 cart 中
#
#
TODO: WARNING: snapshots
#
def create
#
order_or_cart = Shotengai::Order.find_by_id(params[:order_id]) || @buyer.order_cart
#
@resource = order_or_cart.product_snapshots.create!(resource_params)
#
respond_with @resource, template: "#{@@template_dir}/show", status: 201
#
end
private
def
buyer_auth
@buyer
=
params
[
:buyer_type
].
constantize
.
find
(
params
[
:buyer_id
])
end
#
private
#
def buyer_auth
#
@buyer = params[:buyer_type].constantize.find(params[:buyer_id])
#
end
def
resource_params
params
.
require
(
resource_key
).
permit
(
:count
,
:shotengai_series_id
)
end
#
def resource_params
#
params.require(resource_key).permit(
#
:count, :shotengai_series_id
#
)
#
end
def
edit_only_unpaid
raise
Shotengai
::
WebError
.
new
(
'订单已支付,不可修改。'
,
'-1'
,
403
)
unless
@resource
.
order
.
unpaid?
end
#
def edit_only_unpaid
#
raise Shotengai::WebError.new('订单已支付,不可修改。', '-1', 403) unless @resource.order.unpaid?
#
end
end
end
end
...
...
lib/shotengai/order.rb
View file @
73f9c6f4
...
...
@@ -40,7 +40,7 @@ module Shotengai
state
:unpaid
,
initial:
true
state
:paid
,
:delivering
,
:received
,
:canceled
,
:evaluated
event
:pay
,
before:
[
:fill_snapshot
,
:set_pay_time
]
do
event
:pay
,
before:
[
:fill_snapshot
,
:
cut_stock
,
:
set_pay_time
]
do
transitions
from: :unpaid
,
to: :paid
end
...
...
@@ -77,6 +77,12 @@ module Shotengai
}
end
def
cut_stock
ActiveRecord
::
Base
.
transaction
{
self
.
snapshots
.
each
(
&
:cut_stock
)
}
end
def
set_pay_time
self
.
update!
(
pay_time:
Time
.
now
)
end
...
...
lib/shotengai/series.rb
View file @
73f9c6f4
...
...
@@ -6,7 +6,7 @@ module Shotengai
# id :integer not null, primary key
# original_price :decimal(9, 2)
# price :decimal(9, 2)
# stock :integer
# stock :integer
default(-1)
# spec :json
# type :string(255)
# meta :json
...
...
@@ -32,10 +32,13 @@ module Shotengai
# where(spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ? ,红色,S)
scope
:query_spec_with_product
,
->
(
val
,
product
)
{
return
none
unless
val
.
keys
.
sort
==
product
.
spec
.
keys
.
sort
keys
=
[];
values
=
[]
val
.
map
{
|
k
,
v
|
keys
<<
"spec->'$.
\"
#{
k
}
\"
' = ? "
;
values
<<
v
}
where
(
keys
.
join
(
' and '
),
*
values
)
if
val
.
keys
.
sort
==
product
.
spec
.
keys
.
sort
keys
=
[];
values
=
[]
val
.
map
{
|
k
,
v
|
keys
<<
"spec->'$.
\"
#{
k
}
\"
' = ? "
;
values
<<
v
}
where
(
keys
.
join
(
' and '
),
*
values
)
else
self
.
none
end
}
class
<<
self
...
...
@@ -56,8 +59,7 @@ module Shotengai
end
def
cut_stock
count
self
.
stock
-=
count
self
.
save!
self
.
stock
.
eql?
(
-
1
)
||
self
.
update!
(
stock:
self
.
stock
-
count
)
end
private
...
...
@@ -71,7 +73,9 @@ module Shotengai
end
def
uniq_spec
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
if
self
.
class
.
query_spec_with_product
(
self
.
spec
,
self
.
product
).
any?
if
self
.
class
.
query_spec_with_product
(
self
.
spec
,
self
.
product
).
where
.
not
(
id:
self
.
id
).
any?
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
end
end
def
validate_stock
...
...
lib/shotengai/snapshot.rb
View file @
73f9c6f4
...
...
@@ -72,6 +72,7 @@ module Shotengai
# 订单支付后 存储当时信息快照
def
copy_info
# cut_stock
self
.
update!
(
original_price:
series
.
original_price
,
price:
series
.
price
,
...
...
@@ -83,6 +84,10 @@ module Shotengai
)
end
def
cut_stock
self
.
series
.
cut_stock
end
def
meta
read_attribute
(
:meta
)
||
series
.
product
.
meta
.
merge
(
series
.
meta
)
end
...
...
spec/factories/shotengai_series.rb
View file @
73f9c6f4
...
...
@@ -5,7 +5,7 @@
# id :integer not null, primary key
# original_price :decimal(9, 2)
# price :decimal(9, 2)
# stock :integer
# stock :integer
default(-1)
# spec :json
# type :string(255)
# meta :json
...
...
@@ -18,7 +18,7 @@ FactoryGirl.define do
factory
:test_series
,
class:
'TestGoodSeries'
do
original_price
100
price
80
stock
10
#
stock 10
spec
{
{
"颜色"
=>
"红色"
,
...
...
@@ -41,7 +41,7 @@ FactoryGirl.define do
spec
{
{
"颜色"
=>
"红色"
,
"大小"
=>
"
S
"
,
"大小"
=>
"
L
"
,
}
}
# type
...
...
spec/shotengai/models_spec.rb
View file @
73f9c6f4
...
...
@@ -67,20 +67,36 @@ RSpec.describe 'Shotengai Models' do
it
'validate'
do
expect
(
@series
.
spec
.
class
).
to
eq
(
Hash
)
# 非法关键字
expect
{
expect
{
@series
.
update!
(
spec:
{
"颜色"
=>
"红色"
,
"大小"
=>
1111
})
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
# 关键字缺失
# require 'irb'
# binding.irb
expect
{
@series
.
update!
(
spec:
{
"颜色"
=>
"红色"
})
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
# uniq validate about spec
TestGoodSeries
.
create!
(
FactoryGirl
.
attribute_for
(
:test_series
).
merge
(
{
test_good:
@good
}
expect
{
TestGoodSeries
.
create!
(
FactoryGirl
.
attributes_for
(
:test_series
).
merge
(
{
test_good:
@good
}
)
)
)
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
end
it
'About Stock'
do
# 默认为 -1 (无限库存)
@series
.
cut_stock
(
1000
)
expect
(
@series
.
reload
.
stock
).
to
eq
(
-
1
)
# 非无限库存
@series
.
update!
(
stock:
10
)
@series
.
cut_stock
(
10
)
expect
(
@series
.
reload
.
stock
).
to
eq
(
0
)
expect
{
@series
.
cut_stock
(
20
)
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
end
it
'Associations'
do
...
...
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