orbit-4-2/app/models/site.rb

126 lines
3.8 KiB
Ruby

class Site
METAS = ['description', 'keywords']
include Mongoid::Document
include Mongoid::Timestamps
field :in_use_locales, :type => Array
field :valid_locales, :type => Array
field :roaming_id
field :private_key, :type => Binary
field :public_key, :type => Binary
field :frontend_open, :type => Boolean, :default => true
field :backend_openness_on, :type => Boolean, :default => false
field :mobile_api_openness_on, :type => Boolean, :default => false
field :desktop_closed, :type => Boolean, :default => false
field :sitemap_menu_in_header, :type => Boolean, :default => false
field :enable_terms_of_use, :type => Boolean, :default => false
field :enable_language_detection, :type => Boolean, :default => false
field :announcement_category, :type => Array, :default=>[]
field :title_always_on, :type => Boolean, :default => false
field :dashbroad_allow_visitor, :type => Boolean, :default => false
field :mail_settings, :type => Hash
field :disk_space, :type => String
field :system_package_info, :type => Hash,:default=>{}
field :school
field :department
field :default_locale
mount_uploader :default_image, ImageUploader
mount_uploader :favicon, ImageUploader
mount_uploader :mobile_icon, ImageUploader
field :search,:type => Hash
field :resque_namespace,:type => String, :default=>APP_CONFIG['orbit']
field :title, localize: true
field :footer, localize: true
field :sub_menu, localize: true
field :terms_of_use, localize: true
field :google_analytics
field :address
field :phone_number, :type => Array,:default=>[]
field :mobile_bar_color, :type => Array, :default=>[]
field :site_token
field :mobile_on, :type => Boolean, :default => false
field :store_confirmation, :type => Boolean, :default => false
field :meta_viewport, :type => Boolean, :default => false
field :month_traffic_cache
belongs_to :design
has_many :site_metas, :autosave => true, :dependent => :destroy
validate :in_use_locales, :minimum_enabled_locales
index({ access_token: 1}, { unique: true })
def confirm_store(site_token)
if self.site_token.eql?(site_token)
self.store_confirmation = true
self.save
end
end
def minimum_enabled_locales
size = self.in_use_locales.length
if size < 1
errors.add(:size, "Needs at least one language in use")
end
end
def maximum_enabled_locales
size = self.in_use_locales.length
if size >= 2
errors.add(:size, "Max 2 values allowed")
end
end
def generate_keys
private_key = OpenSSL::PKey::RSA.generate(2048)
self.public_key = private_key.public_key.to_s
self.private_key = private_key.to_s
end
def registered?
!self.roaming_id.blank?
end
def up_to_date?
res = res.split('rails_3_1').pop.gsub('(', '').gsub(')','').strip rescue nil
res.eql?('local out of date') ? false : true
end
METAS.each do |meta|
define_method meta do |locale|
fetch_meta = self.site_metas.where(key: meta).limit(1)
fetch_meta.blank? ? nil : fetch_meta[0].title_translations[locale]
end
define_method "#{meta}=" do |values|
fetch_meta = self.site_metas.where(key: meta).limit(1)[0] rescue nil
fetch_meta = self.site_metas.build(key: meta) if fetch_meta.blank?
fetch_meta.title_translations = values
fetch_meta.save
end
end
def generate_site_token
if self.site_token.nil?
self.site_token = SecureRandom.uuid.gsub('-','')
self.save
end
end
def register_site(url,university,department,email,country)
api_key = STORE_CONFIG[:store_settings]["api_key"]
self.generate_site_token
store = Store.new(self.id.to_s,self.site_token,api_key)
store.post_client(self.id.to_s,self.site_token,self.title,url,university,department,email,country)
end
end