ask/app/models/ask_category_setting.rb

138 lines
5.6 KiB
Ruby

class AskCategorySetting
include Mongoid::Document
include Mongoid::Timestamps
after_initialize do
if !self.new_record?
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.save
end
end
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
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}
end
end
true
end
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
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
@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
delete_custom_fields = delete_custom_fields.map{|f| f.sub('default@', '')}
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|
self.custom_fields.delete(f)
end
end
@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
@skip_callback = true
self.save
@skip_callback = false
end
def get_email_regex(frontend=false)
tmp = self.email_regex
if tmp
tmp = tmp.gsub("\\A","^").gsub("\\z","$").gsub("\\","\\\\\\\\")
end
tmp = nil if !(self.email_regex_enable) || (self.application_form_signup_field_sets.where(:field_name=>'email').pluck(:disabled)[0] rescue false)
tmp
end
field :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}
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}
field :default_setting_field_name, type: Hash,default: {}
field :default_setting_prompt_word, type: Hash,default: {}
field :use_default, type: Boolean, default: false
field :custom_fields, type: Hash,default: {}
field :usage_rule
field :category_id
field :title_layout, type: Integer
field :email_regex_enable, type: Boolean, default: false
field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z'
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, ->{where(:use_default.ne=>true)}
end