Skip to content

Commit 1eba606

Browse files
committed
Merge pull request #193 from bentoncreation/page_ids
Getting a list of pages (for sitemaps, etc.)
2 parents a823bf4 + f281912 commit 1eba606

File tree

11 files changed

+193
-4
lines changed

11 files changed

+193
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pkg/
44
*.swp
55
*.gem
66
gemfiles/*.lock
7+
spec/fixtures/log/*.log

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ link_to 'Q4 Reports', page_path('about/corporate/policies/HR/en_US/biz/sales/Qua
4343

4444
Bam.
4545

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+
4657
## Configuration
4758

4859
#### Routing overview

lib/high_voltage.rb

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
require 'high_voltage/configuration'
55
require 'high_voltage/constraints/root_route'
6+
require "high_voltage/page"
7+
require "high_voltage/page_collector"
68
require 'high_voltage/page_finder'
79
require 'high_voltage/route_drawers/default'
810
require 'high_voltage/route_drawers/root'

lib/high_voltage/configuration.rb

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def page_caching=(value)
4545
@page_caching = value
4646
end
4747

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+
4856
def set_default_configuration
4957
@action_caching = false
5058
@action_caching_layout = true

lib/high_voltage/page.rb

+44
Original file line numberDiff line numberDiff line change
@@ -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

lib/high_voltage/page_collector.rb

+23
Original file line numberDiff line numberDiff line change
@@ -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

spec/fake_app.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ class ApplicationController < ActionController::Base
66
module Dummy
77
class Application < Rails::Application
88
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"]
139
config.eager_load = false
1410
config.action_controller.perform_caching = true
1511
config.action_controller.cache_store = :memory_store
12+
config.root = "spec/fixtures"
1613
end
1714
end
1815
Dummy::Application.initialize!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
partial
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
text page
+36
Original file line numberDiff line numberDiff line change
@@ -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

spec/high_voltage/page_spec.rb

+65
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)