Uniformized: @page and @ link in @item

Change parser for page edition.
This commit is contained in:
chris2tof 2011-05-25 14:50:56 +08:00
parent 3d97c5bfda
commit d971a8ecbe
20 changed files with 246 additions and 73 deletions

View File

@ -13,7 +13,6 @@ class Admin::ItemsController < ApplicationController
@item = Item.find(params[:item_id])
else
@item = Item.first(:conditions => {:parent_id => nil})
@page = @item
end
end

View File

@ -7,60 +7,60 @@ class Admin::LinksController < ApplicationController
before_filter :is_admin?
def show
@link ||= Link.find(params[:id])
@item ||= Link.find(params[:id])
end
def new
@link = Link.new
@link.is_published = true
@link.parent_id = Page.find(params[:parent_id]).id rescue nil
@item = Link.new
@item.is_published = true
@item.parent_id = Page.find(params[:parent_id]).id rescue nil
end
def edit
@link = Link.find(params[:id])
@i18n_variable = @link.i18n_variable
@item = Link.find(params[:id])
@i18n_variable = @item.i18n_variable
end
def create
@link = Link.new(params[:link])
@item = Link.new(params[:link])
if @link.save
if @item.save
flash.now[:notice] = t('admin.create_success_link')
respond_to do |format|
format.html {
redirect_to admin_link_url(@link)
redirect_to admin_link_url(@item)
}
format.js {}
end
else
flash.now[:error] = t('admin.create_error_link')
@i18n_variable = @link.i18n_variable
@i18n_variable = @item.i18n_variable
render :action => "new"
end
end
def update
@link = Link.find(params[:id])
@item = Link.find(params[:id])
if @link.update_attributes(params[:link])
if @item.update_attributes(params[:link])
flash.now[:notice] = t('admin.update_success_link')
respond_to do |format|
format.html {
redirect_to admin_link_url(@link)
redirect_to admin_link_url(@item)
}
format.js {}
end
else
flash.now[:error] = t('admin.update_error_link')
@i18n_variable = @link.i18n_variable
@i18n_variable = @item.i18n_variable
render :action => "edit"
end
end
def destroy
@link = Link.find(params[:id])
@link.destroy
@link.destroy_i18n_variable
@item = Link.find(params[:id])
@item.destroy
@item.destroy_i18n_variable
respond_to do |format|
format.html {
redirect_to admin_items_url

View File

@ -0,0 +1,54 @@
class Admin::PagePartsController < ApplicationController
layout "content"
before_filter :authenticate_user!
before_filter :is_admin?
before_filter :set_current_item
def show
@part = PagePart.find(params[:id])
respond_to do |format|
format.html {
render 'admin/items/index'
}
format.js {}
end
end
def new
end
def edit
@part = PagePart.find(params[:id])
#@part.content = parse_content(@part.content, {:locale => 'show'})
# @part = @part.i18n_variable
end
def create
end
def update
@part = PagePart.find(params[:id])
parse_content(@part.content, {:locale => 'destroy'})
if @part.update_attributes(params[:page])
@part.content = parse_content(@part.content, {:locale => 'create'})
@part.save!
flash[:notice] = t('admin.update_success_page')
redirect_to admin_items_url( :parent_id => @part.page_id )
else
render :action => "edit"
end
end
def destroy
@item = Page.find(params[:id])
@item.destroy
@item.destroy_i18n_variable
redirect_to admin_items_url( :parent_id => @item.parent_id )
end
end

View File

@ -8,10 +8,9 @@ class Admin::PagesController < ApplicationController
before_filter :set_current_item
def show
@page = Page.find(params[:id])
@item = Page.find(params[:id])
respond_to do |format|
format.html {
@item = @page
render 'admin/items/index'
}
format.js {}
@ -19,47 +18,47 @@ class Admin::PagesController < ApplicationController
end
def new
@page = Page.new
@page.is_published = true
@page.parent_id = @parent_item.id rescue nil
@item = Page.new
@item.is_published = true
@item.parent_id = @parent_item.id rescue nil
end
def edit
@page = Page.find(params[:id])
@page.content = parse_content(@page.content, {:locale => 'show'})
@i18n_variable = @page.i18n_variable
@item = Page.find(params[:id])
@item.content = parse_content(@item.content, {:locale => 'show'})
@i18n_variable = @item.i18n_variable
end
def create
@page = Page.new(params[:page])
@page.content = parse_content(@page.content, {:locale => 'create'})
if @page.save
@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 => @page.parent_id )
redirect_to admin_items_url( :parent_id => @item.parent_id )
else
render :action => "new"
end
end
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!
@item = Page.find(params[:id])
parse_content(@item.content, {:locale => 'destroy'})
if @item.update_attributes(params[:page])
@item.content = parse_content(@item.content, {:locale => 'create'})
@item.save!
flash[:notice] = t('admin.update_success_page')
redirect_to admin_items_url( :parent_id => @page.parent_id )
redirect_to admin_items_url( :parent_id => @item.parent_id )
else
render :action => "edit"
end
end
def destroy
@page = Page.find(params[:id])
@page.destroy
@page.destroy_i18n_variable
@item = Page.find(params[:id])
@item.destroy
@item.destroy_i18n_variable
redirect_to admin_items_url( :parent_id => @page.parent_id )
redirect_to admin_items_url( :parent_id => @item.parent_id )
end
end

