Skip to content

Commit f5313da

Browse files
committed
Merge pull request #232 from cattekin/master
Handle URL with explicit .html format on root routes
2 parents 018c9a0 + e562af6 commit f5313da

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

lib/high_voltage/constraints/root_route.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@ module HighVoltage
22
module Constraints
33
# Routing constraint to validate request.path has a corresponding view
44
class RootRoute
5-
def self.matches?(request)
6-
pattern = file_pattern(request.path)
5+
class << self
6+
def matches?(request)
7+
page_id = clean_page_path(request.path)
8+
pattern = file_pattern(page_id)
79

8-
Dir.glob(pattern).any?
9-
end
10+
Dir.glob(pattern).any?
11+
end
1012

11-
private
13+
private
1214

13-
def self.file_pattern(page_id)
14-
"#{content_path}#{page_id}.html*"
15-
end
15+
def clean_page_path(request_path)
16+
request_path.sub(/\.html$/, "")
17+
end
18+
19+
def file_pattern(page_id)
20+
"#{content_path}#{page_id}.html*"
21+
end
1622

17-
def self.content_path
18-
Rails.root.join('app', 'views', HighVoltage.content_path).to_s
23+
def content_path
24+
Rails.root.join("app", "views", HighVoltage.content_path).to_s
25+
end
1926
end
2027
end
2128
end

spec/constraints/root_route_spec.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
require 'spec_helper'
1+
require "spec_helper"
22

3-
describe HighVoltage::Constraints::RootRoute, '.matches?' do
4-
it 'returns true when the view file exists' do
3+
describe HighVoltage::Constraints::RootRoute, ".matches?" do
4+
it "returns true when the view file exists" do
55
request = double(path: 'index')
6-
allow(Dir).to receive(:glob).and_return(["about.html.erb"])
6+
file_path = Rails.root.join("app", "views", "pages", "index.html*").to_s
7+
8+
allow(Dir).to receive(:glob).with(file_path).and_return(["about.html.erb"])
79

810
result = HighVoltage::Constraints::RootRoute.matches?(request)
911

1012
expect(result).to be true
1113
end
1214

13-
it 'returns false when the view files does not exist' do
15+
it "returns true when the view file exists and url ends with .html" do
16+
request = double(path: "index.html")
17+
file_path = Rails.root.join("app", "views", "pages", "index.html*").to_s
18+
19+
allow(Dir).to receive(:glob).with(file_path).and_return(["about.html.erb"])
20+
21+
result = HighVoltage::Constraints::RootRoute.matches?(request)
22+
23+
expect(result).to be true
24+
end
25+
26+
it "returns false when the view files does not exist" do
1427
request = double(path: 'index')
15-
allow(File).to receive(:glob).and_return([])
28+
file_path = Rails.root.join("app", "views", "pages", "index", ".html*").to_s
29+
30+
allow(File).to receive(:glob).with(file_path).and_return([])
1631

1732
result = HighVoltage::Constraints::RootRoute.matches?(request)
1833

0 commit comments

Comments
 (0)