Add site_info
This commit is contained in:
1 parent
48f8fbde7f
commit
1fb6c20b86
13 files changed
+147
-32
No files matched your search
@@ -1,15 +0,0 @@
|
||||
class Meta
|
||||
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
|
||||
field :key
|
||||
field :value, :default => nil
|
||||
|
||||
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
|
||||
def get_name_content
|
||||
[self.key, self.value ? self.value : self.i18n_variable[I18n.locale]]
|
||||
end
|
||||
|
||||
end
|
||||
@@ -7,5 +7,13 @@ class Meta
|
||||
field :value, :default => nil
|
||||
|
||||
has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
|
||||
def get_name_content
|
||||
[self.key, get_content]
|
||||
end
|
||||
|
||||
def get_content
|
||||
self.value ? self.value : self.i18n_variable[I18n.locale]
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,5 +1,7 @@
|
||||
class Site
|
||||
|
||||
METAS = ['description', 'keywords']
|
||||
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
|
||||
@@ -13,8 +15,11 @@ class Site
|
||||
field :school
|
||||
field :department
|
||||
|
||||
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
has_many :site_metas, :autosave => true, :dependent => :destroy
|
||||
|
||||
before_save :set_key
|
||||
|
||||
def generate_keys
|
||||
private_key = OpenSSL::PKey::RSA.generate(2048)
|
||||
self.public_key = private_key.public_key.to_s
|
||||
@@ -30,5 +35,30 @@ class Site
|
||||
res = res.split('rails_3_1').pop.gsub('(', '').gsub(')','').strip rescue nil
|
||||
res.eql?('local out of date') ? false : true
|
||||
end
|
||||
|
||||
def title
|
||||
@title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
|
||||
end
|
||||
|
||||
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
|
||||
title.key = 'title' if title
|
||||
end
|
||||
|
||||
end
|
||||
Reference in new issue
Block a user