high_voltage 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dfdb165dc9944d13d14ccfcfab0358c2d575b688
4
- data.tar.gz: 769e4a05d918743c86ce21f58b4ab2ea3d8bc838
3
+ metadata.gz: 56801929181e1df1b48361637a33d0e49431827f
4
+ data.tar.gz: e5e8b40606fd4863b423cd682ae3bc0426d11af2
5
5
  SHA512:
6
- metadata.gz: e036c73851faf23320d6b5fccbd8c65cacdb73e736422e0df062cdea64ea8c6a88d26bfc084403a979955eeb695e82d4bc89fdcdd6905516dd7adf85734425b7
7
- data.tar.gz: 29b92c7a8e76d78f6084bfbc6987d5022af4a048461eaff10192d32e94bdd217b41ca27e9ac1c42a38e70fae0603519075cba4e7d2f0a9cc78dc5b4ddaf11206
6
+ metadata.gz: 7baeff6c124d223bbe28962a65ad6fcfdddc7a505c15dd63c6fb0e52530508809c03acee1bc37bd10f65e36aea1d430ef714d00518c38370941d9fc397eaa3f6
7
+ data.tar.gz: c9dd536f08b5ce08cf6ae7a479e3e4f99556d117611656c7ce795843046627f87da726bb5a83163a6a4131968a8081fbc637b89015d76f41daad9c4fab79d167
data/.gitignore CHANGED
@@ -4,3 +4,4 @@ pkg/
4
4
  *.swp
5
5
  *.gem
