Commit 2a046bc4 by ivan Lan

Improve validate uniq of spec in series

parent 2f82abd4
......@@ -19,8 +19,8 @@ module Shotengai
has_one cart_name.to_sym, class_name: klass.cart_class.name, as: :buyer
# User.new Cart 相关
class_eval("
after_create :create_#{cart_name}
after_create :#{cart_name}
def #{cart_name}
super || create_#{cart_name}
end
......
......@@ -104,7 +104,7 @@ module Shotengai
tag_name = options[:as] || catalog_class.model_name.singular
acts_as_taggable_on tag_name.to_sym
# 只有完整替换(只属于一个分类)的时候才进行验证,add remove 暂时未添加
# Just ctalogs_list = have a validation
# Just catalogs_list= have a validation
class_eval do
define_method("#{tag_name}_list=") { |value|
super catalog_class.validate_name_chain(value)
......
......@@ -26,9 +26,11 @@ module Shotengai
validate :check_spec_value
# Using validates_uniqueness_of do not work if the order of Hash is diff
validate :uniq_spec
validate :validate_stock
delegate :title, :detail, :banners, :cover_image, :status, :status_zh, to: :product
# where(spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ? ,红色,S)
scope :query_spec_with_product, ->(val, product) {
return none unless val.keys.sort == product.spec.keys.sort
keys = []; values = []
......@@ -64,9 +66,11 @@ module Shotengai
end
def uniq_spec
self.product.series.each { |series|
errors.add(:spec, 'Non uniq spec for the product.') if series.spec == spec
}
errors.add(:spec, 'Non uniq spec for the product.') if self.class.where(self.spec, self.product).any?
end
def validate_stock
errors.add(:stock, '该商品系列库存不足.') unless self.stock_was.eql?(-1) || self.stock >=0
end
end
end
......@@ -74,6 +74,13 @@ RSpec.describe 'Shotengai Models' do
expect{
@series.update!(spec: {"颜色" => "红色"})
}.to raise_error(ActiveRecord::RecordInvalid)
# uniq validate about spec
TestGoodSeries.create!(
FactoryGirl.attribute_for(:test_series).merge(
{test_good: @good}
)
)
end
it 'Associations' do
......@@ -86,6 +93,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)
end
end
......@@ -160,7 +169,7 @@ RSpec.describe 'Shotengai Models' do
@order.send_out!
# set delivery_time
expect(@order.reload.delivery_time).not_to be_nil
@order.get_it!
@order.confirm!
# set receipt_time
expect(@order.reload.receipt_time).not_to be_nil
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