Clean the parsers and introduce the metas

This commit is contained in:
Christophe Vilayphiou 2012-03-21 18:30:12 +08:00
parent 472744fc87
commit a126935d2c
9 changed files with 165 additions and 173 deletions

11
app/models/meta.rb Normal file
View File

@ -0,0 +1,11 @@
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
end

11
app/models/meta/meta.rb Normal file
View File

@ -0,0 +1,11 @@
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
end

View File

@ -0,0 +1,5 @@
class PageMeta < Meta
belongs_to :page
end

View File

@ -0,0 +1,5 @@
class SiteMeta < Meta
belongs_to :site
end

View File

@ -6,11 +6,13 @@ class Page < Item
belongs_to :design belongs_to :design
belongs_to :module_app belongs_to :module_app
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
has_many :page_parts, :autosave => true, :dependent => :destroy has_many :page_parts, :autosave => true, :dependent => :destroy
has_many :page_contexts, :autosave => true, :dependent => :destroy has_many :page_contexts, :autosave => true, :dependent => :destroy
has_many :page_metas, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :page_parts, :allow_destroy => true accepts_nested_attributes_for :page_parts, :allow_destroy => true
before_save :create_parts before_save :create_parts, :set_key
# embeds_many :custom_images, :class_name => 'Image', as: :design_image # embeds_many :custom_images, :class_name => 'Image', as: :design_image
@ -18,6 +20,10 @@ class Page < Item
self.parent ? false : true self.parent ? false : true
end end
def title
@title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
end
protected protected
def create_parts def create_parts
@ -27,4 +33,10 @@ class Page < Item
end end
end end
def set_key
if title.new_record?
title.key = 'title'
end
end
end end

View File

@ -13,6 +13,8 @@ class Site
field :school field :school
field :department field :department
has_many :site_metas, :autosave => true, :dependent => :destroy
def generate_keys def generate_keys
private_key = OpenSSL::PKey::RSA.generate(2048) private_key = OpenSSL::PKey::RSA.generate(2048)
self.public_key = private_key.public_key.to_s self.public_key = private_key.public_key.to_s

View File

@ -13,63 +13,6 @@ module ParserBackEnd
def parser_context_edit(page, attributes = {}) def parser_context_edit(page, attributes = {})
Radius::Context.new do |c| Radius::Context.new do |c|
c.define_tag 'snippet' do |tag|
snippet = Snippet.first(:conditions => {:name => tag.attr['name']})
if snippet
snippet.content
else
t('nothing')
end
end
c.define_tag 'language_bar' do
@site.in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
c.define_tag 'css' do |tag|
assets = Asset.any_in(:filename => tag.attr['name'].split(',').map(&:strip))
res = ''
assets.each do |asset|
res << "<link href='#{asset.data.file.url}' rel='stylesheet' type='text/css' /> " if asset.data.file.content_type.eql?('text/css')
end
res
end
c.define_tag 'image' do |tag|
# image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
# image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
image = page.design.images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
if image
res = "<img src=#{image.file.url} "
tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
res << '>'
end
end
c.define_tag 'stylesheets' do |tag|
res = ''
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.reset_css
res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.default_css
theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res
end
c.define_tag 'javascripts' do |tag|
res = ''
res << "<script type='text/javascript' src='/static/jquery.js'></script>"
res << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>"
res << "<script type='text/javascript' src='/static/kernel.js'></script>"
res << "<script type='text/javascript' src='/assets/event.js'></script>"
page.design.javascripts.each do |js|
res << "<script type='text/javascript' src='#{js.file.url}'></script>"
end
res
end
c.define_tag 'ad_banner' do |tag| c.define_tag 'ad_banner' do |tag|
res = '' res = ''
ad_banner = AdBanner.find(tag.attr["id"]) rescue nil ad_banner = AdBanner.find(tag.attr["id"]) rescue nil
@ -89,13 +32,6 @@ module ParserBackEnd
res << "</div>" res << "</div>"
end end
end end
c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
ret = ''
ret << "<div id='#{tag.attr['name']}' part_id='#{part.id}'>"
ret << tag.expand
ret << '</div>'
end
c.define_tag 'content' do |tag| c.define_tag 'content' do |tag|
ret = '' ret = ''
if (tag.attributes["main"] == "true" && !page.module_app.nil?) if (tag.attributes["main"] == "true" && !page.module_app.nil?)
@ -125,6 +61,39 @@ module ParserBackEnd
ret << '</div>' ret << '</div>'
end end
end end
c.define_tag 'image' do |tag|
# image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
# image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
image = page.design.images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
if image
res = "<img src=#{image.file.url} "
tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
res << '>'
end
end
c.define_tag 'javascripts' do |tag|
res = ''
res << "<script type='text/javascript' src='/static/jquery.js'></script>"
res << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>"
res << "<script type='text/javascript' src='/static/kernel.js'></script>"
res << "<script type='text/javascript' src='/assets/event.js'></script>"
page.design.javascripts.each do |js|
res << "<script type='text/javascript' src='#{js.file.url}'></script>"
end
res
end
c.define_tag 'language_bar' do
@site.in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
c.define_tag 'link' do |tag| c.define_tag 'link' do |tag|
item = Item.first(:conditions => { :full_name => tag.attr['name'] }) item = Item.first(:conditions => { :full_name => tag.attr['name'] })
ret = '' ret = ''
@ -139,6 +108,20 @@ module ParserBackEnd
menu = page.design.layout.menu menu = page.design.layout.menu
menu_level(home, 0, menu, true) menu_level(home, 0, menu, true)
end end
c.define_tag 'meta' do |tag|
"<meta name='bob' content=#{@site.school} />"
end
c.define_tag 'stylesheets' do |tag|
res = ''
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.reset_css
res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.default_css
theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res
end
c.define_tag 'title' do |tag|
"<title>#{page.title ? page.title : page.i18n_variable[I18n.locale]}</title>"
end
end end
end end

