From 6a443f0976214275564d12e8aa733a72dabbde54 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Fri, 11 May 2012 16:16:09 +0800 Subject: [PATCH] Add mongoid-tree to Item --- Gemfile | 1 + Gemfile.lock | 3 + app/controllers/pages_controller.rb | 8 +-- app/helpers/admin/item_helper.rb | 21 ++---- app/helpers/application_helper.rb | 2 +- app/models/item.rb | 70 +++++-------------- app/models/page.rb | 4 -- .../admin/items/_site_map_left_bar.html.erb | 3 - lib/parsers/parser_back_end.rb | 2 +- lib/parsers/parser_common.rb | 18 ++--- lib/parsers/parser_front_end.rb | 2 +- lib/tasks/items.rake | 19 +++++ .../page_contexts/_page_context.html.erb | 2 +- 13 files changed, 60 insertions(+), 95 deletions(-) create mode 100644 lib/tasks/items.rake diff --git a/Gemfile b/Gemfile index 24ee0383..c24754c6 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ gem 'kaminari', :git => 'git://github.com/amatsuda/kaminari.git' gem 'mini_magick' gem 'mongoid' +gem 'mongoid-tree', :require => 'mongoid/tree' gem "mongo_session_store-rails3" gem 'nokogiri' gem 'radius' diff --git a/Gemfile.lock b/Gemfile.lock index e850e52b..0dfca3b6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,6 +118,8 @@ GEM activemodel (~> 3.1) mongo (~> 1.3) tzinfo (~> 0.3.22) + mongoid-tree (0.7.0) + mongoid (~> 2.0) multi_json (1.1.0) nokogiri (1.5.2) nokogiri (1.5.2-x86-mingw32) @@ -277,6 +279,7 @@ DEPENDENCIES mini_magick mongo_session_store-rails3 mongoid + mongoid-tree nokogiri radius rails (>= 3.1.0, < 3.2.0) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index c4e00aa2..b781f202 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -15,7 +15,7 @@ class PagesController < ApplicationController def show #begin - @item = Item.first(:conditions => {:full_name => params[:page_name]}) + @item = Item.first(:conditions => {:path => params[:page_name]}) if @item && @item.is_published && (@item.enabled_for.nil? ? true : @item.enabled_for.include?(I18n.locale.to_s)) case @item._type when 'Page' @@ -33,16 +33,16 @@ class PagesController < ApplicationController def index_from_link if params[:page] - redirect_to "/#{@item.full_name}?page=#{params[:page]}&category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}" + redirect_to "/#{@item.path}?page=#{params[:page]}&category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}" else - redirect_to "/#{@item.full_name}?category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}" + redirect_to "/#{@item.path}?category_id=#{params[:category_id]}&tag_id=#{params[:tag_id]}" end end def show_from_link # debugger # a=1 - redirect_to "/#{@item.full_name}?id=#{params[:id]}&preview=#{params[:preview]}" + redirect_to "/#{@item.path}?id=#{params[:id]}&preview=#{params[:preview]}" end def load_orbit_bar diff --git a/app/helpers/admin/item_helper.rb b/app/helpers/admin/item_helper.rb index dc78db88..aeadfdd0 100644 --- a/app/helpers/admin/item_helper.rb +++ b/app/helpers/admin/item_helper.rb @@ -8,9 +8,9 @@ module Admin::ItemHelper dest = admin_page_path(node) when 'Link' dest = admin_link_path(node) + no_nested = 'no-nest' end - # ret << "
    " unless node.parent.nil? - ret << "
  1. " + ret << "
  2. " ret << "
    " ret << (link_to node.i18n_variable[I18n.locale], dest) ret << "
    " @@ -22,14 +22,13 @@ module Admin::ItemHelper ret << "
    " ret << render_children(node) ret << "
  3. " - # ret << "
