From ce1fecaf9318875a538f801adccc771d24d9999c Mon Sep 17 00:00:00 2001
From: Christophe Vilayphiou
Date: Sun, 6 May 2012 00:35:13 +0800
Subject: [PATCH] Add position ordering and hiding page for i18n
---
app/controllers/pages_controller.rb | 2 +-
app/helpers/admin/item_helper.rb | 5 ++++-
app/models/item.rb | 16 ++++++++++++++++
app/views/admin/pages/_form.html.erb | 12 ++++++++++--
lib/parsers/parser_common.rb | 8 ++++----
5 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb
index 644f0223..c4e00aa2 100644
--- a/app/controllers/pages_controller.rb
+++ b/app/controllers/pages_controller.rb
@@ -16,7 +16,7 @@ class PagesController < ApplicationController
def show
#begin
@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
when 'Page'
render_page(params)
diff --git a/app/helpers/admin/item_helper.rb b/app/helpers/admin/item_helper.rb
index d1025c55..8af4db34 100644
--- a/app/helpers/admin/item_helper.rb
+++ b/app/helpers/admin/item_helper.rb
@@ -28,12 +28,15 @@ module Admin::ItemHelper
end
def render_children(parent)
- if children = parent.children
+ children = parent.ordered_children
+ if !children.entries.blank?
ret = ''
children.each do |child|
ret << render_node_and_children(child)
end
ret
+ else
+ ''
end
end
diff --git a/app/models/item.rb b/app/models/item.rb
index 44c6762d..8cd8bec0 100644
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -7,6 +7,7 @@ class Item
field :full_name, :index => true
field :position, :type => Integer
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 :name, :exclusion => { :in => LIST[:forbidden_item_names] }
@@ -35,6 +36,21 @@ class Item
urls = ancestors.map{ |a| a.name } << self.name
urls.join("/")
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
diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb
index 5271af8f..460bbce3 100644
--- a/app/views/admin/pages/_form.html.erb
+++ b/app/views/admin/pages/_form.html.erb
@@ -30,7 +30,15 @@
<%= select('page','category', @categories.collect{|category| [category.i18n_variable[I18n.locale], category.id]}, :selected => @item[:category], :include_blank => true ) rescue ''%>
-<%= f.label :is_published, "#{t('admin.is_published')} ?" %>
-<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
+ <%= f.label :is_published, "#{t('admin.is_published')} ?" %>
+ <%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
+
+
+ <%= 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][]', '' %>
diff --git a/lib/parsers/parser_common.rb b/lib/parsers/parser_common.rb
index 6e7fea47..66491232 100644
--- a/lib/parsers/parser_common.rb
+++ b/lib/parsers/parser_common.rb
@@ -12,7 +12,7 @@ module ParserCommon
res << ""
i = nil
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)
i += 1 if i
end
@@ -23,7 +23,7 @@ module ParserCommon
else
res << ''
res << "- " + "#{page.i18n_variable[I18n.locale]}" + "
"
- page.children.each do |child|
+ page.ordered_and_visible_children.each do |child|
res << "- " + menu_level(child, current + 1, menu, edit) + "
"
end
res << '
'
@@ -37,7 +37,7 @@ module ParserCommon
res << menu.values["li_class_#{current}"]
res << "_#{i}" if i
res << ">"
- if page.children.size > 0
+ if page.ordered_and_visible_children.size > 0
res << menu_level(page, current + 1, menu, edit)
else
res << "#{page.i18n_variable[I18n.locale]}"
@@ -114,7 +114,7 @@ module ParserCommon
res << ""
res << "
#{page.i18n_variable[I18n.locale]}
"
res << "