high_voltage 1.1.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +6 -0
  3. data/Appraisals +4 -6
  4. data/Gemfile.lock +3 -3
  5. data/MIT-LICENSE +1 -1
  6. data/NEWS.md +7 -0
  7. data/README.md +86 -16
  8. data/Rakefile +10 -6
  9. data/app/controllers/high_voltage/pages_controller.rb +12 -15
  10. data/config/routes.rb +3 -2
  11. data/gemfiles/{rails-3.0.10.gemfile → rails_3.0.18.gemfile} +1 -1
  12. data/gemfiles/{rails-3.1.0.gemfile → rails_3.1.9.gemfile} +1 -1
  13. data/gemfiles/rails_3.2.10.gemfile +7 -0
  14. data/lib/high_voltage/constraints/root_route.rb +22 -0
  15. data/lib/high_voltage/engine.rb +0 -2
  16. data/lib/high_voltage/page_finder.rb +37 -0
  17. data/lib/high_voltage/route_drawers/default.rb +15 -0
  18. data/lib/high_voltage/route_drawers/root.rb +16 -0
  19. data/lib/high_voltage/version.rb +1 -1
  20. data/lib/high_voltage.rb +12 -2
  21. data/spec/constraints/root_route_spec.rb +21 -0
  22. data/spec/controllers/alternative_finder_controller_spec.rb +12 -0
  23. data/spec/controllers/pages_controller_spec.rb +63 -41
  24. data/spec/controllers/subclassed_pages_controller_spec.rb +19 -20
  25. data/spec/dummy/app/controllers/alternative_finder_controller.rb +14 -0
  26. data/spec/dummy/app/controllers/subclassed_pages_controller.rb +0 -4
  27. data/spec/dummy/app/views/pages/also_dir/also_nested.html.erb +1 -0
  28. data/spec/dummy/app/views/pages/also_exists.html.erb +1 -0
  29. data/spec/dummy/app/views/pages/also_exists_but_references_nonexistent_partial.html.erb +2 -0
  30. data/spec/dummy/app/views/pages/rot13.html.erb +1 -0
  31. data/spec/dummy/config/application.rb +0 -1
  32. data/spec/dummy/config/environments/test.rb +0 -5
  33. data/spec/dummy/config/routes.rb +1 -0
  34. data/spec/high_voltage/page_finder_spec.rb +52 -0
  35. data/spec/high_voltage_spec.rb +2 -2
  36. data/spec/integration/navigation_spec.rb +3 -3
  37. data/spec/routing/routes_spec.rb +114 -32
  38. data/spec/spec_helper.rb +4 -16
  39. metadata +97 -88
  40. data/gemfiles/rails-3.0.10.gemfile.lock +0 -124
  41. data/gemfiles/rails-3.1.0.gemfile.lock +0 -137
