Permalink
Browse files

Home page configuration option

Allow users to specify a home page and have High Voltage take care of
the root route and redirect to avoid duplicate content.

* Add configuration option for home_page.
* Add new root route and redirect.
  • Loading branch information...
1 parent f6a2e37 commit 4257abe38e75b66eefdfbeda80771cd7bd0301c4 @harlow harlow committed Dec 27, 2013
Showing with 47 additions and 11 deletions.
  1. +9 −11 README.md
  2. +5 −0 config/routes.rb
  3. +2 −0 lib/high_voltage/configuration.rb
  4. +13 −0 spec/requests/home_page_spec.rb
  5. +18 −0 spec/routing/routes_spec.rb
View
@@ -65,22 +65,20 @@ as it will conflict with the High Voltage named route.
#### Specifying a root path
-You can route the root route to a High Voltage page like this:
+You can configure the root route to a High Voltage page like this:
```ruby
-get '/home', to: redirect('/')
-
-root to: 'high_voltage/pages#show', id: 'home'
+# config/initializers/high_voltage.rb
+HighVoltage.configure do |config|
+ config.home_page = 'home'
+end
```
-Which will render a homepage from `app/views/pages/home.html.erb`
-
-We use `get '/home', to: redirect('/')` to prevent High Voltage from serving up
-duplicate content.
+Which will render the page from `app/views/pages/home.html.erb` when the '/'
+route of the site is accessed.
-It creates a search engine friendly 301 redirect. Without this line, High Voltage
-would render the same page on two different paths, as the root route ('/') and a
-home page ('pages/home').
+Note: High Voltage also creates a search engine friendly 301 redirect. Any attempt to
+access the path '/home' will be redirected to '/'.
#### Top-level routes
View
@@ -1,4 +1,9 @@
Rails.application.routes.draw do
+ if HighVoltage.home_page
+ get "/#{HighVoltage.home_page}", to: redirect('/')
+ root to: 'high_voltage/pages#show', id: HighVoltage.home_page
+ end
+
if HighVoltage.routes
get HighVoltage.route_drawer.match_attributes
end
@@ -4,6 +4,7 @@ module Configuration
:action_caching,
:action_caching_layout,
:content_path,
+ :home_page,
:layout,
:page_caching,
:route_drawer,
@@ -22,6 +23,7 @@ def set_default_configuration
self.action_caching = false
self.action_caching_layout = true
self.content_path = 'pages/'
+ self.home_page = nil
self.layout = 'application'
self.page_caching = false
self.route_drawer = HighVoltage::RouteDrawers::Default
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe 'Home page route' do
+ before(:each) do
+ HighVoltage.home_page = 'home'
+ Rails.application.reload_routes!
+ end
+
+ it 'redirects the duplicate content to root' do
+ get '/home'
+ expect(response).to redirect_to('/')
+ end
+end
@@ -107,6 +107,24 @@
end
end
+ context 'home page route' do
+ before(:each) do
+ HighVoltage.home_page = 'home'
+ Rails.application.reload_routes!
+ end
+
+ it 'recognizes the root route' do
+ assert_recognizes(
+ {
+ :controller => 'high_voltage/pages',
+ :action => 'show',
+ :id => 'home'
+ },
+ '/'
+ )
+ end
+ end
+
context 'disabled routes' do
before(:each) do
HighVoltage.routes = false

0 comments on commit 4257abe

Please sign in to comment.