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
c8a1e968
Commit
c8a1e968
authored
Dec 26, 2016
by
Alex Braha Stoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change WikiPage.group_by_directory to use WikiDirectory
parent
b361a67f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
34 deletions
+56
-34
app/models/wiki_page.rb
app/models/wiki_page.rb
+14
-4
spec/models/wiki_page_spec.rb
spec/models/wiki_page_spec.rb
+42
-30
No files found.
app/models/wiki_page.rb
View file @
c8a1e968
...
...
@@ -16,12 +16,22 @@ class WikiPage
#
# pages - an array of WikiPage objects.
#
# Returns a hash whose keys are directories and whose values are WikiPage
# arrays.
# Returns an array of WikiPage and WikiDirectory objects. The entries are
# sorted by alphabetical order (directories and pages inside each directory).
# Pages at the root level come before everything.
def
self
.
group_by_directory
(
pages
)
return
{}
if
pages
.
blank?
return
[]
if
pages
.
blank?
pages
.
sort_by
{
|
page
|
[
page
.
directory
,
page
.
slug
]
}.
group_by
{
|
page
|
page
.
directory
}
group_by
{
|
page
|
page
.
directory
}.
map
do
|
dir
,
pages
|
if
dir
==
'/'
pages
else
WikiDirectory
.
new
(
dir
,
pages
)
end
end
.
flatten
end
def
to_key
...
...
spec/models/wiki_page_spec.rb
View file @
c8a1e968
...
...
@@ -9,9 +9,9 @@ describe WikiPage, models: true do
describe
'.group_by_directory'
do
context
'when there are no pages'
do
it
'returns an empty
hash
'
do
expect
(
WikiPage
.
group_by_directory
(
nil
)).
to
eq
(
{}
)
expect
(
WikiPage
.
group_by_directory
([])).
to
eq
(
{}
)
it
'returns an empty
array
'
do
expect
(
WikiPage
.
group_by_directory
(
nil
)).
to
eq
(
[]
)
expect
(
WikiPage
.
group_by_directory
([])).
to
eq
(
[]
)
end
end
...
...
@@ -23,43 +23,47 @@ describe WikiPage, models: true do
create_page
(
'dir_2/page_4'
,
'content'
)
create_page
(
'page_1'
,
'content'
)
end
let
(
:page_1
)
{
wiki
.
find_page
(
'page_1'
)
}
let
(
:dir_1
)
do
WikiDirectory
.
new
(
'dir_1'
,
[
wiki
.
find_page
(
'dir_1/page_2'
)])
end
let
(
:dir_1_1
)
do
WikiDirectory
.
new
(
'dir_1/dir_1_1'
,
[
wiki
.
find_page
(
'dir_1/dir_1_1/page_3'
)])
end
let
(
:dir_2
)
do
pages
=
[
wiki
.
find_page
(
'dir_2/page_5'
),
wiki
.
find_page
(
'dir_2/page_4'
)]
WikiDirectory
.
new
(
'dir_2'
,
pages
)
end
it
'returns a hash in which keys are directories and values are their pages'
do
page_1
=
wiki
.
find_page
(
'page_1'
)
page_2
=
wiki
.
find_page
(
'dir_1/page_2'
)
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
=
{
'/'
=>
[
page_1
],
'/dir_1'
=>
[
page_2
],
'/dir_1/dir_1_1'
=>
[
page_3
],
'/dir_2'
=>
[
page_4
,
page_5
]
}
it
'returns an array with pages and directories'
do
expected_grouped_entries
=
[
page_1
,
dir_1
,
dir_1_1
,
dir_2
]
grouped_
pag
es
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
grouped_
entri
es
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
grouped_pages
.
each
do
|
dir
,
pages
|
expected_slugs
=
expected_grouped_pages
.
fetch
(
dir
).
map
(
&
:slug
)
slugs
=
pages
.
map
(
&
:slug
)
grouped_entries
.
each_with_index
do
|
page_or_dir
,
i
|
expected_page_or_dir
=
expected_grouped_entries
[
i
]
expected_slugs
=
get_slugs
(
expected_page_or_dir
)
slugs
=
get_slugs
(
page_or_dir
)
expect
(
slugs
).
to
match_array
(
expected_slugs
)
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'
]
it
'returns an array sorted by alphabetical position'
do
# Directories and pages within directories are sorted alphabetically.
# Pages at root come before everything.
expected_order
=
[
'page_1'
,
'dir_1/page_2'
,
'dir_1/dir_1_1/page_3'
,
'dir_2/page_4'
,
'dir_2/page_5'
]
grouped_
pag
es
=
WikiPage
.
group_by_directory
(
wiki
.
pages
)
grouped_
entri
es
=
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
)
actual_order
=
grouped_entries
.
map
do
|
page_or_dir
|
get_slugs
(
page_or_dir
)
end
.
flatten
expect
(
actual_order
).
to
eq
(
expected_order
)
end
end
end
...
...
@@ -336,4 +340,12 @@ describe WikiPage, models: true do
page
=
wiki
.
wiki
.
paged
(
title
)
wiki
.
wiki
.
delete_page
(
page
,
commit_details
)
end
def
get_slugs
(
page_or_dir
)
if
page_or_dir
.
is_a?
WikiPage
[
page_or_dir
.
slug
]
else
page_or_dir
.
pages
.
present?
?
page_or_dir
.
pages
.
map
(
&
:slug
)
:
[]
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