ask/app/models/ask_list_setting.rb

27 lines
1.4 KiB
Ruby

class AskListSetting
include Mongoid::Document
include Mongoid::Timestamps
DefaultFields = ['serial_number','situation','ask_category_id','title','name','sex','mail','phone','appointment','created_at','ip']
DefaultEnabled = ['serial_number','situation','ask_category_id','title','name','phone','appointment','created_at','ip']
field :default_fields, type: Array, default: []
field :custom_fields, type: Array, default: []
field :category_id
def default_fields
tmp = super()
return tmp if tmp.length>0
self.class::DefaultEnabled
end
def custom_fields_list
locale = I18n.locale
default_setting = AskSetting.first
if self.category_id.blank?
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