View File

@ -4,88 +4,6 @@ module ParserFrontEnd
def parser_context(page, attributes = {}, id = nil) def parser_context(page, attributes = {}, id = nil)
Radius::Context.new do |c| Radius::Context.new do |c|
c.define_tag 'snippet' do |tag|
snippet = Snippet.first(:conditions => {:name => tag.attr['name']})
if snippet
snippet.content
else
t('nothing')
end
end
c.define_tag 'language_bar' do
@site.in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
c.define_tag 'locale' do |tag|
case attributes[:locale]
when 'create'
var = I18nVariable.new(:key => (tag.attr['name'] rescue nil), :document_class => 'Text')
@site.valid_locales.each do |locale|
var[locale] = tag.attr[locale] rescue nil
end
var.save!
res = ''
res << "<r:locale id='#{var.id}' "
res << "name='#{var.key}' " if var.key
@site.valid_locales.each do |locale|
res << "#{locale}='#{var[locale]}' "
end
res << '/>'
when 'show'
var = I18nVariable.find(tag.attr['id'])
res = ''
res << "<r:locale "
res << "name='#{var.key}' " if var.key
@site.valid_locales.each do |locale|
res << "#{locale}='#{var[locale]}' "
end
res << '/>'
when 'destroy'
var = I18nVariable.find(tag.attr['id'])
var.destroy
end
end
c.define_tag 'css' do |tag|
assets = Asset.any_in(:filename => tag.attr['name'].split(',').map(&:strip))
res = ''
assets.each do |asset|
res << "<link href='#{asset.data.file.url}' rel='stylesheet' type='text/css' /> " if asset.data.file.content_type.eql?('text/css')
end
res
end
c.define_tag 'stylesheets' do |tag|
res = ''
res << "<link href='/assets/bootstrap.css.erb' rel='stylesheet' type='text/css'/>"
res << "<link href='/assets/bootstrap-orbit.css' rel='stylesheet' type='text/css'/>"
res << "<link href='/assets/style.css.erb' rel='stylesheet' type='text/css'/>"
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.reset_css
res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' /> " if page.design.default_css
theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res
end
c.define_tag 'javascripts' do |tag|
res = ''
res << "<script type='text/javascript' src='/static/jquery.js'></script>"
res << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>"
res << "<script type='text/javascript' src='/static/kernel.js'></script>"
res << "<script type='text/javascript' src='/assets/bootstrap.js'></script>"
res << "<script type='text/javascript' src='/assets/jquery.tinyscrollbar.min.js'></script>"
res << "<script type='text/javascript' src='/assets/jquery.isotope.min.js'></script>"
res << "<script type='text/javascript' src='/assets/orbit-1.0.js'></script>"
res << "<script type='text/javascript' src='/assets/orbit_bar.js'></script>"
res << "<script type='text/javascript' src='/assets/event.js'></script>"
page.design.javascripts.each do |js|
# res << "<script type='text/javascript' src='#{js.file.url}'></script>"
end
res
end
c.define_tag 'ad_banner' do |tag| c.define_tag 'ad_banner' do |tag|
res = '' res = ''
ad_banner = AdBanner.find(tag.attr["id"]) rescue nil ad_banner = AdBanner.find(tag.attr["id"]) rescue nil
@ -106,22 +24,6 @@ module ParserFrontEnd
end end
res res
end end
c.define_tag 'image' do |tag|
# image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
# image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
image = page.design.images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
if image
res = "<img src=#{image.file.url} "
tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
res << '>'
end
end
c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s } rescue nil
part.content
end
c.define_tag 'content' do |tag| c.define_tag 'content' do |tag|
ret = '' ret = ''
if (tag.attributes["main"] == "true" && !page.module_app.nil?) if (tag.attributes["main"] == "true" && !page.module_app.nil?)
@ -149,6 +51,44 @@ module ParserFrontEnd
end end
ret ret
end end
c.define_tag 'image' do |tag|
# image = page.custom_images.detect{|image| image.name.eql?(tag.attr['name']) }
# image = page.design.custom_images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
image = page.design.images.detect{|image| image.name.eql?(tag.attr['name']) } unless image
if image
res = "<img src=#{image.file.url} "
tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
res << '>'
end
end
c.define_tag 'javascripts' do |tag|
res = ''
res << "<script type='text/javascript' src='/static/jquery.js'></script>"
res << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>"
res << "<script type='text/javascript' src='/static/kernel.js'></script>"
res << "<script type='text/javascript' src='/assets/bootstrap.js'></script>"
res << "<script type='text/javascript' src='/assets/jquery.tinyscrollbar.min.js'></script>"
res << "<script type='text/javascript' src='/assets/jquery.isotope.min.js'></script>"
res << "<script type='text/javascript' src='/assets/orbit-1.0.js'></script>"
res << "<script type='text/javascript' src='/assets/orbit_bar.js'></script>"
res << "<script type='text/javascript' src='/assets/event.js'></script>"
page.design.javascripts.each do |js|
# res << "<script type='text/javascript' src='#{js.file.url}'></script>"
end
res
end
c.define_tag 'language_bar' do
@site.in_use_locales.map{ |locale|
lang = I18nVariable.first(:conditions => {:key => locale})[locale]
if I18n.locale.to_s.eql?(locale)
lang
else
"<a href='?locale=#{locale}'>#{lang}</a>"
end
}.join(' | ')
end
c.define_tag 'link' do |tag| c.define_tag 'link' do |tag|
item = Item.first(:conditions => { :full_name => tag.attr['name'] }) item = Item.first(:conditions => { :full_name => tag.attr['name'] })
ret = '' ret = ''
@ -161,6 +101,24 @@ module ParserFrontEnd
menu = page.design.layout.menu menu = page.design.layout.menu
menu_level(home, 0, menu) menu_level(home, 0, menu)
end end
c.define_tag 'meta' do |tag|
res = ''
#res << "<meta name='bob' content='#{@site.school}' />"
end
c.define_tag 'stylesheets' do |tag|
res = ''
res << "<link href='/assets/bootstrap.css.erb' rel='stylesheet' type='text/css'/>"
res << "<link href='/assets/bootstrap-orbit.css' rel='stylesheet' type='text/css'/>"
res << "<link href='/assets/style.css.erb' rel='stylesheet' type='text/css'/>"
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.reset_css
res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' /> " if page.design.default_css
theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res
end
c.define_tag 'title' do |tag|
"<title>#{page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale]}</title>"
end
end end
end end

