child_pages_with_depth1.patch

Alexander Oryol, 2012-04-27 11:17

Download (2.31 KB)

View differences:

lib/redmine/wiki_formatting/macros.rb
90 90
             "  !{{child_pages}} -- can be used from a wiki page only\n" +
91 91
             "  !{{child_pages(Foo)}} -- lists all children of page Foo\n" +
92 92
             "  !{{child_pages(Foo, parent=1)}} -- same as above with a link to page Foo"
93
             "  !{{child_pages(Foo, parent=1, depth=3)}} -- same as above with specified nesting level (default 2)"
93 94
      macro :child_pages do |obj, args|
94
        args, options = extract_macro_options(args, :parent)
95
        args, options = extract_macro_options(args, :parent, :depth)
95 96
        page = nil
96 97
        if args.size > 0
97 98
          page = Wiki.find_page(args.first.to_s, :project => @project)
......
101 102
          raise 'With no argument, this macro can be called from wiki pages only.'
102 103
        end
103 104
        raise 'Page not found' if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
104
        pages = ([page] + page.descendants).group_by(&:parent_id)
105

  
106
        pages = ([page] + page.descendants(options[:depth])).group_by(&:parent_id)
105 107
        render_page_hierarchy(pages, options[:parent] ? page.parent_id : page.id)
106 108
      end
107 109

  
vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb
73 73
        # Returns list of descendants.
74 74
        #
75 75
        #   root.descendants # => [child1, subchild1, subchild2]
76
        def descendants
77
          children + children.collect(&:children).flatten
76
        #   root.descendants(1) # => [child1]
77
        #
78
        #   Defaults: depth = 2 (old behavior)
79
        def descendants(depth=2)
80
          depth = 2 if depth.nil?
81
          depth = depth.to_i
82
          result = children.dup
83
          result += children.collect { |item| item.descendants(depth-1) }.flatten if depth > 1
84
          result
78 85
        end
79 86

  
80 87
        # Returns list of descendants and a reference to the current node.