Commit 62c5418f3b4a5b7a7d1d7e9979a746e278b79c5b

Authored by Antonio Terceiro
1 parent 51dcd76c

Rendering a "not found" message for missing docs

It will also happen when a user tries to browse the documentation but it
was not generated by the site administrator.
app/controllers/public/doc_controller.rb
... ... @@ -19,6 +19,11 @@ class DocController < PublicController
19 19 @topic = @section.find(params[:topic])
20 20 end
21 21  
  22 + rescue_from DocItem::NotFound, :with => :not_found
  23 + def not_found
  24 + render_not_found
  25 + end
  26 +
22 27 protected
23 28  
24 29 def load_toc
... ...
app/models/doc_item.rb
1 1 class DocItem
  2 + class NotFound < Exception; end
2 3 attr_accessor :id, :title, :text, :language
3 4 def initialize(attrs = {})
4 5 attrs.each do |name,value|
... ...
app/models/doc_section.rb
... ... @@ -20,7 +20,12 @@ class DocSection &lt; DocItem
20 20 if id.blank?
21 21 root(language)
22 22 else
23   - all(language, force).find {|item| item.id == id }
  23 + section = all(language, force).find {|item| item.id == id }
  24 + if section
  25 + section
  26 + else
  27 + raise DocItem::NotFound
  28 + end
24 29 end
25 30 end
26 31  
... ... @@ -50,7 +55,7 @@ class DocSection &lt; DocItem
50 55 [
51 56 File.join(dir, "#{id}#{language_suffix}.xhtml"),
52 57 File.join(dir, "#{id}.en.xhtml")
53   - ].find {|file| File.exist?(file) }
  58 + ].find {|file| File.exist?(file) } || raise(DocItem::NotFound)
54 59 end
55 60  
56 61 def load_items
... ...
app/models/doc_topic.rb
1 1 class DocTopic < DocItem
2 2 def self.loadfile(file)
  3 + if !File.exist?(file)
  4 + raise DocItem::NotFound
  5 + end
3 6 lines = File.readlines(file)
4 7 title = _find_title(lines)
5 8 File.basename(file) =~ /(.*)\.([^\.]+)\.xhtml$/
... ...
test/functional/doc_controller_test.rb
... ... @@ -40,5 +40,11 @@ class DocControllerTest &lt; ActionController::TestCase
40 40 assert_equal 'pt', assigns(:topic).language
41 41 end
42 42  
  43 + should 'bail out gracefully for unexisting sections or topics' do
  44 + assert_nothing_raised do
  45 + get :section, :section => 'something-very-unlikely'
  46 + get :section, :section => 'something-very-unlikely', :topic => 'other-thing-very-unlikely'
  47 + end
  48 + end
43 49  
44 50 end
... ...
test/unit/doc_section_test.rb
... ... @@ -91,8 +91,10 @@ class DocSectionTest &lt; ActiveSupport::TestCase
91 91 end
92 92 end
93 93  
94   - should 'not load null section (the root) for unexisting sections' do
95   - assert_nil DocSection.find('something-very-unlikely')
  94 + should 'raise DocItem::NotFound when loading unexisting section' do
  95 + assert_raise DocItem::NotFound do
  96 + DocSection.find('something-very-unlikely')
  97 + end
96 98 end
97 99  
98 100 end
... ...
test/unit/doc_topic_test.rb
... ... @@ -18,4 +18,11 @@ class DocTopicTest &lt; ActiveSupport::TestCase
18 18 assert_equal 'pt', doc.language
19 19 assert_equal 'Teste da documentação', doc.title
20 20 end
  21 +
  22 + should 'raise DocTopic::NotFound when trying to load an unexisting topic' do
  23 + assert_raise DocItem::NotFound do
  24 + DocTopic.loadfile('/path/to/unexisting/file.en.xhtml')
  25 + end
  26 + end
  27 +
21 28 end
... ...