Commit f25344e3 authored by Alex Braha Stoll's avatar Alex Braha Stoll

Change WikiPage.group_by_directory to order by directory and file alphabetical order

parent 7f914ec7
...@@ -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
......
...@@ -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
......
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