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
e6c21f24
Commit
e6c21f24
authored
Aug 28, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix spec uniq validation & Add query_spec_with_product to Series
parent
7f274e4c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
10 deletions
+24
-10
series.rb
lib/shotengai/series.rb
+22
-8
models_spec.rb
spec/shotengai/models_spec.rb
+2
-2
No files found.
lib/shotengai/series.rb
View file @
e6c21f24
...
...
@@ -22,10 +22,19 @@ module Shotengai
class
Series
<
ActiveRecord
::
Base
self
.
table_name
=
'shotengai_series'
validate
:check_spec
,
if: :spec
validates_uniqueness_of
:spec
,
scope: :shotengai_product_id
validates_presence_of
:spec
validate
:check_spec_value
# Using validates_uniqueness_of do not work if the order of Hash is diff
validate
:uniq_spec
delegate
:title
,
:detail
,
:banners
,
:cover_image
,
to: :product
delegate
:detail
,
:banners
,
:cover_image
,
to: :product
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
)
}
class
<<
self
def
inherited
subclass
...
...
@@ -46,13 +55,18 @@ module Shotengai
private
# spec 字段
def
check_spec
raise
Shotengai
::
WebError
.
new
(
'spec 必须是个 Hash'
,
'-1'
,
400
)
unless
spec
.
is_a?
(
Hash
)
raise
Shotengai
::
WebError
.
new
(
'非法的关键字,或关键字缺失'
,
'-1'
,
400
)
unless
(
product
.
spec
.
keys
-
spec
.
keys
).
empty?
def
check_spec
_value
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
product
.
spec
.
keys
-
spec
.
keys
).
empty?
illegal_values
=
{}
spec
.
each
{
|
key
,
value
|
illegal_values
[
key
]
=
value
unless
value
.
in?
(
product
.
spec
[
key
])
}
# p Shotengai::WebError.new("非法的值,#{illegal_values}", '-1', 422)
raise
Shotengai
::
WebError
.
new
(
"非法的值,
#{
illegal_values
}
"
,
'-1'
,
400
)
unless
illegal_values
.
empty?
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
end
def
uniq_spec
self
.
product
.
series
.
each
{
|
series
|
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
if
series
.
spec
==
spec
}
end
end
end
spec/shotengai/models_spec.rb
View file @
e6c21f24
...
...
@@ -69,11 +69,11 @@ RSpec.describe 'Shotengai Models' do
# 非法关键字
expect
{
@series
.
update!
(
spec:
{
"颜色"
=>
"红色"
,
"大小"
=>
1111
})
}.
to
raise_error
(
Shotengai
::
WebError
)
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
# 关键字缺失
expect
{
@series
.
update!
(
spec:
{
"颜色"
=>
"红色"
})
}.
to
raise_error
(
Shotengai
::
WebError
)
}.
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