Commit 757e76d6 by ivan Lan

Fit models to the new change

parent 91aac217
...@@ -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
......
...@@ -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
...@@ -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
...@@ -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
......
...@@ -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
......
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