Commit 13bf393f by ivan Lan

Allow merchant to modify spec_rewrite

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