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

Refactor WikiPage.group_by_directory

parent 083442bc
...@@ -13,17 +13,14 @@ class WikiPage ...@@ -13,17 +13,14 @@ class WikiPage
end end
def self.group_by_directory(pages) def self.group_by_directory(pages)
directories = {} return {} if pages.blank?
directories = { '/' => [] }
pages.each do |page| pages.each do |page|
if page.slug.include?('/') directory = page.wiki.page_title_and_dir(page.slug).last
# Directory hierarchy is given by matching from the beginning up to directory = '/' if directory.blank?
# the last forward slash. directories[directory] ||= []
directory = page.slug.match(/\A(.+)\//)[1] directories[directory] << page
directories[directory] = add_to_directory(directories[directory], page)
else
directories['root'] = add_to_directory(directories['root'], page)
end
end end
directories directories
......
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