Commit 568590dd by ivan Lan

Add batch_event to series & Add soft_delete to series

parent 17f38f46
...@@ -36,6 +36,7 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1] ...@@ -36,6 +36,7 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1]
t.decimal :original_price, precision: 9, scale: 2 t.decimal :original_price, precision: 9, scale: 2
t.decimal :price, precision: 9, scale: 2 t.decimal :price, precision: 9, scale: 2
t.integer :stock, default: -1 t.integer :stock, default: -1
t.string :aasm_state
t.json :spec t.json :spec
# Single Table Inheritance # Single Table Inheritance
t.string :type t.string :type
......
...@@ -30,6 +30,21 @@ module Shotengai ...@@ -30,6 +30,21 @@ module Shotengai
def remove_actions *actions def remove_actions *actions
actions.each { |name| remove_possible_method name } actions.each { |name| remove_possible_method name }
end end
# def add_action *actions
# action_methods = {}
# action_methods[:batch_event] = %Q{
# 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
# ActiveRecord::Base.transaction do
# default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
# end
# head 200
# end
# }
# actions.each { |action| class_eval(action_methods[action]) }
# end
end end
before_action :set_resource, except: [:index, :create] before_action :set_resource, except: [:index, :create]
......
...@@ -10,7 +10,7 @@ module Shotengai ...@@ -10,7 +10,7 @@ module Shotengai
remove_actions :create, :update, :destroy remove_actions :create, :update, :destroy
def default_query resources def default_query resources
resources.where( resources.alive.where(
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
......
...@@ -6,13 +6,28 @@ module Shotengai ...@@ -6,13 +6,28 @@ module Shotengai
self.template_dir = 'shotengai/merchant/series/' self.template_dir = 'shotengai/merchant/series/'
before_action :manager_auth before_action :manager_auth
# add_actions :batch_event
def default_query resources def default_query resources
resources.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 destroy
@resource.soft_delete!
head 204
end
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
ActiveRecord::Base.transaction do
default_resources.where(id: params[:ids]).each(&"#{event}!".to_sym)
end
head 200
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!)
......
...@@ -6,6 +6,7 @@ module Shotengai ...@@ -6,6 +6,7 @@ module Shotengai
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
# add_actions :batch_event
def default_query resources def default_query resources
resources.where(@manager && { manager: @manager }) resources.where(@manager && { manager: @manager })
......
...@@ -7,6 +7,7 @@ module Shotengai ...@@ -7,6 +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)
# spec :json # spec :json
# type :string(255) # type :string(255)
# meta :json # meta :json
...@@ -41,6 +42,20 @@ module Shotengai ...@@ -41,6 +42,20 @@ module Shotengai
end end
} }
include AASM_DLC
aasm column: :aasm_state do
state :alive, initial: true
state :deleted
event :soft_delete do
transitions from: :alive, to: :deleted
end
event :relive do
transitions from: :deleted, to: :alive
end
end
class << self class << self
def inherited subclass def inherited subclass
product_name = /^(.+)Series$/.match(subclass.name)[1] product_name = /^(.+)Series$/.match(subclass.name)[1]
......
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