Commit b3fde4f2 by ivan Lan

Add recycle_bin to Product & Series

parent b9e60275
......@@ -5,15 +5,19 @@ module Shotengai
self.base_resources = ProductSeries
self.template_dir = 'shotengai/merchant/series/'
skip_before_action :set_resource, only: :batch_event
# add_actions :batch_event
skip_before_action :set_resource, only: [:batch_event, :recycle_bin]
def default_query resources
resources.alive.where(
params[:product_id] && { shotengai_product_id: params[:product_id] }
)
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
@resource.soft_delete!
......@@ -23,12 +27,15 @@ module Shotengai
def batch_event # params[ids] params[:event]
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
# :relive only work for products in recycle_bin
resources = event.eql?(:relive) ? default_resources.recycle_bin : default_resources
ActiveRecord::Base.transaction do
default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
end
head 200
end
private
def resource_params
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
......
......@@ -5,11 +5,11 @@ module Shotengai
self.base_resources = Product
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
def default_query resources
resources.where(@manager && { manager: @manager })
resources.alive.where(@manager && { manager: @manager })
end
def index_query resources
......@@ -31,6 +31,7 @@ module Shotengai
end
def relive
@resource = default_resources.recycle_bin.find(params[:id])
@resource.relive!
respond_with @resource, template: "#{@template_dir}/show", status: 200
end
......@@ -43,12 +44,21 @@ module Shotengai
def batch_event # params[ids] params[:event]
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
# :relive only work for products in recycle_bin
resources = event.eql?(:relive) ? default_resources.recycle_bin : default_resources
ActiveRecord::Base.transaction do
default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
end
head 200
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
def resource_params
# QUESTION: need these ?
......
......@@ -33,6 +33,9 @@ module Shotengai
belongs_to :manager, polymorphic: true, optional: true#, touch: true
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
aasm column: :status do
state :not_on, initial: true
......
......@@ -7,7 +7,7 @@ module Shotengai
# original_price :decimal(9, 2)
# price :decimal(9, 2)
# stock :integer default(-1)
# state :string(255)
# aasm_state :string(255)
# spec :json
# type :string(255)
# meta :json
......@@ -31,6 +31,9 @@ module Shotengai
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)
scope :query_spec_with_product, ->(val, product) {
if val.keys.sort == product.spec.keys.sort
......@@ -111,7 +114,7 @@ module Shotengai
end
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.')
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