Prohlížeč zdrojového kódu
app/controllers/source_controller.rb
class SourceController < ApplicationController
before_action :load_tree
# Force HTML format regardless of the URL's file extension. Without this, Rails infers
# the format from the path segment (e.g. "application.js" → :js) and skips the layout.
# Combining defaults: { format: "html" } on the route with trailing_slash: true is not
# possible — the defaults option suppresses trailing slash generation in path helpers.
before_action { request.format = :html }
def show
@path = params[:path]
return if @path.nil?
unless SourceBrowser.file?(@path)
raise ActionController::RoutingError, "Not Found"
end
if SourceBrowser.binary?(@path)
send_file SourceBrowser.full_path(@path), disposition: :attachment
else
@content = SourceBrowser.read(@path)
@language = SourceBrowser.language_for(@path)
end
rescue SourceBrowser::NotAllowed
raise ActionController::RoutingError, "Not Found"
end
private
def load_tree
@tree = SourceBrowser.tree
end
end