class SiteServer include Mongoid::Document include Mongoid::Timestamps include OrbitCategory::Categorizable include OrbitTag::Taggable include OrbitModel::Status field :server_name , type: String ,default: '' field :enable_redirect_default_domain, type: Boolean, default: false field :default_domain_names, type: Array, default: [] field :domain_name , type: String ,default: '' field :domain_names, type: Array, default: [] field :ip , type: String ,default: '' field :account , type: String ,default: '' field :password , type: String ,default: '' field :active , type: Boolean ,default: true field :has_certbot, type: Boolean , default: false field :need_update_site_ids, type: Array, default: [] def domain_names if self.domain_name != '' [self.domain_name] else super end end def site_constructs SiteConstruct.where(server_type: self.server_name) end before_save do if self.domain_name != '' && self.domain_names.length !=0 self.domain_name = '' end tmp_site_constructs = site_constructs need_change = false if self.enable_redirect_default_domain_changed? tmp_site_constructs = tmp_site_constructs.where(:enable_redirect_default_domain.in=>[nil,0]) need_change = true end if (need_change || (!need_change && self.enable_redirect_default_domain)) if self.default_domain_names_changed? check_domains = [] removed_default_domains = self.default_domain_names_was - self.default_domain_names add_default_domains = self.default_domain_names - self.default_domain_names_was check_domains += removed_default_domains check_domains += add_default_domains check_domains = check_domains.map{|domain| Regexp.new("(\\A|\\s)"+domain.gsub(".","\\.").gsub("*","[^.]*") + "($|\\s)")} tmp_site_constructs = tmp_site_constructs.where(:domain_name.in=>check_domains) need_change = true elsif need_change check_domains = self.default_domain_names check_domains = check_domains.map{|domain| Regexp.new("(\\A|\\s)"+domain.gsub(".","\\.").gsub("*","[^.]*") + "($|\\s)")} tmp_site_constructs = tmp_site_constructs.where(:domain_name.in=>check_domains) end end if need_change self.need_update_site_ids += tmp_site_constructs.pluck(:id) self.need_update_site_ids = self.need_update_site_ids.uniq end end end