From 3d97c5bfda8cccc0d555ed6ebd489d1ebead2c32 Mon Sep 17 00:00:00 2001 From: chris2tof Date: Wed, 18 May 2011 10:00:00 +0800 Subject: [PATCH] Modification of the page model --- app/helpers/application_helper.rb | 4 ++-- app/models/layout.rb | 31 ++++++++++++++------------- app/models/layout_part.rb | 11 ++++++++++ app/models/page.rb | 1 + app/models/page_part.rb | 11 ++++++++++ lib/parser.rb | 22 +++++++++++-------- lib/tasks/dev.rake | 14 +++++++++--- lib/template/{root.home => home.page} | 0 lib/template/root.layout | 13 ++++++----- 9 files changed, 71 insertions(+), 36 deletions(-) create mode 100644 app/models/layout_part.rb create mode 100644 app/models/page_part.rb rename lib/template/{root.home => home.page} (100%) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e17b54fe..62801b54 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -54,9 +54,9 @@ module ApplicationHelper if node case node._type when 'Page' - dest = admin_page_path(:id => node.id) + dest = admin_page_path(node) when 'Link' - dest = admin_link_path(:id => node.id) + dest = admin_link_path(node) end ret << "
  • " ret << (link_to node.name, dest) diff --git a/app/models/layout.rb b/app/models/layout.rb index f4802ec6..53da1f35 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -1,20 +1,21 @@ class Layout - include Mongoid::Document + include Mongoid::Document include Mongoid::Timestamps - - field :name, :index => true - field :description - field :content - - references_many :children, :class_name => "Item" - - validates_presence_of :name - validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ - validates_uniqueness_of :name - - def self.exist_one? - Layout.count > 0 - end + + field :name, :index => true + field :description + field :content + + references_many :children, :class_name => "Item" + has_many :layout_parts + + validates_presence_of :name + validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ + validates_uniqueness_of :name + + def self.exist_one? + Layout.count > 0 + end end diff --git a/app/models/layout_part.rb b/app/models/layout_part.rb new file mode 100644 index 00000000..063e1b38 --- /dev/null +++ b/app/models/layout_part.rb @@ -0,0 +1,11 @@ +class LayoutPart + + include Mongoid::Document + include Mongoid::Timestamps + + field :name + field :content + + belongs_to :layout + +end \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index 2c141841..927cb874 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -9,6 +9,7 @@ class Page < Item validates_presence_of :layout_name, :layout_id referenced_in :layout + has_many :page_parts protected diff --git a/app/models/page_part.rb b/app/models/page_part.rb new file mode 100644 index 00000000..f4ef9736 --- /dev/null +++ b/app/models/page_part.rb @@ -0,0 +1,11 @@ +class PagePart + + include Mongoid::Document + include Mongoid::Timestamps + + field :name + field :content + + belongs_to :page + +end \ No newline at end of file diff --git a/lib/parser.rb b/lib/parser.rb index 8ba2d08f..347de0eb 100644 --- a/lib/parser.rb +++ b/lib/parser.rb @@ -1,10 +1,7 @@ module Parser - def parser_context(page_content, attributes = {}) + def parser_context(page, attributes = {}) Radius::Context.new do |c| - c.define_tag 'content' do |tag| - page_content - end c.define_tag 'snippet' do |tag| snippet = Snippet.first(:conditions => {:name => tag.attr['name']}) if snippet @@ -72,6 +69,13 @@ module Parser res << '>' end end + c.define_tag 'layout_part' do |tag| + ret = '' + ret << "
    " + ret << (page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }).content rescue '' + ret << tag.expand + ret << '
    ' + end end end @@ -83,11 +87,11 @@ module Parser def parse_page(page) if page._type == 'Page' - layout_content = (page.layout)? page.layout.content : "" - context = parser_context(page.content) - parser = Radius::Parser.new(context, :tag_prefix => 'r') - parser.parse(parser.parse(layout_content)) - end + layout_content = page.layout.content + context = parser_context(page) + parser = Radius::Parser.new(context, :tag_prefix => 'r') + parser.parse(parser.parse(layout_content)) + end end def self.included(base) diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index c19fef1e..4dd451d7 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -32,6 +32,8 @@ namespace :dev do var_11 = I18nVariable.create!( :document_class => 'Page', :key => 'about', :en => 'About', :zh_tw => '關於我們' ) var_12 = I18nVariable.create!( :document_class => 'Link', :key => 'google', :en => 'Google', :zh_tw => 'Google' ) + + # TODO: modify for the new model urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id, :built_in => true ) urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [], :built_in => true ) urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [], :built_in => true ) @@ -45,11 +47,17 @@ namespace :dev do uim_1.attribute_models.build( :key => 'first_name', :locale => true, :i18n_variable_id => var_9.id, :markup => 'text_field', :list_options => [], :built_in => true ) uim_1.save! - layout = Layout.create!( :name => 'root', :description => 'root', :content => File.open("#{RAILS_ROOT}/lib/template/root.layout").read ) - home = Page.create!( :i18n_variable_id => var_10.id, :layout_id => layout.id, :name => 'home', :is_published => true, :content => File.open("#{RAILS_ROOT}/lib/template/root.home").read ) + layout = Layout.create!( :name => 'root', :description => 'root', :content => File.open("#{RAILS_ROOT}/lib/template/root.layout").read ) + layout.layout_parts.create!( :name => 'header', :content => "" ) + layout.layout_parts.create!( :name => 'main_content' ) + layout.layout_parts.create!( :name => 'footer', :content => "" ) - Page.create!( :i18n_variable_id => var_11.id, :layout_id => layout.id, :name => 'about', :is_published => true, :parent_id => home.id, :content => File.open("#{RAILS_ROOT}/lib/template/about.page").read ) + home = Page.create!( :i18n_variable_id => var_10.id, :layout_id => layout.id, :name => 'home', :is_published => true ) + home.page_parts.create!( :name => 'main_content', :content => "" ) + + about = Page.create!( :i18n_variable_id => var_11.id, :layout_id => layout.id, :name => 'about', :is_published => true, :parent_id => home.id ) + about.page_parts.create!( :name => 'main_content', :content => "This is about" ) Link.create!( :i18n_variable_id => var_12.id, :name => 'google', :is_published => true, :parent_id => home.id, :url => 'www.google.com' ) diff --git a/lib/template/root.home b/lib/template/home.page similarity index 100% rename from lib/template/root.home rename to lib/template/home.page diff --git a/lib/template/root.layout b/lib/template/root.layout index 1c49152b..6646d6ca 100644 --- a/lib/template/root.layout +++ b/lib/template/root.layout @@ -8,18 +8,17 @@ - + -
    - -
    + + - +