2022-10-11 15:16:34 +00:00
|
|
|
class AskListSetting
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
2023-05-20 15:25:00 +00:00
|
|
|
DefaultFields = ['serial_number','situation','ask_category_id','title','name','sex','mail','phone','appointment','created_at','ip','reviewer','review_time']
|
|
|
|
DefaultEnabled = ['serial_number','situation','ask_category_id','title','name','phone','appointment','created_at','ip','reviewer','review_time']
|
2022-10-11 15:16:34 +00:00
|
|
|
field :default_fields, type: Array, default: []
|
|
|
|
field :custom_fields, type: Array, default: []
|
|
|
|
field :category_id
|
|
|
|
def default_fields
|
|
|
|
tmp = super()
|
2023-05-20 15:25:00 +00:00
|
|
|
if tmp.empty?
|
|
|
|
tmp = self.class::DefaultEnabled
|
|
|
|
end
|
|
|
|
return tmp
|
2022-10-11 15:16:34 +00:00
|
|
|
end
|
|
|
|
def custom_fields_list
|
|
|
|
locale = I18n.locale
|
|
|
|
default_setting = AskSetting.first
|
2022-10-15 03:22:41 +00:00
|
|
|
use_default = self.category_id.blank? || AskCategorySetting.where(:category_id=>self.category_id, :use_default.ne=>true).first.nil?
|
|
|
|
if use_default
|
2022-10-11 15:16:34 +00:00
|
|
|
fields = default_setting ? {I18n.t(:default) => default_setting.custom_fields.map{|k,v| ["default@#{k}",v['field'][locale]]}.to_h} : {}
|
|
|
|
cat_ids_map = Category.where(:id.in=> AskCategorySetting.all.pluck(:category_id),:disabled.ne=>true).map{|c| [c.id.to_s, c.title]}.to_h
|
|
|
|
fields = fields.merge(AskCategorySetting.where(:category_id.in => cat_ids_map.keys()).map{|c| [cat_ids_map[c.category_id] , c.custom_fields.select{|k,v| !(k.start_with?('default@'))}.map{|k,v| ["custom@#{c.id}@#{k}",v['field'][locale]]}]}.to_h)
|
|
|
|
else
|
|
|
|
fields = AskCategorySetting.where(:category_id=>self.category_id).to_a.map{|c| [(Category.find(c.category_id).title rescue ''), c.custom_fields.map{|k,v| [k,v['field'][locale]]}]}.to_h
|
|
|
|
end
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
end
|