- No more Home object

- Sitemap on the left side and view of the current page in the main block
This commit is contained in:
chris2tof 2011-05-09 18:49:57 +08:00
parent 9fdfb140d3
commit d9ba76ebd5
21 changed files with 122 additions and 149 deletions

View File

@ -1,50 +0,0 @@
class Admin::HomesController < ApplicationController
layout "admin"
before_filter :authenticate_user!
before_filter :is_admin?
def show
#TODO
end
def new
@home = Home.new
@home.is_published = true
@home.parent_id = nil
end
def edit
@home = Home.find(params[:id])
@i18n_variable = @home.i18n_variable
end
def create
@home = Home.new(params[:home])
if @home.save
flash[:notice] = t('admin.create_success_home')
redirect_to admin_items_url
else
render :action => "new"
end
end
def update
@home = Home.find(params[:id])
if @home.update_attributes(params[:home])
flash[:notice] = t('admin.update_success_home')
redirect_to admin_items_url
else
render :action => "edit"
end
end
def destroy
@home = Home.find(params[:id])
@home.destroy
redirect_to admin_items_url
end
end

View File

@ -1,6 +1,6 @@
class Admin::ItemsController < ApplicationController class Admin::ItemsController < ApplicationController
layout "admin" layout "content"
before_filter :authenticate_user! before_filter :authenticate_user!
before_filter :find_parent_item before_filter :find_parent_item
@ -8,8 +8,11 @@ class Admin::ItemsController < ApplicationController
before_filter :is_admin? before_filter :is_admin?
def index def index
@items = Item.where(:parent_id => @parent_item.id).entries rescue [] if params[:item_id]
@items = Item.where(:_type => 'Home').entries unless (!@items.empty? || @parent_item) @page = Item.find(params[:item_id])
else
@page = Item.first(:conditions => {:parent_id => nil})
end
end end

View File

@ -1,6 +1,6 @@
class Admin::PagesController < ApplicationController class Admin::PagesController < ApplicationController
layout "admin" layout "content"
before_filter :authenticate_user! before_filter :authenticate_user!
before_filter :find_parent_item before_filter :find_parent_item
before_filter :is_admin? before_filter :is_admin?

View File

@ -1,7 +1,7 @@
class PagesController < ApplicationController class PagesController < ApplicationController
def index def index
@page = Home.find_by_name('home') @page = Page.find_by_name('home')
if @page if @page
render_page render_page
else else

View File

@ -49,4 +49,25 @@ module ApplicationHelper
end end
end end
def render_node_and_children(node, current_name = 'home')
ret = ''
if node
ret << "<li>"
ret << (link_to node.name, admin_items_path(:item_id => node.id), :remote => true, :class => (current_name.eql?(node.name) ? 'sidebar_no_link' : 'sidebar_link'))
ret << render_children(node) if node.children.size > 0
ret << "</li>"
end
ret.html_safe
end
def render_children(parent)
ret = ''
ret << "<ul>"
parent.children.each do |child|
ret << render_node_and_children(child)
end
ret << "</ul>"
ret
end
end end

10
app/models/component.rb Normal file
View File

@ -0,0 +1,10 @@
class Component
include Mongoid::Document
include Mongoid::Timestamps
field :key
field :document_class, :type => String
field :parent_id, :type => BSON::ObjectId, :index => true
end

View File

@ -1,10 +0,0 @@
class Home < Page
private
# Remove the validation for parent_id
def validates_presence_of_parent_id?
false
end
end

View File

@ -15,10 +15,9 @@ class Item
validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
validates_uniqueness_of :name, :scope => :parent_id validates_uniqueness_of :name, :scope => :parent_id
validates_presence_of :name, :full_name, :position, :is_published validates_presence_of :name, :full_name, :position, :is_published
validates_presence_of :parent_id, :if => :validates_presence_of_parent_id?
referenced_in :parent, :class_name => "Item" belongs_to :parent, :class_name => "Item"
references_many :children, :class_name => "Item" has_many :children, :class_name => "Item", :as => 'parent'
before_validation :setup_default_value before_validation :setup_default_value
@ -59,11 +58,6 @@ class Item
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end end
# Check if the page is home
def is_home?
self.name.eql?('home') && (self.parent_id.nil? || self.parent_id.empty?)
end
# Build the url from the array of ancestors # Build the url from the array of ancestors
def url def url
urls = ancestors.map{ |a| a.name } << self.name urls = ancestors.map{ |a| a.name } << self.name

View File

@ -1,30 +0,0 @@
<%= f.hidden_field :parent_id %>
<p>
<%= f.label :name, t('admin.name') %>
<%= f.text_field :name, :class => 'text', :value => 'home', :disabled => true %>
<%= f.hidden_field :name, :value => 'home' %>
</p>
<% @site_valid_locales.each do |locale| %>
<p>
<%= label_tag "home[title]", "#{t('admin.title')} #{locale}" %>
<%= text_field_tag "home[i18n_variable][#{locale}]", (@i18n_variable[locale] if @i18n_variable), :class => 'text' %>
</p>
<% end %>
<p>
<%= f.label :layout_id, t('admin.layout_name') %>
<%= 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
</p>

View File

@ -1,10 +0,0 @@
<h1><%= t('admin.editing_home') %></h1>
<%= form_for @home, :url => admin_home_path(@home) do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit t('update') %> <%= link_back %>
</p>
<% end %>

View File

@ -1,11 +0,0 @@
<h1><%= t('admin.new_home') %></h1>
<%= form_for :home, :url => admin_homes_path do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit t('create') %> <%= link_back %>
</p>
<% end %>

View File

