Create page.

This commit is contained in:
chris2tof 2011-06-01 10:24:14 +08:00
parent 2522cde2f5
commit ddc7c74436
21 changed files with 163 additions and 83 deletions

View File

@ -40,6 +40,8 @@ class Admin::PagePartsController < ApplicationController
@part = PagePart.find(params[:id])
if @part.update_attributes(params[:page_part])
flash.now[:notice] = t('admin.update_success_content')
@part.build_content(@site_valid_locales)
@part.save
respond_to do |format|
format.html {
redirect_to admin_page_url( @part.page )

View File

@ -31,12 +31,20 @@ class Admin::PagesController < ApplicationController
def create
@item = Page.new(params[:page])
@item.content = parse_content(@item.content, {:locale => 'create'})
if @item.save
flash[:notice] = t('admin.create_success_page')
redirect_to admin_items_url( :parent_id => @item.parent_id )
@item.create_parts
@item.save
flash.now[:notice] = t('admin.create_success_page')
respond_to do |format|
format.html {
redirect_to admin_item_url(@item)
}
format.js {}
end
else
render :action => "new"
flash.now[:error] = t('admin.create_error_page')
@i18n_variable = @item.i18n_variable
render :action => "new"
end
end

View File

@ -0,0 +1,23 @@
module Admin::ItemHelper
def render_node_and_children(node)
ret = ''
if node
case node._type
when 'Page'
dest = admin_page_path(node)
when 'Link'
dest = admin_link_path(node)
end
ret << "<li>"
ret << (link_to node.name, dest)
ret << ' | ' << (link_to t('admin.new_page'), new_admin_page_path(:parent_id => node.id)) if node._type.eql?('Page')
ret << ' | ' << (link_to t('admin.new_link'), new_admin_link_path(:parent_id => node.id)) if node._type.eql?('Page')
ret << ' | ' << (link_to t(:delete), delete_admin_link_path(node, :authenticity_token => form_authenticity_token), :confirm => t('sure?'), :class => 'delete') if node._type.eql?('Link')
ret << render_children(node)
ret << "</li>"
end
ret.html_safe
end
end

View File

@ -49,25 +49,6 @@ module ApplicationHelper
end
end
def render_node_and_children(node)
ret = ''
if node
case node._type
when 'Page'
dest = admin_page_path(node)
when 'Link'
dest = admin_link_path(node)
end
ret << "<li>"
ret << (link_to node.name, dest)
ret << ' | ' << (link_to 'new_link', new_admin_link_path(:parent_id => node.id)) if node._type.eql?('Page')
ret << ' | ' << (link_to t(:delete), delete_admin_link_path(node, :authenticity_token => form_authenticity_token), :confirm => t('sure?'), :class => 'delete') if node._type.eql?('Link')
ret << render_children(node)
ret << "</li>"
end
ret.html_safe
end
def render_children(parent)
if children = parent.children
ret = ''

View File

@ -7,4 +7,8 @@ class I18nVariable
field :document_class, :type => String
field :parent_id, :type => BSON::ObjectId, :index => true
def method_missing(field)
self[field]
end
end

View File

@ -11,13 +11,24 @@ class Page < Item
referenced_in :layout
has_many :page_parts
def create_parts
page_layout = self.get_layout
page_layout.layout_parts.each do |p|
self.page_parts.create( :name => p.name, :i18n_variable_id => I18nVariable.create.id, :kind => 'text' )
end
end
def get_layout
Layout.find(layout_id)
end
protected
def setup_default_value
super
if self.layout_id
self.layout_name = Layout.find(layout_id).name
self.layout_name = get_layout.name
end
end

View File

@ -25,5 +25,18 @@ class PagePart
self.i18n_variable_id = var.id
end
end
# Build the content from the i18n_variable
def build_content(locales)
res = ''
res << "<r:multi_lang i18n_id='#{i18n_variable.id}'>"
locales.each do |locale|
res << "<r:lang name='#{locale.to_s}'>"
res << i18n_variable[locale.to_s]
res << "</r:lang>"
end
res << "</r:multi_lang>"
self.content = res
end
end

View File

@ -1,3 +1,4 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_id %>
<p>
@ -18,11 +19,6 @@
<%= f.select :layout_id, Layout.all.map{ |l| [l.description, l.id] } %>
</p>
<p>
<%= f.label "content", t('admin.content') %>
<%= f.text_area "content", :size => '100x30' %>
</p>
<p>
<%= f.label :is_published, "#{t('admin.is_published')} ?" %>
<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No

View File

@ -0,0 +1,13 @@
<h1><%= t('admin.new_page') %></h1>
<%= flash_messages %>
<%= form_for @item, :url => admin_pages_path, :html => { :class => 'form' } do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit t('create') %> <%= link_back %>
</p>
<% end %>

View File

@ -0,0 +1,2 @@
$('#sidebar').html("<%= escape_javascript(render(:partial => 'admin/items/site_map_left_bar')) %>");
$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>");

View File

@ -1,11 +1,5 @@
<h1><%= t('admin.new_page') %></h1>
<% content_for :sidebar do %>
<div id='sidebar'><%= render 'admin/items/site_map_left_bar' %></div>
<% end -%>
<%= form_for :page, :url => admin_pages_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit t('create') %> <%= link_back %>
</p>
<% end %>
<%= render 'new' %>

View File

@ -0,0 +1 @@
$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/new')) %>");

View File

@ -9,8 +9,7 @@
<%= stylesheet_link_tag "easyprint", :media => "print" %>
<%= javascript_include_tag :ckeditor %>
<%= javascript_include_tag "jquery", "jquery-ui", "rails", "easy", :cache => 'all' %>
<%= javascript_include_tag "application", :cache => 'all' %>
<%= javascript_include_tag "jquery", "jquery-ui", "rails", "easy", "application", :cache => 'all' %>
<%= yield :page_specific_javascript %>
<!--[if IE]>
<%= stylesheet_link_tag "ie", :media => "screen, projection" %>
@ -29,7 +28,12 @@
<li><%= link_to t('admin.user_role'), admin_user_role_models_path %></li>
<li><%= link_to t('admin.translation'), admin_translations_path %></li>
</ul>
<%= render 'layouts/lang_menu' %>
<ul class="hmenu">
<li>
<%= render 'layouts/lang_menu' %>
</li>
<%= render 'devise/menu/login_items' %>
</ul>
</div>
<div id="content" class="content">

View File

@ -29,6 +29,8 @@ en:
asset: Asset
attributes: Attributes
class: Class
create_error_link: Error when creating link.
create_error_page: Error when creating page.
create_success_home: Homepage was successfully created.
create_success_layout: Layout was successfully created.
create_success_link: Link was successfully created.
@ -89,6 +91,7 @@ en:
translation: Translation
type: Type
update_error_link: Error when updating link.
update_error_page: Error when updating page.
update_success_content: Content was successfully updated.
update_success_home: Homepage was successfully updated.
update_success_layout: Layout was successfully updated.

View File

@ -27,6 +27,8 @@ zh_tw:
attributes: 屬性
class: 階級
content: 內容
create_error_link: 創建連接時出錯。
create_error_page: 創建頁面時出錯。
create_success_home: 首頁已成功創建。
create_success_layout: 樣板已成功創建。
create_success_link: 連結已成功創建。
@ -86,6 +88,7 @@ zh_tw:
translation: 翻譯
type: 類型
update_error_link: 更新鏈接時出現錯誤。
update_error_page: 更新頁面時出現錯誤。
update_success_content: 內容已成功更新。
update_success_home: 首頁已成功更新。
update_success_layout: 樣板已成功更新。

View File

@ -47,10 +47,14 @@ module Parser
when 'destroy'
var = I18nVariable.find(tag.attr['id'])
var.destroy
else
tag.attr[I18n.locale.to_s] rescue nil
end
end
c.define_tag 'multi_lang' do |tag|
tag.expand
end
c.define_tag 'multi_lang:lang' do |tag|
tag.expand if tag.attr['name'].eql?(I18n.locale.to_s)
end
c.define_tag 'css' do |tag|
assets = Asset.any_in(:filename => tag.attr['name'].split(',').map(&:strip))
res = ''
@ -123,36 +127,11 @@ module Parser
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
else
tag.attr[I18n.locale.to_s] rescue nil
end
c.define_tag 'multi_lang' do |tag|
tag.expand
end
c.define_tag 'multi_lang:lang' do |tag|
tag.expand if tag.attr['name'].eql?(I18n.locale.to_s)
end
c.define_tag 'css' do |tag|
assets = Asset.any_in(:filename => tag.attr['name'].split(',').map(&:strip))

View File

@ -51,15 +51,15 @@ namespace :dev do
layout = Layout.create!( :name => 'root', :description => 'root', :content => File.open("#{RAILS_ROOT}/lib/template/root.layout").read )
layout.layout_parts.create!( :name => 'header', :content => "<r:language_bar /><r:snippet name='nav' />" )
layout.layout_parts.create!( :name => 'header', :content => File.open("#{RAILS_ROOT}/lib/template/nav.snippet").read )
layout.layout_parts.create!( :name => 'main_content' )
layout.layout_parts.create!( :name => 'footer', :content => "<r:snippet name='footer' />" )
layout.layout_parts.create!( :name => 'footer', :content => File.open("#{RAILS_ROOT}/lib/template/footer.snippet").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 => "<r:locale en='This is the homepage' zh_tw='這是首頁' />", :kind => 'text', :i18n_variable_id => var_13.id )
home.page_parts.create!( :name => 'main_content', :content => File.open("#{RAILS_ROOT}/lib/template/home.page").read, :kind => 'text', :i18n_variable_id => var_13.id )
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 => "<r:locale en='This is about' zh_tw='這是關於' />", :kind => 'text', :i18n_variable_id => var_14.id )
about.page_parts.create!( :name => 'main_content', :content => File.open("#{RAILS_ROOT}/lib/template/about.page").read, :kind => 'text', :i18n_variable_id => var_14.id )
Link.create!( :i18n_variable_id => var_12.id, :name => 'google', :is_published => true, :parent_id => home.id, :url => 'www.google.com' )

View File

@ -1 +1,8 @@
This is about
<r:multi_lang>
<r:lang name='en'>
<p>This is about</p>
</r:lang>
<r:lang name='zh_tw'>
<p>這是關於</p>
</r:lang>
</r:multi_lang>

View File

@ -1 +1,8 @@
<p>Footer</p>
<r:multi_lang>
<r:lang name='en'>
<p>Footer</p>
</r:lang>
<r:lang name='zh_tw'>
<p>頁腳</p>
</r:lang>
</r:multi_lang>

View File

@ -1 +1,8 @@
<r:locale en='This is the homepage' zh_tw='這是首頁' />
<r:multi_lang>
<r:lang name='en'>
<p>This is the homepage</p>
</r:lang>
<r:lang name='zh_tw'>
<p>t這是首頁</p>
</r:lang>
</r:multi_lang>

View File

@ -1,5 +1,27 @@
<ul>
<li><a href='/'><r:locale en='Home' zh_tw='首頁' /></a></li>
<li><a href='/about'><r:locale en='About' zh_tw='關於我們' /></a></li>
<li>
<a href='/'>
<r:multi_lang>
<r:lang name='en'>
Home
</r:lang>
<r:lang name='zh_tw'>
首頁
</r:lang>
</r:multi_lang>
</a>
</li>
<li>
<a href='/about'>
<r:multi_lang>
<r:lang name='en'>
About
</r:lang>
<r:lang name='zh_tw'>
關於我們
</r:lang>
</r:multi_lang>
</a>
</li>
<li><a href='/google'>Google</a></li>
</ul>