add permalinks route constraint

This commit is contained in:
Neil Lalonde 2014-08-29 11:28:16 -04:00
parent e823f568a7
commit 6b41c6b335
5 changed files with 11 additions and 21 deletions

View file

@ -148,14 +148,6 @@ module Discourse
require 'auth'
Discourse.activate_plugins! unless Rails.env.test? and ENV['LOAD_PLUGINS'] != "1"
# FIXME: needs to work with engines such as docker manager
# this mounts routes_last before the engine, one option here if a hook is to hard
# is to add a constraint on the route that ensures its only captured if its a permalink
#
# initializer :add_last_routes, :after => :add_routing_paths do |app|
# app.routes_reloader.paths << File.join(Rails.root, 'config', 'routes_last.rb')
# end
config.after_initialize do
# So open id logs somewhere sane
OpenID::Util.logger = Rails.logger

View file

@ -3,6 +3,7 @@ require_dependency "scheduler/web"
require_dependency "admin_constraint"
require_dependency "staff_constraint"
require_dependency "homepage_constraint"
require_dependency "permalink_constraint"
# This used to be User#username_format, but that causes a preload of the User object
# and makes Guard not work properly.
@ -425,5 +426,5 @@ Discourse::Application.routes.draw do
# special case for top
root to: "list#top", constraints: HomePageConstraint.new("top"), :as => "top_lists"
# See config/routes_last.rb for more
get "*url", to: 'permalinks#show', constraints: PermalinkConstraint.new
end

View file

@ -1,10 +0,0 @@
# These routes must be loaded after all others.
# Routes are loaded in this order:
#
# 1. config/routes.rb
# 2. routes in engines
# 3. config/routes_last.rb
Discourse::Application.routes.draw do
get "*url", to: 'permalinks#show'
end

View file

@ -0,0 +1,7 @@
class PermalinkConstraint
def matches?(request)
Permalink.where(url: request.fullpath[1..-1]).exists?
end
end

View file

@ -2,7 +2,7 @@ require 'spec_helper'
describe PermalinksController do
describe 'show' do
pending "should redirect to a permalink's target_url with status 301" do
it "should redirect to a permalink's target_url with status 301" do
permalink = Fabricate(:permalink)
Permalink.any_instance.stubs(:target_url).returns('/t/the-topic-slug/42')
get :show, url: permalink.url
@ -10,7 +10,7 @@ describe PermalinksController do
response.status.should == 301
end
pending 'return 404 if permalink record does not exist' do
it 'return 404 if permalink record does not exist' do
get :show, url: '/not/a/valid/url'
response.status.should == 404
end