From ddc7c74436949a8e375ff0488b6805fbd504806b Mon Sep 17 00:00:00 2001 From: chris2tof Date: Wed, 1 Jun 2011 10:24:14 +0800 Subject: [PATCH] Create page. --- .../admin/page_parts_controller.rb | 2 + app/controllers/admin/pages_controller.rb | 16 +++++-- app/helpers/admin/item_helper.rb | 23 ++++++++++ app/helpers/application_helper.rb | 19 -------- app/models/i18n_variable.rb | 4 ++ app/models/page.rb | 13 +++++- app/models/page_part.rb | 13 ++++++ app/views/admin/pages/_form.html.erb | 6 +-- app/views/admin/pages/_new.html.erb | 13 ++++++ app/views/admin/pages/create.js.erb | 2 + app/views/admin/pages/new.html.erb | 14 ++---- app/views/admin/pages/new.js.erb | 1 + app/views/layouts/content.html.erb | 10 +++-- config/locales/en.yml | 3 ++ config/locales/zh_tw.yml | 3 ++ lib/parser.rb | 43 +++++-------------- lib/tasks/dev.rake | 8 ++-- lib/template/about.page | 9 +++- lib/template/footer.snippet | 9 +++- lib/template/home.page | 9 +++- lib/template/nav.snippet | 26 ++++++++++- 21 files changed, 163 insertions(+), 83 deletions(-) create mode 100644 app/helpers/admin/item_helper.rb create mode 100644 app/views/admin/pages/_new.html.erb create mode 100644 app/views/admin/pages/create.js.erb create mode 100644 app/views/admin/pages/new.js.erb diff --git a/app/controllers/admin/page_parts_controller.rb b/app/controllers/admin/page_parts_controller.rb index dc062448f..cde4a4254 100644 --- a/app/controllers/admin/page_parts_controller.rb +++ b/app/controllers/admin/page_parts_controller.rb @@ -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 ) diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index 87eedbeac..811ed6d4b 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -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 diff --git a/app/helpers/admin/item_helper.rb b/app/helpers/admin/item_helper.rb new file mode 100644 index 000000000..816e6a587 --- /dev/null +++ b/app/helpers/admin/item_helper.rb @@ -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 << "
  • " + 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 << "
  • " + end + ret.html_safe + end + +end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6c81e647c..3514b5159 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 << "
  • " - 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 << "
  • " - end - ret.html_safe - end - def render_children(parent) if children = parent.children ret = '' diff --git a/app/models/i18n_variable.rb b/app/models/i18n_variable.rb index b59588e73..e68353479 100644 --- a/app/models/i18n_variable.rb +++ b/app/models/i18n_variable.rb @@ -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 diff --git a/app/models/page.rb b/app/models/page.rb index 927cb8749..fe85ba9dd 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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 diff --git a/app/models/page_part.rb b/app/models/page_part.rb index e48609901..39ce1b80e 100644 --- a/app/models/page_part.rb +++ b/app/models/page_part.rb @@ -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 << "" + locales.each do |locale| + res << "" + res << i18n_variable[locale.to_s] + res << "" + end + res << "" + self.content = res + end end \ No newline at end of file diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb index f6f8c567c..89c56e696 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -1,3 +1,4 @@ +<%= f.error_messages %> <%= f.hidden_field :parent_id %>

    @@ -18,11 +19,6 @@ <%= f.select :layout_id, Layout.all.map{ |l| [l.description, l.id] } %>

    -

    -<%= f.label "content", t('admin.content') %> -<%= f.text_area "content", :size => '100x30' %> -

    -

    <%= f.label :is_published, "#{t('admin.is_published')} ?" %> <%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No diff --git a/app/views/admin/pages/_new.html.erb b/app/views/admin/pages/_new.html.erb new file mode 100644 index 000000000..a2991bece --- /dev/null +++ b/app/views/admin/pages/_new.html.erb @@ -0,0 +1,13 @@ +

    <%= t('admin.new_page') %>

    + +<%= flash_messages %> + +<%= form_for @item, :url => admin_pages_path, :html => { :class => 'form' } do |f| %> + + <%= render :partial => "form", :locals => { :f => f } %> + +

    + <%= f.submit t('create') %> <%= link_back %> +

    + +<% end %> \ No newline at end of file diff --git a/app/views/admin/pages/create.js.erb b/app/views/admin/pages/create.js.erb new file mode 100644 index 000000000..5d59fd1d1 --- /dev/null +++ b/app/views/admin/pages/create.js.erb @@ -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')) %>"); \ No newline at end of file diff --git a/app/views/admin/pages/new.html.erb b/app/views/admin/pages/new.html.erb index 5ba7cae06..cf930b917 100644 --- a/app/views/admin/pages/new.html.erb +++ b/app/views/admin/pages/new.html.erb @@ -1,11 +1,5 @@ -

    <%= t('admin.new_page') %>

    +<% content_for :sidebar do %> + +<% end -%> -<%= form_for :page, :url => admin_pages_path do |f| %> - <%= f.error_messages %> - <%= render :partial => "form", :locals => { :f => f } %> - -

    - <%= f.submit t('create') %> <%= link_back %> -

    - -<% end %> +<%= render 'new' %> \ No newline at end of file diff --git a/app/views/admin/pages/new.js.erb b/app/views/admin/pages/new.js.erb new file mode 100644 index 000000000..7dd4e0489 --- /dev/null +++ b/app/views/admin/pages/new.js.erb @@ -0,0 +1 @@ +$('#main').html("<%= escape_javascript(render(:partial => 'admin/pages/new')) %>"); \ No newline at end of file diff --git a/app/views/layouts/content.html.erb b/app/views/layouts/content.html.erb index f3e8d4db2..c69eb22aa 100644 --- a/app/views/layouts/content.html.erb +++ b/app/views/layouts/content.html.erb @@ -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 %>