Skip to content

Commit

Permalink
Allow content_path override for PagesController subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Feb 23, 2012
1 parent d4ba056 commit 8884f70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
8 changes: 6 additions & 2 deletions app/controllers/high_voltage/pages_controller.rb
Expand Up @@ -4,7 +4,7 @@ class HighVoltage::PagesController < ApplicationController
layout Proc.new { HighVoltage::layout }

rescue_from ActionView::MissingTemplate do |exception|
if exception.message =~ %r{Missing template #{HighVoltage::content_path}}
if exception.message =~ %r{Missing template #{content_path}}
raise ActionController::RoutingError, "No such page: #{params[:id]}"
else
raise exception
Expand All @@ -18,12 +18,16 @@ def show
protected

def current_page
"#{HighVoltage::content_path}#{clean_path}"
"#{content_path}#{clean_path}"
end

def clean_path
path = Pathname.new "/#{params[:id]}"
path.cleanpath.to_s[1..-1]
end

def content_path
HighVoltage::content_path
end

end
17 changes: 13 additions & 4 deletions spec/controllers/subclassed_pages_controller_spec.rb
Expand Up @@ -4,12 +4,12 @@

render_views

describe "on GET to /subclassed_pages/exists" do
before { get :show, :id => 'exists' }
describe "on GET to /subclassed_pages/also_exists" do
before { get :show, :id => 'also_exists' }

it "should respond with success and render template" do
response.should be_success
response.should render_template('exists')
response.should render_template('also_exists')
end

it "should use the custom configured layout" do
Expand All @@ -18,6 +18,15 @@
end
end

describe "on GET to /subclassed_pages/also_dir/nested" do
before { get :show, :id => 'also_dir/also_nested' }

it "should respond with success and render template" do
response.should be_success
response.should render_template('other_pages/also_dir/also_nested')
end
end

it "should raise a routing error for an invalid page" do
lambda { get :show, :id => "invalid" }.should raise_error(ActionController::RoutingError)
end
Expand All @@ -27,6 +36,6 @@
end

it "should raise missing template error for valid page with invalid partial" do
lambda { get :show, :id => "exists_but_references_nonexistent_partial" }.should raise_error(ActionView::MissingTemplate)
lambda { get :show, :id => "also_exists_but_references_nonexistent_partial" }.should raise_error(ActionView::MissingTemplate)
end
end
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/subclassed_pages_controller.rb
@@ -1,3 +1,7 @@
class SubclassedPagesController < HighVoltage::PagesController
layout 'alternate'

def content_path
'other_pages/'
end
end

0 comments on commit 8884f70

Please sign in to comment.