Class: Landable::Directory

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::SerializerSupport
Defined in:
app/models/landable/directory.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, subdirectories = [], pages = []) ⇒ Directory

Returns a new instance of Directory.



17
18
19
20
21
# File 'app/models/landable/directory.rb', line 17

def initialize(path, subdirectories = [], pages = [])
  @path = path.squeeze '/'
  @subdirectories = subdirectories
  @pages = pages
end

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



14
15
16
# File 'app/models/landable/directory.rb', line 14

def pages
  @pages
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'app/models/landable/directory.rb', line 14

def path
  @path
end

#subdirectoriesObject (readonly)

Returns the value of attribute subdirectories.



14
15
16
# File 'app/models/landable/directory.rb', line 14

def subdirectories
  @subdirectories
end

Class Method Details

.listing(parent) ⇒ Object



5
6
7
8
9
10
11
12
# File 'app/models/landable/directory.rb', line 5

def self.listing(parent)
  parent_with_slash = parent.gsub(%r{^(.*?)\/?$}, '\1/')
  pages   = Page.where('path LIKE ?', "#{parent_with_slash}%").to_a
  subdirs = pages.group_by { |page| page.directory_after(parent_with_slash) }
  notdirs = subdirs.delete(nil) || []
  subdirs = subdirs.map { |name, _contents| Directory.new("#{parent}/#{name}") }
  Directory.new(parent, subdirs.sort_by(&:path), notdirs.sort_by(&:path))
end