Skip to content
This repository
Browse code

Fix subclassed PagesController with custom layout

  • Loading branch information...
commit e4b1ce9dfc936b90ef33975ca73f4eaac2fd9a43 1 parent 7556fcd
Matthew Horan authored February 22, 2012
4  app/controllers/high_voltage/pages_controller.rb
... ...
@@ -1,7 +1,7 @@
1 1
 class HighVoltage::PagesController < ApplicationController
2 2
 
3 3
   unloadable
4  
-  layout HighVoltage::layout
  4
+  layout Proc.new { HighVoltage::layout }
5 5
 
6 6
   rescue_from ActionView::MissingTemplate do |exception|
7 7
     if exception.message =~ %r{Missing template #{HighVoltage::content_path}}
@@ -12,7 +12,7 @@ class HighVoltage::PagesController < ApplicationController
12 12
   end
13 13
 
14 14
   def show
15  
-    render :template => current_page, :layout => HighVoltage::layout
  15
+    render :template => current_page
16 16
   end
17 17
 
18 18
   protected
32  spec/controllers/subclassed_pages_controller_spec.rb
... ...
@@ -0,0 +1,32 @@
  1
+require 'spec_helper'
  2
+
  3
+describe SubclassedPagesController do
  4
+
  5
+  render_views
  6
+
  7
+  describe "on GET to /subclassed_pages/exists" do
  8
+    before { get :show, :id => 'exists' }
  9
+
  10
+    it "should respond with success and render template" do
  11
+      response.should be_success
  12
+      response.should render_template('exists')
  13
+    end
  14
+
  15
+    it "should use the custom configured layout" do
  16
+      response.should_not render_template("layouts/application")
  17
+      response.should render_template('layouts/alternate')
  18
+    end
  19
+  end
  20
+
  21
+  it "should raise a routing error for an invalid page" do
  22
+    lambda { get :show, :id => "invalid" }.should raise_error(ActionController::RoutingError)
  23
+  end
  24
+
  25
+  it "should raise a routing error for a page in another directory" do
  26
+    lambda { get :show, :id => "../other/wrong" }.should raise_error(ActionController::RoutingError)
  27
+  end
  28
+
  29
+  it "should raise missing template error for valid page with invalid partial" do
  30
+    lambda { get :show, :id => "exists_but_references_nonexistent_partial" }.should raise_error(ActionView::MissingTemplate)
  31
+  end
  32
+end
3  spec/dummy/app/controllers/subclassed_pages_controller.rb
... ...
@@ -0,0 +1,3 @@
  1
+class SubclassedPagesController < HighVoltage::PagesController
  2
+  layout 'alternate'
  3
+end
57  spec/dummy/config/routes.rb
... ...
@@ -1,58 +1,3 @@
1 1
 Dummy::Application.routes.draw do
2  
-  # The priority is based upon order of creation:
3  
-  # first created -> highest priority.
4  
-
5  
-  # Sample of regular route:
6  
-  #   match 'products/:id' => 'catalog#view'
7  
-  # Keep in mind you can assign values other than :controller and :action
8  
-
9  
-  # Sample of named route:
10  
-  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
11  
-  # This route can be invoked with purchase_url(:id => product.id)
12  
-
13  
-  # Sample resource route (maps HTTP verbs to controller actions automatically):
14  
-  #   resources :products
15  
-
16  
-  # Sample resource route with options:
17  
-  #   resources :products do
18  
-  #     member do
19  
-  #       get 'short'
20  
-  #       post 'toggle'
21  
-  #     end
22  
-  #
23  
-  #     collection do
24  
-  #       get 'sold'
25  
-  #     end
26  
-  #   end
27  
-
28  
-  # Sample resource route with sub-resources:
29  
-  #   resources :products do
30  
-  #     resources :comments, :sales
31  
-  #     resource :seller
32  
-  #   end
33  
-
34  
-  # Sample resource route with more complex sub-resources
35  
-  #   resources :products do
36  
-  #     resources :comments
37  
-  #     resources :sales do
38  
-  #       get 'recent', :on => :collection
39  
-  #     end
40  
-  #   end
41  
-
42  
-  # Sample resource route within a namespace:
43  
-  #   namespace :admin do
44  
-  #     # Directs /admin/products/* to Admin::ProductsController
45  
-  #     # (app/controllers/admin/products_controller.rb)
46  
-  #     resources :products
47  
-  #   end
48  
-
49  
-  # You can have the root of your site routed with "root"
50  
-  # just remember to delete public/index.html.
51  
-  # root :to => "welcome#index"
52  
-
53  
-  # See how all your routes lay out with "rake routes"
54  
-
55  
-  # This is a legacy wild controller route that's not recommended for RESTful applications.
56  
-  # Note: This route will make all actions in every controller accessible via GET requests.
57  
-  # match ':controller(/:action(/:id(.:format)))'
  2
+  match "/subclassed_pages/*id" => 'subclassed_pages#show', :format => false
58 3
 end

0 notes on commit e4b1ce9

Please sign in to comment.
Something went wrong with that request. Please try again.