Commit 6072cd27 by ivan Lan

Improve detail

parent 2ef915a2
...@@ -13,6 +13,7 @@ module Shotengai ...@@ -13,6 +13,7 @@ module Shotengai
autoload :AASM_DLC, 'shotengai/aasm_dlc' autoload :AASM_DLC, 'shotengai/aasm_dlc'
autoload :WebError, 'shotengai/web_error' autoload :WebError, 'shotengai/web_error'
autoload :JsonColumn, 'shotengai/json_column' autoload :JsonColumn, 'shotengai/json_column'
autoload :Harray, 'shotengai/harray'
autoload :Model, 'shotengai/model' autoload :Model, 'shotengai/model'
module Controller module Controller
......
...@@ -23,9 +23,10 @@ module Shotengai ...@@ -23,9 +23,10 @@ module Shotengai
end end
def snapshot_params def snapshot_params
remark_value = params.fetch(:snapshot).fetch(:remark_input, nil)&.permit!
params.require(:snapshot).permit( params.require(:snapshot).permit(
:shotengai_series_id, :count, remark_input: [:key, :val] :shotengai_series_id, :count
) ).merge(remark_value: remark_value)
end end
def edit_only_unpaid def edit_only_unpaid
......
...@@ -47,10 +47,10 @@ module Shotengai ...@@ -47,10 +47,10 @@ module Shotengai
end end
def snapshot_params def snapshot_params
remark_input = params.fetch(:snapshot).fetch(:remark_input, nil)&.map(&:permit!) remark_value = params.fetch(:snapshot).fetch(:remark_input, nil)&.permit!
params.fetch(:snapshot).permit( params.fetch(:snapshot).permit(
:shotengai_series_id, :count, :shotengai_series_id, :count,
).merge(remark_input: remark_input) ).merge(remark_value: remark_value)
end end
def edit_only_unpaid def edit_only_unpaid
......
...@@ -30,8 +30,8 @@ module Shotengai ...@@ -30,8 +30,8 @@ module Shotengai
private private
def resource_params def resource_params
remark_value = params.require(resource_key).fetch(:remark_value, nil)&.map(&:permit!) remark_value = params.require(resource_key).fetch(:remark_value, nil)&.permit!
info_value = params.require(resource_key).fetch(:info_value, nil)&.map(&:permit!) info_value = params.require(resource_key).fetch(:info_value, nil)&.permit!
params.require(resource_key).permit( params.require(resource_key).permit(
:count, :shotengai_series_id :count, :shotengai_series_id
).merge({ remark_value: remark_value, info_value: info_value }) ).merge({ remark_value: remark_value, info_value: info_value })
......
...@@ -39,10 +39,10 @@ module Shotengai ...@@ -39,10 +39,10 @@ module Shotengai
private private
def resource_params def resource_params
spec_value = params.require(resource_key).fetch(:spec_value, nil)&.map(&:permit!) spec_value = params.require(resource_key).fetch(:spec_value, nil)&.permit!
info_value = params.require(resource_key).fetch(:info_value, nil)&.map(&:permit!) info_value = params.require(resource_key).fetch(:info_value, nil)&.permit!
remark_value = params.require(resource_key).fetch(:remark_value, nil)&.map(&:permit!) remark_value = params.require(resource_key).fetch(:remark_value, nil)&.permit!
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!) meta = params.require(resource_key).fetch(:meta, nil)&.permit!
# ????????!!!!!, spec_value: [:key, :val] 一样的输出值 却在test报错??? # ????????!!!!!, spec_value: [:key, :val] 一样的输出值 却在test报错???
# QUESTION: WARNING: 文档bug吧????? # QUESTION: WARNING: 文档bug吧?????
params.require(resource_key).permit( params.require(resource_key).permit(
......
...@@ -22,7 +22,7 @@ module Shotengai ...@@ -22,7 +22,7 @@ module Shotengai
private private
def resource_params def resource_params
remark_value = params.require(resource_key).fetch(:remark_value, nil)&.map(&:permit!) remark_value = params.require(resource_key).fetch(:remark_value, nil)&.permit!
params.require(resource_key).permit( params.require(resource_key).permit(
:revised_amount :revised_amount
).merge({ remark_value: remark_value }) ).merge({ remark_value: remark_value })
......
...@@ -66,8 +66,8 @@ module Shotengai ...@@ -66,8 +66,8 @@ module Shotengai
spec_template = params.require(resource_key).fetch(:spec_template, nil)&.map(&:permit!) spec_template = params.require(resource_key).fetch(:spec_template, nil)&.map(&:permit!)
remark_template = params.require(resource_key).fetch(:remark_template, nil)&.map(&:permit!) remark_template = params.require(resource_key).fetch(:remark_template, nil)&.map(&:permit!)
info_template = params.require(resource_key).fetch(:info_template, nil)&.map(&:permit!) info_template = params.require(resource_key).fetch(:info_template, nil)&.map(&:permit!)
detail = params.require(resource_key).fetch(:detail, nil).try(:permit!) detail = params.require(resource_key).fetch(:detail, nil)&.permit!
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!) meta = params.require(resource_key).fetch(:meta, nil)&.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 # 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( params.require(resource_key).permit(
:title, :default_series_id, :title, :default_series_id,
......
...@@ -9,7 +9,8 @@ module Shotengai ...@@ -9,7 +9,8 @@ module Shotengai
end end
def val_at key def val_at key
self.bsearch { |obj| obj['key'].eql?(key) }&.[]('val') self.each { |obj| return obj['val'] if obj['key'].eql?(key) }
nil
end end
end end
end end
\ No newline at end of file
...@@ -31,25 +31,25 @@ module Shotengai ...@@ -31,25 +31,25 @@ module Shotengai
# like meta, detail these json using for code development # like meta, detail these json using for code development
end end
def generate_hash_template_column_for names def generate_hash_template_column_for *names
names.each do |name| names.each do |name|
class_eval %Q{ class_eval %Q{
def #{name}_value def #{name}_template
Shotengai::Harray.new(super()) Shotengai::Harray.new(super() || [])
end end
def #{name}_value= val def #{name}_template= val
raise Shotengai::WebError.new('#{name}_val 必须是个 Array'), -1 , 401) unless val.nil? || Array === val raise Shotengai::WebError.new('#{name}_val 必须是个 Array', -1 , 401) unless val.nil? || Array === val
super(val) super(val)
end end
} }
end end
end end
def generate_hash_value_column_for names, options={} def generate_hash_value_column_for *names, delegate_template_to: nil
names.each do |name| names.each do |name|
class_eval %Q{ class_eval %Q{
delegate :#{name}_template, to: :#{options[:delegate_template_to]} if self.respond_to?(#{options[:delegate_template_to]}) delegate :#{name}_template, to: :#{delegate_template_to}
def #{name} def #{name}
{ {
template: self.#{name}_template, template: self.#{name}_template,
...@@ -59,7 +59,7 @@ module Shotengai ...@@ -59,7 +59,7 @@ module Shotengai
end end
def #{name}_value= val def #{name}_value= val
raise Shotengai::WebError.new('#{name}_val 必须是个 Hash'), -1 , 401) unless val.nil? || Hash === val raise Shotengai::WebError.new('#{name}_val 必须是个 Hash', -1 , 401) unless val.nil? || Hash === val
super(val) super(val)
end end
} }
......
...@@ -38,8 +38,6 @@ module Shotengai ...@@ -38,8 +38,6 @@ module Shotengai
after_create :set_seq after_create :set_seq
custom_hash_columns :spec
include AASM_DLC include AASM_DLC
aasm column: :status do aasm column: :status do
state :unpaid, initial: true state :unpaid, initial: true
......
...@@ -31,7 +31,7 @@ module Shotengai ...@@ -31,7 +31,7 @@ module Shotengai
require 'acts-as-taggable-on' require 'acts-as-taggable-on'
self.table_name = 'shotengai_products' self.table_name = 'shotengai_products'
generate_hash_template_column_for [:spec, :info, :remark] 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
......
...@@ -23,17 +23,19 @@ module Shotengai ...@@ -23,17 +23,19 @@ module Shotengai
class Series < Shotengai::Model class Series < Shotengai::Model
self.table_name = 'shotengai_series' self.table_name = 'shotengai_series'
validates_presence_of :spec_value, unless: :product_spec_template_empty? validates_presence_of :spec_value, unless: :spec_template_empty?
validates_presence_of :price validates_presence_of :price
validate :check_spec_value, unless: :product_spec_template_empty? # validate spec_value
# Using validates_uniqueness_of do not work if the order of Hash is diff validate :check_spec_value, unless: :spec_template_empty?
validate :uniq_spec_value, unless: :product_spec_template_empty? ## Using validates_uniqueness_of do not work if the order of Hash is diff
validate :only_one_series, if: :product_spec_template_empty? validate :uniq_spec_value, unless: :spec_template_empty?
validate :only_one_series, if: :spec_template_empty?
validate :check_remark_value
# validate remark
validate :check_remark_value, unless: :remark_template_empty?
generate_hash_value_column_for [:spec, :info, :remark], delegate_template_to: :product 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
...@@ -44,7 +46,7 @@ module Shotengai ...@@ -44,7 +46,7 @@ module Shotengai
scope :query_spec_value_with_product, ->(val, product) { scope :query_spec_value_with_product, ->(val, product) {
if val.keys.sort == product.spec_template.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_value->'$.\"#{k}\"' = ? "; values << v }
where(product: product).where(keys.join(' and '), *values) where(product: product).where(keys.join(' and '), *values)
else else
self.none self.none
...@@ -104,15 +106,19 @@ module Shotengai ...@@ -104,15 +106,19 @@ module Shotengai
private private
# spec 字段 # spec 字段
def product_spec_template_empty? def spec_template_empty?
product.spec_template.empty? spec_template.empty? && spec_value.nil? # 当且仅当二者都为空才跳过验证
end
def remark_template_empty?
remark_template.empty? && remark_value.nil? # 当且仅当二者都为空才跳过验证
end end
def check_spec_value def check_spec_value
errors.add(:spec_value, 'spec_value 必须是个 Hash') unless spec_value.is_a?(Hash) 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? errors.add(:spec_value, '非法的关键字,或关键字缺失') unless (product.spec_template.keys - spec_value.keys).empty?
illegal_values = {} illegal_values = {}
spec_value.each { |key, value| illegal_values[key] = value unless value.in?(product.spec_template.val_at(key)) } 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? errors.add(:spec_value, "非法的值,#{illegal_values}") unless illegal_values.empty?
end end
...@@ -126,10 +132,10 @@ module Shotengai ...@@ -126,10 +132,10 @@ module Shotengai
errors.add(:spec_value, "无规格系列仅允许存在一项") unless product.series.empty? errors.add(:spec_value, "无规格系列仅允许存在一项") unless product.series.empty?
end end
def check_remark def check_remark_value
errors.add(:remark_value, 'remark_value 必须是个 Hash') unless remark_value.is_a?(Hash) errors.add(:remark_value, 'remark_value 必须是个 Hash') unless remark_value.is_a?(Hash)
# product.remark_value.keys 包含 remark_value.keys # product.remark_value.keys 包含 remark_value.keys
illegal_key = (remark_value.keys - product.remark_template.keys) illegal_key = (remark_value.keys - product.remark_template&.keys)
errors.add(:remark_value, "非法的关键字, #{illegal_key}") unless illegal_key.empty? errors.add(:remark_value, "非法的关键字, #{illegal_key}") unless illegal_key.empty?
end end
end end
......
...@@ -33,10 +33,10 @@ module Shotengai ...@@ -33,10 +33,10 @@ module Shotengai
class Snapshot < Shotengai::Model class Snapshot < Shotengai::Model
self.table_name = 'shotengai_snapshots' self.table_name = 'shotengai_snapshots'
validate :check_spec_value validate :check_spec_value
validate :check_remark_value validate :check_remark_value, unless: :remark_template_empty?
validates :count, numericality: { only_integer: true, greater_than: 0 } validates :count, numericality: { only_integer: true, greater_than: 0 }
generate_hash_value_column_for [:spec, :info, :remark], delegate_template_to: :series 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_value, children: ['series'], as: :snapshot column_has_children :info_value, children: ['series'], as: :snapshot
...@@ -77,10 +77,10 @@ module Shotengai ...@@ -77,10 +77,10 @@ module Shotengai
# 支付前 信息 delegate to series # 支付前 信息 delegate to series
%i{ %i{
original_price price spec banners original_price price spec_value banners
cover_image detail title cover_image detail title
}.each do |column| }.each do |column|
define_method(column) { super || self.series.send(column) } define_method(column) { super() || self.series.send(column) }
end end
def already_disable def already_disable
...@@ -149,7 +149,11 @@ module Shotengai ...@@ -149,7 +149,11 @@ module Shotengai
# spec 字段 # spec 字段
def check_spec_value def check_spec_value
errors.add(:spec_value, 'spec 与 所给系列不符。') unless spec_value == series.spec_value errors.add(:spec_value, 'spec 与 所给系列不符。') unless spec_value.nil? || spec_value == series.spec_value
end
def remark_template_empty?
remark_template.empty? && remark_value.nil? # 当且仅当二者都为空才跳过验证
end end
def check_remark_value def check_remark_value
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment