Skip to content

Commit

Permalink
Added semi-static pages via high-voltage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tammer Saleh committed Oct 20, 2009
1 parent 6e994e5 commit 376163b
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 1 deletion.
3 changes: 3 additions & 0 deletions gold/app/controllers/pages_controller.rb
@@ -0,0 +1,3 @@
class PagesController < HighVoltage::PagesController
skip_before_filter :require_user
end
2 changes: 2 additions & 0 deletions gold/app/helpers/pages_helper.rb
@@ -0,0 +1,2 @@
module PagesHelper
end
Empty file.
4 changes: 3 additions & 1 deletion gold/config/routes.rb
@@ -1,5 +1,7 @@
ActionController::Routing::Routes.draw do |map|
map.resource :user_session
map.resources :users
map.root :controller => :users, :action => :index
map.resources :pages, :controller => 'pages', :only => [:show]

map.root :controller => "pages", :action => "show", :id => "home"
end
15 changes: 15 additions & 0 deletions gold/test/functional/pages_controller_test.rb
@@ -0,0 +1,15 @@
require 'test_helper'

class PagesControllerTest < ActionController::TestCase
as_a_visitor do
%w(home).each do |page|
context "on GET to /pages/#{page}" do
setup { get :show, :id => page }

should_respond_with :success
should_render_template page
end
end
end
end

4 changes: 4 additions & 0 deletions gold/test/unit/helpers/pages_helper_test.rb
@@ -0,0 +1,4 @@
require 'test_helper'

class PagesHelperTest < ActionView::TestCase
end
20 changes: 20 additions & 0 deletions gold/vendor/plugins/high_voltage/MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2009 [name of plugin creator]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
75 changes: 75 additions & 0 deletions gold/vendor/plugins/high_voltage/README.markdown
@@ -0,0 +1,75 @@
High Voltage
============

Rails engine for static pages.

... but be careful. [Danger!](http://www.youtube.com/watch?v=HD5tnb2RBYg)

Static pages?
-------------

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

Installation
------------

script/plugin install git://github.com/thoughtbot/high_voltage.git

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")

Bam.

Override
--------

Most common reasons to override? Authentication, layouts.

Create a PagesController of your own:

script/generate controller pages

Then modify it to subclass from High Voltage, adding whatever you need:

class PagesController < HighVoltage::PagesController
before_filter :authenticate
layout "danger"
end

Don't forget to add the new route:

map.resources :pages,
:controller => 'pages',
:only => [:show]

Testing
-------

Just a suggestion, but you can test your pages using Shoulda pretty easily:

class PagesControllerTest < ActionController::TestCase
tests PagesController

%w(earn_money screencast about contact).each do |page|
context "on GET to /pages/#{page}" do
setup { get :show, :id => page }

should_respond_with :success
should_render_template page
end
end
end

Enjoy!

Copyright (c) 2009 Thoughtbot, released under the MIT license

23 changes: 23 additions & 0 deletions gold/vendor/plugins/high_voltage/Rakefile
@@ -0,0 +1,23 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the nothing_to_see_here_move_along plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the nothing_to_see_here_move_along plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'NothingToSeeHereMoveAlong'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
@@ -0,0 +1,26 @@
class HighVoltage::PagesController < ApplicationController
unloadable
before_filter :ensure_valid

def show
render :template => current_page
end

protected

def ensure_valid
unless template_exists?(current_page)
render :nothing => true, :status => 404 and return false
end
end

def current_page
"pages/#{params[:id].to_s.downcase}"
end

def template_exists?(path)
view_paths.find_template(path, response.template.template_format)
rescue ActionView::MissingTemplate
false
end
end
@@ -0,0 +1,5 @@
ActionController::Routing::Routes.draw do |map|
map.resources :pages,
:controller => 'high_voltage/pages',
:only => [:show]
end
2 changes: 2 additions & 0 deletions gold/vendor/plugins/high_voltage/init.rb
@@ -0,0 +1,2 @@
require 'high_voltage'

1 change: 1 addition & 0 deletions gold/vendor/plugins/high_voltage/install.rb
@@ -0,0 +1 @@
# Install hook code here
5 changes: 5 additions & 0 deletions gold/vendor/plugins/high_voltage/lib/high_voltage.rb
@@ -0,0 +1,5 @@
module HighVoltage
end

require 'high_voltage/extensions/routes'

@@ -0,0 +1,15 @@
if defined?(ActionController::Routing::RouteSet)
class ActionController::Routing::RouteSet
def load_routes_with_high_voltage!
lib_path = File.dirname(__FILE__)
high_voltage_routes = File.join(lib_path, *%w[.. .. .. config high_voltage_routes.rb])
unless configuration_files.include?(high_voltage_routes)
add_configuration_file(high_voltage_routes)
end
load_routes_without_high_voltage!
end

alias_method_chain :load_routes!, :high_voltage
end
end

@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :nothing_to_see_here_move_along do
# # Task goes here
# end
44 changes: 44 additions & 0 deletions gold/vendor/plugins/high_voltage/test/pages_controller_test.rb
@@ -0,0 +1,44 @@
require 'test_helper'

class ApplicationController < ActionController::Base
end

require "high_voltage/pages_controller"
require File.join(File.dirname(__FILE__), '..', 'config', 'high_voltage_routes')

class HighVoltage::PagesControllerTest < ActionController::TestCase

HighVoltage::PagesController.view_paths << "/tmp"

context "on GET to a page that exists" do
setup do
@filename = '/tmp/pages/exists.html.erb'

FileUtils.mkdir(File.dirname(@filename))
File.open(@filename, 'w') do |file|
file << "hello <%= 'world' %>"
end
assert File.exists?(@filename)

get :show, :id => 'exists'
end

should_respond_with :success
should_render_template 'exists'

teardown do
begin
FileUtils.rm(@filename)
FileUtils.rmdir(File.dirname(@filename))
rescue StandardError => e
puts "Error while removing files: #{e}"
end
end
end

context "on GET to /pages/invalid" do
setup { get :show, :id => "invalid" }
should_respond_with :missing
end

end
14 changes: 14 additions & 0 deletions gold/vendor/plugins/high_voltage/test/test_helper.rb
@@ -0,0 +1,14 @@
ENV['RAILS_ENV'] = 'test'

require 'rubygems'
require 'active_support'
require 'active_support/test_case'
require 'action_controller'
require 'test_help'
require 'high_voltage'

$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'app', 'controllers')
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'app', 'models')

require 'shoulda'
require 'shoulda/rails'
1 change: 1 addition & 0 deletions gold/vendor/plugins/high_voltage/uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit 376163b

Please sign in to comment.