Class: WikiDirectory
- Inherits:
-
Object
- Object
- WikiDirectory
- Includes:
- ActiveModel::Validations
- Defined in:
- app/models/wiki_directory.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#slug ⇒ Object
(also: #to_param)
Returns the value of attribute slug.
Class Method Summary collapse
-
.group_pages(pages) ⇒ Array<WikiPage, WikiDirectory>
Groups a list of wiki pages into a nested collection of WikiPage and WikiDirectory objects, preserving the order of the passed pages.
Instance Method Summary collapse
-
#initialize(slug, entries = []) ⇒ WikiDirectory
constructor
A new instance of WikiDirectory.
- #title ⇒ Object
-
#to_partial_path ⇒ Object
Relative path to the partial to be used when rendering collections of this object.
Constructor Details
#initialize(slug, entries = []) ⇒ WikiDirectory
Returns a new instance of WikiDirectory.
54 55 56 57 |
# File 'app/models/wiki_directory.rb', line 54 def initialize(slug, entries = []) @slug = slug @entries = entries end |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
6 7 8 |
# File 'app/models/wiki_directory.rb', line 6 def entries @entries end |
#slug ⇒ Object Also known as: to_param
Returns the value of attribute slug.
6 7 8 |
# File 'app/models/wiki_directory.rb', line 6 def slug @slug end |
Class Method Details
.group_pages(pages) ⇒ Array<WikiPage, WikiDirectory>
Groups a list of wiki pages into a nested collection of WikiPage and WikiDirectory objects, preserving the order of the passed pages.
Returns an array with all entries for the toplevel directory.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/wiki_directory.rb', line 19 def group_pages(pages) # Build a hash to map paths to created WikiDirectory objects, # and recursively create them for each level of the path. # For the toplevel directory we use '' as path, as that's what WikiPage#directory returns. directories = Hash.new do |_, path| directories[path] = new(path).tap do |directory| if path.present? parent = File.dirname(path) parent = '' if parent == '.' directories[parent].entries << directory directories[parent].entries.delete_if do |item| item.is_a?(WikiPage) && item.slug.casecmp?(directory.slug) end end end end pages.each do |page| next unless directory_for_page?(directories[page.directory], page) directories[page.directory].entries << page end directories[''].entries end |
Instance Method Details
#title ⇒ Object
59 60 61 |
# File 'app/models/wiki_directory.rb', line 59 def title WikiPage.unhyphenize(File.basename(slug)) end |
#to_partial_path ⇒ Object
Relative path to the partial to be used when rendering collections of this object.
65 66 67 |
# File 'app/models/wiki_directory.rb', line 65 def to_partial_path 'shared/wikis/wiki_directory' end |