Sha256: 58abeb15bf1a3f90292874c7250d77a729d9a2ee8d444721a22c117a2db186c3

Contents?: true

Size: 779 Bytes

Versions: 16

Compression:

Stored size: 779 Bytes

Contents

module Landable
  class Directory
    include ActiveModel::SerializerSupport

    def self.listing(parent)
      parent_with_slash = parent.gsub(/^(.*?)\/?$/, '\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

16 entries across 16 versions & 1 rubygems

Version Path
landable-1.13.1 app/models/landable/directory.rb
landable-1.12.3 app/models/landable/directory.rb
landable-1.12.2 app/models/landable/directory.rb
landable-1.12.1 app/models/landable/directory.rb
landable-1.11.1 app/models/landable/directory.rb
landable-1.11.0 app/models/landable/directory.rb
landable-1.10.0.rc2 app/models/landable/directory.rb
landable-1.10.0.rc1 app/models/landable/directory.rb
landable-1.9.2 app/models/landable/directory.rb
landable-1.9.1 app/models/landable/directory.rb
landable-1.9.0 app/models/landable/directory.rb
landable-1.9.0.rc2 app/models/landable/directory.rb
landable-1.9.0.rc1 app/models/landable/directory.rb
landable-1.8.0 app/models/landable/directory.rb
landable-1.7.1.rc1 app/models/landable/directory.rb
landable-1.7.0 app/models/landable/directory.rb