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
042f9186
Commit
042f9186
authored
Oct 02, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow the spec of product to be nil
parent
24d865b8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
11 deletions
+25
-11
series.rb
lib/shotengai/series.rb
+20
-10
snapshot.rb
lib/shotengai/snapshot.rb
+5
-1
No files found.
lib/shotengai/series.rb
View file @
042f9186
...
@@ -23,13 +23,15 @@ module Shotengai
...
@@ -23,13 +23,15 @@ module Shotengai
class
Series
<
Shotengai
::
Model
class
Series
<
Shotengai
::
Model
self
.
table_name
=
'shotengai_series'
self
.
table_name
=
'shotengai_series'
validates_presence_of
:spec
validates_presence_of
:spec
,
unless: :product_spec_empty?
validates_presence_of
:price
validates_presence_of
:price
validate
:check_spec_value
validate
:check_spec_value
,
unless: :product_spec_empty?
validate
:check_remark
# 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
,
unless: :product_spec_empty?
validate
:only_one_series
,
if: :product_spec_empty?
validate
:check_remark
custom_hash_columns
:spec
,
:remark
,
:info
custom_hash_columns
:spec
,
:remark
,
:info
...
@@ -102,6 +104,10 @@ module Shotengai
...
@@ -102,6 +104,10 @@ module Shotengai
private
private
# spec 字段
# spec 字段
def
product_spec_empty?
product
.
spec
.
empty?
end
def
check_spec_value
def
check_spec_value
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
product
.
spec
.
keys
-
spec
.
keys
).
empty?
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
product
.
spec
.
keys
-
spec
.
keys
).
empty?
...
@@ -110,17 +116,21 @@ module Shotengai
...
@@ -110,17 +116,21 @@ module Shotengai
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
end
end
def
uniq_spec
if
self
.
class
.
query_spec_with_product
(
self
.
spec
,
self
.
product
).
alive
.
where
.
not
(
id:
self
.
id
).
any?
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
end
end
def
only_one_series
errors
.
add
(
:spec
,
"无规格系列仅允许存在一项"
)
unless
product
.
series
.
empty?
end
def
check_remark
def
check_remark
errors
.
add
(
:remark
,
'remark 必须是个 Hash'
)
unless
remark
.
is_a?
(
Hash
)
errors
.
add
(
:remark
,
'remark 必须是个 Hash'
)
unless
remark
.
is_a?
(
Hash
)
# product.remark.keys 包含 remark.keys
# product.remark.keys 包含 remark.keys
illegal_key
=
(
remark
.
keys
-
product
.
remark
.
keys
)
illegal_key
=
(
remark
.
keys
-
product
.
remark
.
keys
)
errors
.
add
(
:remark
,
"非法的关键字,
#{
illegal_key
}
"
)
unless
illegal_key
.
empty?
errors
.
add
(
:remark
,
"非法的关键字,
#{
illegal_key
}
"
)
unless
illegal_key
.
empty?
end
end
def
uniq_spec
if
self
.
class
.
query_spec_with_product
(
self
.
spec
,
self
.
product
).
alive
.
where
.
not
(
id:
self
.
id
).
any?
errors
.
add
(
:spec
,
'Non uniq spec for the product.'
)
end
end
end
end
end
end
lib/shotengai/snapshot.rb
View file @
042f9186
...
@@ -32,7 +32,7 @@ module Shotengai
...
@@ -32,7 +32,7 @@ module Shotengai
class
Snapshot
<
Shotengai
::
Model
class
Snapshot
<
Shotengai
::
Model
self
.
table_name
=
'shotengai_snapshots'
self
.
table_name
=
'shotengai_snapshots'
validate
:check_spec
validate
:check_spec
,
unless: :product_spec_empty?
validate
:check_remark
validate
:check_remark
validates
:count
,
numericality:
{
only_integer:
true
,
greater_than:
0
}
validates
:count
,
numericality:
{
only_integer:
true
,
greater_than:
0
}
...
@@ -166,6 +166,10 @@ module Shotengai
...
@@ -166,6 +166,10 @@ module Shotengai
private
private
# spec 字段
# spec 字段
def
product_spec_empty?
product
.
spec
.
empty?
end
def
check_spec
def
check_spec
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
series
.
product
.
spec
.
keys
-
spec
.
keys
).
empty?
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
series
.
product
.
spec
.
keys
-
spec
.
keys
).
empty?
...
...
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