Commit 507a69ba by ivan Lan

Fix the strength params' bug

parent fbf6aff1
...@@ -30,9 +30,11 @@ module Shotengai ...@@ -30,9 +30,11 @@ module Shotengai
private private
def resource_params def resource_params
remark_input = params.require(resource_key).fetch(:remark_input, nil)&.map(&:permit!)
params.require(resource_key).permit( params.require(resource_key).permit(
:count, :shotengai_series_id, remark_input: [:key, :val] :count, :shotengai_series_id
) ).merge({ remark_input: remark_input })
end end
def edit_only_unpaid def edit_only_unpaid
......
...@@ -12,6 +12,7 @@ module Shotengai ...@@ -12,6 +12,7 @@ module Shotengai
params[:product_id] && { shotengai_product_id: params[:product_id] } params[:product_id] && { shotengai_product_id: params[:product_id] }
) )
end end
def recycle_bin def recycle_bin
page = params[:page] || 1 page = params[:page] || 1
per_page = params[:per_page] || 10 per_page = params[:per_page] || 10
...@@ -38,12 +39,14 @@ module Shotengai ...@@ -38,12 +39,14 @@ module Shotengai
private private
def resource_params def resource_params
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!) spec_input = params.require(resource_key).fetch(:spec_input, nil)&.map(&:permit!)
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!) meta = params.require(resource_key).fetch(:meta, nil).try(:permit!)
# ????????!!!!!, spec_input: [:key, :val] 一样的输出值 却在test报错???
# QUESTION: WARNING: 文档bug吧?????
params.require(resource_key).permit( params.require(resource_key).permit(
:original_price, :price, :stock :original_price, :price, :stock#, spec_input: [:key, :val]
).merge( ).merge(
{ spec: spec, meta: meta } { spec_input: spec_input, meta: meta }
) )
end end
......
...@@ -62,19 +62,20 @@ module Shotengai ...@@ -62,19 +62,20 @@ module Shotengai
private private
def resource_params def resource_params
# QUESTION: need these ? # QUESTION: need these ?
spec = params.require(resource_key).fetch(:spec, nil).try(:permit!) # spec = params.require(resource_key).fetch(:spec, nil).try(:permit!)
spec_input = params.require(resource_key).fetch(:spec_input, nil)&.map(&:permit!)
remark_input = params.require(resource_key).fetch(:remark_input, nil)&.map(&:permit!)
detail = params.require(resource_key).fetch(:detail, nil).try(:permit!) detail = params.require(resource_key).fetch(:detail, nil).try(:permit!)
meta = params.require(resource_key).fetch(:meta, nil).try(:permit!) meta = params.require(resource_key).fetch(:meta, nil).try(:permit!)
remark = params.require(resource_key).fetch(:remark, nil).try(:permit!)
# NOTE: :catalog_list is a default catalog list for template example, maybe should move it to the template controller, but it need add controller template for every controller # NOTE: :catalog_list is a default catalog list for template example, maybe should move it to the template controller, but it need add controller template for every controller
params.require(resource_key).permit( params.require(resource_key).permit(
:title, :default_series_id, :title, :default_series_id,
:need_express, :need_time_attr, :cover_image, catalog_ids: [], :need_express, :need_time_attr, :cover_image, catalog_ids: [],
banners: [], banners: [],
spec_input: [:key, val: []], detail_input: [:key, :val], # spec_input: [:key, val: []],
meta_input: [:key, :val], remark_input: [:key, :val] # remark_input: [:key, :val],
).merge( ).merge(
{ spec: spec, detail: detail, meta: meta, remark: remark } { spec_input: spec_input, remark_input: remark_input, detail: detail, meta: meta }
) )
end end
end end
......
...@@ -5,7 +5,7 @@ module Shotengai ...@@ -5,7 +5,7 @@ module Shotengai
end end
class_methods do class_methods do
def hash_columns *columns def custom_hash_columns *columns
columns.each do |column| columns.each do |column|
# QUESTION: 这样可以避免 send("#{column}="), 合适? # QUESTION: 这样可以避免 send("#{column}="), 合适?
class_eval %Q{ class_eval %Q{
...@@ -14,8 +14,10 @@ module Shotengai ...@@ -14,8 +14,10 @@ module Shotengai
end end
define_method("#{column}_input=") do |val| define_method("#{column}_input=") do |val|
parsed_val = val.map{ |h| { (h[:key] || h['key']) => (h[:val] || h['val']) } }.reduce(&:merge) parsed_val = val && val.map{ |h| { (h[:key] || h['key']) => (h[:val] || h['val']) } }.reduce(&:merge)
self.#{column} = parsed_val self.#{column} = parsed_val
# self.assign_attributes('#{column}' => parsed_val)
end end
define_method("#{column}_output") do define_method("#{column}_output") do
......
...@@ -38,7 +38,7 @@ module Shotengai ...@@ -38,7 +38,7 @@ module Shotengai
after_create :set_seq after_create :set_seq
hash_columns :spec, :meta custom_hash_columns :spec, :meta
include AASM_DLC include AASM_DLC
aasm column: :status do aasm column: :status do
......
...@@ -30,7 +30,7 @@ module Shotengai ...@@ -30,7 +30,7 @@ module Shotengai
require 'acts-as-taggable-on' require 'acts-as-taggable-on'
self.table_name = 'shotengai_products' self.table_name = 'shotengai_products'
hash_columns :spec, :meta, :detail, :remark custom_hash_columns :spec, :remark
belongs_to :manager, polymorphic: true, optional: true#, touch: true belongs_to :manager, polymorphic: true, optional: true#, touch: true
validate :check_spec, if: :spec validate :check_spec, if: :spec
......
...@@ -29,7 +29,7 @@ module Shotengai ...@@ -29,7 +29,7 @@ module Shotengai
# Using validates_uniqueness_of do not work if the order of Hash is diff # Using validates_uniqueness_of do not work if the order of Hash is diff
validate :uniq_spec validate :uniq_spec
hash_columns :spec, :meta, :remark custom_hash_columns :spec, :remark
delegate :title, :detail, :banners, :cover_image, :status, :status_zh, :manager, to: :product delegate :title, :detail, :banners, :cover_image, :status, :status_zh, :manager, to: :product
......
...@@ -36,7 +36,7 @@ module Shotengai ...@@ -36,7 +36,7 @@ module Shotengai
validate :check_remark validate :check_remark
validates :count, numericality: { only_integer: true, greater_than: 0 } validates :count, numericality: { only_integer: true, greater_than: 0 }
hash_columns :spec, :meta, :detail, :remark custom_hash_columns :spec, :meta, :detail, :remark
validate :cannot_edit, if: :order_was_paid validate :cannot_edit, if: :order_was_paid
before_destroy :cannot_edit, if: :order_was_paid before_destroy :cannot_edit, if: :order_was_paid
......
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