Commit b3fde4f2 by ivan Lan

Add recycle_bin to Product & Series

parent b9e60275
...@@ -5,15 +5,19 @@ module Shotengai ...@@ -5,15 +5,19 @@ module Shotengai
self.base_resources = ProductSeries self.base_resources = ProductSeries
self.template_dir = 'shotengai/merchant/series/' self.template_dir = 'shotengai/merchant/series/'
skip_before_action :set_resource, only: :batch_event skip_before_action :set_resource, only: [:batch_event, :recycle_bin]
# add_actions :batch_event
def default_query resources def default_query resources
resources.alive.where( resources.alive.where(
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
def recycle_bin
page = params[:page] || 1
per_page = params[:per_page] || 10
@resources = default_resources.recycle_bin.paginate(page: page, per_page: per_page)
respond_with @resources, template: "#{@template_dir}/index"
end
def destroy def destroy
@resource.soft_delete! @resource.soft_delete!
...@@ -23,12 +27,15 @@ module Shotengai ...@@ -23,12 +27,15 @@ module Shotengai
def batch_event # params[ids] params[:event] def batch_event # params[ids] params[:event]
event = (@base_resources.where(nil).klass.aasm.events.map(&:name) & Array[params[:event].to_sym]).first event = (@base_resources.where(nil).klass.aasm.events.map(&:name) & Array[params[:event].to_sym]).first
raise ::Shotengai::WebError.new('Invaild event', '-1', 400) unless event raise ::Shotengai::WebError.new('Invaild event', '-1', 400) unless event
# :relive only work for products in recycle_bin
resources = event.eql?(:relive) ? default_resources.recycle_bin : default_resources
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym) resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
end end
head 200 head 200
end end
private private
def resource_params def resource_params
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!) spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
......
...@@ -5,11 +5,11 @@ module Shotengai ...@@ -5,11 +5,11 @@ module Shotengai
self.base_resources = Product self.base_resources = Product
self.template_dir = 'shotengai/merchant/products/' self.template_dir = 'shotengai/merchant/products/'
skip_before_action :set_resource, only: :batch_event skip_before_action :set_resource, only: [:batch_event, :relive, :recycle_bin]
# add_actions :batch_event # add_actions :batch_event
def default_query resources def default_query resources
resources.where(@manager && { manager: @manager }) resources.alive.where(@manager && { manager: @manager })
end end
def index_query resources def index_query resources
...@@ -31,6 +31,7 @@ module Shotengai ...@@ -31,6 +31,7 @@ module Shotengai
end end
def relive def relive
@resource = default_resources.recycle_bin.find(params[:id])
@resource.relive! @resource.relive!
respond_with @resource, template: "#{@template_dir}/show", status: 200 respond_with @resource, template: "#{@template_dir}/show", status: 200
end end
...@@ -43,12 +44,21 @@ module Shotengai ...@@ -43,12 +44,21 @@ module Shotengai
def batch_event # params[ids] params[:event] def batch_event # params[ids] params[:event]
event = (@base_resources.where(nil).klass.aasm.events.map(&:name) & Array[params[:event].to_sym]).first event = (@base_resources.where(nil).klass.aasm.events.map(&:name) & Array[params[:event].to_sym]).first
raise ::Shotengai::WebError.new('Invaild event', '-1', 400) unless event raise ::Shotengai::WebError.new('Invaild event', '-1', 400) unless event
# :relive only work for products in recycle_bin
resources = event.eql?(:relive) ? default_resources.recycle_bin : default_resources
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym) resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
end end
head 200 head 200
end end
def recycle_bin
page = params[:page] || 1
per_page = params[:per_page] || 10
@resources = default_resources.recycle_bin.paginate(page: page, per_page: per_page)
respond_with @resources, template: "#{@template_dir}/index"
end
private private
def resource_params def resource_params
# QUESTION: need these ? # QUESTION: need these ?
......
...@@ -33,6 +33,9 @@ module Shotengai ...@@ -33,6 +33,9 @@ module Shotengai
belongs_to :manager, polymorphic: true, optional: true#, touch: true belongs_to :manager, polymorphic: true, optional: true#, touch: true
validate :check_spec, if: :spec validate :check_spec, if: :spec
scope :alive, -> { where.not(status: 'deleted') }
scope :recycle_bin, ->{ unscope(where: :status).deleted.where('updated_at > ?', Time.now - 10.day )}
include AASM_DLC include AASM_DLC
aasm column: :status do aasm column: :status do
state :not_on, initial: true state :not_on, initial: true
......
...@@ -7,7 +7,7 @@ module Shotengai ...@@ -7,7 +7,7 @@ module Shotengai
# original_price :decimal(9, 2) # original_price :decimal(9, 2)
# price :decimal(9, 2) # price :decimal(9, 2)
# stock :integer default(-1) # stock :integer default(-1)
# state :string(255) # aasm_state :string(255)
# spec :json # spec :json
# type :string(255) # type :string(255)
# meta :json # meta :json
...@@ -31,6 +31,9 @@ module Shotengai ...@@ -31,6 +31,9 @@ module Shotengai
delegate :title, :detail, :banners, :cover_image, :status, :status_zh, :manager, to: :product delegate :title, :detail, :banners, :cover_image, :status, :status_zh, :manager, to: :product
scope :alive, -> { where.not(aasm_state: 'deleted') }
scope :recycle_bin, ->{ unscope(where: :aasm_state).deleted.where('updated_at < ?', Time.now - 10.day )}
# where("spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ?" ,红色,S) # where("spec->'$.\"颜色\"' = ? and spec->'$.\"大小\"' = ?" ,红色,S)
scope :query_spec_with_product, ->(val, product) { scope :query_spec_with_product, ->(val, product) {
if val.keys.sort == product.spec.keys.sort if val.keys.sort == product.spec.keys.sort
...@@ -111,7 +114,7 @@ module Shotengai ...@@ -111,7 +114,7 @@ module Shotengai
end end
def uniq_spec def uniq_spec
if self.class.query_spec_with_product(self.spec, self.product).where.not(id: self.id).any? 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.') errors.add(:spec, 'Non uniq spec for the product.')
end end
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