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
267b8da0
Commit
267b8da0
authored
Sep 27, 2017
by
ivan Lan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix JsonColumn & Add new remark logic
parent
af83bf9d
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
9 deletions
+43
-9
products_controller.rb
lib/shotengai/controller/merchant/products_controller.rb
+5
-2
json_column.rb
lib/shotengai/json_column.rb
+10
-3
product.rb
lib/shotengai/product.rb
+1
-1
series.rb
lib/shotengai/series.rb
+17
-1
snapshot.rb
lib/shotengai/snapshot.rb
+10
-2
No files found.
lib/shotengai/controller/merchant/products_controller.rb
View file @
267b8da0
...
...
@@ -65,13 +65,16 @@ module Shotengai
spec
=
params
.
require
(
resource_key
).
fetch
(
:spec
,
nil
).
try
(
:permit!
)
detail
=
params
.
require
(
resource_key
).
fetch
(
:detail
,
nil
).
try
(
:permit!
)
meta
=
params
.
require
(
resource_key
).
fetch
(
:meta
,
nil
).
try
(
:permit!
)
remark
=
params
.
require
(
resource_key
).
fetch
(
:remark
,
nil
).
try
(
:permit!
)
# NOTE: :catalog_list is a default catalog list for template example, maybe should move it to the template controller, but it need add controller template for every controller
params
.
require
(
resource_key
).
permit
(
:title
,
:default_series_id
,
:need_express
,
:need_time_attr
,
:cover_image
,
catalog_ids:
[],
banners:
[]
banners:
[],
spec_input:
[
:key
,
val:
[]],
detail_input:
[
:key
,
:val
],
meta_input:
[
:key
,
:val
],
remark_input:
[
:key
,
:val
]
).
merge
(
{
spec:
spec
,
detail:
detail
,
meta:
meta
}
{
spec:
spec
,
detail:
detail
,
meta:
meta
,
remark:
remark
}
)
end
end
...
...
lib/shotengai/json_column.rb
View file @
267b8da0
...
...
@@ -7,14 +7,21 @@ module Shotengai
class_methods
do
def
hash_columns
*
columns
columns
.
each
do
|
column
|
# QUESTION: 这样可以避免 send("#{column}="), 合适?
class_eval
%Q{
define_method('
#{
column
}
') do
super() || {}
end
define_method("
#{
column
}
_input=") do |val|
val
=
val
.
map
{
|
h
|
{
h
[
:key
]
=>
h
[
:val
]
}
}.
reduce
(
&
:merge
)
write_attribute
(
column
,
val
)
parsed_val = val.map{ |h| { (h[:key] || h['key']) => (h[:val] || h['val'])
} }.reduce(&:merge)
self.
#{
column
}
= parsed_val
end
define_method("
#{
column
}
_output") do
read_attribute
(
column
)
.
map
{
|
key
,
val
|
{
key:
key
,
val:
val
}
}
self.
#{
column
}
.map {|key, val| { key: key, val: val } }
end
}
end
end
end
...
...
lib/shotengai/product.rb
View file @
267b8da0
...
...
@@ -30,7 +30,7 @@ module Shotengai
require
'acts-as-taggable-on'
self
.
table_name
=
'shotengai_products'
hash_columns
:spec
,
:meta
,
:detail
hash_columns
:spec
,
:meta
,
:detail
,
:remark
belongs_to
:manager
,
polymorphic:
true
,
optional:
true
#, touch: true
validate
:check_spec
,
if: :spec
...
...
lib/shotengai/series.rb
View file @
267b8da0
...
...
@@ -25,11 +25,12 @@ module Shotengai
self
.
table_name
=
'shotengai_series'
validates_presence_of
:spec
validate
:check_spec_value
validate
:check_remark
# Using validates_uniqueness_of do not work if the order of Hash is diff
validate
:uniq_spec
validate
:validate_stock
hash_columns
:spec
,
:meta
hash_columns
:spec
,
:meta
,
:remark
delegate
:title
,
:detail
,
:banners
,
:cover_image
,
:status
,
:status_zh
,
:manager
,
to: :product
...
...
@@ -84,6 +85,12 @@ module Shotengai
end
end
def
initialize
*
arg
super
(
*
arg
)
self
.
remark
=
Hash
[
product
.
remark
.
map
{
|
k
,
v
|
[
k
,
true
]
}]
self
end
def
cut_stock
count
self
.
stock
.
eql?
(
-
1
)
||
self
.
update!
(
stock:
self
.
stock
-
count
)
end
...
...
@@ -115,6 +122,15 @@ module Shotengai
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
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?
# illegal_values = remark.reject { |k, v| !!v == v }.keys
# errors.add(:spec, "非法的值,仅允许布尔值。#{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.'
)
...
...
lib/shotengai/snapshot.rb
View file @
267b8da0
...
...
@@ -32,10 +32,11 @@ module Shotengai
class
Snapshot
<
Shotengai
::
Model
self
.
table_name
=
'shotengai_snapshots'
validate
:check_spec
,
if: :spec
validate
:check_spec
validate
:check_remark
validates
:count
,
numericality:
{
only_integer:
true
,
greater_than:
0
}
hash_columns
:spec
,
:meta
,
:detail
hash_columns
:spec
,
:meta
,
:detail
,
:remark
validate
:cannot_edit
,
if: :order_was_paid
before_destroy
:cannot_edit
,
if: :order_was_paid
...
...
@@ -153,6 +154,13 @@ module Shotengai
errors
.
add
(
:spec
,
"非法的值,
#{
illegal_values
}
"
)
unless
illegal_values
.
empty?
end
def
check_remark
errors
.
add
(
:remark
,
'remark 必须是个 Hash'
)
unless
remark
.
is_a?
(
Hash
)
required_key
=
series
.
series
.
remark
.
select
{
|
k
,
v
|
v
}.
keys
# remark 可添加多余字段
errors
.
add
(
:remark
,
'非法的关键字,或关键字缺失'
)
unless
(
required_key
-
remark
.
keys
).
empty?
end
# NOTE: Shotengai::Snapshot.find_by_id(self.id) to get the self before changed
def
cannot_edit
errors
.
add
(
:id
,
'订单已支付,禁止修改商品快照。'
)
...
...
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