Commit 13bf393f by ivan Lan

Allow merchant to modify spec_rewrite

parent 5e976c3e
...@@ -89,9 +89,10 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1] ...@@ -89,9 +89,10 @@ class CreateShotengaiProductsAndOrders < ActiveRecord::Migration[5.1]
# Single Table Inheritance # Single Table Inheritance
t.string :type t.string :type
t.json :meta t.json :meta
t.json :spec_value t.json :spec_value
t.json :remark_value t.json :spec_rewrite
t.json :info_value t.json :remark_value
t.json :info_value
t.references :shotengai_series, foreign_key: true t.references :shotengai_series, foreign_key: true
t.references :shotengai_order, foreign_key: true t.references :shotengai_order, foreign_key: true
......
...@@ -6,7 +6,6 @@ module Shotengai ...@@ -6,7 +6,6 @@ module Shotengai
self.template_dir = 'shotengai/merchant/snapshots/' self.template_dir = 'shotengai/merchant/snapshots/'
remove_actions :create, :destroy remove_actions :create, :destroy
before_action :edit_only_unpaid, only: :update
def default_query resources def default_query resources
resources.in_order resources.in_order
...@@ -20,6 +19,15 @@ module Shotengai ...@@ -20,6 +19,15 @@ module Shotengai
) )
end end
def update
@resource.class.transaction do
@resource.attributes = resource_params
@resource.has_changes_to_save? && edit_only_unpaid
@resource.update_columns(rewrite_params.to_h) # skip validation
end
head 200
end
private private
def resource_params def resource_params
remark_value = params.require(resource_key).fetch(:remark_value, {}).permit! remark_value = params.require(resource_key).fetch(:remark_value, {}).permit!
...@@ -28,6 +36,10 @@ module Shotengai ...@@ -28,6 +36,10 @@ module Shotengai
).merge(remark_value: remark_value) ).merge(remark_value: remark_value)
end end
def rewrite_params
params.require(resource_key).permit(spec_rewrite: {})
end
def edit_only_unpaid def edit_only_unpaid
raise Shotengai::WebError.new('订单已支付,不可修改该快照。', '-1', 403) unless @resource.order.unpaid? raise Shotengai::WebError.new('订单已支付,不可修改该快照。', '-1', 403) unless @resource.order.unpaid?
end end
......
...@@ -36,20 +36,22 @@ module Shotengai ...@@ -36,20 +36,22 @@ module Shotengai
def template_with_value key, value: "#{key}_value", template: "#{key}_template", trans: nil, trans_to: :en def template_with_value key, value: "#{key}_value", template: "#{key}_template", trans: nil, trans_to: :en
trans_result = trans.is_a?(Hash) ? %Q{ trans_result = trans.is_a?(Hash) ? %Q{
#{trans_to}_value: val&.reduce({}) do |o, obj| {
trans = #{trans} #{trans_to}_value: val&.reduce({}) do |o, obj|
val = obj[1].is_a?(Array) ? obj[1].map{ |x| trans[x] } : trans[ obj[1] ] trans = #{trans}
o.merge( obj[0] => val ) val = obj[1].is_a?(Array) ? obj[1].map{ |x| trans[x] } : trans[ obj[1] ]
end o.merge( obj[0] => val )
} : {} end
}
} : '{}'
class_eval %Q{ class_eval %Q{
def #{key} def #{key}
val = #{value}.is_a?(Harray) ? #{value}.decode : #{value} val = #{value}.is_a?(Harray) ? #{value}.decode : #{value}
{ {
template: Shotengai::Harray.encode(#{template}).keys, template: Shotengai::Harray.encode(#{template}).keys,
value: val, value: val,
}.merge(#{trans_result}) }.merge(#{trans_result}).merge(try(:#{key}_rewrite) || {})
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