FIX: Allow redirection for slugs that start with digits

This commit is contained in:
Robin Ward 2016-09-19 13:31:19 -04:00
parent 332cc2bb95
commit 2766b2edc3
2 changed files with 16 additions and 0 deletions

View file

@ -60,6 +60,13 @@ class TopicsController < ApplicationController
opts[:slow_platform] = true if slow_platform?
opts[:username_filters] = username_filters.split(',') if username_filters.is_a?(String)
# Special case: a slug with a number in front should look by slug first before looking
# up that particular number
if params[:id] && params[:id] =~ /^\d+[^\d\\]+$/
topic = Topic.find_by(slug: params[:id].downcase)
return redirect_to_correct_topic(topic, opts[:post_number]) if topic && topic.visible
end
begin
@topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, opts)
rescue Discourse::NotFound

View file

@ -569,6 +569,15 @@ describe TopicsController do
expect(response).to redirect_to(topic.relative_url)
end
it 'can find a topic when a slug has a number in front' do
another_topic = Fabricate(:post).topic
topic.update_column(:slug, "#{another_topic.id}-reasons-discourse-is-awesome")
xhr :get, :show, id: "#{another_topic.id}-reasons-discourse-is-awesome"
expect(response).to redirect_to(topic.relative_url)
end
it 'keeps the post_number parameter around when redirecting' do
xhr :get, :show, id: topic.slug, post_number: 42
expect(response).to redirect_to(topic.relative_url + "/42")