Commit 85c8abfc by ivan Lan

Fix the stock validation of series

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