diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 1d79bc41..eacf9053 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -18,13 +18,14 @@ class Admin::PagesController < ApplicationController def edit @page = Page.find(params[:id]) + @page.content = parse_content(@page.content, {:locale => 'show'}) @i18n_variable = @page.i18n_variable session[:last_page] = get_go_back || admin_items_url end def create @page = Page.new(params[:page]) - + @page.content = parse_content(@page.content, {:locale => 'create'}) if @page.save flash[:notice] = t('admin.create_success_page') redirect_to admin_items_url( :parent_id => @page.parent_id ) @@ -35,7 +36,10 @@ class Admin::PagesController < ApplicationController def update @page = Page.find(params[:id]) + parse_content(@page.content, {:locale => 'destroy'}) if @page.update_attributes(params[:page]) + @page.content = parse_content(@page.content, {:locale => 'create'}) + @page.save! flash[:notice] = t('admin.update_success_page') redirect_to admin_items_url( :parent_id => @page.parent_id ) else diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 393adec4..9a32345f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,8 @@ class ApplicationController < ActionController::Base protect_from_forgery + include Parser + helper :all before_filter :set_locale, :set_site @@ -36,41 +38,10 @@ class ApplicationController < ActionController::Base redirect_to root_url unless current_user.admin? end - # Parse and render the pages with Radius + # Render the page def render_page if @page - @layout = @page.layout - @page_content = @page.content - @layout_content = (@page.layout)? @layout.content : "" - - - context = 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 - 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 - end - - parser = Radius::Parser.new(context, :tag_prefix => 'r') - - render :text => parser.parse(@layout_content) + render :text => parse_page(@page) else render :text => '404 Not Found' end @@ -97,10 +68,10 @@ class ApplicationController < ActionController::Base # Set the site variables def set_site # set site if exist or create site - site = Site.first || Site.create({:valid_locales => [], :in_use_locales => []}) - session[:site] = site.id - @site_in_use_locales = site.in_use_locales - @site_valid_locales = site.valid_locales + @site = Site.first || Site.create({:valid_locales => [], :in_use_locales => []}) + session[:site] = @site.id + @site_in_use_locales = @site.in_use_locales + @site_valid_locales = @site.valid_locales end end diff --git a/app/models/parser.rb b/app/models/parser.rb new file mode 100644 index 00000000..ca834ac8 --- /dev/null +++ b/app/models/parser.rb @@ -0,0 +1,73 @@ +module Parser + + def parser_context(page_content, 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 + 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 + else + tag.attr[I18n.locale.to_s] rescue nil + end + end + end + end + + def parse_content(page_content, attributes = {}) + context = parser_context(page_content, attributes) + parser = Radius::Parser.new(context, :tag_prefix => 'r') + parser.parse(page_content) + end + + def parse_page(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 + +end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index e024a9e0..012b6478 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -1,42 +1,62 @@ -# encoding: utf-8 +# encoding: utf-8 namespace :dev do task :build => :environment do - [Item, Layout, Site, Snippet, User, UserAttributeModel].each { |m| m.delete_all } + + puts 'Empty the MongoDB database, exclude System stuff' + Mongoid.master.collections.select do |collection| + include = collection.name !~ /system/ + puts 'Dropping ' + collection.name if include + include + end.each(&:drop) + + Site.create!( :valid_locales => [ 'en', 'zh_tw' ], :in_use_locales => [ 'zh_tw', 'en' ] ) - User.create!( :email => 'chris@rulingcom.com', - :password => 'password', - :password_confirmation => 'password', - :admin => true, - :active_attributes => ["teacher"], - :user_attributes => [ - {:key => 'teacher'}]) + user = User.new( :email => 'chris@rulingcom.com', :password => 'password', :password_confirmation => 'password', :admin => true, :active_roles => ['teacher']) + user.user_roles.build(:key => 'teacher', :discipline_en => 'Database', :discipline_zh_tw => '數據庫' ) + user.save! - Home.create!( :type => "Home", - :content => "This is the home page\r\n{% t bob %}", - :full_name => "home", - :is_published => true,"layout_id" : ObjectId("4d23dadf5b0bad0b1100000e"), "layout_name" : "root", "name" : "home", "parent_id" : null, "parent_name" : null, "position" : 1, "title_en" : "Homepage", "title_zh_tw" : "首頁" - - - Layout.create!( :name => 'root', :description => 'root', :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.layout.zh_tw").read, - :content_en => File.open("#{RAILS_ROOT}/lib/template/root.layout.en").read) + I18nVariable.create!( :document_class => 'language', :key => 'en', :en => 'English', :zh_tw => '英文' ) + I18nVariable.create!( :document_class => 'language', :key => 'zh_tw', :en => 'Chinese', :zh_tw => '中文' ) + var_1 = I18nVariable.create!( :document_class => 'UserRoleModel', :key => 'teacher', :en => 'Teacher', :zh_tw => '老師' ) + var_2 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'discipline', :en => 'Discipline', :zh_tw => '學科', :parent_id => var_1.id ) + var_3 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'department', :en => 'Department', :zh_tw => '學系', :parent_id => var_1.id ) + var_4 = I18nVariable.create!( :document_class => 'UserRoleModel', :key => 'student', :en => 'Student', :zh_tw => '學生' ) + var_5 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'department', :en => 'Department', :zh_tw => '學系', :parent_id => var_4.id ) + var_6 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'major', :en => 'Major', :zh_tw => '主修', :parent_id => var_4.id ) + var_7 = I18nVariable.create!( :document_class => 'UserInfoModel', :key => 'profile', :en => 'Profile', :zh_tw => '個人檔案' ) + var_8 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'family_name', :en => 'Family name', :zh_tw => '姓氏', :parent_id => var_7.id ) + var_9 = I18nVariable.create!( :document_class => 'AttributeModel', :key => 'first_name', :en => 'First name', :zh_tw => '名字', :parent_id => var_7.id ) + var_10 = I18nVariable.create!( :document_class => 'Home', :key => 'home', :en => 'Homepage', :zh_tw => '首頁') + 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' ) - Page.create!( :name => "root", :title => I18n.t(:homepage), :layout_name => "root", :parent_name => nil, - :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/root.page.zh_tw").read, - :content_en => File.open("#{RAILS_ROOT}/lib/template/root.page.en").read ) - - ["about"].each do |page_name| - Page.create!( :name => page_name, :title => page_name, :layout_name => "root", :parent_name => "root", - :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.page.zh_tw").read, - :content_en => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.page.en").read ) - end - - ["nav", "footer", "locale"].each do |page_name| - Snippet.create!( :name => page_name, :parent_name => "root", - :content_zh_tw => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.snippet.zh_tw").read, - :content_en => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.snippet.en").read ) - end + urm_1 = UserRoleModel.new( :key => 'teacher', :i18n_variable_id => var_1.id ) + urm_1.attribute_models.build( :key => 'discipline', :locale => true, :i18n_variable_id => var_2.id, :markup => 'text_field', :list_options => [] ) + urm_1.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_3.id, :markup => 'text_field', :list_options => [] ) + urm_1.save! + urm_2 = UserRoleModel.new( :key => 'student', :i18n_variable_id => var_4.id ) + urm_2.attribute_models.build( :key => 'department', :locale => true, :i18n_variable_id => var_5.id, :markup => 'text_field', :list_options => [] ) + urm_2.attribute_models.build( :key => 'major', :locale => true, :i18n_variable_id => var_6.id, :markup => 'text_field', :list_options => [] ) + urm_2.save! + uim_1 = UserInfoModel.new( :key => 'profile', :i18n_variable_id => var_7.id ) + uim_1.attribute_models.build( :key => 'family_name', :locale => true, :i18n_variable_id => var_8.id, :markup => 'text_field', :list_options => [] ) + uim_1.attribute_models.build( :key => 'first_name', :locale => true, :i18n_variable_id => var_9.id, :markup => 'text_field', :list_options => [] ) + uim_1.save! + + layout = Layout.create!( :name => 'root', :description => 'root', :content => File.open("#{RAILS_ROOT}/lib/template/root.layout").read ) + + home = Home.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 ) + + 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 ) + + Link.create!( :i18n_variable_id => var_12.id, :name => 'google', :is_published => true, :parent_id => home.id, :url => 'www.google.com' ) + + ["nav", "footer"].each do |page_name| + Snippet.create!( :name => page_name, :parent_id => home.id, :content => File.open("#{RAILS_ROOT}/lib/template/#{page_name}.snippet").read ) + end + end end diff --git a/lib/template/about.page b/lib/template/about.page new file mode 100644 index 00000000..e18db735 --- /dev/null +++ b/lib/template/about.page @@ -0,0 +1 @@ +This is about diff --git a/lib/template/footer.snippet b/lib/template/footer.snippet new file mode 100644 index 00000000..8db1fffd --- /dev/null +++ b/lib/template/footer.snippet @@ -0,0 +1 @@ +

Footer

diff --git a/lib/template/nav.snippet b/lib/template/nav.snippet new file mode 100644 index 00000000..792d4151 --- /dev/null +++ b/lib/template/nav.snippet @@ -0,0 +1,5 @@ + diff --git a/lib/template/root.home b/lib/template/root.home new file mode 100644 index 00000000..d0976eda --- /dev/null +++ b/lib/template/root.home @@ -0,0 +1 @@ + diff --git a/lib/template/root.layout b/lib/template/root.layout new file mode 100644 index 00000000..1c49152b --- /dev/null +++ b/lib/template/root.layout @@ -0,0 +1,25 @@ + + + + + + RulingSite + + + + + +
+ +
+ + + + +