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