Commit 267b8da0 by ivan Lan

Fix JsonColumn & Add new remark logic

parent af83bf9d
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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.')
......
......@@ -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, '订单已支付,禁止修改商品快照。')
......
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