Commit 0835d741 by ivan Lan

Improve snapshot validate

parent a1a3743c
...@@ -8,6 +8,10 @@ module Shotengai ...@@ -8,6 +8,10 @@ module Shotengai
remove_actions :create, :update, :destroy remove_actions :create, :update, :destroy
skip_before_action :buyer_auth skip_before_action :buyer_auth
def default_query resources
resources.alive
end
def index_query resources def index_query resources
resources.catalog_list_filter(::Catalog.where(id: params[:catalog_ids])) resources.catalog_list_filter(::Catalog.where(id: params[:catalog_ids]))
end end
......
...@@ -58,6 +58,8 @@ module Shotengai ...@@ -58,6 +58,8 @@ module Shotengai
end end
end end
scope :alive, -> { where.not(status: 'deleted') }
def status_zh def status_zh
{ {
not_on: '未上架', not_on: '未上架',
......
...@@ -16,15 +16,18 @@ module Shotengai ...@@ -16,15 +16,18 @@ module Shotengai
# type :string(255) # type :string(255)
# meta :json # meta :json
# shotengai_series_id :integer # shotengai_series_id :integer
# shotengai_order_id :integer # shotengai_order_id :integer
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null
# # manager_type :string(255)
# Indexes # manager_id :integer
# #
# index_shotengai_snapshots_on_shotengai_order_id (shotengai_order_id) # Indexes
# index_shotengai_snapshots_on_shotengai_series_id (shotengai_series_id) #
# index_shotengai_snapshots_on_type (type) # index_shotengai_snapshots_on_manager_type_and_manager_id (manager_type,manager_id)
# index_shotengai_snapshots_on_shotengai_order_id (shotengai_order_id)
# index_shotengai_snapshots_on_shotengai_series_id (shotengai_series_id)
# index_shotengai_snapshots_on_type (type)
# #
class Snapshot < ActiveRecord::Base class Snapshot < ActiveRecord::Base
...@@ -32,8 +35,11 @@ module Shotengai ...@@ -32,8 +35,11 @@ module Shotengai
validate :check_spec, if: :spec validate :check_spec, if: :spec
validates :count, numericality: { only_integer: true, greater_than: 0 } validates :count, numericality: { only_integer: true, greater_than: 0 }
validate :cannot_edit_if_order_is_paid validate :cannot_edit, if: :order_was_paid
before_destroy :cannot_edit, if: :order_was_paid
validate :cannot_edit_or_create, if: :already_disable?
belongs_to :shotengai_order, foreign_key: :shotengai_order_id, belongs_to :shotengai_order, foreign_key: :shotengai_order_id,
class_name: 'Shotengai::Order', optional: true#, touch: true class_name: 'Shotengai::Order', optional: true#, touch: true
belongs_to :shotengai_cart, foreign_key: :shotengai_order_id, belongs_to :shotengai_cart, foreign_key: :shotengai_order_id,
...@@ -76,7 +82,11 @@ module Shotengai ...@@ -76,7 +82,11 @@ module Shotengai
end end
def already_disable def already_disable
series.deleted? series.deleted? || product.on_sale?.!
end
def order_was_paid
Shotengai::Snapshot.find_by_id(self.id)&.order_status.in?(['unpaid', 'cart', nil]).!
end end
def manager def manager
...@@ -138,10 +148,12 @@ module Shotengai ...@@ -138,10 +148,12 @@ module Shotengai
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
def cannot_edit_if_order_is_paid def cannot_edit
unless Shotengai::Snapshot.find_by_id(self.id)&.order_status.in?(['unpaid', 'cart', nil]) errors.add(:id, '订单已支付,禁止修改商品快照。')
errors.add(:id, '订单已支付,禁止修改商品快照。') end
end
def cannot_edit_or_create
error.add(:id, '商品已下架,无法购买。')
end end
def set_manager def set_manager
......
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