Add position ordering and hiding page for i18n

This commit is contained in:
Christophe Vilayphiou 2012-05-06 00:35:13 +08:00
parent f190ce18ee
commit ce1fecaf93
5 changed files with 35 additions and 8 deletions

View File

@ -16,7 +16,7 @@ class PagesController < ApplicationController
def show def show
#begin #begin
@item = Item.first(:conditions => {:full_name => params[:page_name]}) @item = Item.first(:conditions => {:full_name => params[:page_name]})
if @item && @item.is_published if @item && @item.is_published && (@item.enabled_for.nil? ? true : @item.enabled_for.include?(I18n.locale.to_s))
case @item._type case @item._type
when 'Page' when 'Page'
render_page(params) render_page(params)

View File

@ -28,12 +28,15 @@ module Admin::ItemHelper
end end
def render_children(parent) def render_children(parent)
if children = parent.children children = parent.ordered_children
if !children.entries.blank?
ret = '' ret = ''
children.each do |child| children.each do |child|
ret << render_node_and_children(child) ret << render_node_and_children(child)
end end
ret ret
else
''
end end
end end

View File

@ -7,6 +7,7 @@ class Item
field :full_name, :index => true field :full_name, :index => true
field :position, :type => Integer field :position, :type => Integer
field :is_published, :type => Boolean, :default => false, :index => true field :is_published, :type => Boolean, :default => false, :index => true
field :enabled_for, :type => Array, :default => nil
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
@ -36,6 +37,21 @@ class Item
urls.join("/") urls.join("/")
end end
def ordered_children
self.children.asc(:position)
end
def ordered_and_visible_children
objects = ordered_children
a = []
if objects
objects.each do |object|
a << object if object.enabled_for.nil? ? true : object.enabled_for.include?(I18n.locale.to_s)
end
end
a
end
protected protected
def setup_default_value def setup_default_value

View File

@ -30,7 +30,15 @@
<span id="app_page_category"><%= select('page','category', @categories.collect{|category| [category.i18n_variable[I18n.locale], category.id]}, :selected => @item[:category], :include_blank => true ) rescue ''%> </span> <span id="app_page_category"><%= select('page','category', @categories.collect{|category| [category.i18n_variable[I18n.locale], category.id]}, :selected => @item[:category], :include_blank => true ) rescue ''%> </span>
</p> </p>
<p> <p>
<%= f.label :is_published, "#{t('admin.is_published')} ?" %> <%= f.label :is_published, "#{t('admin.is_published')} ?" %>
<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No <%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
</p>
<p>
<%= f.label :enabled_for, "#{t('admin.enabled_for')} ?" %>
<% @site_valid_locales.each do |valid_locale| %>
<%= check_box_tag 'page[enabled_for][]', valid_locale, (@item.enabled_for.nil? ? true : @item.enabled_for.include?(valid_locale)) %>
<%= I18nVariable.from_locale(valid_locale) %>
<% end %>
<%= hidden_field_tag 'page[enabled_for][]', '' %>
</p> </p>

View File

@ -12,7 +12,7 @@ module ParserCommon
res << "<ul class='ini_list'>" res << "<ul class='ini_list'>"
i = nil i = nil
i = 1 if menu.values["li_incremental_#{current}"] i = 1 if menu.values["li_incremental_#{current}"]
page.children.each do |child| page.ordered_and_visible_children.each do |child|
res << menu_li(child, current, menu, i, edit) res << menu_li(child, current, menu, i, edit)
i += 1 if i i += 1 if i
end end
@ -23,7 +23,7 @@ module ParserCommon
else else
res << '<ul>' res << '<ul>'
res << "<li>" + "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='dm_ctrl'>#{page.i18n_variable[I18n.locale]}</a>" + "</li>" res << "<li>" + "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='dm_ctrl'>#{page.i18n_variable[I18n.locale]}</a>" + "</li>"
page.children.each do |child| page.ordered_and_visible_children.each do |child|
res << "<li>" + menu_level(child, current + 1, menu, edit) + "</li>" res << "<li>" + menu_level(child, current + 1, menu, edit) + "</li>"
end end
res << '</ul>' res << '</ul>'
@ -37,7 +37,7 @@ module ParserCommon
res << menu.values["li_class_#{current}"] res << menu.values["li_class_#{current}"]
res << "_#{i}" if i res << "_#{i}" if i
res << ">" res << ">"
if page.children.size > 0 if page.ordered_and_visible_children.size > 0
res << menu_level(page, current + 1, menu, edit) res << menu_level(page, current + 1, menu, edit)
else else
res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='nav dm_ctrl'>#{page.i18n_variable[I18n.locale]}</a>" res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='nav dm_ctrl'>#{page.i18n_variable[I18n.locale]}</a>"
@ -114,7 +114,7 @@ module ParserCommon
res << "<div class='category_list'>" res << "<div class='category_list'>"
res << "<h3 class='h3'>#{page.i18n_variable[I18n.locale]}</h3>" res << "<h3 class='h3'>#{page.i18n_variable[I18n.locale]}</h3>"
res << "<ul class='list'>" res << "<ul class='list'>"
page.children.each do |child| page.ordered_and_visible_children.each do |child|
res << "<li>" res << "<li>"
res << "<a href='#{child.full_name}'>#{child.i18n_variable[I18n.locale]}</a>" res << "<a href='#{child.full_name}'>#{child.i18n_variable[I18n.locale]}</a>"
res << "</li>" res << "</li>"