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
757e76d6
Commit
757e76d6
authored
7 years ago
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fit models to the new change
parent
91aac217
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
56 deletions
+47
-56
json_column.rb
lib/shotengai/json_column.rb
+6
-0
product.rb
lib/shotengai/product.rb
+1
-9
series.rb
lib/shotengai/series.rb
+22
-22
snapshot.rb
lib/shotengai/snapshot.rb
+16
-23
models_spec.rb
spec/shotengai/models_spec.rb
+2
-2
No files found.
lib/shotengai/json_column.rb
View file @
757e76d6
...
...
@@ -57,6 +57,11 @@ module Shotengai
}
end
def
#{
name
}
_value= val
raise Shotengai::WebError.new('
#{
name
}
_val 必须是个 Hash'), -1 , 401) unless val.nil? || Hash === val
super(val)
end
}
end
end
...
...
@@ -79,6 +84,7 @@ module Shotengai
end
define_method('
#{
column
}
=') do |val|
val = super(val)
self.full_
#{
column
}
= full_
#{
column
}
.merge('snapshot' => val)
end
...
...
This diff is collapsed.
Click to expand it.
lib/shotengai/product.rb
View file @
757e76d6
...
...
@@ -31,10 +31,9 @@ module Shotengai
require
'acts-as-taggable-on'
self
.
table_name
=
'shotengai_products'
custom_hash_columns
:spec
,
:remark
,
:info
generate_hash_template_column_for
[
:spec
,
:info
,
:remark
]
belongs_to
:manager
,
polymorphic:
true
,
optional:
true
#, touch: true
validate
:check_spec
,
if: :spec
default_scope
{
order
(
created_at: :desc
)
}
scope
:alive
,
->
{
where
.
not
(
status:
'deleted'
)
}
...
...
@@ -147,12 +146,5 @@ module Shotengai
end
end
end
private
# spec 字段
def
check_spec
raise
Shotengai
::
WebError
.
new
(
'spec 必须是个 Hash'
,
'-1'
,
400
)
unless
spec
.
is_a?
(
Hash
)
spec
.
values
{
|
val
|
raise
Shotengai
::
WebError
.
new
(
'值必须为 Array'
,
'-1'
,
400
)
unless
val
.
is_a?
(
Array
)
}
end
end
end
This diff is collapsed.
Click to expand it.
lib/shotengai/series.rb
View file @
757e76d6
...
...
@@ -23,17 +23,17 @@ module Shotengai
class
Series
<
Shotengai
::
Model
self
.
table_name
=
'shotengai_series'
validates_presence_of
:spec
,
unless: :product_spec
_empty?
validates_presence_of
:spec
_value
,
unless: :product_spec_template
_empty?
validates_presence_of
:price
validate
:check_spec_value
,
unless: :product_spec_empty?
validate
:check_spec_value
,
unless: :product_spec_
template_
empty?
# Using validates_uniqueness_of do not work if the order of Hash is diff
validate
:uniq_spec
,
unless: :product_spec
_empty?
validate
:only_one_series
,
if: :product_spec_empty?
validate
:uniq_spec
_value
,
unless: :product_spec_template
_empty?
validate
:only_one_series
,
if: :product_spec_
template_
empty?
validate
:check_remark
validate
:check_remark
_value
custom_hash_columns
:spec
,
:remark
,
:info
generate_hash_value_column_for
[
:spec
,
:info
,
:remark
],
delegate_template_to: :product
delegate
:title
,
:detail
,
:banners
,
:cover_image
,
:status
,
:status_zh
,
:manager
,
to: :product
...
...
@@ -41,8 +41,8 @@ module Shotengai
scope
:recycle_bin
,
->
{
unscope
(
where: :aasm_state
).
deleted
.
where
(
'updated_at < ?'
,
Time
.
now
-
10
.
day
)}
# where("spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ?" ,红色,S)
scope
:query_spec_with_product
,
->
(
val
,
product
)
{
if
val
.
keys
.
sort
==
product
.
spec
.
keys
.
sort
scope
:query_spec_
value_
with_product
,
->
(
val
,
product
)
{
if
val
.
keys
.
sort
==
product
.
spec
_template
.
keys
.
sort
keys
=
[];
values
=
[]
val
.
map
{
|
k
,
v
|
keys
<<
"spec->'$.
\"
#{
k
}
\"
' = ? "
;
values
<<
v
}
where
(
product:
product
).
where
(
keys
.
join
(
' and '
),
*
values
)
...
...
@@ -104,33 +104,33 @@ module Shotengai
private
# spec 字段
def
product_spec_empty?
product
.
spec
.
empty?
def
product_spec_
template_
empty?
product
.
spec
_template
.
empty?
end
def
check_spec_value
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
product
.
spec
.
keys
-
spec
.
keys
).
empty?
errors
.
add
(
:spec
_value
,
'spec_value 必须是个 Hash'
)
unless
spec_value
.
is_a?
(
Hash
)
errors
.
add
(
:spec
_value
,
'非法的关键字,或关键字缺失'
)
unless
(
product
.
spec_value
.
keys
-
spec_value
.
keys
).
empty?
illegal_values
=
{}
spec
.
each
{
|
key
,
value
|
illegal_values
[
key
]
=
value
unless
value
.
in?
(
product
.
spec
[
key
]
)
}
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
spec
_value
.
each
{
|
key
,
value
|
illegal_values
[
key
]
=
value
unless
value
.
in?
(
product
.
spec_template
.
val_at
(
key
)
)
}
errors
.
add
(
:spec
_value
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
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.'
)
def
uniq_spec
_value
if
self
.
class
.
query_spec_
value_with_product
(
self
.
spec_value
,
self
.
product
).
alive
.
where
.
not
(
id:
self
.
id
).
any?
errors
.
add
(
:spec
_value
,
'Non uniq spec_value
for the product.'
)
end
end
def
only_one_series
errors
.
add
(
:spec
,
"无规格系列仅允许存在一项"
)
unless
product
.
series
.
empty?
errors
.
add
(
:spec
_value
,
"无规格系列仅允许存在一项"
)
unless
product
.
series
.
empty?
end
def
check_remark
errors
.
add
(
:remark
,
'remark 必须是个 Hash'
)
unless
remark
.
is_a?
(
Hash
)
# product.remark
.keys 包含 remark
.keys
illegal_key
=
(
remark
.
keys
-
product
.
remark
.
keys
)
errors
.
add
(
:remark
,
"非法的关键字,
#{
illegal_key
}
"
)
unless
illegal_key
.
empty?
errors
.
add
(
:remark
_value
,
'remark_value 必须是个 Hash'
)
unless
remark_value
.
is_a?
(
Hash
)
# product.remark
_value.keys 包含 remark_value
.keys
illegal_key
=
(
remark
_value
.
keys
-
product
.
remark_template
.
keys
)
errors
.
add
(
:remark
_value
,
"非法的关键字,
#{
illegal_key
}
"
)
unless
illegal_key
.
empty?
end
end
end
This diff is collapsed.
Click to expand it.
lib/shotengai/snapshot.rb
View file @
757e76d6
...
...
@@ -32,13 +32,14 @@ module Shotengai
class
Snapshot
<
Shotengai
::
Model
self
.
table_name
=
'shotengai_snapshots'
validate
:check_spec
,
unless: :product_spec_empty?
validate
:check_remark
validate
:check_spec
_value
validate
:check_remark
_value
validates
:count
,
numericality:
{
only_integer:
true
,
greater_than:
0
}
custom_hash_columns
:spec
,
:info
,
:remark
generate_hash_value_column_for
[
:spec
,
:info
,
:remark
],
delegate_template_to: :series
column_has_children
:meta
,
children:
[
'product'
,
'series'
],
as: :snapshot
column_has_children
:info
,
children:
[
'series'
],
as: :snapshot
column_has_children
:info
_value
,
children:
[
'series'
],
as: :snapshot
validate
:cannot_edit
,
if: :order_was_paid
before_destroy
:cannot_edit
,
if: :order_was_paid
...
...
@@ -79,7 +80,7 @@ module Shotengai
original_price price spec banners
cover_image detail title
}
.
each
do
|
column
|
define_method
(
column
)
{
read_attribute
(
column
)
||
self
.
series
.
send
(
column
)
}
define_method
(
column
)
{
super
||
self
.
series
.
send
(
column
)
}
end
def
already_disable
...
...
@@ -105,7 +106,7 @@ module Shotengai
title:
series
.
title
,
original_price:
series
.
original_price
,
price:
series
.
price
,
spec
:
series
.
spec
,
spec
_value:
series
.
spec_value
,
banners:
series
.
banners
,
cover_image:
series
.
cover_image
,
detail:
series
.
detail
,
...
...
@@ -114,9 +115,9 @@ module Shotengai
series:
series
.
meta
,
snapshot:
meta
,
},
full_info:
{
series:
series
.
info
,
snapshot:
info
,
full_info
_value
:
{
series:
series
.
info
_value
,
snapshot:
info
_value
,
}
)
end
...
...
@@ -146,25 +147,17 @@ module Shotengai
private
# spec 字段
def
product_spec_empty?
product
.
spec
.
empty?
end
def
check_spec
errors
.
add
(
:spec
,
'spec 必须是个 Hash'
)
unless
spec
.
is_a?
(
Hash
)
errors
.
add
(
:spec
,
'非法的关键字,或关键字缺失'
)
unless
(
series
.
product
.
spec
.
keys
-
spec
.
keys
).
empty?
illegal_values
=
{}
spec
.
each
{
|
key
,
value
|
illegal_values
[
key
]
=
value
unless
value
.
in?
(
Array
(
series
.
product
.
spec
[
key
]))
}
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
def
check_spec_value
errors
.
add
(
:spec_value
,
'spec 与 所给系列不符。'
)
unless
spec_value
==
series
.
spec_value
end
def
check_remark
errors
.
add
(
:remark
,
'remark 必须是个 Hash'
)
unless
remark
.
is_a?
(
Hash
)
nullable_keys
=
series
.
remark
.
select
{
|
k
,
v
|
v
}.
keys
required_keys
=
product
.
remark
.
keys
-
nullable_keys
def
check_remark_value
nullable_keys
=
series
.
remark_value
.
select
{
|
k
,
v
|
v
}.
keys
required_keys
=
product
.
remark_template
.
keys
-
nullable_keys
absent_keys
=
required_keys
-
remark
.
keys
# remark 可添加多余字段
errors
.
add
(
:remark
,
"必填remark值为空,
#{
absent_keys
}
"
)
unless
absent_keys
.
empty?
errors
.
add
(
:remark
_value
,
"必填remark值为空,
#{
absent_keys
}
"
)
unless
absent_keys
.
empty?
end
# NOTE: Shotengai::Snapshot.find_by_id(self.id) to get the self before changed
...
...
This diff is collapsed.
Click to expand it.
spec/shotengai/models_spec.rb
View file @
757e76d6
...
...
@@ -111,8 +111,8 @@ RSpec.describe 'Shotengai Models' do
it
'methods'
do
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
)
# scope :query_spec_
value_
with_product
expect
(
TestGoodSeries
.
query_spec_
value_
with_product
(
@series
.
spec
,
@series
.
product
).
first
).
to
eq
(
@series
)
end
end
...
...
This diff is collapsed.
Click to expand it.
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