class SourceController < ApplicationController
def index
@path = params[:path] || ""
@entries = SourceBrowser.list(@path)
@breadcrumbs = build_breadcrumbs(@path)
end
def show
@path = params[:path]
if SourceBrowser.file?(@path)
@content = SourceBrowser.read(@path)
@language = SourceBrowser.language_for(@path)
@breadcrumbs = build_breadcrumbs(@path)
else
@entries = SourceBrowser.list(@path)
if @entries.empty?
raise ActionController::RoutingError, "Not Found"
end
@breadcrumbs = build_breadcrumbs(@path)
render :index
end
rescue SourceBrowser::NotAllowed
raise ActionController::RoutingError, "Not Found"
end
private
def build_breadcrumbs(path)
return [] if path.blank?
parts = path.split("/")
parts.each_with_index.map do |part, i|
{
name: part,
path: parts[0..i].join("/"),
last: i == parts.length - 1
}
end
end
end