Commit 85c8abfc by ivan Lan

Fix the stock validation of series

parent eb7f797e
......@@ -28,7 +28,6 @@ module Shotengai
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, :remark
......@@ -86,7 +85,9 @@ module Shotengai
end
def cut_stock count
self.stock.eql?(-1) || self.update!(stock: self.stock - count)
return true if self.stock.eql?(-1)
raise Shotengai::WebError.new('该商品系列库存不足.', -1, 400) unless (stock = self.stock - count) >=0
self.update!(stock: stock)
end
def original_price
......@@ -119,9 +120,5 @@ module Shotengai
errors.add(:spec, 'Non uniq spec for the product.')
end
end
def validate_stock
errors.add(:stock, '该商品系列库存不足.') unless self.stock_was.eql?(-1) || self.stock >=0
end
end
end
......@@ -20,6 +20,9 @@ RSpec.describe 'Shotengai Models' do
ActiveRecord::Migration[5.1].subclasses.each do |migrate|
migrate.migrate(:up)
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
end
describe 'Product & Order' do
......
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