Commit 757e76d6 by ivan Lan

Fit models to the new change

parent 91aac217
......@@ -57,6 +57,11 @@ module Shotengai
}
end
def #{name}_value= val
raise Shotengai::WebError.new('#{name}_val 必须是个 Hash'), -1 , 401) unless val.nil? || Hash === val
super(val)
end
}
end
end
......@@ -79,6 +84,7 @@ module Shotengai
end
define_method('#{column}=') do |val|
val = super(val)
self.full_#{column} = full_#{column}.merge('snapshot' => val)
end
......
......@@ -31,10 +31,9 @@ module Shotengai
require 'acts-as-taggable-on'
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
validate :check_spec, if: :spec
default_scope { order(created_at: :desc) }
scope :alive, -> { where.not(status: 'deleted') }
......@@ -147,12 +146,5 @@ module Shotengai
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
......@@ -23,17 +23,17 @@ module Shotengai
class Series < Shotengai::Model
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
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
validate :uniq_spec, unless: :product_spec_empty?
validate :only_one_series, if: :product_spec_empty?
validate :uniq_spec_value, unless: :product_spec_template_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
......@@ -41,8 +41,8 @@ module Shotengai
scope :recycle_bin, ->{ unscope(where: :aasm_state).deleted.where('updated_at < ?', Time.now - 10.day )}
# where("spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ?" ,红色,S)
scope :query_spec_with_product, ->(val, product) {
if val.keys.sort == product.spec.keys.sort
scope :query_spec_value_with_product, ->(val, product) {
if val.keys.sort == product.spec_template.keys.sort
keys = []; values = []
val.map { |k, v| keys << "spec->'$.\"#{k}\"' = ? "; values << v }
where(product: product).where(keys.join(' and '), *values)
......@@ -104,33 +104,33 @@ module Shotengai
private
# spec 字段
def product_spec_empty?
product.spec.empty?
def product_spec_template_empty?
product.spec_template.empty?
end
def check_spec_value
errors.add(:spec, 'spec 必须是个 Hash') unless spec.is_a?(Hash)
errors.add(:spec, '非法的关键字,或关键字缺失') unless (product.spec.keys - spec.keys).empty?
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?
illegal_values = {}
spec.each { |key, value| illegal_values[key] = value unless value.in?(product.spec[key]) }
errors.add(:spec, "非法的值,#{illegal_values}") unless illegal_values.empty?
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?
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.')
def uniq_spec_value
if self.class.query_spec_value_with_product(self.spec_value, self.product).alive.where.not(id: self.id).any?
errors.add(:spec_value, 'Non uniq spec_value for the product.')
end
end
def only_one_series
errors.add(:spec, "无规格系列仅允许存在一项") unless product.series.empty?
errors.add(:spec_value, "无规格系列仅允许存在一项") unless product.series.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?
errors.add(:remark_value, 'remark_value 必须是个 Hash') unless remark_value.is_a?(Hash)
# product.remark_value.keys 包含 remark_value.keys
illegal_key = (remark_value.keys - product.remark_template.keys)
errors.add(:remark_value, "非法的关键字, #{illegal_key}") unless illegal_key.empty?
end
end
end
......@@ -32,13 +32,14 @@ module Shotengai
class Snapshot < Shotengai::Model
self.table_name = 'shotengai_snapshots'
validate :check_spec, unless: :product_spec_empty?
validate :check_remark
validate :check_spec_value
validate :check_remark_value
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 :info, children: ['series'], as: :snapshot
column_has_children :info_value, children: ['series'], as: :snapshot
validate :cannot_edit, if: :order_was_paid
before_destroy :cannot_edit, if: :order_was_paid
......@@ -79,7 +80,7 @@ module Shotengai
original_price price spec banners
cover_image detail title
}.each do |column|
define_method(column) { read_attribute(column) || self.series.send(column) }
define_method(column) { super || self.series.send(column) }
end
def already_disable
......@@ -105,7 +106,7 @@ module Shotengai
title: series.title,
original_price: series.original_price,
price: series.price,
spec: series.spec,
spec_value: series.spec_value,
banners: series.banners,
cover_image: series.cover_image,
detail: series.detail,
......@@ -114,9 +115,9 @@ module Shotengai
series: series.meta,
snapshot: meta,
},
full_info: {
series: series.info,
snapshot: info,
full_info_value: {
series: series.info_value,
snapshot: info_value,
}
)
end
......@@ -146,25 +147,17 @@ module Shotengai
private
# spec 字段
def product_spec_empty?
product.spec.empty?
end
def check_spec
errors.add(:spec, 'spec 必须是个 Hash') unless spec.is_a?(Hash)
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?
def check_spec_value
errors.add(:spec_value, 'spec 与 所给系列不符。') unless spec_value == series.spec_value
end
def check_remark
errors.add(:remark, 'remark 必须是个 Hash') unless remark.is_a?(Hash)
nullable_keys = series.remark.select{ |k, v| v }.keys
required_keys = product.remark.keys - nullable_keys
def check_remark_value
nullable_keys = series.remark_value.select{ |k, v| v }.keys
required_keys = product.remark_template.keys - nullable_keys
absent_keys = required_keys - remark.keys
# remark 可添加多余字段
errors.add(:remark, "必填remark值为空, #{absent_keys}") unless absent_keys.empty?
errors.add(:remark_value, "必填remark值为空, #{absent_keys}") unless absent_keys.empty?
end
# NOTE: Shotengai::Snapshot.find_by_id(self.id) to get the self before changed
......
......@@ -111,8 +111,8 @@ RSpec.describe 'Shotengai Models' do
it 'methods' do
expect(@good.default_series).to eq(@series)
# scope :query_spec_with_product
expect(TestGoodSeries.query_spec_with_product(@series.spec, @series.product).first).to eq(@series)
# scope :query_spec_value_with_product
expect(TestGoodSeries.query_spec_value_with_product(@series.spec, @series.product).first).to eq(@series)
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