View File

@ -18,8 +18,8 @@ class ApplicationController < ActionController::Base
# Render the page
def render_page
if @page
render :text => parse_page(@page)
if @item
render :text => parse_page(@item)
else
render :text => '404 Not Found'
end
@ -53,7 +53,7 @@ class ApplicationController < ActionController::Base
end
def set_current_item
session[:current_page] = params[:id] || @page.id || @link.id || @item.id rescue nil
session[:current_page] = params[:id] || @item.id rescue nil
end
end

View File

@ -1,8 +1,8 @@
class PagesController < ApplicationController
def index
@page = Page.find_by_name('home')
if @page
@item = Page.find_by_name('home')
if @item
render_page
else
render :text => 'You need a home page'
@ -14,7 +14,7 @@ class PagesController < ApplicationController
item = Item.first(:conditions => {:full_name => params[:page_name]})
case item._type
when 'Page'
@page = item
@item = item
render_page
when 'Link'
redirect_to "http://#{item[:url]}"

View File

@ -4,6 +4,6 @@ switch ("<%= escape_javascript(@item._type)%>") {
$('#main').append("<%= escape_javascript(render(:partial => 'admin/links/show')) %>");
break;
case 'Page':
$('#main').append("<%= escape_javascript(render(:partial => 'page')) %>");
$('#main').append("<%= escape_javascript(render(:partial => 'admin/pages/show')) %>");
break;
};

View File

@ -2,7 +2,7 @@
<%= flash_messages %>
<%= form_for @link, :url => admin_link_path(@link), :html => { :class => 'form' } do |f| %>
<%= form_for @item, :url => admin_link_path(@item), :html => { :class => 'form' } do |f| %>
<%= render :partial => "admin/links/form", :locals => { :f => f } %>

View File

@ -2,7 +2,7 @@
<%= flash_messages %>
<%= form_for @link, :url => admin_links_path, :html => { :class => 'form' } do |f| %>
<%= form_for @item, :url => admin_links_path, :html => { :class => 'form' } do |f| %>
<%= render :partial => "admin/links/form", :locals => { :f => f } %>

View File

@ -1,24 +1,24 @@
<%= flash_messages %>
<p>
<b><%= t('admin.name') %></b>
<%= @link.name %>
<%= @item.name %>
</p>
<% @site_valid_locales.each do |locale| %>
<p>
<b><%= "#{t('admin.title')} #{locale}" %></b>
<%= @link.i18n_variable[locale] if @link.i18n_variable %>
<%= @item.i18n_variable[locale] if @item.i18n_variable %>
</p>
<% end %>
<p>
<b><%= t('admin.is_published') %></b>
<%= @link.is_published.to_s %>
<%= @item.is_published.to_s %>
</p>
<p>
<b><%= t('admin.url') %></b>
<%= @link.url %>
<%= @item.url %>
</p>
<%= link_to t(:edit), edit_admin_link_path(@link) %>
<%= link_to t(:edit), edit_admin_link_path(@item) %>

View File

