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