data/.gitignore CHANGED
@@ -5,3 +5,5 @@ spec/dummy/db/*.sqlite3
5
5
  spec/dummy/log/*.log
6
6
  spec/dummy/tmp/
7
7
  *.swp
8
+ gemfiles/*.lock
9
+ *.gem
data/.travis.yml CHANGED
@@ -1,3 +1,9 @@
1
+ language: ruby
1
2
  rvm:
2
3
  - 1.8.7
3
4
  - 1.9.2
5
+ - 1.9.3
6
+ gemfile:
7
+ - gemfiles/rails_3.0.18.gemfile
8
+ - gemfiles/rails_3.1.9.gemfile
9
+ - gemfiles/rails_3.2.10.gemfile
data/Appraisals CHANGED
@@ -1,7 +1,5 @@
1
- appraise "rails-3.1.0" do
2
- gem "rails", "3.1.0"
3
- end
4
-
5
- appraise "rails-3.0.10" do
6
- gem "rails", "3.0.10"
1
+ ['3.2.10', '3.1.9', '3.0.18'].each do |rails_version|
2
+ appraise "rails_#{rails_version}" do
3
+ gem "rails", rails_version
4
+ end
7
5
  end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- high_voltage (1.1.0)
4
+ high_voltage (1.2.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -22,7 +22,7 @@ GEM
22
22
  builder (~> 2.1.2)
23
23
  i18n (~> 0.5.0)
24
24
  activesupport (3.0.10)
25
- appraisal (0.3.8)
25
+ appraisal (0.4.1)
26
26
  bundler
27
27
  rake
28
28
  builder (2.1.2)
@@ -55,7 +55,7 @@ GEM
55
55
  rake (>= 0.8.7)
56
56
  rdoc (~> 3.4)
57
57
  thor (~> 0.14.4)
58
- rake (0.9.2)
58
+ rake (0.9.2.2)
59
59
  rdoc (3.9.4)
60
60
  rspec (2.6.0)
61
61
  rspec-core (~> 2.6.0)
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2010 thoughtbot, inc.
1
+ Copyright 2010-2013 thoughtbot, inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/NEWS.md ADDED
@@ -0,0 +1,7 @@
1
+ New for 1.2.2:
2
+ + Bug fix for RootRoute constraint. Support haml, slim, etc.
3
+ + README updated for root routes.
4
+ New for 1.2.1:
5
+ + Ability to disable HighVoltage routes.
6
+ + New RootRoute constraint.
7
+ + Updated README, with new TravisCI url.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # High Voltage [![Build Status](http://travis-ci.org/thoughtbot/high_voltage.png)](http://travis-ci.org/thoughtbot/high_voltage)
1
+ # High Voltage [![Build Status](https://travis-ci.org/thoughtbot/high_voltage.png)](http://travis-ci.org/thoughtbot/high_voltage)
2
2
 
3
3
  Rails engine for static pages.
4
4
 
@@ -57,26 +57,59 @@ In that case, you'd need an app/views/pages/home.html.erb file.
57
57
 
58
58
  Generally speaking, you need to route to the 'show' action with an :id param of the view filename.
59
59
 
60
- High Voltage will generate a named route method of `page_path` which you can use, as well. If you
61
- want to generate a named route (with the :as routing option) for some route which will be handled
62
- by High Voltage, make sure not to use :page as the name, because that will conflict with the named
63
- route generated by High Voltage itself. For example, this will work for top-level routes (we will
60
+ High Voltage will generate a named route method of `page_path` which you can use, as well. If you
61
+ want to generate a named route (with the :as routing option) for some route which will be handled
62
+ by High Voltage, make sure not to use :page as the name, because that will conflict with the named
63
+ route generated by High Voltage itself.
64
+
65
+ For example, this will work for top-level routes (we will
64
66
  get a named route called `static_path` which does not conflict with the generated `page_path` method):
65
67
 
66
68
  match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get
67
69
 
70
+ #### Specifying a root path
71
+
68
72
  You can route the root url to a high voltage page like this:
69
73
 
70
74
  root :to => 'high_voltage/pages#show', :id => 'home'
71
75
 
72
76
  Which will render a homepage from app/views/pages/home.html.erb
73
77
 
78
+ #### Enabling root domain routes
79
+
80
+ You can remove the directory `pages` from the URL path and serve up routes from
81
+ the root of the domain path:
82
+
83
+ http://www.example.com/about
84
+ http://www.example.com/company
85
+
86
+ Would look for corresponding files:
87
+
88
+ app/views/pages/about.html.erb
89
+ app/views/pages/company.html.erb
90
+
91
+ This is accomplished by changing the `HighVoltage.route_drawer` to `HighVoltage::RouteDrawers::Root`
92
+
93
+ # config/initializers/high_voltage.rb
94
+ HighVoltage.route_drawer = HighVoltage::RouteDrawers::Root
95
+
96
+ Note: This is not a catchall route. It will check the `HighVoltage.content_path`
97
+ (by default this is `app/views/pages`) and validate the view exists.
98
+
99
+ #### Disabling routes
100
+
101
+ The default routes can be completely removed by changing the
102
+ `HighVoltage.routes` to `false`
103
+
104
+ # config/initializers/high_voltage.rb
105
+ HighVoltage.routes = false
106
+
74
107
  Customize
75
108
  --------
76
109
 
77
110
  High Voltage uses a default path and folder of 'pages', i.e. 'url.com/pages/contact' , 'app/views/pages'
78
111
 
79
- You can change this in an initializer:
112
+ You can change this in an initializer:
80
113
 
81
114
  HighVoltage.content_path = "site/"
82
115
 
@@ -98,6 +131,9 @@ Override the default route:
98
131
  # in config/routes.rb
99
132
  match "/pages/*id" => 'pages#show', :as => :page, :format => false
100
133
 
134
+ # if routing the root path, update for your controller
135
+ root :to => 'pages#show', :id => 'home'
136
+
101
137
  Then modify it to subclass from High Voltage, adding whatever you need:
102
138
 
103
139
  class PagesController < HighVoltage::PagesController
@@ -115,25 +151,58 @@ Then modify it to subclass from High Voltage, adding whatever you need:
115
151
  end
116
152
  end
117
153
 
154
+ Custom finding
155
+ --------------
156
+
157
+ You can further control the algorithm used to find pages by overriding
158
+ the `page_finder_factory` method:
159
+
160
+ class PagesController < HighVoltage::PagesController
161
+ private
162
+
163
+ def page_finder_factory
164
+ Rot13PageFinder
165
+ end
166
+ end
167
+
168
+ The easiest thing is to subclass `HighVoltage::PageFinder`, which
169
+ provides you with `page_id`:
170
+
171
+ class Rot13PageFinder < HighVoltage::PageFinder
172
+ def find
173
+ paths = super.split('/')
174
+ directory = paths[0..-2]
175
+ filename = paths[-1].tr('a-z','n-za-m')
176
+
177
+ File.join(*directory, filename)
178
+ end
179
+ end
180
+
181
+ Use this to create a custom file mapping, clean filenames for your file
182
+ system, A/B test, and so on.
183
+
118
184
  Testing
119
185
  -------
120
186
 
121
- Just a suggestion, but you can test your pages using Shoulda pretty easily:
122
-
123
- class PagesControllerTest < ActionController::TestCase
124
- tests PagesController
187
+ You can test your static pages using [RSpec](https://github.com/rspec/rspec-rails)
188
+ and [shoulda-matchers](https://github.com/thoughtbot/shoulda-matchers):
125
189
 
190
+ # spec/controllers/pages_controller_spec.rb
191
+ describe PagesController, '#show' do
126
192
  %w(earn_money screencast about contact).each do |page|
127
- context "on GET to /pages/#{page}" do
128
- setup { get :show, :id => page }
193
+ context 'on GET to /pages/#{page}' do
194
+ before do
195
+ get :show, :id => page
196
+ end
129
197
 
130
- should_respond_with :success
131
- should_render_template page
198
+ it { should respond_with(:success) }
199
+ it { should render_template(page) }
132
200
  end
133
201
  end
134
202
  end
135
203
 
136
- If you're not using a custom PagesController be sure to test <code>HighVoltage::PagesController</code> instead.
204
+ If you're not using a custom PagesController be sure to test
205
+ `HighVoltage::PagesController` instead.
137
206
 
138
207
  Enjoy!
139
208
 
@@ -156,4 +225,5 @@ The names and logos for thoughtbot are trademarks of thoughtbot, inc.
156
225
  License
157
226
  -------
158
227
 
159
- High Voltage is Copyright © 2009-2011 thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
228
+ High Voltage is Copyright © 2009-2013 thoughtbot. It is free software, and may
229
+ be redistributed under the terms specified in the MIT-LICENSE file.
data/Rakefile CHANGED
@@ -1,14 +1,18 @@
1
1
  require 'bundler/setup'
2
2
  require 'bundler/gem_tasks'
3
3
  require 'appraisal'
4
- require 'rspec/core/rake_task'
5
4
 
5
+ require 'rspec/core/rake_task'
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
- desc "Default: run the unit tests."
9
- task :default => [:all]
8
+ task :default do |t|
9
+ if ENV['BUNDLE_GEMFILE'] =~ /gemfiles/
10
+ exec 'rake spec'
11
+ else
12
+ Rake::Task['appraise'].execute
13
+ end
14
+ end
10
15
 
11
- desc 'Test the plugin under all supported Rails versions.'
12
- task :all => ["appraisal:install"] do |t|
13
- exec('rake appraisal spec')
16
+ task :appraise => ['appraisal:install'] do |t|
17
+ exec 'rake appraisal'
14
18
  end
@@ -1,10 +1,9 @@
1
1
  class HighVoltage::PagesController < ApplicationController
2
-
3
2
  unloadable
4
- layout Proc.new { HighVoltage::layout }
3
+ layout Proc.new { |_| HighVoltage.layout }
5
4
 
6
5
  rescue_from ActionView::MissingTemplate do |exception|
7
- if exception.message =~ %r{Missing template #{content_path}}
6
+ if exception.message =~ %r{Missing template #{page_finder.content_path}}
8
7
  raise ActionController::RoutingError, "No such page: #{params[:id]}"
9
8
  else
10
9
  raise exception
@@ -15,19 +14,17 @@ class HighVoltage::PagesController < ApplicationController
15
14
  render :template => current_page
16
15
  end
17
16
 
18
- protected
19
-
20
- def current_page
21
- "#{content_path}#{clean_path}"
22
- end
17
+ private
23
18
 
24
- def clean_path
25
- path = Pathname.new "/#{params[:id]}"
26
- path.cleanpath.to_s[1..-1]
27
- end
19
+ def current_page
20
+ page_finder.find
21
+ end
28
22
 
29
- def content_path
30
- HighVoltage::content_path
31
- end
23
+ def page_finder
24
+ page_finder_factory.new(params[:id])
25
+ end
32
26
 
27
+ def page_finder_factory
28
+ HighVoltage::PageFinder
29
+ end
33
30
  end
data/config/routes.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  Rails.application.routes.draw do
2
- match "/#{HighVoltage::content_path}*id" => 'high_voltage/pages#show', :as => :page, :format => false
3
-
2
+ if HighVoltage.routes
3
+ get HighVoltage.route_drawer.match_attributes
4
+ end
4
5
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "3.0.10"
5
+ gem "rails", "3.0.18"
6
6
 
7
7
  gemspec :path=>"../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "3.1.0"
5
+ gem "rails", "3.1.9"
6
6
 
7
7
  gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "http://rubygems.org"
4
+
5
+ gem "rails", "3.2.10"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,22 @@
1
+ module HighVoltage
2
+ module Constraints
3
+ # Routing constraint to validate request.path has a corresponding view
4
+ class RootRoute
5
+ def self.matches?(request)
6
+ pattern = file_pattern(request.path)
7
+
8
+ Dir.glob(pattern).any?
9
+ end
10
+
11
+ private
12
+
13
+ def self.file_pattern(page_id)
14
+ "#{content_path}#{page_id}.html*"
15
+ end
16
+
17
+ def self.content_path
18
+ Rails.root.join('app', 'views', HighVoltage.content_path).to_s
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,6 +1,4 @@
1
1
  module HighVoltage
2
-
3
2
  class Engine < Rails::Engine
4
3
  end
5
-
6
4
  end
@@ -0,0 +1,37 @@
1
+ module HighVoltage
2
+ # A command for finding pages by id. This encapsulates the concepts of
3
+ # mapping page names to file names.
4
+ class PageFinder
5
+ VALID_CHARACTERS = "a-zA-Z0-9~!@$%^&*()#`_+-=<>\"{}|[];',?".freeze
6
+
7
+ def initialize(page_id)
8
+ @page_id = page_id
9
+ end
10
+
11
+ # Produce a template path to the page, in a format understood by
12
+ # `render :template => find`
13
+ def find
14
+ "#{content_path}#{clean_path}"
15
+ end
16
+
17
+ def content_path
18
+ HighVoltage.content_path
19
+ end
20
+
21
+ protected
22
+
23
+ # The raw page id passed in by the user
24
+ attr_reader :page_id
25
+
26
+ private
27
+
28
+ def clean_path
29
+ path = Pathname.new("/#{clean_id}")
30
+ path.cleanpath.to_s[1..-1]
31
+ end
32
+
33
+ def clean_id
34
+ @page_id.tr("^#{VALID_CHARACTERS}", '')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module HighVoltage
2
+ module RouteDrawers
3
+ # Matches routes in the HighVoltage.content_path directory. By default this looks
4
+ # for /pages/id. e.g. http://www.example.com/pages/about_us
5
+ class Default
6
+ def self.match_attributes
7
+ {
8
+ "/#{HighVoltage.content_path}*id" => 'high_voltage/pages#show',
9
+ :as => :page,
10
+ :format => false
11
+ }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module HighVoltage
2
+ module RouteDrawers
3
+ # Matches routes from root of the domain e.g. http://www.example.com/about_us
4
+ # Uses HighVoltage::Constraints::RootRoute to validate view exists.
5
+ class Root
6
+ def self.match_attributes
7
+ {
8
+ "/*id" => 'high_voltage/pages#show',
9
+ :as => :page,
10
+ :format => false,
11
+ :constraints => HighVoltage::Constraints::RootRoute
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module HighVoltage
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.2'.freeze
3
3
  end
data/lib/high_voltage.rb CHANGED
@@ -1,11 +1,21 @@
1
1
  require 'high_voltage/version'
2
+ require 'high_voltage/constraints/root_route'
3
+ require 'high_voltage/page_finder'
4
+ require 'high_voltage/route_drawers/default'
5
+ require 'high_voltage/route_drawers/root'
2
6
 
3
7
  module HighVoltage
4
8
  mattr_accessor :layout
5
- @@layout = "application"
9
+ @@layout = 'application'
6
10
 
7
11
  mattr_accessor :content_path
8
- @@content_path = "pages/"
12
+ @@content_path = 'pages/'
13
+
14
+ mattr_accessor :route_drawer
15
+ @@route_drawer = HighVoltage::RouteDrawers::Default
16
+
17
+ mattr_accessor :routes
18
+ @@routes = true
9
19
 
10
20
  def self.setup
11
21
  yield self
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe HighVoltage::Constraints::RootRoute, '.matches?' do
4
+ context 'view file exists' do
5
+ it 'should return true' do
6
+ request = stub(:path => 'index')
7
+ Dir.stub!(:glob).and_return(['about.html.erb'])
8
+
9
+ HighVoltage::Constraints::RootRoute.matches?(request).should be_true
10
+ end
11
+ end
12
+
13
+ context 'view file does not exist' do
14
+ it 'should return false' do
15
+ request = stub(:path => 'index')
16
+ File.stub!(:glob).and_return([])
17
+
18
+ HighVoltage::Constraints::RootRoute.matches?(request).should be_false
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlternativeFinderController do
4
+ render_views
5
+
6
+ it 'renders the file from the alternative directory' do
7
+ get :show, :id => 'ebg13'
8
+
9
+ response.should be_success
10
+ response.should render_template('rot13')
11
+ end
12
+ end