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
2a046bc4
Commit
2a046bc4
authored
Sep 11, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve validate uniq of spec in series
parent
2f82abd4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
8 deletions
+21
-8
buyer.rb
lib/shotengai/buyer.rb
+2
-2
product.rb
lib/shotengai/product.rb
+1
-1
series.rb
lib/shotengai/series.rb
+8
-4
models_spec.rb
spec/shotengai/models_spec.rb
+10
-1
No files found.
lib/shotengai/buyer.rb
View file @
2a046bc4
...
@@ -19,8 +19,8 @@ module Shotengai
...
@@ -19,8 +19,8 @@ module Shotengai
has_one
cart_name
.
to_sym
,
class_name:
klass
.
cart_class
.
name
,
as: :buyer
has_one
cart_name
.
to_sym
,
class_name:
klass
.
cart_class
.
name
,
as: :buyer
# User.new Cart 相关
# User.new Cart 相关
class_eval
(
"
class_eval
(
"
after_create :
create_
#{
cart_name
}
after_create :
#{
cart_name
}
def
#{
cart_name
}
def
#{
cart_name
}
super || create_
#{
cart_name
}
super || create_
#{
cart_name
}
end
end
...
...
lib/shotengai/product.rb
View file @
2a046bc4
...
@@ -104,7 +104,7 @@ module Shotengai
...
@@ -104,7 +104,7 @@ module Shotengai
tag_name
=
options
[
:as
]
||
catalog_class
.
model_name
.
singular
tag_name
=
options
[
:as
]
||
catalog_class
.
model_name
.
singular
acts_as_taggable_on
tag_name
.
to_sym
acts_as_taggable_on
tag_name
.
to_sym
# 只有完整替换(只属于一个分类)的时候才进行验证,add remove 暂时未添加
# 只有完整替换(只属于一个分类)的时候才进行验证,add remove 暂时未添加
# Just c
talogs_list
= have a validation
# Just c
atalogs_list
= have a validation
class_eval
do
class_eval
do
define_method
(
"
#{
tag_name
}
_list="
)
{
|
value
|
define_method
(
"
#{
tag_name
}
_list="
)
{
|
value
|
super
catalog_class
.
validate_name_chain
(
value
)
super
catalog_class
.
validate_name_chain
(
value
)
...
...
lib/shotengai/series.rb
View file @
2a046bc4
...
@@ -26,9 +26,11 @@ module Shotengai
...
@@ -26,9 +26,11 @@ module Shotengai
validate
:check_spec_value
validate
:check_spec_value
# Using validates_uniqueness_of do not work if the order of Hash is diff
# Using validates_uniqueness_of do not work if the order of Hash is diff
validate
:uniq_spec
validate
:uniq_spec
validate
:validate_stock
delegate
:title
,
:detail
,
:banners
,
:cover_image
,
:status
,
:status_zh
,
to: :product
delegate
:title
,
:detail
,
:banners
,
:cover_image
,
:status
,
:status_zh
,
to: :product
# where(spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ? ,红色,S)
scope
:query_spec_with_product
,
->
(
val
,
product
)
{
scope
:query_spec_with_product
,
->
(
val
,
product
)
{
return
none
unless
val
.
keys
.
sort
==
product
.
spec
.
keys
.
sort
return
none
unless
val
.
keys
.
sort
==
product
.
spec
.
keys
.
sort
keys
=
[];
values
=
[]
keys
=
[];
values
=
[]
...
@@ -64,9 +66,11 @@ module Shotengai
...
@@ -64,9 +66,11 @@ module Shotengai
end
end
def
uniq_spec
def
uniq_spec
self
.
product
.
series
.
each
{
|
series
|
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
if
self
.
class
.
where
(
self
.
spec
,
self
.
product
).
any?
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
if
series
.
spec
==
spec
end
}
def
validate_stock
errors
.
add
(
:stock
,
'该商品系列库存不足.'
)
unless
self
.
stock_was
.
eql?
(
-
1
)
||
self
.
stock
>=
0
end
end
end
end
end
end
spec/shotengai/models_spec.rb
View file @
2a046bc4
...
@@ -74,6 +74,13 @@ RSpec.describe 'Shotengai Models' do
...
@@ -74,6 +74,13 @@ RSpec.describe 'Shotengai Models' do
expect
{
expect
{
@series
.
update!
(
spec:
{
"颜色"
=>
"红色"
})
@series
.
update!
(
spec:
{
"颜色"
=>
"红色"
})
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
# uniq validate about spec
TestGoodSeries
.
create!
(
FactoryGirl
.
attribute_for
(
:test_series
).
merge
(
{
test_good:
@good
}
)
)
end
end
it
'Associations'
do
it
'Associations'
do
...
@@ -86,6 +93,8 @@ RSpec.describe 'Shotengai Models' do
...
@@ -86,6 +93,8 @@ RSpec.describe 'Shotengai Models' do
it
'methods'
do
it
'methods'
do
expect
(
@good
.
default_series
).
to
eq
(
@series
)
expect
(
@good
.
default_series
).
to
eq
(
@series
)
# scope :query_spec_with_product
expect
(
TestGoodSeries
.
query_spec_with_product
(
@series
.
spec
,
@series
.
product
).
first
).
to
eq
(
@series
)
end
end
end
end
...
@@ -160,7 +169,7 @@ RSpec.describe 'Shotengai Models' do
...
@@ -160,7 +169,7 @@ RSpec.describe 'Shotengai Models' do
@order
.
send_out!
@order
.
send_out!
# set delivery_time
# set delivery_time
expect
(
@order
.
reload
.
delivery_time
).
not_to
be_nil
expect
(
@order
.
reload
.
delivery_time
).
not_to
be_nil
@order
.
get_it
!
@order
.
confirm
!
# set receipt_time
# set receipt_time
expect
(
@order
.
reload
.
receipt_time
).
not_to
be_nil
expect
(
@order
.
reload
.
receipt_time
).
not_to
be_nil
end
end
...
...
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