Commit ee4072cf by ivan Lan

Add column_has_children to JsonColumn & Add info_input to customer snapshots controller

parent 326c0cb3
...@@ -31,9 +31,10 @@ module Shotengai ...@@ -31,9 +31,10 @@ module Shotengai
private private
def resource_params def resource_params
remark_input = params.require(resource_key).fetch(:remark_input, nil)&.map(&:permit!) remark_input = params.require(resource_key).fetch(:remark_input, nil)&.map(&:permit!)
info_input = params.require(resource_key).fetch(:info_input, nil)&.map(&:permit!)
params.require(resource_key).permit( params.require(resource_key).permit(
:count, :shotengai_series_id :count, :shotengai_series_id
).merge({ remark_input: remark_input }) ).merge({ remark_input: remark_input, info_input: info_input })
end end
def edit_only_unpaid def edit_only_unpaid
......
...@@ -28,6 +28,35 @@ module Shotengai ...@@ -28,6 +28,35 @@ module Shotengai
def hash_column def hash_column
# like meta, detail these json using for code development # like meta, detail these json using for code development
end end
def column_has_children column, options
ArgumentError.new("Please give #{column} one child at least.") unless options[:children]
children_names = options[:children].map(&:to_s)
self_name = options[:as] || self.model_name.singular
class_eval %Q{
define_method('full_#{column}') do
read_attribute(:#{column}) || {}
end
define_method('full_#{column}=') do |val|
write_attribute(:#{column}, val)
end
define_method('#{column}') do
full_#{column}['#{self_name}'] || {}
end
define_method('#{column}=') do |val|
self.full_#{column} = full_#{column}.merge('snapshot' => val)
end
#{children_names}.each do |child|
define_method(\"\#{child}_#{column}\") do
full_#{column}[child]
end
end
}
end
end end
end end
end end
\ No newline at end of file
...@@ -61,8 +61,6 @@ module Shotengai ...@@ -61,8 +61,6 @@ module Shotengai
transitions from: :deleted, to: :not_on transitions from: :deleted, to: :not_on
end end
end end
scope :alive, -> { where.not(status: 'deleted') }
def status_zh def status_zh
{ {
......
...@@ -37,6 +37,8 @@ module Shotengai ...@@ -37,6 +37,8 @@ module Shotengai
validates :count, numericality: { only_integer: true, greater_than: 0 } validates :count, numericality: { only_integer: true, greater_than: 0 }
custom_hash_columns :spec, :info, :remark custom_hash_columns :spec, :info, :remark
column_has_children :meta, children: ['product', 'snapshot'], as: :snapshot
column_has_children :info, children: ['product', 'snapshot'], as: :snapshot
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
...@@ -80,31 +82,6 @@ module Shotengai ...@@ -80,31 +82,6 @@ module Shotengai
define_method(column) { read_attribute(column) || self.series.send(column) } define_method(column) { read_attribute(column) || self.series.send(column) }
end end
def full_meta
read_attribute(:meta) || {}
end
def full_meta= val
write_attribute(:meta, val)
end
def meta
full_meta['snapshot'] || {}
end
def product_meta
full_meta['product']
end
def series_meta
full_meta['series']
end
def meta= val
self.full_meta = full_meta.merge('snapshot' => val)
end
def already_disable def already_disable
series.deleted? || product.on_sale?.! series.deleted? || product.on_sale?.!
end end
...@@ -137,6 +114,11 @@ module Shotengai ...@@ -137,6 +114,11 @@ module Shotengai
product: product.meta, product: product.meta,
series: series.meta, series: series.meta,
snapshot: meta, snapshot: meta,
},
full_info: {
product: product.info,
series: series.info,
snapshot: info,
} }
) )
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