ask/app/models/ask_category_setting.rb

163 lines
6.5 KiB
Ruby
Raw Normal View History

2020-07-04 13:36:32 +00:00
class AskCategorySetting
include Mongoid::Document
include Mongoid::Timestamps
before_save do
unless @skip_callback
if self.sort_number_changed?
self.sort_number = self.sort_number.map{|k,v| [k,v.to_i]}.sort_by{|k,v| v}.to_h
need_recalc_sort = true
else
need_recalc_sort = false
end
if self.default_sort_number_changed?
need_recalc_sort = true
end
if self.custom_fields_changed?
delete_custom_fields = self.custom_fields.select{|k,v| v['delete'] == true}.keys
delete_custom_fields.each{|f| self.sort_number.delete(f)}
delete_custom_fields = delete_custom_fields.select{|k| k.include?("default@")}
self.delete_customs_func(delete_custom_fields, true)
self.custom_fields = self.custom_fields.select{|k,v| v['delete'] != true}
need_recalc_sort = true
end
2023-11-06 23:59:49 +00:00
# if need_recalc_sort
self.recalc_sort(false)
# end
end
true
end
def migrate_old
unless self.new_record?
2020-07-04 13:36:32 +00:00
if self.default_setting.class != Hash && self.default_setting.class != BSON::Document
self.default_setting = {}
end
if self.default_setting[:agree_show].nil?
self.default_setting[:agree_show] = true
end
if self.default_setting[:agree_usage].nil?
self.default_setting[:agree_usage] = false
end
if self.custom_fields.class != Hash && self.custom_fields.class != BSON::Document
self.custom_fields = {}
end
self.recalc_sort(true)
2020-07-04 13:36:32 +00:00
end
end
def recalc_sort(save_flag=true)
self.tmp_sort_number = AskSetting.get_sort_number_only(true, self, true).map.with_index{|(k,v), i| [k,i]}.to_h
self.save if save_flag
end
def get_cache_sort_number #For Frontend
if self.tmp_sort_number.blank?
self.recalc_sort
2022-08-18 03:07:45 +00:00
end
self.tmp_sort_number
2020-07-04 13:36:32 +00:00
end
2022-03-09 17:00:05 +00:00
def field_name(k,locale=I18n.locale.to_s)
(self.default_setting_field_name[k]&&self.default_setting_field_name[k][locale]) ? self.default_setting_field_name[k][locale] : self.default_field_name(k,locale)
end
def default_field_name(k,locale=I18n.locale.to_s)
I18n.with_locale(locale){(['agree_show','agree_usage'].include?(k.to_s) ? I18n.t("ask.#{k}_checkbox") : I18n.t("mongoid.attributes.ask_question.#{k}"))}
end
def field_name_translations(k)
I18n.available_locales.map{|v| [v.to_s,self.field_name(k,v.to_s)]}.to_h
end
2022-08-18 03:07:45 +00:00
def prompt_word(k,locale=I18n.locale.to_s,use_checkbox_trans=false)
tmp = self.default_setting_prompt_word[k]
(tmp&&tmp[locale]) ? tmp[locale] : self.field_name(k,locale)
end
def prompt_word_translations(k)
I18n.available_locales.map{|v| [v.to_s,self.prompt_word(k,v.to_s)]}.to_h
end
def is_required(k)
self.default_setting_required[k]
end
def agree_customs_func(apply_custom_fields, check_custom_fields=nil)
if apply_custom_fields.count != 0
self.agree_customs += apply_custom_fields
self.agree_customs.uniq!
if check_custom_fields.nil?
check_custom_fields = AskSetting.first.custom_fields
end
apply_custom_fields.each{|k| self.custom_fields["default@#{k}"] = check_custom_fields[k]}
self.need_check_customs -= apply_custom_fields
self.reject_customs -= apply_custom_fields
self.recalc_sort(false)
2022-08-18 03:07:45 +00:00
@skip_callback = true
self.save
@skip_callback = false
end
true
end
def delete_customs_func(delete_custom_fields, add_to_reject=false)
if delete_custom_fields.count != 0
2022-09-22 08:06:12 +00:00
delete_custom_fields = delete_custom_fields.map{|f| f.sub(/^default@/, '')}
2022-08-18 03:07:45 +00:00
self.need_check_customs -= delete_custom_fields
self.agree_customs -= delete_custom_fields
if add_to_reject
self.reject_customs += delete_custom_fields
self.reject_customs.uniq!
else
self.reject_customs -= delete_custom_fields
delete_custom_fields.each do |f|
2022-09-22 08:06:12 +00:00
self.custom_fields.delete("default@#{f}")
2022-08-18 03:07:45 +00:00
end
end
self.recalc_sort(false)
2022-08-18 03:07:45 +00:00
@skip_callback = true
self.save
@skip_callback = false
end
true
end
def update_need_check_customs(check_custom_fields, is_locale=false, need_check=false)
if need_check
check_custom_fields = check_custom_fields.map{|k,v| [k, v["field"][I18n.default_locale.to_s]]}.to_h unless is_locale
category_custom_fields = self.custom_fields.map{|k,v| v["field"][I18n.default_locale.to_s]}
self.need_check_customs += check_custom_fields.select{|k,v| v.blank? || !(category_custom_fields.include?(v))}.keys
else
self.need_check_customs += check_custom_fields.keys
end
self.need_check_customs = self.need_check_customs.uniq
self.need_check_customs -= self.reject_customs
self.need_check_customs -= self.agree_customs
unless is_locale
self.agree_customs.each{|k| self.custom_fields["default@#{k}"] = check_custom_fields[k]}
end
self.recalc_sort(false)
2022-08-18 03:07:45 +00:00
@skip_callback = true
self.save
@skip_callback = false
end
def get_email_regex(frontend=false)
tmp = self.email_regex
if tmp && frontend
2022-08-18 03:07:45 +00:00
tmp = tmp.gsub("\\A","^").gsub("\\z","$").gsub("\\","\\\\\\\\")
end
tmp = nil if !(self.email_regex_enable) || ((self.default_setting[:mail] == false) rescue false)
2022-08-18 03:07:45 +00:00
tmp
end
field :tmp_sort_number, type: Hash, default: {} # For Frontend
field :sort_number, type: Hash, default: {}
field :default_sort_number, type: Hash, default: {}
field :default_setting, type: Hash,default: {title:true,ask_category_id: true,name: true,sex: true,mail: true,phone: true,appointment: true,recaptcha: true,agree_show: true,agree_usage: true}
2022-08-18 03:07:45 +00:00
field :default_setting_required, type: Hash,default: {title:true,ask_category_id: true,name: true,sex: true,mail: true,phone: false,appointment: false,recaptcha: true,agree_show: false,agree_usage: true}
2022-03-09 17:00:05 +00:00
field :default_setting_field_name, type: Hash,default: {}
2022-08-18 03:07:45 +00:00
field :default_setting_prompt_word, type: Hash,default: {}
field :use_default, type: Boolean, default: false
2020-07-04 13:36:32 +00:00
field :custom_fields, type: Hash,default: {}
2022-09-21 10:07:01 +00:00
field :usage_rule, type: String, default: ''
field :category_id, type: String
2022-09-21 10:07:01 +00:00
field :title_layout, type: Integer, default: 0
2022-05-25 06:19:05 +00:00
field :email_regex_enable, type: Boolean, default: false
field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z'
2022-08-18 03:07:45 +00:00
field :need_check_customs, type: Array, default: [] #From AskSetting
field :reject_customs, type: Array, default: [] #From AskSetting
field :agree_customs, type: Array, default: [] #From AskSetting
scope :enabled, ->{any_of([{:use_default.ne=>true}, {:use_default=>true, :default_sort_number.nin=>[nil, {}]}])}
2022-08-18 03:07:45 +00:00
end