high_voltage 1.2.2 → 2.1.0

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.
@@ -0,0 +1,42 @@
1
+ module HighVoltage::StaticPage
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ layout ->(_) { HighVoltage.layout }
6
+
7
+ rescue_from ActionView::MissingTemplate do |exception|
8
+ if exception.message =~ %r{Missing template #{page_finder.content_path}}
9
+ raise ActionController::RoutingError, "No such page: #{params[:id]}"
10
+ else
11
+ raise exception
12
+ end
13
+ end
14
+
15
+ if respond_to?(:caches_action)
16
+ caches_action :show, layout: HighVoltage.action_caching_layout,
17
+ if: -> { HighVoltage.action_caching }
18
+ end
19
+
20
+ if respond_to?(:caches_page)
21
+ caches_page :show, if: -> { HighVoltage.page_caching }
22
+ end
23
+
24
+ hide_action :current_page, :page_finder, :page_finder_factory
25
+ end
26
+
27
+ def show
28
+ render template: current_page
29
+ end
30
+
31
+ def current_page
32
+ page_finder.find
33
+ end
34
+
35
+ def page_finder
36
+ page_finder_factory.new(params[:id])
37
+ end
38
+
39
+ def page_finder_factory
40
+ HighVoltage::PageFinder
41
+ end
42
+ end
@@ -1,30 +1,3 @@
1
1
  class HighVoltage::PagesController < ApplicationController
2
- unloadable
3
- layout Proc.new { |_| HighVoltage.layout }
4
-
5
- rescue_from ActionView::MissingTemplate do |exception|
6
- if exception.message =~ %r{Missing template #{page_finder.content_path}}
7
- raise ActionController::RoutingError, "No such page: #{params[:id]}"
8
- else
9
- raise exception
10
- end
11
- end
12
-
13
- def show
14
- render :template => current_page
15
- end
16
-
17
- private
18
-
19
- def current_page
20
- page_finder.find
21
- end
22
-
23
- def page_finder
24
- page_finder_factory.new(params[:id])
25
- end
26
-
27
- def page_finder_factory
28
- HighVoltage::PageFinder
29
- end
2
+ include HighVoltage::StaticPage
30
3
  end
data/config/routes.rb CHANGED
@@ -1,4 +1,9 @@
1
1
  Rails.application.routes.draw do
2
+ if HighVoltage.home_page
3
+ get "/#{HighVoltage.home_page}", to: redirect('/')
4
+ root to: 'high_voltage/pages#show', id: HighVoltage.home_page
5
+ end
6
+
2
7
  if HighVoltage.routes
3
8
  get HighVoltage.route_drawer.match_attributes
4
9
  end
data/high_voltage.gemspec CHANGED
@@ -8,15 +8,17 @@ Gem::Specification.new do |s|
8
8
  s.email = ['support@thoughtbot.com']
9
9
  s.homepage = 'http://github.com/thoughtbot/high_voltage'
10
10
  s.summary = 'Simple static page rendering controller'
11
- s.description = 'Fire in the disco. Fire in the ... taco bell.'
11
+ s.description = 'Fire in the disco. Fire in the ... taco bell.'
12
+ s.license = 'MIT'
12
13
 
13
14
  s.files = `git ls-files`.split("\n")
14
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
17
  s.require_paths = ["lib"]
17
18
 
19
+ s.add_development_dependency("activesupport", ">= 3.1.0")
18
20
  s.add_development_dependency("appraisal")
21
+ s.add_development_dependency("capybara", "= 2.0.3")
22
+ s.add_development_dependency("pry")
19
23
  s.add_development_dependency("rspec-rails")
20
- s.add_development_dependency("capybara", ">= 0.4.0")
21
- s.add_development_dependency("sqlite3")
22
24
  end
@@ -0,0 +1,33 @@
1
+ module HighVoltage
2
+ module Configuration
3
+ attr_accessor(
4
+ :action_caching,
5
+ :action_caching_layout,
6
+ :content_path,
7
+ :home_page,
8
+ :layout,
9
+ :page_caching,
10
+ :route_drawer,
11
+ :routes,
12
+ )
13
+
14
+ def configure
15
+ yield self
16
+ end
17
+
18
+ def self.extended(base)
19
+ base.set_default_configuration
20
+ end
21
+
22
+ def set_default_configuration
23
+ self.action_caching = false
24
+ self.action_caching_layout = true
25
+ self.content_path = 'pages/'
26
+ self.home_page = nil
27
+ self.layout = 'application'
28
+ self.page_caching = false
29
+ self.route_drawer = HighVoltage::RouteDrawers::Default
30
+ self.routes = true
31
+ end
32
+ end
33
+ end
@@ -1,4 +1,13 @@
1
1
  module HighVoltage
2
2
  class Engine < Rails::Engine
3
+ initializer 'Require concerns path' do |app|
4
+ concerns_path = 'app/controllers/concerns'
5
+
6
+ unless app.paths.keys.include?(concerns_path)
7
+ app.paths.add(concerns_path)
8
+ end
9
+
10
+ require 'concerns/high_voltage/static_page'
11
+ end
3
12
  end
4
13
  end
@@ -1,3 +1,3 @@
1
1
  module HighVoltage
2
- VERSION = '1.2.2'.freeze
2
+ VERSION = '2.1.0'.freeze
3
3
  end
data/lib/high_voltage.rb CHANGED
@@ -1,25 +1,13 @@
1
- require 'high_voltage/version'
1
+ require 'active_support/core_ext/module/attribute_accessors'
2
+
3
+ require 'high_voltage/configuration'
2
4
  require 'high_voltage/constraints/root_route'
3
5
  require 'high_voltage/page_finder'
4
6
  require 'high_voltage/route_drawers/default'
5
7
  require 'high_voltage/route_drawers/root'
8
+ require 'high_voltage/version'
6
9
 
7
10
  module HighVoltage
8
- mattr_accessor :layout
9
- @@layout = 'application'
10
-
11
- mattr_accessor :content_path
12
- @@content_path = 'pages/'
13
-
14
- mattr_accessor :route_drawer
15
- @@route_drawer = HighVoltage::RouteDrawers::Default
16
-
17
- mattr_accessor :routes
18
- @@routes = true
19
-
20
- def self.setup
21
- yield self
22
- end
23
-
11
+ extend Configuration
24
12
  require 'high_voltage/engine' if defined?(Rails)
25
13
  end
@@ -3,19 +3,23 @@ require 'spec_helper'
3
3
  describe HighVoltage::Constraints::RootRoute, '.matches?' do
4
4
  context 'view file exists' do
5
5
  it 'should return true' do
6
- request = stub(:path => 'index')
7
- Dir.stub!(:glob).and_return(['about.html.erb'])
6
+ request = double(path: 'index')
7
+ Dir.stub(:glob).and_return(['about.html.erb'])
8
8
 
9
- HighVoltage::Constraints::RootRoute.matches?(request).should be_true
9
+ result = HighVoltage::Constraints::RootRoute.matches?(request)
10
+
11
+ expect(result).to be_true
10
12
  end
11
13
  end
12
14
 
13
15
  context 'view file does not exist' do
14
16
  it 'should return false' do
15
- request = stub(:path => 'index')
16
- File.stub!(:glob).and_return([])
17
+ request = double(path: 'index')
18
+ File.stub(:glob).and_return([])
19
+
20
+ result = HighVoltage::Constraints::RootRoute.matches?(request)
17
21
 
18
- HighVoltage::Constraints::RootRoute.matches?(request).should be_false
22
+ expect(result).to be_false
19
23
  end
20
24
  end
21
25
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe HighVoltage::PagesController, '#action_caching' do
4
+ let(:page_name) { :exists }
5
+
6
+ context 'action_caching set to true' do
7
+ it 'caches the action', enable_caching: true do
8
+ HighVoltage.action_caching = true
9
+ concern_reload
10
+
11
+ expect { get :show, id: page_name }.to change { action_cache_exists? }
12
+ end
13
+ end
14
+
15
+ context 'action_caching set to false' do
16
+ it 'does not cache the action', enable_caching: true do
17
+ HighVoltage.action_caching = false
18
+ concern_reload
19
+
20
+ expect { get :show, id: page_name }.to_not change { action_cache_exists? }
21
+ end
22
+ end
23
+
24
+ def action_cache_exists?
25
+ ActionController::Base.cache_store.exist?("views/test.host#{page_path(page_name)}")
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe HighVoltage::PagesController, '#cache_page' do
4
+ let(:page_name) { :exists }
5
+
6
+ context 'page_caching set to true' do
7
+ it 'caches the page', enable_caching: true do
8
+ HighVoltage.page_caching = true
9
+ concern_reload
10
+
11
+ controller.should_receive(:cache_page).at_least(:once)
12
+
13
+ get :show, id: page_name
14
+ end
15
+ end
16
+
17
+ context 'page_caching set to false' do
18
+ it 'does not cache the page', enable_caching: true do
19
+ HighVoltage.page_caching = false
20
+ concern_reload
21
+
22
+ controller.should_receive(:cache_page).never
23
+
24
+ get :show, id: page_name
25
+ end
26
+ end
27
+ end
28
+
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe HighVoltage::PagesController do
@@ -50,15 +49,10 @@ describe HighVoltage::PagesController do
50
49
  end
51
50
 
52
51
  context 'using custom layout' do
53
- before(:all) do
54
- @original_layout = HighVoltage.layout
52
+ before(:each) do
55
53
  HighVoltage.layout = 'alternate'
56
54
  end
57
55
 
58
- after(:all) do
59
- HighVoltage.layout = @original_layout
60
- end
61
-
62
56
  describe 'on GET to /pages/exists' do
63
57
  before { get :show, :id => 'exists' }
64
58
 
@@ -70,13 +64,9 @@ describe HighVoltage::PagesController do
70
64
  end
71
65
 
72
66
  context 'using custom content path' do
73
- before(:all) do
74
- @original_content_path = HighVoltage.content_path
67
+ before(:each) do
75
68
  HighVoltage.content_path = 'other_pages/'
76
- end
77
-
78
- after(:all) do
79
- HighVoltage.content_path = @original_content_path
69
+ Rails.application.reload_routes!
80
70
  end
81
71
 
82
72
  describe 'on GET to /other_pages/also_exists' do
@@ -98,21 +88,21 @@ describe HighVoltage::PagesController do
98
88
  end
99
89
 
100
90
  it 'should raise a routing error for an invalid page' do
101
- lambda {
102
- get :show,
103
- :id => 'also_invalid'
91
+ lambda {
92
+ get :show,
93
+ :id => 'also_invalid'
104
94
  }.should raise_error(ActionController::RoutingError)
105
95
  end
106
96
 
107
97
  it 'should raise a routing error for a page in another directory' do
108
98
  lambda {
109
99
  get :show,
110
- :id => '../other/wrong'
100
+ :id => '../other/wrong'
111
101
  }.should raise_error(ActionController::RoutingError)
112
102
  end
113
103
 
114
104
  it 'should raise a routing error for a page in another directory when using a Unicode exploit' do
115
- lambda {
105
+ lambda {
116
106
  get :show,
117
107
  :id => '/\\../other/wrong'
118
108
  }.should raise_error(ActionController::RoutingError)
@@ -7,8 +7,7 @@ Dummy::Application.configure do
7
7
  # and recreated between test runs. Don't rely on the data there!
8
8
  config.cache_classes = true
9
9
 
10
- # Log error messages when you accidentally call methods on nil.
11
- config.whiny_nils = true
10
+ config.eager_load = false
12
11
 
13
12
  # Show full error reports and disable caching
14
13
  config.consider_all_requests_local = true
@@ -0,0 +1 @@
1
+ Dummy::Application.config.secret_key_base = '9c0b876ab8f513cc23d354b6d684c88eb9bc5f73ddc8d5843e7c6ac9176c5819adc2620e220980a3cb818de687753f1bfc56c66c4454b74fc7d432813f8e555a'
@@ -1,4 +1,4 @@
1
1
  Dummy::Application.routes.draw do
2
- match "/subclassed_pages/*id" => 'subclassed_pages#show', :format => false
3
- match "/alternative_finder/*id" => 'alternative_finder#show', :format => false
2
+ get "/subclassed_pages/*id" => 'subclassed_pages#show', :format => false
3
+ get "/alternative_finder/*id" => 'alternative_finder#show', :format => false
4
4
  end
File without changes
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe HighVoltage::Configuration do
4
+ let(:config_value) { 'fake_config_value' }
5
+
6
+ before(:each) do
7
+ HighVoltage.configure do |config|
8
+ config.action_caching = config_value
9
+ config.action_caching_layout = config_value
10
+ config.content_path = config_value
11
+ config.layout = config_value
12
+ config.page_caching = config_value
13
+ config.route_drawer = config_value
14
+ config.routes = config_value
15
+ end
16
+ end
17
+
18
+ it { expect(HighVoltage.action_caching).to eq config_value }
19
+ it { expect(HighVoltage.action_caching_layout).to eq config_value }
20
+ it { expect(HighVoltage.content_path).to eq config_value }
21
+ it { expect(HighVoltage.layout).to eq config_value }
22
+ it { expect(HighVoltage.page_caching).to eq config_value }
23
+ it { expect(HighVoltage.route_drawer).to eq config_value }
24
+ it { expect(HighVoltage.routes).to eq config_value }
25
+ end
@@ -1,7 +1,11 @@
1
- require 'spec_helper'
1
+ require 'minimal_spec_helper'
2
2
 
3
3
  describe HighVoltage do
4
4
  it 'should be valid' do
5
5
  HighVoltage.should be_a(Module)
6
6
  end
7
+
8
+ it 'should be loadable without preloading rails' do
9
+ expect { require 'high_voltage' }.not_to raise_error
10
+ end
7
11
  end
@@ -0,0 +1,5 @@
1
+ RSpec.configure do |config|
2
+ require 'rspec/expectations'
3
+ config.include RSpec::Matchers
4
+ config.mock_with :rspec
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Home page route' do
4
+ before(:each) do
5
+ HighVoltage.home_page = 'home'
6
+ Rails.application.reload_routes!
7
+ end
8
+
9
+ it 'redirects the duplicate content to root' do
10
+ get '/home'
11
+ expect(response).to redirect_to('/')
12
+ end
13
+ end
@@ -1,42 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'routes' do
4
- context 'using default configuration' do
5
- it 'should generate normal resource route with id' do
6
- page_path(:id => 'one').should eq '/pages/one'
7
- end
8
-
9
- it 'should generate normal resource route with string' do
4
+ context 'default configuration' do
5
+ it 'generates a route' do
10
6
  page_path('one').should eq '/pages/one'
11
7
  end
12
8
 
13
- it 'should generate nested route with string' do
9
+ it 'generates a nested route' do
14
10
  page_path('one/two').should eq '/pages/one/two'
15
11
  end
16
12
 
17
- it 'should recognize nested route' do
13
+ it 'recognizes a route' do
18
14
  assert_recognizes(
19
15
  {
20
16
  :controller => 'high_voltage/pages',
21
17
  :action => 'show',
22
- :id => 'one/two'
18
+ :id => 'one'
23
19
  },
24
- '/pages/one/two'
20
+ '/pages/one'
25
21
  )
26
22
  end
27
23
 
28
- it 'should recognize normal route' do
24
+ it 'recognizes a nested route' do
29
25
  assert_recognizes(
30
26
  {
31
27
  :controller => 'high_voltage/pages',
32
28
  :action => 'show',
33
- :id => 'one'
29
+ :id => 'one/two'
34
30
  },
35
- '/pages/one'
31
+ '/pages/one/two'
36
32
  )
37
33
  end
38
34
 
39
- it 'should recognize normal route with dots' do
35
+ it 'recognizes a route with dots' do
40
36
  assert_recognizes(
41
37
  {
42
38
  :controller => 'high_voltage/pages',
@@ -48,78 +44,58 @@ describe 'routes' do
48
44
  end
49
45
  end
50
46
 
51
- context 'using root routing configuration' do
52
- around do |example|
53
- cached_high_voltage_route_drawer = HighVoltage.route_drawer
47
+ context 'using top-level routing configuration' do
48
+ before(:each) do
54
49
  HighVoltage.route_drawer = HighVoltage::RouteDrawers::Root
55
50
  Rails.application.reload_routes!
56
-
57
- example.run
58
-
59
- HighVoltage.route_drawer = cached_high_voltage_route_drawer
60
- Rails.application.reload_routes!
61
51
  end
62
52
 
63
- it 'should generate normal resource route with id' do
64
- page_path(:id => 'one').should eq '/one'
65
- end
66
-
67
- it 'should generate normal resource route with string' do
53
+ it 'generates a route' do
68
54
  page_path('one').should eq '/one'
69
55
  end
70
56
 
71
- it 'should generate nested route with string' do
57
+ it 'generates a nested route' do
72
58
  page_path('one/two').should eq '/one/two'
73
59
  end
74
60
  end
75
61
 
76
- context 'using a custom content_path' do
77
- around do |example|
78
- cached_high_voltage_content_path = HighVoltage.content_path
62
+ context 'custom content path' do
63
+ before(:each) do
79
64
  HighVoltage.content_path = 'other_pages/'
80
65
  Rails.application.reload_routes!
81
-
82
- example.run
83
-
84
- HighVoltage.content_path = cached_high_voltage_content_path
85
- Rails.application.reload_routes!
86
66
  end
87
67
 
88
- it 'should generate normal resource route with id' do
89
- page_path(:id => 'one').should eq '/other_pages/one'
90
- end
91
-
92
- it 'should generate normal resource route with string' do
68
+ it 'generates a route' do
93
69
  page_path('one').should eq '/other_pages/one'
94
70
  end
95
71
 
96
- it 'should generate nested route with string' do
72
+ it 'generates a nested route' do
97
73
  page_path('one/two').should eq '/other_pages/one/two'
98
74
  end
99
75
 
100
- it 'should recognize nested route' do
76
+ it 'recognizes a route' do
101
77
  assert_recognizes(
102
78
  {
103
79
  :controller => 'high_voltage/pages',
104
80
  :action => 'show',
105
- :id => 'one/two'
81
+ :id => 'one'
106
82
  },
107
- '/other_pages/one/two'
83
+ '/other_pages/one'
108
84
  )
109
85
  end
110
86
 
111
- it 'should recognize normal route' do
87
+ it 'recognizes a nested route' do
112
88
  assert_recognizes(
113
89
  {
114
90
  :controller => 'high_voltage/pages',
115
91
  :action => 'show',
116
- :id => 'one'
92
+ :id => 'one/two'
117
93
  },
118
- '/other_pages/one'
94
+ '/other_pages/one/two'
119
95
  )
120
96
  end
121
97
 
122
- it 'should recognize normal route with dots' do
98
+ it 'recognizes a route with dots' do
123
99
  assert_recognizes(
124
100
  {
125
101
  :controller => 'high_voltage/pages',
@@ -131,19 +107,33 @@ describe 'routes' do
131
107
  end
132
108
  end
133
109
 
134
- context 'with default configuration disabled' do
135
- around do |example|
136
- cached_high_voltage_routes = HighVoltage.routes
137
- HighVoltage.routes = false
110
+ context 'home page route' do
111
+ it 'recognizes the root route' do
112
+ HighVoltage.home_page = 'home'
138
113
  Rails.application.reload_routes!
139
114
 
140
- example.run
115
+ assert_recognizes(
116
+ {
117
+ :controller => 'high_voltage/pages',
118
+ :action => 'show',
119
+ :id => 'home'
120
+ },
121
+ '/'
122
+ )
123
+ end
124
+ end
141
125
 
142
- HighVoltage.routes = cached_high_voltage_routes
143
- Rails.application.reload_routes!
126
+ context 'no home page route' do
127
+ it 'does generate a home page route' do
128
+ { :get => '/' }.should_not be_routable
144
129
  end
130
+ end
131
+
132
+ context 'disabled routes' do
133
+ it 'does not recognize routes' do
134
+ HighVoltage.routes = false
135
+ Rails.application.reload_routes!
145
136
 
146
- it 'should not recognize routes' do
147
137
  { :get => '/pages/one/two' }.should_not be_routable
148
138
  end
149
139
  end
data/spec/spec_helper.rb CHANGED
@@ -1,18 +1,26 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
2
 
3
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
3
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
4
+
5
+ require 'capybara/rails'
6
+ require 'pry'
4
7
  require 'rails/test_help'
8
+ require 'rspec/expectations'
5
9
  require 'rspec/rails'
6
- require 'capybara/rails'
7
10
 
8
11
  Rails.backtrace_cleaner.remove_silencers!
9
- Capybara.default_driver = :rack_test
12
+ Capybara.default_driver = :rack_test
10
13
  Capybara.default_selector = :css
11
14
 
12
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
+ Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |file| require file }
13
16
 
14
17
  RSpec.configure do |config|
15
- require 'rspec/expectations'
18
+ config.after(:each) do
19
+ HighVoltage.set_default_configuration
20
+ Rails.application.reload_routes!
21
+ end
22
+
16
23
  config.include RSpec::Matchers
17
24
  config.mock_with :rspec
25
+ config.order = 'random'
18
26
  end
@@ -0,0 +1,15 @@
1
+ RSpec.configure do |config|
2
+ config.around(:each, enable_caching: true) do |example|
3
+ previous_cache_store = ActionController::Base.cache_store
4
+
5
+ ActionController::Base.perform_caching = true
6
+ ActionController::Base.cache_store = :memory_store
7
+ ActionController::Base.cache_store.clear
8
+
9
+ example.run
10
+
11
+ ActionController::Base.cache_store.clear
12
+ ActionController::Base.cache_store = previous_cache_store
13
+ ActionController::Base.perform_caching = false
14
+ end
15
+ end