Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

DRY up the Book finder for DocsController

  • Loading branch information...
commit 407ec9bcfc8bcec2a6346a6248dbd5f8b3bda01c 1 parent c114c3b
Bryan Goines bry4n authored
15 app/controllers/application_controller.rb
View
@@ -1,9 +1,24 @@
+PageNotFound = Class.new(Exception)
+
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :determine_os
+ rescue_from PageNotFound, :with => :page_not_found
+
# Mac, Windows, Linux are valid
def determine_os
@os = 'linux'
end
+
+ private
+
+ def page_not_found
+ render :file => not_found_template, :layout => false
+ end
+
+ def not_found_template
+ File.join(Rails.root, "public/404")
+ end
+
end
21 app/controllers/doc_controller.rb
View
@@ -1,8 +1,9 @@
class DocController < ApplicationController
layout "layout"
+ before_filter :book_resource, :only => [:index, :book, :book_section, :progit]
+
def index
- @book = Book.where(:code => 'en').first
@videos = VIDEOS
end
@@ -65,14 +66,11 @@ def man
end
def book
- lang = params[:lang] || 'en'
- @book = Book.where(:code => lang).first
end
def book_section
- lang = params[:lang]
- slug = params[:slug]
- @content = Book.where(:code => lang).first.sections.where(:slug => slug).first
+ @content = @book.sections.where(:slug => params[:slug]).first
+ raise PageNotFound unless @content
@related = @content.get_related(8)
end
@@ -106,10 +104,9 @@ def related_update
def progit
chapter = params[:chapter].to_i
section = params[:section].to_i
- lang = params[:lang] || 'en'
- book = Book.where(:code => lang).first
- chapter = book.chapters.where(:number => chapter).first
+ chapter = @book.chapters.where(:number => chapter).first
@content = chapter.sections.where(:number => section).first
+ raise PageNotFound unless @content
render 'book_section'
end
@@ -161,6 +158,12 @@ def ext
private
+ def book_resource
+ @book ||= Book.where(:code => (params[:lang] || "en")).first
+ raise PageNotFound unless @book
+ @book
+ end
+
def doc_for(filename, version = nil)
if version
doc_version = DocVersion.for_version filename, version
Please sign in to comment.
Something went wrong with that request. Please try again.