From ea86cfd4ef1ab73da4492647c8e87e5706fedd4c Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 24 Mar 2014 13:36:23 -0400 Subject: [PATCH] FIX: categories with non-ascii names will have broken next page urls --- app/controllers/list_controller.rb | 8 ++++---- app/models/category.rb | 4 ++++ spec/models/category_spec.rb | 7 ++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 83b27462b2d..c390223811f 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -104,9 +104,9 @@ class ListController < ApplicationController discourse_expires_in 1.minute @title = @category.name - @link = "#{Discourse.base_url}/category/#{@category.slug}" + @link = "#{Discourse.base_url}/category/#{@category.slug_for_url}" @description = "#{I18n.t('topics_in_category', category: @category.name)} #{@category.description}" - @atom_link = "#{Discourse.base_url}/category/#{@category.slug}.rss" + @atom_link = "#{Discourse.base_url}/category/#{@category.slug_for_url}.rss" @topic_list = TopicQuery.new.list_new_in_category(@category) render 'list', formats: [:rss] @@ -222,8 +222,8 @@ class ListController < ApplicationController def page_params(opts = nil) opts ||= {} route_params = {format: 'json'} - route_params[:category] = @category.slug if @category - route_params[:parent_category] = @category.parent_category.slug if @category && @category.parent_category + route_params[:category] = @category.slug_for_url if @category + route_params[:parent_category] = @category.parent_category.slug_for_url if @category && @category.parent_category route_params[:sort_order] = opts[:sort_order] if opts[:sort_order].present? route_params[:sort_descending] = opts[:sort_descending] if opts[:sort_descending].present? route_params diff --git a/app/models/category.rb b/app/models/category.rb index e435e0d3de8..c137be6b7dc 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -186,6 +186,10 @@ SQL end end + def slug_for_url + slug.present? ? self.slug : "#{self.id}-category" + end + def publish_categories_list MessageBus.publish('/categories', {categories: ActiveModel::ArraySerializer.new(Category.latest).as_json}) end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 98f2dcf4f7d..5c10f2f2128 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -150,6 +150,7 @@ describe Category do it "creates a blank slug, this is OK." do category.slug.should be_blank + category.slug_for_url.should == "#{category.id}-category" end end @@ -158,6 +159,7 @@ describe Category do it 'creates a blank slug' do category.slug.should be_blank + category.slug_for_url.should == "#{category.id}-category" end end @@ -169,6 +171,7 @@ describe Category do it 'is created correctly' do @category.slug.should == 'amazing-category' + @category.slug_for_url.should == @category.slug @category.description.should be_blank @@ -204,7 +207,9 @@ describe Category do describe "creating a new category with the same slug" do it "should have a blank slug" do - Fabricate(:category, name: "Amazing Categóry").slug.should be_blank + category = Fabricate(:category, name: "Amazing Categóry") + category.slug.should be_blank + category.slug_for_url.should == "#{category.id}-category" end end