Commit 267b8da0 by ivan Lan

Fix JsonColumn & Add new remark logic

parent af83bf9d
...@@ -65,13 +65,16 @@ module Shotengai ...@@ -65,13 +65,16 @@ module Shotengai
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!) spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
detail = params.require(resource_key).fetch(:detail, nil).try(:permit!) detail = params.require(resource_key).fetch(:detail, nil).try(:permit!)
meta = params.require(resource_key).fetch(:meta, 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 # 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,
:need_express, :need_time_attr, :cover_image, catalog_ids: [], :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( ).merge(
{ spec: spec, detail: detail, meta: meta } { spec: spec, detail: detail, meta: meta, remark: remark }
) )
end end
end end
......
...@@ -7,14 +7,21 @@ module Shotengai ...@@ -7,14 +7,21 @@ module Shotengai
class_methods do class_methods do
def hash_columns *columns def hash_columns *columns
columns.each do |column| columns.each do |column|
define_method("#{column}_input=") do |val| # QUESTION: 这样可以避免 send("#{column}="), 合适?
val = val.map{ |h| { h[:key] => h[:val] } }.reduce(&:merge) class_eval %Q{
write_attribute(column, val) define_method('#{column}') do
end super() || {}
end
define_method("#{column}_output") do define_method("#{column}_input=") do |val|
read_attribute(column).map {|key, val| { key: key, val: val } } parsed_val = val.map{ |h| { (h[:key] || h['key']) => (h[:val] || h['val']) } }.reduce(&:merge)
end self.#{column} = parsed_val
end
define_method("#{column}_output") do
self.#{column}.map {|key, val| { key: key, val: val } }
end
}
end end
end end
end end
......
...@@ -30,7 +30,7 @@ module Shotengai ...@@ -30,7 +30,7 @@ module Shotengai
require 'acts-as-taggable-on' require 'acts-as-taggable-on'
self.table_name = 'shotengai_products' 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 belongs_to :manager, polymorphic: true, optional: true#, touch: true
validate :check_spec, if: :spec validate :check_spec, if: :spec
......
...@@ -25,11 +25,12 @@ module Shotengai ...@@ -25,11 +25,12 @@ module Shotengai
self.table_name = 'shotengai_series' self.table_name = 'shotengai_series'
validates_presence_of :spec validates_presence_of :spec
validate :check_spec_value validate :check_spec_value
validate :check_remark
# 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 validate :uniq_spec
validate :validate_stock validate :validate_stock
hash_columns :spec, :meta hash_columns :spec, :meta, :remark
delegate :title, :detail, :banners, :cover_image, :status, :status_zh, :manager, to: :product delegate :title, :detail, :banners, :cover_image, :status, :status_zh, :manager, to: :product
...@@ -84,6 +85,12 @@ module Shotengai ...@@ -84,6 +85,12 @@ module Shotengai
end end
end end
def initialize *arg
super(*arg)
self.remark = Hash[product.remark.map { |k, v| [k, true] }]
self
end
def cut_stock count def cut_stock count
self.stock.eql?(-1) || self.update!(stock: self.stock - count) self.stock.eql?(-1) || self.update!(stock: self.stock - count)
end end
...@@ -115,6 +122,15 @@ module Shotengai ...@@ -115,6 +122,15 @@ module Shotengai
errors.add(:spec, "非法的值,#{illegal_values}") unless illegal_values.empty? errors.add(:spec, "非法的值,#{illegal_values}") unless illegal_values.empty?
end 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 def uniq_spec
if self.class.query_spec_with_product(self.spec, self.product).alive.where.not(id: self.id).any? 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.') errors.add(:spec, 'Non uniq spec for the product.')
......
...@@ -32,10 +32,11 @@ module Shotengai ...@@ -32,10 +32,11 @@ module Shotengai
class Snapshot < Shotengai::Model class Snapshot < Shotengai::Model
self.table_name = 'shotengai_snapshots' 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 } 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 validate :cannot_edit, if: :order_was_paid
before_destroy :cannot_edit, if: :order_was_paid before_destroy :cannot_edit, if: :order_was_paid
...@@ -153,6 +154,13 @@ module Shotengai ...@@ -153,6 +154,13 @@ module Shotengai
errors.add(:spec, "非法的值,#{illegal_values}") unless illegal_values.empty? errors.add(:spec, "非法的值,#{illegal_values}") unless illegal_values.empty?
end 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 # NOTE: Shotengai::Snapshot.find_by_id(self.id) to get the self before changed
def cannot_edit def cannot_edit
errors.add(:id, '订单已支付,禁止修改商品快照。') 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