6
6
  gemfiles/*.lock
7
+ spec/fixtures/log/*.log
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- high_voltage (2.3.0)
4
+ high_voltage (2.4.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -95,3 +95,6 @@ DEPENDENCIES
95
95
  high_voltage!
96
96
  pry
97
97
  rspec-rails (~> 3.2.0)
98
+
99
+ BUNDLED WITH
100
+ 1.10.0
data/NEWS.md CHANGED
@@ -1,3 +1,8 @@
1
+ New for 2.4.0
2
+ + Retrieve a list of available static pages via `HighVoltage.page_ids`.
3
+ + Properly handle page ids which are blank when being sanitized.
4
+ + Only provide deprecation warnings around caching when caching is enabled.
5
+
1
6
  New for 2.3.0:
2
7
  + Ruby 2.2 and Rails 4.2 testing
3
8
  + Raise InvalidPageIdError when visiting a page with invalid characters
data/README.md CHANGED
@@ -15,7 +15,7 @@ Yeah, like "About us", "Directions", marketing pages, etc.
15
15
  Include in your Gemfile:
16
16
 
17
17
  ```ruby
18
- gem 'high_voltage', '~> 2.3.0`
18
+ gem 'high_voltage', '~> 2.3.0'
19
19
  ```
20
20
 
21
21
  For Rails versions prior to 3.0, use the 0.9.2 tag of high_voltage:
@@ -43,6 +43,17 @@ link_to 'Q4 Reports', page_path('about/corporate/policies/HR/en_US/biz/sales/Qua
43
43
 
44
44
  Bam.
45
45
 
46
+ You can also get a list of your static pages by calling `HighVoltage.page_ids`
47
+ This might be useful if you need to build a sitemap. For example, if you are
48
+ using the [sitemap_generator](https://github.com/kjvarga/sitemap_generator) gem,
49
+ you could add something like this to your sitemap config file:
50
+
51
+ ```ruby
52
+ HighVoltage.page_ids.each do |page|
53
+ add page, changefreq: 'monthly'
54
+ end
55
+ ```
56
+
46
57
  ## Configuration
47
58
 
48
59
  #### Routing overview
@@ -3,6 +3,8 @@ require 'active_support/core_ext/module/attribute_accessors'
3
3
 
4
4
  require 'high_voltage/configuration'
5
5
  require 'high_voltage/constraints/root_route'
6
+ require "high_voltage/page"
7
+ require "high_voltage/page_collector"
6
8
  require 'high_voltage/page_finder'
7
9
  require 'high_voltage/route_drawers/default'
8
10
  require 'high_voltage/route_drawers/root'
@@ -25,20 +25,34 @@ module HighVoltage
25
25
  end
26
26
 
27
27
  def action_caching=(value)
28
- ActiveSupport::Deprecation.warn(CACHING_DEPRECATION_WARNING)
28
+ unless value == false
29
+ ActiveSupport::Deprecation.warn(CACHING_DEPRECATION_WARNING)
30
+ end
29
31
  @action_caching = value
30
32
  end
31
33
 
32
34
  def action_caching_layout=(value)
33
- ActiveSupport::Deprecation.warn(CACHING_DEPRECATION_WARNING)
35
+ unless value == false
36
+ ActiveSupport::Deprecation.warn(CACHING_DEPRECATION_WARNING)
37
+ end
34
38
  @action_caching_layout = value
35
39
  end
36
40
 
37
41
  def page_caching=(value)
38
- ActiveSupport::Deprecation.warn(CACHING_DEPRECATION_WARNING)
42
+ unless value == false
43
+ ActiveSupport::Deprecation.warn(CACHING_DEPRECATION_WARNING)
44
+ end
39
45
  @page_caching = value
40
46
  end
41
47
 
48
+ def page_ids
49
+ HighVoltage::PageCollector.new(HighVoltage.full_path).page_ids
50
+ end
51
+
52
+ def full_path
53
+ Rails.root.join("app", "views", HighVoltage.content_path)
54
+ end
55
+
42
56
  def set_default_configuration
43
57
  @action_caching = false
44
58
  @action_caching_layout = true
@@ -0,0 +1,44 @@
1
+ module HighVoltage
2
+ class Page
3
+ attr_reader :content_path, :file_path
4
+
5
+ def initialize(content_path, file_path)
6
+ @content_path = content_path
7
+ @file_path = file_path
8
+ end
9
+
10
+ def id
11
+ file_path.gsub(content_path, "").gsub(html_file_pattern, "")
12
+ end
13
+
14
+ def valid?
15
+ exists? && file_in_content_path? && !directory? && !partial? && html?
16
+ end
17
+
18
+ private
19
+
20
+ def exists?
21
+ File.exists?(file_path)
22
+ end
23
+
24
+ def file_in_content_path?
25
+ file_path.start_with?(content_path)
26
+ end
27
+
28
+ def directory?
29
+ FileTest.directory?(file_path)
30
+ end
31
+
32
+ def partial?
33
+ File.basename(file_path).first == "_"
34
+ end
35
+
36
+ def html?
37
+ !file_path.match(html_file_pattern).nil?
38
+ end
39
+
40
+ def html_file_pattern
41
+ /\.(html)(\.[a-z]+)?$/
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module HighVoltage
2
+ require "find"
3
+
4
+ class PageCollector
5
+ attr_reader :content_path
6
+
7
+ def initialize(content_path)
8
+ @content_path = content_path.to_s
9
+ end
10
+
11
+ def page_ids
12
+ pages.select(&:valid?).map(&:id)
13
+ end
14
+
15
+ private
16
+
17
+ def pages
18
+ Find.find(content_path).map do |file_path|
19
+ HighVoltage::Page.new(content_path, file_path)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -27,7 +27,11 @@ module HighVoltage
27
27
 
28
28
  def clean_path
29
29
  path = Pathname.new("/#{clean_id}")
30
- path.cleanpath.to_s[1..-1]
30
+ path.cleanpath.to_s[1..-1].tap do |p|
31
+ if p.blank?
32
+ raise InvalidPageIdError.new "Invalid page id: #{@page_id}"
33
+ end
34
+ end
31
35
  end
32
36
 
33
37
  def clean_id
@@ -1,3 +1,3 @@
1
1
  module HighVoltage
2
- VERSION = "2.3.0".freeze
2
+ VERSION = "2.4.0".freeze
3
3
  end
@@ -6,13 +6,10 @@ end
6
6
  module Dummy
7
7
  class Application < Rails::Application
8
8
  config.secret_key_base = "test"
9
- config.paths["public"] = ["spec/fixtures/public"]
10
- config.paths["config/routes.rb"] = ["spec/fixtures/config/routes.rb"]
11
- config.paths["app/views"] = ["spec/fixtures/app/views"]
12
- config.paths["app/controllers"] = ["spec/support/app/controllers"]
13
9
  config.eager_load = false
14
10
  config.action_controller.perform_caching = true
15
11
  config.action_controller.cache_store = :memory_store
12
+ config.root = "spec/fixtures"
16
13
  end
17
14
  end
18
15
  Dummy::Application.initialize!
@@ -35,42 +35,87 @@ describe HighVoltage::Configuration do
35
35
  it { expect(HighVoltage.routes).to eq config_value }
36
36
  end
37
37
 
38
- describe '#action_caching=' do
39
- it 'displays a deprecation warning' do
40
- allow(ActiveSupport::Deprecation).to receive(:warn)
38
+ describe "#action_caching" do
39
+ context "action caching is enabled" do
40
+ it 'displays a deprecation warning' do
41
+ allow(ActiveSupport::Deprecation).to receive(:warn)
41
42
 
42
- HighVoltage.configure do |config|
43
- config.action_caching = true
43
+ HighVoltage.configure do |config|
44
+ config.action_caching = true
45
+ end
46
+
47
+ expect(ActiveSupport::Deprecation).to have_received(:warn)
48
+ .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
44
49
  end
50
+ end
51
+
52
+ context "action caching is disabled" do
53
+ it 'does not display a deprecation warning' do
54
+ allow(ActiveSupport::Deprecation).to receive(:warn)
55
+
56
+ HighVoltage.configure do |config|
57
+ config.action_caching = false
58
+ end
45
59
 
46
- expect(ActiveSupport::Deprecation).to have_received(:warn)
47
- .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
60
+ expect(ActiveSupport::Deprecation).not_to have_received(:warn)
61
+ .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
62
+ end
48
63
  end
49
64
  end
50
65
 
51
- describe '#action_caching_layout=' do
52
- it 'displays a deprecation warning' do
53
- allow(ActiveSupport::Deprecation).to receive(:warn)
66
+ describe "#action_caching_layout" do
67
+ context "action caching layout is enabled" do
68
+ it 'displays a deprecation warning' do
69
+ allow(ActiveSupport::Deprecation).to receive(:warn)
54
70
 
55
- HighVoltage.configure do |config|
56
- config.action_caching_layout = true
71
+ HighVoltage.configure do |config|
72
+ config.action_caching_layout = true
73
+ end
74
+
75
+ expect(ActiveSupport::Deprecation).to have_received(:warn)
76
+ .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
57
77
  end
78
+ end
58
79
 
59
- expect(ActiveSupport::Deprecation).to have_received(:warn)
60
- .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
80
+ context "action caching layout is disabled" do
81
+ it "does not display a deprecation warning" do
82
+ allow(ActiveSupport::Deprecation).to receive(:warn)
83
+
84
+ HighVoltage.configure do |config|
85
+ config.action_caching_layout = false
86
+ end
87
+
88
+ expect(ActiveSupport::Deprecation).not_to have_received(:warn)
89
+ .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
90
+ end
61
91
  end
62
92
  end
63
93
 
64
- describe '#page_caching=' do
65
- it 'displays a deprecation warning' do
66
- allow(ActiveSupport::Deprecation).to receive(:warn)
94
+ describe "#page_caching" do
95
+ context "page caching is enabled" do
96
+ it "displays a deprecation warning" do
97
+ allow(ActiveSupport::Deprecation).to receive(:warn)
67
98
 
68
- HighVoltage.configure do |config|
69
- config.page_caching = true
99
+ HighVoltage.configure do |config|
100
+ config.page_caching = true
101
+ end
102
+
103
+ expect(ActiveSupport::Deprecation).to have_received(:warn)
104
+ .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
70
105
  end
106
+ end
107
+
108
+ context "page caching is disbled" do
109
+ it "does not display a deprecation warning" do
110
+ allow(ActiveSupport::Deprecation).to receive(:warn)
71
111
 
72
- expect(ActiveSupport::Deprecation).to have_received(:warn)
73
- .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
112
+ HighVoltage.configure do |config|
113
+ config.page_caching = false
114
+ end
115
+
116
+ expect(ActiveSupport::Deprecation).not_to have_received(:warn)
117
+ .with(HighVoltage::Configuration::CACHING_DEPRECATION_WARNING)
118
+ end
74
119
  end
75
120
  end
76
121
 
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ describe HighVoltage::PageCollector do
4
+ it "produces an array of all page_ids under pages/" do
5
+ expect(HighVoltage.page_ids).to eq [
6
+ "also_dir/also_nested",
7
+ "also_exists",
8
+ "also_exists_but_references_nonexistent_partial",
9
+ "dir/nested",
10
+ "exists",
11
+ "exists_but_references_nonexistent_partial",
12
+ "rot13"
13
+ ]
14
+ end
15
+
16
+ it "produces an array of all page_ids under other_pages/" do
17
+ with_content_path("other_pages/") do
18
+ expect(HighVoltage.page_ids).to eq [
19
+ "also_dir/also_nested",
20
+ "also_exists",
21
+ "also_exists_but_references_nonexistent_partial",
22
+ ]
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def with_content_path(path)
29
+ original_content_path = HighVoltage.content_path
30
+ HighVoltage.content_path = path
31
+
32
+ yield
33
+
34
+ HighVoltage.content_path = original_content_path
35
+ end
36
+ end
@@ -47,6 +47,10 @@ describe HighVoltage::PageFinder do
47
47
  end
48
48
  end
49
49
 
50
+ it "throws an exception if the path is empty" do
51
+ expect { find("關於我們/合作伙伴") }.to raise_error HighVoltage::InvalidPageIdError
52
+ end
53
+
50
54
  private
51
55
 
52
56
  def find(page_id)
@@ -0,0 +1,65 @@
1
+ require "spec_helper"
2
+
3
+ describe HighVoltage::Page do
4
+ it "produces the id for a page" do
5
+ page = page(full_file_path("exists.html.erb"))
6
+
7
+ expect(page.id).to eq "exists"
8
+ end
9
+
10
+ it "produces the id for a page in a subdirectory" do
11
+ page = page(full_file_path("dir/nested.html.erb"))
12
+
13
+ expect(page.id).to eq "dir/nested"
14
+ end
15
+
16
+ it "is valid for a page" do
17
+ page = page(full_file_path("exists.html.erb"))
18
+
19
+ expect(page).to be_valid
20
+ end
21
+
22
+ it "is valid for a page in a subdirectory" do
23
+ page = page(full_file_path("dir/nested.html.erb"))
24
+
25
+ expect(page).to be_valid
26
+ end
27
+
28
+ it "is invalid for a directory" do
29
+ page = page(full_file_path("dir"))
30
+
31
+ expect(page).to_not be_valid
32
+ end
33
+
34
+ it "is invalid for a partial" do
35
+ page = page(full_file_path("_partial.html.erb"))
36
+
37
+ expect(page).to_not be_valid
38
+ end
39
+
40
+ it "is invalid for a non-existent page" do
41
+ page = page(full_file_path("nonexistent.html.erb"))
42
+
43
+ expect(page).to_not be_valid
44
+ end
45
+
46
+ it "is invalid for a text page" do
47
+ page = page(full_file_path("text.txt.erb"))
48
+
49
+ expect(page).to_not be_valid
50
+ end
51
+
52
+ private
53
+
54
+ def full_content_path
55
+ HighVoltage.full_path.to_s
56
+ end
57
+
58
+ def page(file_path)
59
+ HighVoltage::Page.new(full_content_path, file_path)
60
+ end
61
+
62
+ def full_file_path(file_path)
63
+ "#{full_content_path}#{file_path}"
64
+ end
65
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: high_voltage
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Jankowski
@@ -16,7 +16,7 @@ authors:
16
16
  autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
- date: 2015-04-17 00:00:00.000000000 Z
19
+ date: 2015-07-17 00:00:00.000000000 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: activesupport
@@ -88,6 +88,8 @@ files:
88
88
  - lib/high_voltage/configuration.rb
89
89
  - lib/high_voltage/constraints/root_route.rb
90
90
  - lib/high_voltage/engine.rb
91
+ - lib/high_voltage/page.rb
92
+ - lib/high_voltage/page_collector.rb
91
93
  - lib/high_voltage/page_finder.rb
92
94
  - lib/high_voltage/route_drawers/default.rb
93
95
  - lib/high_voltage/route_drawers/root.rb
@@ -105,6 +107,7 @@ files:
105
107
  - spec/fixtures/app/views/other_pages/also_dir/also_nested.html.erb
106
108
  - spec/fixtures/app/views/other_pages/also_exists.html.erb
107
109
  - spec/fixtures/app/views/other_pages/also_exists_but_references_nonexistent_partial.html.erb
110
+ - spec/fixtures/app/views/pages/_partial.html.erb
108
111
  - spec/fixtures/app/views/pages/also_dir/also_nested.html.erb
109
112
  - spec/fixtures/app/views/pages/also_exists.html.erb
110
113
  - spec/fixtures/app/views/pages/also_exists_but_references_nonexistent_partial.html.erb
@@ -112,11 +115,14 @@ files:
112
115
  - spec/fixtures/app/views/pages/exists.html.erb
113
116
  - spec/fixtures/app/views/pages/exists_but_references_nonexistent_partial.html.erb
114
117
  - spec/fixtures/app/views/pages/rot13.html.erb
118
+ - spec/fixtures/app/views/pages/text.txt.erb
115
119
  - spec/fixtures/config/database.yml
116
120
  - spec/fixtures/config/routes.rb
117
121
  - spec/fixtures/public/pages/exists.html
118
122
  - spec/high_voltage/configuration_spec.rb
123
+ - spec/high_voltage/page_collector_spec.rb
119
124
  - spec/high_voltage/page_finder_spec.rb
125
+ - spec/high_voltage/page_spec.rb
120
126
  - spec/high_voltage_spec.rb
121
127
  - spec/integration/navigation_spec.rb
122
128
  - spec/minimal_spec_helper.rb
@@ -163,6 +169,7 @@ test_files:
163
169
  - spec/fixtures/app/views/other_pages/also_dir/also_nested.html.erb
164
170
  - spec/fixtures/app/views/other_pages/also_exists.html.erb
165
171
  - spec/fixtures/app/views/other_pages/also_exists_but_references_nonexistent_partial.html.erb
172
+ - spec/fixtures/app/views/pages/_partial.html.erb
166
173
  - spec/fixtures/app/views/pages/also_dir/also_nested.html.erb
167
174
  - spec/fixtures/app/views/pages/also_exists.html.erb
168
175
  - spec/fixtures/app/views/pages/also_exists_but_references_nonexistent_partial.html.erb
@@ -170,11 +177,14 @@ test_files:
170
177
  - spec/fixtures/app/views/pages/exists.html.erb
171
178
  - spec/fixtures/app/views/pages/exists_but_references_nonexistent_partial.html.erb
172
179
  - spec/fixtures/app/views/pages/rot13.html.erb
180
+ - spec/fixtures/app/views/pages/text.txt.erb
173
181
  - spec/fixtures/config/database.yml
174
182
  - spec/fixtures/config/routes.rb
175
183
  - spec/fixtures/public/pages/exists.html
176
184
  - spec/high_voltage/configuration_spec.rb
185
+ - spec/high_voltage/page_collector_spec.rb
177
186
  - spec/high_voltage/page_finder_spec.rb
187
+ - spec/high_voltage/page_spec.rb
178
188
  - spec/high_voltage_spec.rb
179
189
  - spec/integration/navigation_spec.rb
180
190
  - spec/minimal_spec_helper.rb