@ -1,27 +1,36 @@
<% content_for :secondary do %> <% content_for :sidebar do %>
<ul class="list"> <div id='sidebar'><%= render 'layouts/site_map_left_bar' %></div>
<% if (@items.empty? && !@parent_item ) || (!@items.empty? && @items[0].is_home?) %>
<li><%= t('admin.new_page') %></li>
<li><%= t('admin.new_link') %></li>
<li><%= t('admin.new_snippet') %></li>
<% else %>
<li><%= link_to t('admin.new_page'), new_admin_page_path( :parent_id => @parent_item.id ), :class => 'button positive' %></li>
<li><%= link_to t('admin.new_link'), new_admin_link_path( :parent_id => @parent_item.id ) %>
<li><%= link_to t('admin.new_snippet'), new_admin_snippet_path( :parent_id => @parent_item.id ), :class => 'button positive' %></li>
<% end %>
</ul>
<% end -%> <% end -%>
<%= flash_messages %> <% content_for :page_specific_javascript do %>
<script type="text/javascript" charset="utf-8">
$('a.sidebar_link').live( "click", function(){
$(this).removeClass('sidebar_link');
$('.sidebar_no_link').addClass('sidebar_link');
$('.sidebar_no_link').removeClass('sidebar_no_link');
$(this).addClass('sidebar_no_link');
});
</script>
<% end -%>
<% content_for :page_specific_css do %>
<style type="text/css">
.sidebar_no_link {
pointer-events: none;
cursor: default;
}
</style>
<% end %>
<%= render 'layouts/page' %>
<!-- <%= flash_messages %>
<h1><%= t('admin.list_items') %>: <%= show_parent_items_link unless @parent_item.nil? %></h1> <h1><%= t('admin.list_items') %>: <%= show_parent_items_link unless @parent_item.nil? %></h1>
<% if !Layout.exist_one? %> <% if !Layout.exist_one? %>
<div style='color:red'><%= t('admin.no_layout') %>: <%= link_to t('create').downcase, new_admin_layout_path %></div> <div style='color:red'><%= t('admin.no_layout') %>: <%= link_to t('create').downcase, new_admin_layout_path %></div>
<br/> <br/>
<% elsif (@items.empty? && !@parent_item ) %>
<div style='color:red'><%= t('admin.no_home_page') %>: <%= link_to t('create').downcase, new_admin_home_path(:parent_id => nil) %></div>
<br/>
<% else %> <% else %>
<table> <table>
<tr> <tr>
@ -43,4 +52,4 @@
</table> </table>
<% end %> <% end %>
<%= render :partial => "snippets", :locals => { :snippets => @snippets } %> <%= render :partial => "snippets", :locals => { :snippets => @snippets } %> -->

View File

@ -0,0 +1,9 @@
$('#main').empty();
switch ("<%= escape_javascript(@page._type)%>") {
case 'Link':
$('#main').append("TODO: page to edit the link");
break;
case 'Page':
$('#main').append("<%= escape_javascript(render(:partial => 'layouts/page')) %>");
break;
};

View File

@ -2,12 +2,7 @@
<p> <p>
<%= f.label :name, t('admin.name') %> <%= f.label :name, t('admin.name') %>
<% if @page.parent_id.nil? %> <%= f.text_field :name, :class => 'text' %>
<%= f.text_field :name, :class => 'text', :value => 'home', :disabled => true %>
<%= f.hidden_field :name, :value => 'home' %>
<% else %>
<%= f.text_field :name, :class => 'text' %>
<% end %>
</p> </p>

View File

@ -0,0 +1 @@
<%= parse_page(@page).html_safe if @page._type.eql?('Page') %>

View File

@ -0,0 +1 @@
<%= render_node_and_children(@page) %>

View File

@ -0,0 +1,30 @@
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%= @title %></title>
<link rel="shortcut icon" href="/favicon.ico">
<%= yield :page_specific_link %>
<%= stylesheet_link_tag "easy", "main", "devise", "content", :media => "screen, projection" %>
<%= stylesheet_link_tag "easyprint", :media => "print" %>
<%= javascript_include_tag "jquery", "jquery-ui", "rails", "easy", "application", :cache => 'all' %>
<%= yield :page_specific_javascript %>
<!--[if IE]>
<%= stylesheet_link_tag "ie", :media => "screen, projection" %>
<![endif]-->
<%= yield :page_specific_css %>
<%= csrf_meta_tag %>
</head>
<body>
<div id="header">
</div>
<div id="content" class="content">
<div id="sidebar" style='float: left;'><%= yield :sidebar %></div>
<div id="main" style='float: left;'><%= yield %></div>
</div>
<div id="footer">
</div>
</body>
</html>

View File

@ -24,6 +24,7 @@ module PrototypeR4
# Custom directories with classes and modules you want to be autoloadable. # Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras) # config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths = %W(#{config.root}/lib)
# Only load the plugins named here, in the order given (default is alphabetical). # Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named. # :all can be used as a placeholder for all plugins not explicitly named.

View File

@ -82,10 +82,16 @@ module Parser
end end
def parse_page(page) def parse_page(page)
if page._type == 'Page'
layout_content = (page.layout)? page.layout.content : "<r:content />" layout_content = (page.layout)? page.layout.content : "<r:content />"
context = parser_context(page.content) context = parser_context(page.content)
parser = Radius::Parser.new(context, :tag_prefix => 'r') parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(parser.parse(layout_content)) parser.parse(parser.parse(layout_content))
end end
end
def self.included(base)
base.send :helper_method, :parse_page if base.respond_to? :helper_method
end
end end

View File

@ -47,7 +47,7 @@ namespace :dev do
layout = Layout.create!( :name => 'root', :description => 'root', :content => File.open("#{RAILS_ROOT}/lib/template/root.layout").read ) 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 ) 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 )
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 ) 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 )

View File

@ -0,0 +1,4 @@
html{
height:100%;
width:100%;
}