Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #232 from cattekin/master
Handle URL with explicit .html format on root routes
  • Loading branch information
dgalarza committed Apr 15, 2016
2 parents 018c9a0 + e562af6 commit f5313da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
27 changes: 17 additions & 10 deletions lib/high_voltage/constraints/root_route.rb
Expand Up @@ -2,20 +2,27 @@ module HighVoltage
module Constraints
# Routing constraint to validate request.path has a corresponding view
class RootRoute
def self.matches?(request)
pattern = file_pattern(request.path)
class << self
def matches?(request)
page_id = clean_page_path(request.path)
pattern = file_pattern(page_id)

Dir.glob(pattern).any?
end
Dir.glob(pattern).any?
end

private
private

def self.file_pattern(page_id)
"#{content_path}#{page_id}.html*"
end
def clean_page_path(request_path)
request_path.sub(/\.html$/, "")
end

def file_pattern(page_id)
"#{content_path}#{page_id}.html*"
end

def self.content_path
Rails.root.join('app', 'views', HighVoltage.content_path).to_s
def content_path
Rails.root.join("app", "views", HighVoltage.content_path).to_s
end
end
end
end
Expand Down
27 changes: 21 additions & 6 deletions spec/constraints/root_route_spec.rb
@@ -1,18 +1,33 @@
require 'spec_helper'
require "spec_helper"

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

allow(Dir).to receive(:glob).with(file_path).and_return(["about.html.erb"])

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

expect(result).to be true
end

it 'returns false when the view files does not exist' do
it "returns true when the view file exists and url ends with .html" do
request = double(path: "index.html")
file_path = Rails.root.join("app", "views", "pages", "index.html*").to_s

allow(Dir).to receive(:glob).with(file_path).and_return(["about.html.erb"])

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

expect(result).to be true
end

it "returns false when the view files does not exist" do
request = double(path: 'index')
allow(File).to receive(:glob).and_return([])
file_path = Rails.root.join("app", "views", "pages", "index", ".html*").to_s

allow(File).to receive(:glob).with(file_path).and_return([])

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

Expand Down

0 comments on commit f5313da

Please sign in to comment.