This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/models/site.rb

81 lines
2.5 KiB
Ruby
Raw Normal View History

class Site
2012-05-06 16:54:18 +00:00
METAS = ['description', 'keywords']
include Mongoid::Document
2011-03-08 09:25:46 +00:00
include Mongoid::Timestamps
field :in_use_locales, :type => Array
field :valid_locales, :type => Array
2011-08-18 05:43:33 +00:00
field :roaming_id
field :private_key, :type => Binary
field :public_key, :type => Binary
field :title_always_on, :type => Boolean, :default => false
2012-08-23 08:05:14 +00:00
field :dashbroad_allow_visitor, :type => Boolean, :default => false
2012-05-09 03:38:29 +00:00
field :mail_settings, :type => Hash
2011-08-18 05:43:33 +00:00
field :school
field :department
2012-07-26 08:39:29 +00:00
mount_uploader :default_image, ImageUploader
2012-05-06 16:54:18 +00:00
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
2012-05-07 03:25:33 +00:00
has_one :footer, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
2012-05-08 09:27:02 +00:00
has_one :sub_menu, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
has_many :site_metas, :autosave => true, :dependent => :destroy
2011-08-18 05:43:33 +00:00
2012-05-06 16:54:18 +00:00
before_save :set_key
2011-08-18 05:43:33 +00:00
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
2011-08-23 11:40:49 +00:00
def up_to_date?
2011-08-25 00:30:09 +00:00
res = res.split('rails_3_1').pop.gsub('(', '').gsub(')','').strip rescue nil
2011-08-23 11:40:49 +00:00
res.eql?('local out of date') ? false : true
end
2012-05-06 16:54:18 +00:00
def title
@title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
end
2012-05-07 03:25:33 +00:00
def footer
@footer ||= I18nVariable.first(:conditions => {:key => 'footer', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
end
2012-05-08 09:27:02 +00:00
def sub_menu
@sub_menu ||= I18nVariable.first(:conditions => {:key => 'sub_menu', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
end
2012-05-06 16:54:18 +00:00
METAS.each do |meta|
define_method meta do
fetch_meta = self.site_metas.where(key: meta).limit(1)
fetch_meta.blank? ? nil : fetch_meta[0].i18n_variable
end
define_method "#{meta}=" do |values|
if a = self.send(meta)
a.update_attributes(values)
else
a = self.site_metas.build(key: meta)
a.build_i18n_variable(values)
end
end
end
protected
def set_key
2012-05-07 03:25:33 +00:00
title.key = 'title' if title && (title.key.blank? rescue true)
footer.key = 'footer' if footer && (footer.key.blank? rescue true)
2012-05-08 09:27:02 +00:00
sub_menu.key = 'sub_menu' if sub_menu && (sub_menu.key.blank? rescue true)
2012-05-06 16:54:18 +00:00
end
2011-08-23 11:40:49 +00:00
end