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]
t.decimal :original_price, precision: 9, scale: 2
t.decimal :price, precision: 9, scale: 2
t.integer :stock, default: -1
t.string :aasm_state
t.json :spec
# Single Table Inheritance
t.string :type
......
......@@ -30,6 +30,21 @@ module Shotengai
def remove_actions *actions
actions.each { |name| remove_possible_method name }
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
before_action :set_resource, except: [:index, :create]
......
......@@ -10,7 +10,7 @@ module Shotengai
remove_actions :create, :update, :destroy
def default_query resources
resources.where(
resources.alive.where(
params[:product_id] && { shotengai_product_id: params[:product_id] }
)
end
......
......@@ -6,13 +6,28 @@ module Shotengai
self.template_dir = 'shotengai/merchant/series/'
before_action :manager_auth
# add_actions :batch_event
def default_query resources
resources.where(
resources.alive.where(
params[:product_id] && { shotengai_product_id: params[:product_id] }
)
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
def resource_params
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
......
......@@ -6,6 +6,7 @@ module Shotengai
self.template_dir = 'shotengai/merchant/products/'
skip_before_action :set_resource, only: :batch_event
# add_actions :batch_event
def default_query resources
resources.where(@manager && { manager: @manager })
......
......@@ -7,6 +7,7 @@ module Shotengai
# original_price :decimal(9, 2)
# price :decimal(9, 2)
# stock :integer default(-1)
# state :string(255)
# spec :json
# type :string(255)
# meta :json
......@@ -41,6 +42,20 @@ module Shotengai
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
def inherited subclass
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