View File

@ -11,19 +11,8 @@ module ParserLayout
def parser_layout_contents(layout) def parser_layout_contents(layout)
Radius::Context.new do |c| Radius::Context.new do |c|
c.define_tag 'javascripts' do |tag|
end
c.define_tag 'stylesheets' do |tag|
end
c.define_tag 'ad_banner' do |tag| c.define_tag 'ad_banner' do |tag|
end end
c.define_tag 'menu' do |tag|
layout.build_menu(:levels => 0, :values => {}) unless layout.menu
layout.menu.levels = i = tag.attr['level'].to_i
layout.menu.values.merge!({'home' => tag.attr['home']}) if i == 1
layout.menu.values.merge!({"id_#{i}" => tag.attr['id'], "class_#{i}" => tag.attr['class'], "li_class_#{i}" => tag.attr['li_class'], "li_incremental_#{i}" => tag.attr['li_incremental']})
tag.expand
end
c.define_tag 'content' do |tag| c.define_tag 'content' do |tag|
layout.layout_parts.new(:name => tag.attr['name']) layout.layout_parts.new(:name => tag.attr['name'])
end end
@ -31,6 +20,22 @@ module ParserLayout
image = layout.design.images.detect{ |i| i.file_identifier.eql?(parse_html_image(tag.expand)) } image = layout.design.images.detect{ |i| i.file_identifier.eql?(parse_html_image(tag.expand)) }
image.update_attributes(:name => tag.attr['name'], :html_id => tag.attr['id'], :html_class => tag.attr['class']) if image image.update_attributes(:name => tag.attr['name'], :html_id => tag.attr['id'], :html_class => tag.attr['class']) if image
end end
c.define_tag 'javascripts' do |tag|
end
c.define_tag 'menu' do |tag|
layout.build_menu(:levels => 0, :values => {}) unless layout.menu
layout.menu.levels = i = tag.attr['level'].to_i
layout.menu.values.merge!({'home' => tag.attr['home']}) if i == 1
layout.menu.values.merge!({"id_#{i}" => tag.attr['id'], "class_#{i}" => tag.attr['class'], "li_class_#{i}" => tag.attr['li_class'], "li_incremental_#{i}" => tag.attr['li_incremental']})
tag.expand
end
c.define_tag 'meta' do |tag|
end
c.define_tag 'stylesheets' do |tag|
end
c.define_tag 'title' do |tag|
end
end end
end end