Sha256: 8d8acc6e9a0309e067a5690db79927bc83d3e576a0d895879090ebdabb491db5

Contents?: true

Size: 782 Bytes

Versions: 2

Compression:

Stored size: 782 Bytes

Contents

module Landable
  class Directory
    include ActiveModel::SerializerSupport

    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

    attr_reader :path, :subdirectories, :pages
    alias_attribute :id, :path

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
landable-1.14.0 app/models/landable/directory.rb
landable-1.13.2 app/models/landable/directory.rb