Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

high_voltage

Easily include static pages in your Rails app.

Subscribe to updates I use high_voltage


Statistics on high_voltage

Number of watchers on Github 2755
Number of open issues 10
Average time to close an issue 22 days
Main language Ruby
Average time to merge a PR 12 days
Open pull requests 14+
Closed pull requests 23+
Last commit over 1 year ago
Repo Created over 10 years ago
Repo Last Updated over 1 year ago
Size 829 KB
Homepage http://thoughtbot...
Organization / Authorthoughtbot
Latest Releasev2.4.0
Contributors45
Page Updated
Do you use high_voltage? Leave a review!
View open issues (10)
View high_voltage activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating high_voltage for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

High Voltage Build Status

Rails engine for static pages.

... but be careful. Danger!

Static pages?

Yeah, like About us, Directions, marketing pages, etc.

Installation

There are two ways to install High Voltage.

You can run:

$ gem install high_voltage

Or you can include in your Gemfile:

gem 'high_voltage', '~> 3.0.0'

Usage

Write your static pages and put them in the RAILS_ROOT/app/views/pages directory.

$ mkdir app/views/pages
$ touch app/views/pages/about.html.erb

After putting something interesting there, you can link to it from anywhere in your app with:

<%= link_to 'About', page_path('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.

You can also get a list of your static pages by calling HighVoltage.page_ids This might be useful if you need to build a sitemap. For example, if you are using the sitemap_generator gem, you could add something like this to your sitemap config file:

HighVoltage.page_ids.each do |page|
  add page, changefreq: 'monthly'
end

Configuration

Routing overview

By default, the static page routes will be like /pages/:id (where :id is the view filename).

If you want to route to a static page in another location (for example, a homepage), do this:

get 'pages/home' => 'high_voltage/pages#show', id: 'home'

In that case, you'd need an app/views/pages/home.html.erb file.

Generally speaking, you need to route to the 'show' action with an :id param of the view filename.

High Voltage will generate a named route method of page_path. If you want to generate your own named route (with the :as routing option), make sure not to use :page as it will conflict with the High Voltage named route.

Specifying a root path

You can configure the root route to a High Voltage page like this:

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.home_page = 'home'
end

Which will render the page from app/views/pages/home.html.erb when the '/' route of the site is accessed.

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

You can remove the directory pages from the URL path and serve up routes from the root of the domain path:

http://www.example.com/about
http://www.example.com/company

Would look for corresponding files:

app/views/pages/about.html.erb
app/views/pages/company.html.erb

This is accomplished by setting the route_drawer to HighVoltage::RouteDrawers::Root

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.route_drawer = HighVoltage::RouteDrawers::Root
end

Disabling routes

The default routes can be completely removed by setting the routes to false:

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.routes = false
end

Specifying Rails engine for routes

If you are using multiple Rails engines within your application, you can specify which engine to define the default HighVoltage routes on.

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.parent_engine = MyEngine
end

Page titles and meta-data

We suggest using content_for and yield for setting custom page titles and meta-data on High Voltage pages.

# app/views/pages/about.html.erb
<% content_for :page_title, 'About Us - Custom page title' %>

Then print the contents of :title into the layout:

# app/views/layouts/application.html.erb
<title><%= yield(:page_title) %></title>

Content path

High Voltage uses a default path and folder of 'pages', i.e. 'url.com/pages/contact', 'app/views/pages'.

You can change this in an initializer:

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.content_path = 'site/'
end

Caching

Built in caching support has been removed in HighVoltage. See PR 221.

Page caching and action caching can be done via Rails. Visit the Caching with Rails: An overview guide for more details. You can utilize the methods described there by overriding the HighVoltage controller as described below.

Override

Most common reasons to override?

  • You need authentication around the pages to make sure a user is signed in.
  • You need to render different layouts for different pages.
  • You need to render a partial from the app/views/pages directory.

Create a PagesController of your own:

$ rails generate controller pages

Disable the default routes:

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.routes = false
end

Define a route for the new PagesController:

# config/routes.rb
get "/pages/*id" => 'pages#show', as: :page, format: false

# if routing the root path, update for your controller
root to: 'pages#show', id: 'home'

Then modify new PagesController to include the High Voltage static page concern:

# app/controllers/pages_controller.rb
class PagesController < ApplicationController
  include HighVoltage::StaticPage

  before_filter :authenticate
  layout :layout_for_page

  private

  def layout_for_page
    case params[:id]
    when 'home'
      'home'
    else
      'application'
    end
  end
end

To set up a different layout for all High Voltage static pages, use an initializer:

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.layout = 'your_layout'
end

Custom finding

You can further control the algorithm used to find pages by overriding the page_finder_factory method:

# app/controllers/pages_controller.rb
class PagesController < ApplicationController
  include HighVoltage::StaticPage

  private

  def page_finder_factory
    Rot13PageFinder
  end
end

The easiest thing is to subclass HighVoltage::PageFinder, which provides you with page_id:

class Rot13PageFinder < HighVoltage::PageFinder
  def find
    paths = super.split('/')
    directory = paths[0..-2]
    filename = paths[-1].tr('a-z','n-za-m')

    File.join(*directory, filename)
  end
end

Use this to create a custom file mapping, clean filenames for your file system, A/B test, and so on.

Localization

Rails I18n guides.

Add a before filter to the Application controller

# app/controllers/application_controller.rb
before_action :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

Disable the default High Voltage routes

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.routes = false
end
# config/routes.rb
scope "/:locale", locale: /en|bn|hi/ do
  get "/pages/:id" => "high_voltage/pages#show", :as => :page, :format => false
end

Add a static page to the project

# app/views/pages/about.html.erb
<%= t "hello" %>

Make sure that there are corresponding locale files

/config/locale/bn.yml
/config/locale/en.yml
/config/locale/hi.yml

One last note is there is a known issue with High Voltage.

You'll need to specify routes like this <%= link_to "About Us", page_path(id: "about") %>

Testing

You can test your static pages using RSpec and shoulda-matchers:

# spec/controllers/pages_controller_spec.rb
describe PagesController, '#show' do
  %w(earn_money screencast about contact).each do |page|
    context "on GET to /pages/#{page}" do
      before do
        get :show, id: page
      end

      it { should respond_with(:success) }
      it { should render_template(page) }
    end
  end
end

If you're not using a custom PagesController be sure to test HighVoltage::PagesController instead.

Enjoy!

Contributing

Please see CONTRIBUTING.md. Thank you, contributors!

License

High Voltage is copyright 2009-2017 thoughtbot. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About thoughtbot

thoughtbot

High Voltage is maintained and funded by thoughtbot, inc. The names and logos for thoughtbot are trademarks of thoughtbot, inc.

We love open source software! See our other projects. We are available for hire.

high_voltage open issues Ask a question     (View All Issues)
  • almost 3 years can't handle Byte Order Mark (BOM) in the url
  • about 3 years Bare .slim extension not recognized with root route drawer
  • over 3 years High_voltage and Pundit, only auth some pages?
  • over 3 years PagesController: current_page as helper?
  • almost 4 years Bare .haml extension not recognised by HighVoltage.page_ids
  • almost 5 years Error browsing to page with HighVoltage used in an isolated engine
high_voltage open pull requests (View All Pulls)
  • Update readme.md
  • Let travis automatically test against latest patch
  • Test against Rails 5
  • Handle URL with explicit .html format on root routes
  • Rework PageFinder restrictions
  • Fix #211: include non-html filenames in page_ids
  • Test against Rails 5 release instead of Beta
  • switch erb tag to non-output variety
  • Allow Different extension types for views in HighVoltage::Constraints
  • Make current_page available in rendered view
  • Little typo
  • fixed haml extensions not recognized by HighVoltage.page_ids
  • Update README.md to provide clarity.
  • [CI] Test against Ruby 2.5
high_voltage questions on Stackoverflow (View All Questions)
  • High_Voltage, restricting specific pages to logged in users
  • disable any layout for static pages with high_voltage
  • Is there a way to get all the pages used by thoughtbot/high_voltage gem?
  • Rails: how to add active class to high_voltage pages in navigation
  • Using thoughtbot/high_voltage to serve static content
  • Can the high_voltage gem handle .haml.html files?
  • How to add current high_voltage page name as a body class?
  • How can I configure 'high_voltage' gem to serve up static images?
  • high_voltage gem not working for URLs not beginning with /pages
  • Creating folder structure using high_voltage gem in Rails
  • Routing issue while using high_voltage for static pages
high_voltage list of languages used
high_voltage latest release notes
v2.4.0 Version 2.4.0

New

  • Retrieve a list of available static pages via HighVoltage.page_ids. [https://github.com/thoughtbot/high_voltage/pull/193]

Improvements

  • Properly handle page ids which are blank when being sanitized. [https://github.com/thoughtbot/high_voltage/pull/206]
  • Only provide deprecation warnings around caching when caching is enabled. [https://github.com/thoughtbot/high_voltage/pull/199]
Other projects in Ruby