@ -1,4 +1,4 @@
<% form_for :link, :url => admin_link_path(@link), :html => { :method => :delete } do |f| %>
<% form_for :link, :url => admin_link_path(@item), :html => { :method => :delete } do |f| %>
<h2><%= t('sure?') %></h2>
<p>
<%= submit_tag t(:delete) %>

View File

@ -0,0 +1 @@
<h1>EDIT</h1>

View File

@ -0,0 +1,5 @@
<% content_for :sidebar do %>
<div id='sidebar'><%= render 'admin/items/site_map_left_bar' %></div>
<% end -%>
<%= render 'edit' %>

View File

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

View File

@ -1 +1,10 @@
<%= parse_page(@page).html_safe %>
<%= parse_page_edit(@item).html_safe %>
<% content_for :page_specific_javascript do %>
<script type="text/javascript" charset="utf-8">
$("div.editable").live("mouseenter mouseleave",
function (event) {
$(this).next('.edit_link').toggle();
});
</script>
<% end -%>

View File

@ -1,6 +1,6 @@
<h1><%= t('admin.editing_page') %></h1>
<%= form_for @page, :url => admin_page_path(@page) do |f| %>
<%= form_for @item, :url => admin_page_path(@item) do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>

View File

@ -18,6 +18,15 @@
</head>
<body>
<div id="header">
<ul id="nav">
<li><%= link_to t('homepage'), root_path %></li>
<li><%= link_to t('admin.item'), admin_items_path %></li>
<li><%= link_to t('admin.layout'), admin_layouts_path %></li>
<li><%= link_to t('admin.asset'), admin_assets_path %></li>
<li><%= link_to t('admin.user_info'), admin_user_info_models_path %></li>
<li><%= link_to t('admin.user_role'), admin_user_role_models_path %></li>
<li><%= link_to t('admin.translation'), admin_translations_path %></li>
</ul>
</div>
<div id="content" class="content">

View File

@ -49,6 +49,7 @@ PrototypeR4::Application.routes.draw do
end
end
resources :pages
resources :page_parts
resources :homes
resources :snippets
resources :translations

View File

@ -72,7 +72,7 @@ module Parser
c.define_tag 'layout_part' do |tag|
ret = ''
ret << "<div id='#{tag.attr['name']}'>"
ret << (page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }).content rescue ''
ret << page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }.content rescue ''
ret << tag.expand
ret << '</div>'
end
@ -94,8 +94,104 @@ module Parser
end
end
def parse_page_edit(page)
if page._type == 'Page'
layout_content = page.layout.content
context = parser_context_edit(page)
parser = Radius::Parser.new(context, :tag_prefix => 'r')
parser.parse(parser.parse(layout_content))
end
end
def parser_context_edit(page, attributes = {})
Radius::Context.new do |c|
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
"<a href='?locale=#{locale}'>#{lang}</a>"
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
end
c.define_tag 'css' do |tag|
assets = Asset.any_in(:filename => tag.attr['name'].split(',').map(&:strip))
res = ''
assets.each do |asset|
res << "<link href='#{asset.data.file.url}' rel='stylesheet' type='text/css' /> " if asset.data.file.content_type.eql?('text/css')
end
res
end
c.define_tag 'image' do |tag|
asset = Asset.find(tag.attr['id'])
if asset
res = "<img src=#{asset.data.file.url} "
tag.attr.each do |l|
res << "#{l[0]}='#{l[1]}' "
end
res << '>'
end
end
c.define_tag 'layout_part' do |tag|
part = page.page_parts.detect{ |p| p.name.to_s == tag.attr['name'].to_s }
ret = ''
ret << "<div id='#{tag.attr['name']}'"
ret << " part_id='#{part.id}'" if part
ret << " class='editable'" if part
ret << " style='border:solid 1px; margin:5px; padding:5px;'>"
ret << "<div class='edit_link' style='display:block'>" if part
ret << " <a href='#{edit_admin_page_part_path(part.id)}'>#{t(:edit)}</a>" if part
ret << '</div>' if part
ret << part.content rescue ''
ret << tag.expand
ret << '</div>'
end
end
end
def self.included(base)
base.send :helper_method, :parse_page if base.respond_to? :helper_method
base.send :helper_method, :parse_page_edit if base.respond_to? :helper_method
end
end

File diff suppressed because one or more lines are too long