ask/app/models/ask_list_setting.rb

30 lines
1.6 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','reviewer','review_time']
DefaultEnabled = ['serial_number','situation','ask_category_id','title','name','phone','appointment','created_at','ip','reviewer','review_time']
field :default_fields, type: Array, default: []
field :custom_fields, type: Array, default: []
field :category_id
def default_fields
tmp = super()
if tmp.empty?
tmp = self.class::DefaultEnabled
end
return tmp
end
def custom_fields_list
locale = I18n.locale
default_setting = AskSetting.first
use_default = self.category_id.blank? || AskCategorySetting.where(:category_id=>self.category_id, :use_default.ne=>true).first.nil?
if use_default
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