Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add ability to use nested pages

  • Loading branch information...
commit 58057434c675b38c15bf46fc13f3100974593bd6 1 parent d42f21b
@mjankowski mjankowski authored
View
4 README.md
@@ -38,6 +38,10 @@ This will also work, if you like the more explicit style:
link_to "About", page_path(:id => "about")
+You can nest pages in a directory structure, if that makes sense from a URL perspective for you:
+
+ link_to "Q4 Reports", page_path("about/corporate/policies/HR/en_US/biz/sales/Quarter-Four")
+
Bam.
Routes
View
7 app/controllers/high_voltage/pages_controller.rb
@@ -17,7 +17,12 @@ def show
protected
def current_page
- "pages/#{params[:id].to_s.downcase}"
+ "pages/#{clean_path}"
+ end
+
+ def clean_path
+ path = Pathname.new "/#{params[:id]}"
+ path.cleanpath.to_s[1..-1]
end
end
View
2  config/routes.rb
@@ -1,3 +1,3 @@
Rails.application.routes.draw do
- resources :pages, :controller => 'high_voltage/pages', :only => :show
+ match '/pages/*id' => 'high_voltage/pages#show', :as => :page
end
View
1  test/dummy/app/views/other/wrong.html.erb
@@ -0,0 +1 @@
+cant get here from pages
View
1  test/dummy/app/views/pages/dir/nested.html.erb
@@ -0,0 +1 @@
+hello <%= 'world' %> from a nested dir
View
15 test/pages_controller_test.rb
@@ -11,12 +11,27 @@ class HighVoltage::PagesControllerTest < ActionController::TestCase
should render_template('exists')
end
+ context "on GET to /pages/dir/nested" do
+ setup do
+ get :show, :id => 'dir/nested'
+ end
+
+ should respond_with(:success)
+ should render_template('pages/dir/nested')
+ end
+
should "raise a routing error for an invalid page" do
assert_raise ActionController::RoutingError do
get :show, :id => "invalid"
end
end
+ should "raise a routing error for a page in another directory" do
+ assert_raise ActionController::RoutingError do
+ get :show, :id => "../other/wrong"
+ end
+ end
+
should "raise missing template error for valid page with invalid partial" do
assert_raise ActionView::MissingTemplate do
get :show, :id => "exists_but_references_nonexistent_partial"
View
31 test/routes_test.rb
@@ -0,0 +1,31 @@
+require 'test_helper'
+
+class RoutesTest < ActionController::TestCase
+
+ include Rails.application.routes.url_helpers
+
+ test "generates normal resource route with id" do
+ assert_equal "/pages/one", page_path(:id => "one")
+ end
+
+ test "generates normal resource route with string" do
+ assert_equal "/pages/one", page_path("one")
+ end
+
+ test "generates nested route with string" do
+ assert_equal "/pages/one/two", page_path("one/two")
+ end
+
+ test "recognizes nested route" do
+ assert_recognizes({ :controller => "high_voltage/pages", :action => "show", :id => "one/two" }, "/pages/one/two")
+ end
+
+ test "recognizes normal route" do
+ assert_recognizes({ :controller => "high_voltage/pages", :action => "show", :id => "one" }, "/pages/one")
+ end
+
+ test "recognizes normal route with dots" do
+ assert_recognizes({ :controller => "high_voltage/pages", :action => "show", :id => "one.two.three" }, "/pages/one.two.three")
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.