Re-fix image rendering for help pages

This commit is contained in:
Robert Speicher 2015-04-15 12:45:31 -04:00
parent e24cb79f31
commit 3052e89420
4 changed files with 99 additions and 8 deletions

View file

@ -3,13 +3,36 @@ class HelpController < ApplicationController
end end
def show def show
@category = clean_path_info(params[:category]) category = clean_path_info(path_params[:category])
@file = clean_path_info(params[:file]) file = clean_path_info(path_params[:file])
if File.exists?(Rails.root.join('doc', @category, @file + '.md')) respond_to do |format|
render 'show' format.any(:markdown, :md, :html) do
path = Rails.root.join('doc', category, "#{file}.md")
if File.exist?(path)
@markdown = File.read(path)
render 'show.html.haml'
else else
not_found! # Force template to Haml
render 'errors/not_found.html.haml', layout: 'errors', status: 404
end
end
# Allow access to images in the doc folder
format.any(:png, :gif, :jpeg) do
path = Rails.root.join('doc', category, "#{file}.#{params[:format]}")
if File.exist?(path)
send_file(path, disposition: 'inline')
else
head :not_found
end
end
# Any other format we don't recognize, just respond 404
format.any { head :not_found }
end end
end end
@ -21,6 +44,13 @@ class HelpController < ApplicationController
private private
def path_params
params.require(:category)
params.require(:file)
params
end
PATH_SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact) PATH_SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)
# Taken from ActionDispatch::FileHandler # Taken from ActionDispatch::FileHandler

View file

@ -1,2 +1,2 @@
.documentation.wiki .documentation.wiki
= markdown File.read(Rails.root.join('doc', @category, @file + '.md')).gsub("$your_email", current_user.email) = markdown @markdown.gsub('$your_email', current_user.email)

View file

@ -0,0 +1,61 @@
require 'spec_helper'
describe HelpController do
let(:user) { create(:user) }
before do
sign_in(user)
end
describe 'GET #show' do
context 'for Markdown formats' do
context 'when requested file exists' do
before do
get :show, category: 'ssh', file: 'README', format: :md
end
it 'assigns to @markdown' do
expect(assigns[:markdown]).not_to be_empty
end
it 'renders HTML' do
expect(response).to render_template('show.html.haml')
expect(response.content_type).to eq 'text/html'
end
end
context 'when requested file is missing' do
it 'renders not found' do
get :show, category: 'foo', file: 'bar', format: :md
expect(response).to be_not_found
end
end
end
context 'for image formats' do
context 'when requested file exists' do
it 'renders the raw file' do
get :show, category: 'workflow/protected_branches',
file: 'protected_branches1', format: :png
expect(response).to be_success
expect(response.content_type).to eq 'image/png'
expect(response.headers['Content-Disposition']).to match(/^inline;/)
end
end
context 'when requested file is missing' do
it 'renders not found' do
get :show, category: 'foo', file: 'bar', format: :png
expect(response).to be_not_found
end
end
end
context 'for other formats' do
it 'always renders not found' do
get :show, category: 'ssh', file: 'README', format: :foo
expect(response).to be_not_found
end
end
end
end

View file

@ -6,7 +6,7 @@ describe 'Help Pages', feature: true do
login_as :user login_as :user
end end
it 'replace the variable $your_email with the email of the user' do it 'replace the variable $your_email with the email of the user' do
visit help_page_path(category: 'ssh', file: 'README.md') visit help_page_path('ssh', 'README')
expect(page).to have_content("ssh-keygen -t rsa -C \"#{@user.email}\"") expect(page).to have_content("ssh-keygen -t rsa -C \"#{@user.email}\"")
end end
end end