Commit 8bf52a4a authored by Alex Braha Stoll's avatar Alex Braha Stoll

Show directory hierarchy when listing wiki pages

parent 8dc2163c
......@@ -8,6 +8,7 @@ class Projects::WikisController < Projects::ApplicationController
def pages
@wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page])
@wiki_directories = WikiPage.group_by_directory(@wiki_pages)
end
def show
......@@ -116,7 +117,7 @@ class Projects::WikisController < Projects::ApplicationController
# Call #wiki to make sure the Wiki Repo is initialized
@project_wiki.wiki
@sidebar_wiki_pages = @project_wiki.pages.first(15)
@sidebar_wiki_directories = WikiPage.group_by_directory(@project_wiki.pages.first(15))
rescue ProjectWiki::CouldNotCreateWikiError
flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
redirect_to project_path(@project)
......
......@@ -12,6 +12,23 @@ class WikiPage
ActiveModel::Name.new(self, nil, 'wiki')
end
def self.group_by_directory(pages)
directories = {}
pages.each do |page|
if page.slug.include?('/')
# Directory hierarchy is given by matching from the beginning up to
# the last forward slash.
directory = page.slug.match(/\A(.+)\//)[1]
directories[directory] = add_to_directory(directories[directory], page)
else
directories['root'] = add_to_directory(directories['root'], page)
end
end
directories
end
def to_key
[:slug]
end
......@@ -176,6 +193,14 @@ class WikiPage
private
def self.add_to_directory(directory, page)
if directory.present?
directory << page
else
[page]
end
end
def set_attributes
attributes[:slug] = @page.url_path
attributes[:title] = @page.title
......
......@@ -12,7 +12,11 @@
.blocks-container
.block.block-first
%ul.wiki-pages
- @sidebar_wiki_pages.each do |wiki_page|
- @sidebar_wiki_directories.each do |wiki_directory, wiki_pages|
%li
= wiki_directory
%ul
- wiki_pages.each do |wiki_page|
%li{ class: params[:id] == wiki_page.slug ? 'active' : '' }
= link_to namespace_project_wiki_path(@project.namespace, @project, wiki_page) do
= wiki_page.title.capitalize
......
......@@ -14,7 +14,11 @@
Clone repository
%ul.content-list
- @wiki_pages.each do |wiki_page|
- @wiki_directories.each do |wiki_directory, wiki_pages|
%li
= wiki_directory
%ul
- wiki_pages.each do |wiki_page|
%li
= link_to wiki_page.title, namespace_project_wiki_path(@project.namespace, @project, wiki_page)
%small (#{wiki_page.format})
......
---
title: Show directory hierarchy when listing wiki pages
merge_request:
author: Alex Braha Stoll
......@@ -7,6 +7,23 @@ describe WikiPage, models: true do
subject { WikiPage.new(wiki) }
describe '::group_by_directory' do
context 'when there are no pages' do
it 'returns an empty hash' do
end
end
context 'when there are pages' do
let!(:page_1) { create_page('page_1', 'content') }
let!(:page_2) { create_page('directory/page_2', 'content') }
let(:pages) { [page_1, page_2] }
xit 'returns a hash in which keys are directories and values are their pages' do
expected_grouped_pages = { 'root' => [page_1], 'directory' => [page_2] }
end
end
end
describe "#initialize" do
context "when initialized with an existing gollum page" do
before do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment