From 86012ac5798b57f6242fd31f0c1b99de7fc8eba2 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 22 Aug 2013 15:46:17 -0400 Subject: [PATCH] Fix a case when the wrong topic is loaded because the slug starts with a number --- app/controllers/list_controller.rb | 4 ++-- spec/controllers/list_controller_spec.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index ad4e52ae5..68f39931c 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -111,8 +111,8 @@ class ListController < ApplicationController private def set_category - category_slug = params.fetch(:category) - @category = Category.where("slug = ? or id = ?", category_slug, category_slug.to_i).includes(:featured_users).first + slug = params.fetch(:category) + @category = Category.where("slug = ?", slug).includes(:featured_users).first || Category.where("id = ?", slug.to_i).includes(:featured_users).first end def request_is_for_uncategorized? diff --git a/spec/controllers/list_controller_spec.rb b/spec/controllers/list_controller_spec.rb index 1f24d374d..2b5cd36b1 100644 --- a/spec/controllers/list_controller_spec.rb +++ b/spec/controllers/list_controller_spec.rb @@ -63,6 +63,21 @@ describe ListController do it { should respond_with(:success) } end + context 'another category exists with a number at the beginning of its name' do + # One category has another category's id at the beginning of its name + let!(:other_category) { Fabricate(:category, name: "#{category.id} name") } + + before do + xhr :get, :category, category: other_category.slug + end + + it { should respond_with(:success) } + + it 'uses the correct category' do + assigns(:category).should == other_category + end + end + describe 'feed' do it 'renders RSS' do get :category_feed, category: category.slug, format: :rss