From a126935d2c66d8cd57f63f754b04ee549d852b4c Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Wed, 21 Mar 2012 18:30:12 +0800 Subject: [PATCH] Clean the parsers and introduce the metas --- app/models/meta.rb | 11 +++ app/models/meta/meta.rb | 11 +++ app/models/meta/page_meta.rb | 5 ++ app/models/meta/site_meta.rb | 5 ++ app/models/page.rb | 14 ++- app/models/site.rb | 2 + lib/parsers/parser_back_end.rb | 111 ++++++++++------------- lib/parsers/parser_front_end.rb | 154 ++++++++++++-------------------- lib/parsers/parser_layout.rb | 25 +++--- 9 files changed, 165 insertions(+), 173 deletions(-) create mode 100644 app/models/meta.rb create mode 100644 app/models/meta/meta.rb create mode 100644 app/models/meta/page_meta.rb create mode 100644 app/models/meta/site_meta.rb diff --git a/app/models/meta.rb b/app/models/meta.rb new file mode 100644 index 00000000..411a4f0c --- /dev/null +++ b/app/models/meta.rb @@ -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 diff --git a/app/models/meta/meta.rb b/app/models/meta/meta.rb new file mode 100644 index 00000000..411a4f0c --- /dev/null +++ b/app/models/meta/meta.rb @@ -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 diff --git a/app/models/meta/page_meta.rb b/app/models/meta/page_meta.rb new file mode 100644 index 00000000..9b3ded99 --- /dev/null +++ b/app/models/meta/page_meta.rb @@ -0,0 +1,5 @@ +class PageMeta < Meta + + belongs_to :page + +end diff --git a/app/models/meta/site_meta.rb b/app/models/meta/site_meta.rb new file mode 100644 index 00000000..9a17c4fa --- /dev/null +++ b/app/models/meta/site_meta.rb @@ -0,0 +1,5 @@ +class SiteMeta < Meta + + belongs_to :site + +end diff --git a/app/models/page.rb b/app/models/page.rb index ceead525..b9e06143 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -6,17 +6,23 @@ class Page < Item belongs_to :design 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_contexts, :autosave => true, :dependent => :destroy + has_many :page_metas, :autosave => true, :dependent => :destroy 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 def is_home? self.parent ? false : true end + + def title + @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil + end protected @@ -27,4 +33,10 @@ class Page < Item end end + def set_key + if title.new_record? + title.key = 'title' + end + end + end diff --git a/app/models/site.rb b/app/models/site.rb index 065a6f79..8f367c0a 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -12,6 +12,8 @@ class Site field :school field :department + + has_many :site_metas, :autosave => true, :dependent => :destroy def generate_keys private_key = OpenSSL::PKey::RSA.generate(2048) diff --git a/lib/parsers/parser_back_end.rb b/lib/parsers/parser_back_end.rb index 37c22ed1..787f579b 100644 --- a/lib/parsers/parser_back_end.rb +++ b/lib/parsers/parser_back_end.rb @@ -13,63 +13,6 @@ module ParserBackEnd def parser_context_edit(page, attributes = {}) 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 - "#{lang}" - 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 << " " 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 = "' - end - end - c.define_tag 'stylesheets' do |tag| - res = '' - res << "" if page.design.reset_css - res << "" if page.design.default_css - theme = page.design.themes.detect{ |d| d.id == page.theme_id } - res << "" if theme - res - end - c.define_tag 'javascripts' do |tag| - res = '' - res << "" - res << "" - res << "" - res << "" - page.design.javascripts.each do |js| - res << "" - end - res - end c.define_tag 'ad_banner' do |tag| res = '' ad_banner = AdBanner.find(tag.attr["id"]) rescue nil @@ -89,13 +32,6 @@ module ParserBackEnd 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 } - ret = '' - ret << "
" - ret << tag.expand - ret << '
' - end c.define_tag 'content' do |tag| ret = '' if (tag.attributes["main"] == "true" && !page.module_app.nil?) @@ -124,6 +60,39 @@ module ParserBackEnd end 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 = "' + end + end + c.define_tag 'javascripts' do |tag| + res = '' + res << "" + res << "" + res << "" + res << "" + page.design.javascripts.each do |js| + res << "" + 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 + "#{lang}" + end + }.join(' | ') end c.define_tag 'link' do |tag| item = Item.first(:conditions => { :full_name => tag.attr['name'] }) @@ -139,6 +108,20 @@ module ParserBackEnd menu = page.design.layout.menu menu_level(home, 0, menu, true) end + c.define_tag 'meta' do |tag| + "" + end + c.define_tag 'stylesheets' do |tag| + res = '' + res << "" if page.design.reset_css + res << "" if page.design.default_css + theme = page.design.themes.detect{ |d| d.id == page.theme_id } + res << "" if theme + res + end + c.define_tag 'title' do |tag| + "#{page.title ? page.title : page.i18n_variable[I18n.locale]}" + end end end diff --git a/lib/parsers/parser_front_end.rb b/lib/parsers/parser_front_end.rb index 99353cc1..4b5a523f 100644 --- a/lib/parsers/parser_front_end.rb +++ b/lib/parsers/parser_front_end.rb @@ -4,88 +4,6 @@ module ParserFrontEnd def parser_context(page, attributes = {}, id = nil) 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 - "#{lang}" - 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 << "' - when 'show' - var = I18nVariable.find(tag.attr['id']) - res = '' - 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 << " " if asset.data.file.content_type.eql?('text/css') - end - res - end - c.define_tag 'stylesheets' do |tag| - res = '' - res << "" - res << "" - res << "" - res << "" if page.design.reset_css - res << " " if page.design.default_css - theme = page.design.themes.detect{ |d| d.id == page.theme_id } - res << "" if theme - res - end - c.define_tag 'javascripts' do |tag| - res = '' - res << "" - res << "" - res << "" - res << "" - res << "" - res << "" - res << "" - res << "" - res << "" - page.design.javascripts.each do |js| - # res << "" - end - res - end c.define_tag 'ad_banner' do |tag| res = '' ad_banner = AdBanner.find(tag.attr["id"]) rescue nil @@ -106,22 +24,6 @@ module ParserFrontEnd 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 = "' - 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| ret = '' if (tag.attributes["main"] == "true" && !page.module_app.nil?) @@ -149,6 +51,44 @@ module ParserFrontEnd end ret 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 = "' + end + end + c.define_tag 'javascripts' do |tag| + res = '' + res << "" + res << "" + res << "" + res << "" + res << "" + res << "" + res << "" + res << "" + res << "" + page.design.javascripts.each do |js| + # res << "" + 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 + "#{lang}" + end + }.join(' | ') + end c.define_tag 'link' do |tag| item = Item.first(:conditions => { :full_name => tag.attr['name'] }) ret = '' @@ -161,6 +101,24 @@ module ParserFrontEnd menu = page.design.layout.menu menu_level(home, 0, menu) end + c.define_tag 'meta' do |tag| + res = '' + #res << "" + end + c.define_tag 'stylesheets' do |tag| + res = '' + res << "" + res << "" + res << "" + res << "" if page.design.reset_css + res << " " if page.design.default_css + theme = page.design.themes.detect{ |d| d.id == page.theme_id } + res << "" if theme + res + end + c.define_tag 'title' do |tag| + "#{page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale]}" + end end end diff --git a/lib/parsers/parser_layout.rb b/lib/parsers/parser_layout.rb index 4a0232c6..063078df 100644 --- a/lib/parsers/parser_layout.rb +++ b/lib/parsers/parser_layout.rb @@ -10,12 +10,17 @@ module ParserLayout end def parser_layout_contents(layout) - Radius::Context.new do |c| - c.define_tag 'javascripts' do |tag| - end - c.define_tag 'stylesheets' do |tag| - end + Radius::Context.new do |c| c.define_tag 'ad_banner' do |tag| + end + c.define_tag 'content' do |tag| + layout.layout_parts.new(:name => tag.attr['name']) + end + c.define_tag 'image' do |tag| + 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 + end + c.define_tag 'javascripts' do |tag| end c.define_tag 'menu' do |tag| layout.build_menu(:levels => 0, :values => {}) unless layout.menu @@ -24,13 +29,13 @@ module ParserLayout 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| - layout.layout_parts.new(:name => tag.attr['name']) + c.define_tag 'meta' do |tag| end - c.define_tag 'image' do |tag| - 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 + c.define_tag 'stylesheets' do |tag| end + c.define_tag 'title' do |tag| + end + end end