" unless node.parent.nil? end ret.html_safe end def render_children(parent) - children = parent.ordered_children - if !children.entries.blank? + children = parent.children + if !parent.children.entries.blank? ret = '' ret << "
    " children.each do |child| @@ -43,15 +42,3 @@ module Admin::ItemHelper end end - - -#
      -#
    1. Some content
    2. -#
    3. Some content
      -#
        -#
      1. Some sub-item content
      2. -#
      3. Some sub-item content
      4. -#
      -#
    4. -#
    5. Some content
    6. -#
    \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 844fbe14..37c2d0d2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -114,7 +114,7 @@ module ApplicationHelper def page_title(page) res = "" page_title = page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale] - if page.is_home? && @site.title + if page.root? && @site.title res << @site.title[I18n.locale] elsif @site.title && @site.title_always_on res << @site.title[I18n.locale] + ' - ' + page_title diff --git a/app/models/item.rb b/app/models/item.rb index cea088fc..4d6ca001 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -2,59 +2,31 @@ class Item include Mongoid::Document include Mongoid::Timestamps + include Mongoid::Tree + include Mongoid::Tree::Ordering - field :name, :index => true - 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 - field :menu_enabled_for, :type => Array, :default => nil + field :name + field :path + field :is_published, :type => Boolean, :default => false + field :enabled_for, :type => Array, :default => nil + field :menu_enabled_for, :type => Array, :default => nil validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/ validates :name, :exclusion => { :in => LIST[:forbidden_item_names] } validates_uniqueness_of :name, :scope => :parent_id - validates_presence_of :name, :full_name, :position + validates_presence_of :name + + validates_associated :parent, :children + after_rearrange :rebuild_path - belongs_to :parent, :class_name => "Item" has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy - has_many :children, :class_name => "Item", :as => 'parent' - - before_validation :setup_default_value def self.find_by_name(item_name) Item.first(:conditions => { :name => item_name, :is_published => true }) end - - # Get an array of ancestors - def ancestors - node, nodes = self, [] - nodes << node = node.parent while !node.parent.blank? rescue nil - nodes.reverse - end - # Get an array of ancestor's id - def ancestor_ids - node, nodes = self, [] - while !node.parent.blank? do - node = node.parent rescue nil - nodes << node.id if node - end - # nodes << node = node.parent while !node.parent.blank? rescue nil - nodes.reverse - end - - # Build the url from the array of ancestors - def url - 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 + def visible_children + objects = self.children a = [] if objects objects.each do |object| @@ -66,20 +38,10 @@ class Item protected - def setup_default_value - # Set the position value within the parent scope - if self.position.blank? - max_page = Item.where(:parent_id => self.parent_id).count - self.position = (max_page)? max_page + 1 : 1 - end - - # Build the full_name from the ancestors array - full_node = self.ancestors.map{ |a| a.name }.push( self.name ) - # Remove root node if not root - full_node.shift if full_node.size >= 2 - self.full_name = full_node.join("/") + def rebuild_path + self.path = (self.ancestors_and_self - [Item.root]).collect{|x| x.name unless x.root?}.join('/') end - + # Enable the validation for parent_id def validates_presence_of_parent_id? true diff --git a/app/models/page.rb b/app/models/page.rb index 5a742e92..0378b688 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -15,10 +15,6 @@ class Page < Item before_save :create_parts, :set_key # embeds_many :custom_images, :class_name => 'Image', as: :design_image - - def is_home? - self.parent ? false : true - end def title @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil diff --git a/app/views/admin/items/_site_map_left_bar.html.erb b/app/views/admin/items/_site_map_left_bar.html.erb index 69e7851c..fb280299 100644 --- a/app/views/admin/items/_site_map_left_bar.html.erb +++ b/app/views/admin/items/_site_map_left_bar.html.erb @@ -19,9 +19,6 @@ stop: function(event, ui) { $.post("<%= admin_update_position_path %>", { id: ui.item.attr('id'), parent_id: ui.item.parent().closest('li').attr('id'), position: ui.item.index() } ); - // console.log("parent: " + ui.item.parent().closest('li').attr('id')); - // console.log("id: " + ui.item.attr('id')); - // console.log("position: " + ui.item.index()); } }); }); diff --git a/lib/parsers/parser_back_end.rb b/lib/parsers/parser_back_end.rb index 33b19510..e9c08a01 100644 --- a/lib/parsers/parser_back_end.rb +++ b/lib/parsers/parser_back_end.rb @@ -14,7 +14,7 @@ module ParserBackEnd # }.join(' | ') # end # c.define_tag 'link' do |tag| - # item = Item.first(:conditions => { :full_name => tag.attr['name'] }) + # item = Item.first(:conditions => { :path => tag.attr['name'] }) # ret = '' # ret << "<a href='" # ret << eval("admin_#{item._type.downcase}_path(item.id)") diff --git a/lib/parsers/parser_common.rb b/lib/parsers/parser_common.rb index be676544..59bd85b3 100644 --- a/lib/parsers/parser_common.rb +++ b/lib/parsers/parser_common.rb @@ -3,7 +3,7 @@ module ParserCommon def menu_level(page, current_page, current, menu, edit = false) res = '' - if page.ordered_and_visible_children.size > 0 + if page.visible_children.size > 0 res << "<ul class='" res << menu.values["class_#{current}"] rescue nil res << "'>" @@ -12,7 +12,7 @@ module ParserCommon if menu.values['home'] && current == 1 res << menu_li(page, current_page, current, menu, i, edit) end - page.ordered_and_visible_children.each do |child| + page.visible_children.each do |child| res << menu_li(child, current_page, current, menu, i, edit) i += 1 if i end @@ -25,13 +25,13 @@ module ParserCommon res = "<li class='" res << menu.values["li_class_#{current}"] rescue nil res << "_#{i}" if i - res << " active" if (current_page.id.eql?(page.id) || current_page.ancestor_ids.include?(page.id)) + res << " active" if (current_page.id.eql?(page.id) || current_page.descendant_of?(page)) res << "'>" - res << "<a href='/#{edit ? admin_page_path(page.id) : page.full_name}'><span>#{page.i18n_variable[I18n.locale]}</span></a>" - if page.ordered_and_visible_children.size > 0 && current <= menu.levels + res << "<a href='/#{edit ? admin_page_path(page.id) : page.path}'><span>#{page.i18n_variable[I18n.locale]}</span></a>" + if page.visible_children.size > 0 && current <= menu.levels res << "<span class='dot'></span>" res << menu_level(page, current_page, current + 1, menu, edit) - end unless (page.is_home? rescue nil) + end unless (page.root? rescue nil) res << "</li>" end @@ -113,13 +113,13 @@ module ParserCommon body.css('sub_menu').each do |sub_menu| menu_page = Page.find(sub_menu['id']) rescue nil res = '' - if menu_page && menu_page.ordered_and_visible_children.size > 0 + if menu_page && menu_page.visible_children.size > 0 res << "<div class='category_list'>" res << "<h3 class='h3'>#{menu_page.i18n_variable[I18n.locale]}</h3>" res << "<ul class='list'>" - menu_page.ordered_and_visible_children.each do |child| + menu_page.visible_children.each do |child| res << "<li class='#{page.id.eql?(child.id) ? 'active' : nil}'>" - res << "<a href='/#{edit ? admin_page_path(child.id) : child.full_name}'>#{child.i18n_variable[I18n.locale]}</a>" + res << "<a href='/#{edit ? admin_page_path(child.id) : child.path}'>#{child.i18n_variable[I18n.locale]}</a>" res << "</li>" end res << "</ul>" diff --git a/lib/parsers/parser_front_end.rb b/lib/parsers/parser_front_end.rb index a0004f15..6320f808 100644 --- a/lib/parsers/parser_front_end.rb +++ b/lib/parsers/parser_front_end.rb @@ -11,7 +11,7 @@ module ParserFrontEnd # }.join(' | ') # end # c.define_tag 'link' do |tag| - # item = Item.first(:conditions => { :full_name => tag.attr['name'] }) + # item = Item.first(:conditions => { :path => tag.attr['name'] }) # ret = '' # ret << "<a href='#{tag.attr['name']}'>" # ret << item.i18n_variable[I18n.locale] diff --git a/lib/tasks/items.rake b/lib/tasks/items.rake new file mode 100644 index 00000000..8889567d --- /dev/null +++ b/lib/tasks/items.rake @@ -0,0 +1,19 @@ +# encoding: utf-8 + +namespace :items do + task :tree_changes => :environment do + Item.all.each do |item| + item.position -= 1 + item.parent_ids = ancestors(item) + item.rename(:full_name, :path) + item.save + end + end + + def ancestors(item) + node, nodes = item, [] + nodes << node = node.parent while !node.parent.blank? rescue nil + nodes.reverse + end + +end \ No newline at end of file diff --git a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb index 39730de1..81b31b93 100644 --- a/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb +++ b/vendor/built_in_modules/page_content/app/views/panel/page_content/back_end/page_contexts/_page_context.html.erb @@ -1,7 +1,7 @@ <tr id="<%= dom_id page_context %>" class="with_action"> <td> - <%= page_context.page.full_name %> + <%= page_context.page.path %> <div class="quick-edit"> <ul class="nav nav-pills hide"> <li><%= link_to t('page_context.edit'), edit_panel_page_content_back_end_page_context_path(page_context) %></li>