Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract an overridable PageFinder class
- Loading branch information
1 parent
6eee9b0
commit 8494e51
Showing
13 changed files
with
166 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module HighVoltage | ||
# A command for finding pages by id. This encapsulates the concepts of | ||
# mapping page names to file names. | ||
class PageFinder | ||
VALID_CHARACTERS = "a-zA-Z0-9~!@$%^&*()#`_+-=<>\"{}|[];',?".freeze | ||
|
||
def initialize(page_id) | ||
@page_id = page_id | ||
end | ||
|
||
# Produce a template path to the page, in a format understood by | ||
# `render :template => find` | ||
def find | ||
"#{content_path}#{clean_path}" | ||
end | ||
|
||
def content_path | ||
HighVoltage.content_path | ||
end | ||
|
||
protected | ||
|
||
# The raw page id passed in by the user | ||
attr_reader :page_id | ||
|
||
private | ||
|
||
def clean_path | ||
path = Pathname.new("/#{clean_id}") | ||
path.cleanpath.to_s[1..-1] | ||
end | ||
|
||
def clean_id | ||
@page_id.tr("^#{VALID_CHARACTERS}", '') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'spec_helper' | ||
|
||
describe AlternativeFinderController do | ||
|
||
render_views | ||
|
||
it 'renders the file from the alternative directory' do | ||
get :show, :id => 'ebg13' | ||
|
||
response.should be_success | ||
response.should render_template('rot13') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
spec/dummy/app/controllers/alternative_finder_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class AlternativeFinderController < HighVoltage::PagesController | ||
private | ||
|
||
def page_finder_factory | ||
Rot13PageFinder | ||
end | ||
|
||
class Rot13PageFinder < HighVoltage::PageFinder | ||
def find | ||
paths = super.split('/') | ||
"#{paths[0..-2].join('/')}/#{paths[-1].tr('a-z','n-za-m')}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
class SubclassedPagesController < HighVoltage::PagesController | ||
layout 'alternate' | ||
|
||
def content_path | ||
'other_pages/' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello <%= 'world' %> from a nested dir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello <%= 'world' %> |
2 changes: 2 additions & 0 deletions
2
spec/dummy/app/views/pages/also_exists_but_references_nonexistent_partial.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
hello <%= 'world' %> | ||
<%= render 'nonexistent' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alternative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Dummy::Application.routes.draw do | ||
match "/subclassed_pages/*id" => 'subclassed_pages#show', :format => false | ||
match "/alternative_finder/*id" => 'alternative_finder#show', :format => false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'spec_helper' | ||
require 'high_voltage/page_finder' | ||
|
||
describe HighVoltage::PageFinder do | ||
it 'produces the name of an existing template' do | ||
find('existing').should == 'pages/existing' | ||
end | ||
|
||
it 'produces the name of a nested template' do | ||
find('dir/nested').should == 'pages/dir/nested' | ||
end | ||
|
||
it 'uses a custom content path' do | ||
with_content_path('other_pages/') do | ||
find('also_exists').should == 'other_pages/also_exists' | ||
end | ||
end | ||
|
||
it 'exposes the content path' do | ||
with_content_path('another_thing/') do | ||
page_finder.content_path.should == 'another_thing/' | ||
end | ||
end | ||
|
||
it 'provides the page_id' do | ||
subclass = Class.new(HighVoltage::PageFinder) do | ||
def page_name | ||
"the page is #{page_id}" | ||
end | ||
end | ||
|
||
subclass.new('sweet page').page_name.should == 'the page is sweet page' | ||
end | ||
|
||
private | ||
|
||
def find(page_id) | ||
page_finder(page_id).find | ||
end | ||
|
||
def page_finder(page_id = 'whatever') | ||
HighVoltage::PageFinder.new(page_id) | ||
end | ||
|
||
def with_content_path(path) | ||
original_content_path = HighVoltage.content_path | ||
HighVoltage.content_path = path | ||
|
||
yield | ||
|
||
HighVoltage.content_path = original_content_path | ||
end | ||
end |