From ec7d1359301b28a506d3c9e9ad5785cf84d3fcd9 Mon Sep 17 00:00:00 2001
From: Jean-Philippe Lang <jp_lang@yahoo.fr>
Date: Sat, 26 Jul 2008 12:54:54 +0000
Subject: [PATCH] Adds child_pages macro for wiki pages (#528).

git-svn-id: http://redmine.rubyforge.org/svn/trunk@1699 e93f8b46-1217-0410-a6f0-8f06a7374b81
---
 app/controllers/wiki_controller.rb                        | 5 ++++-
 app/helpers/application_helper.rb                         | 2 +-
 app/helpers/wiki_helper.rb                                | 2 +-
 app/views/common/_preview.rhtml                           | 2 +-
 lib/redmine/wiki_formatting/macros.rb                     | 6 ++++++
 test/fixtures/wiki_contents.yml                           | 2 +-
 test/functional/wiki_controller_test.rb                   | 8 +++++++-
 .../plugins/acts_as_tree/lib/active_record/acts/tree.rb   | 7 +++++++
 8 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb
index 2430205cf..5a5f3949f 100644
--- a/app/controllers/wiki_controller.rb
+++ b/app/controllers/wiki_controller.rb
@@ -165,7 +165,10 @@ class WikiController < ApplicationController
     page = @wiki.find_page(params[:page])
     # page is nil when previewing a new page
     return render_403 unless page.nil? || editable?(page)
-    @attachements = page.attachments if page
+    if page
+      @attachements = page.attachments
+      @previewed = page.content
+    end
     @text = params[:content][:text]
     render :partial => 'common/preview'
   end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index f5ce7ecda..6d6eb9107 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -206,7 +206,7 @@ module ApplicationHelper
     options = args.last.is_a?(Hash) ? args.pop : {}
     case args.size
     when 1
-      obj = nil
+      obj = options[:object]
       text = args.shift
     when 2
       obj = args.shift
diff --git a/app/helpers/wiki_helper.rb b/app/helpers/wiki_helper.rb
index d27ce3ea3..0a6b810de 100644
--- a/app/helpers/wiki_helper.rb
+++ b/app/helpers/wiki_helper.rb
@@ -24,7 +24,7 @@ module WikiHelper
       pages[node].each do |page|
         content << "<li>"
         content << link_to(h(page.pretty_title), {:action => 'index', :page => page.title},
-                           :title => l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)))
+                           :title => (page.respond_to?(:updated_on) ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
         content << "\n" + render_page_hierarchy(pages, page.id) if pages[page.id]
         content << "</li>\n"
       end
diff --git a/app/views/common/_preview.rhtml b/app/views/common/_preview.rhtml
index e3bfc3a25..fd95f1188 100644
--- a/app/views/common/_preview.rhtml
+++ b/app/views/common/_preview.rhtml
@@ -1,3 +1,3 @@
 <fieldset class="preview"><legend><%= l(:label_preview) %></legend>
-<%= textilizable @text, :attachments => @attachements %>
+<%= textilizable @text, :attachments => @attachements, :object => @previewed %>
 </fieldset>
diff --git a/lib/redmine/wiki_formatting/macros.rb b/lib/redmine/wiki_formatting/macros.rb
index 0848aee4e..adfc590e4 100644
--- a/lib/redmine/wiki_formatting/macros.rb
+++ b/lib/redmine/wiki_formatting/macros.rb
@@ -77,6 +77,12 @@ module Redmine
         content_tag('dl', out)
       end
       
+      desc "Displays a list of child pages."
+      macro :child_pages do |obj, args|
+        raise 'This macro applies to wiki pages only.' unless obj.is_a?(WikiContent)
+        render_page_hierarchy(obj.page.descendants.group_by(&:parent_id), obj.page.id)
+      end
+      
       desc "Include a wiki page. Example:\n\n  !{{include(Foo)}}\n\nor to include a page of a specific project wiki:\n\n  !{{include(projectname:Foo)}}"
       macro :include do |obj, args|
         project = @project
diff --git a/test/fixtures/wiki_contents.yml b/test/fixtures/wiki_contents.yml
index 5d6d3f1de..8c53d4d97 100644
--- a/test/fixtures/wiki_contents.yml
+++ b/test/fixtures/wiki_contents.yml
@@ -2,7 +2,7 @@
 wiki_contents_001: 
   text: |-
     h1. CookBook documentation

-    

+    {{child_pages}}

     Some updated [[documentation]] here with gzipped history
   updated_on: 2007-03-07 00:10:51 +01:00
   page_id: 1
diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb
index 06d8cacf2..bbfdc8e7f 100644
--- a/test/functional/wiki_controller_test.rb
+++ b/test/functional/wiki_controller_test.rb
@@ -32,10 +32,16 @@ class WikiControllerTest < Test::Unit::TestCase
   end
   
   def test_show_start_page
-    get :index, :id => 1
+    get :index, :id => 'ecookbook'
     assert_response :success
     assert_template 'show'
     assert_tag :tag => 'h1', :content => /CookBook documentation/
+
+    # child_pages macro
+    assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
+               :child => { :tag => 'li',
+                           :child => { :tag => 'a', :attributes => { :href => '/wiki/ecookbook/Page_with_an_inline_image' },
+                                                    :content => 'Page with an inline image' } }
   end
   
   def test_show_page_with_name
diff --git a/vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb b/vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb
index 1f00e90a9..6a6827ee6 100644
--- a/vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb
+++ b/vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb
@@ -70,6 +70,13 @@ module ActiveRecord
           nodes
         end
 
+        # Returns list of descendants.
+        #
+        #   root.descendants # => [child1, subchild1, subchild2]
+        def descendants
+          children + children.collect(&:children).flatten
+        end
+
         # Returns the root node of the tree.
         def root
           node = self
-- 
GitLab