Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
f25344e3
Commit
f25344e3
authored
Dec 19, 2016
by
Alex Braha Stoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change WikiPage.group_by_directory to order by directory and file alphabetical order
parent
7f914ec7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
5 deletions
+54
-5
app/models/wiki_page.rb
app/models/wiki_page.rb
+28
-1
spec/models/wiki_page_spec.rb
spec/models/wiki_page_spec.rb
+26
-4
No files found.
app/models/wiki_page.rb
View file @
f25344e3
...
@@ -12,10 +12,17 @@ class WikiPage
...
@@ -12,10 +12,17 @@ class WikiPage
ActiveModel
::
Name
.
new
(
self
,
nil
,
'wiki'
)
ActiveModel
::
Name
.
new
(
self
,
nil
,
'wiki'
)
end
end
# Sorts and groups pages by directory.
#
# pages - an array of WikiPage objects.
#
# Returns a hash whose keys are directories and whose values are WikiPage
# arrays. See WikiPage.sort_by_directory for more info about the ordering.
def
self
.
group_by_directory
(
pages
)
def
self
.
group_by_directory
(
pages
)
return
{}
if
pages
.
blank?
return
{}
if
pages
.
blank?
directories
=
{
'/'
=>
[]
}
pages
=
sort_by_directory
(
pages
)
directories
=
{}
pages
.
each
do
|
page
|
pages
.
each
do
|
page
|
directories
[
page
.
directory
]
||=
[]
directories
[
page
.
directory
]
||=
[]
directories
[
page
.
directory
]
<<
page
directories
[
page
.
directory
]
<<
page
...
@@ -199,6 +206,26 @@ class WikiPage
...
@@ -199,6 +206,26 @@ class WikiPage
private
private
# Sorts an array of pages by directory and file alphabetical order.
# Pages at the root directory will come first. The next pages will be
# sorted by their directories. Within directories, pages are sorted by
# filename alphabetical order. Pages are sorted in such a fashion that
# nested directories will always follow their parents (e.g. pages in
# dir_1/nested_dir_1 will follow pages inside dir_1).
#
# pages - an array of WikiPage objects.
#
# Returns a sorted array of WikiPage objects.
def
self
.
sort_by_directory
(
pages
)
pages
.
sort
do
|
page
,
next_page
|
if
page
.
directory
==
next_page
.
directory
page
.
slug
<=>
next_page
.
slug
else
page
.
directory
<=>
next_page
.
directory
end
end
end
def
set_attributes
def
set_attributes
attributes
[
:slug
]
=
@page
.
url_path
attributes
[
:slug
]
=
@page
.
url_path
attributes
[
:title
]
=
@page
.
title
attributes
[
:title
]
=
@page
.
title
...
...
spec/models/wiki_page_spec.rb
View file @
f25344e3
...
@@ -17,17 +17,22 @@ describe WikiPage, models: true do
...
@@ -17,17 +17,22 @@ describe WikiPage, models: true do
context
'when there are pages'
do
context
'when there are pages'
do
before
do
before
do
create_page
(
'
page_1
'
,
'content'
)
create_page
(
'
dir_1/dir_1_1/page_3
'
,
'content'
)
create_page
(
'dir_1/page_2'
,
'content'
)
create_page
(
'dir_1/page_2'
,
'content'
)
create_page
(
'dir_1/dir_2/page_3'
,
'content'
)
create_page
(
'dir_2/page_5'
,
'content'
)
create_page
(
'dir_2/page_4'
,
'content'
)
create_page
(
'page_1'
,
'content'
)
end
end
it
'returns a hash in which keys are directories and values are their pages'
do
it
'returns a hash in which keys are directories and values are their pages'
do
page_1
=
wiki
.
find_page
(
'page_1'
)
page_1
=
wiki
.
find_page
(
'page_1'
)
page_2
=
wiki
.
find_page
(
'dir_1/page_2'
)
page_2
=
wiki
.
find_page
(
'dir_1/page_2'
)
page_3
=
wiki
.
find_page
(
'dir_1/dir_2/page_3'
)
page_3
=
wiki
.
find_page
(
'dir_1/dir_1_1/page_3'
)
page_4
=
wiki
.
find_page
(
'dir_2/page_4'
)
page_5
=
wiki
.
find_page
(
'dir_2/page_5'
)
expected_grouped_pages
=
{
expected_grouped_pages
=
{
'/'
=>
[
page_1
],
'/dir_1'
=>
[
page_2
],
'/dir_1/dir_2'
=>
[
page_3
]
'/'
=>
[
page_1
],
'/dir_1'
=>
[
page_2
],
'/dir_1/dir_1_1'
=>
[
page_3
],
'/dir_2'
=>
[
page_4
,
page_5
]
}
}
grouped_pages
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
grouped_pages
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
...
@@ -39,6 +44,23 @@ describe WikiPage, models: true do
...
@@ -39,6 +44,23 @@ describe WikiPage, models: true do
expect
(
slugs
).
to
match_array
(
expected_slugs
)
expect
(
slugs
).
to
match_array
(
expected_slugs
)
end
end
end
end
it
'returns a hash in which keys (directories) are sorted by alphabetical position'
do
expected_ordered_directories
=
[
'/'
,
'/dir_1'
,
'/dir_1/dir_1_1'
,
'/dir_2'
]
grouped_pages
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
expect
(
grouped_pages
.
keys
).
to
eq
(
expected_ordered_directories
)
end
it
'returns a hash in which values (pages) are sorted by alphabetical position'
do
expected_ordered_page_slugs
=
[
'dir_2/page_4'
,
'dir_2/page_5'
]
grouped_pages
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
dir_2_page_slugs
=
grouped_pages
.
fetch
(
'/dir_2'
).
map
(
&
:slug
)
expect
(
dir_2_page_slugs
).
to
eq
(
expected_ordered_page_slugs
)
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment