From 4c00eef8b59a390db26817d192260e7ecb1ba894 Mon Sep 17 00:00:00 2001 From: Erick Guan Date: Wed, 13 May 2015 16:52:48 +0800 Subject: [PATCH] FIX: category custom slug can't be set when generation method is none --- app/models/category.rb | 2 +- lib/slug.rb | 4 ++++ spec/controllers/categories_controller_spec.rb | 14 ++++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index b5dca32e9af..1b9610c9edf 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -207,7 +207,7 @@ SQL if slug.present? # santized custom slug - self.slug = Slug.for(slug, '') + self.slug = Slug.sanitize(slug) errors.add(:slug, 'is already in use') if duplicate_slug? else # auto slug diff --git a/lib/slug.rb b/lib/slug.rb index c736a86d3a5..e2c876de701 100644 --- a/lib/slug.rb +++ b/lib/slug.rb @@ -13,6 +13,10 @@ module Slug slug.blank? ? default : slug end + def self.sanitize(string) + self.encoded_generator(string) + end + private def self.ascii_generator(string) diff --git a/spec/controllers/categories_controller_spec.rb b/spec/controllers/categories_controller_spec.rb index 0f1a4e93476..48b32dac65e 100644 --- a/spec/controllers/categories_controller_spec.rb +++ b/spec/controllers/categories_controller_spec.rb @@ -232,15 +232,21 @@ describe CategoriesController do it 'accepts valid custom slug' do xhr :put, :update_slug, category_id: @category.id, slug: 'valid-slug' expect(response).to be_success - category = Category.find(@category.id) - expect(category.slug).to eq('valid-slug') + expect(@category.reload.slug).to eq('valid-slug') end it 'accepts not well formed custom slug' do xhr :put, :update_slug, category_id: @category.id, slug: ' valid slug' expect(response).to be_success - category = Category.find(@category.id) - expect(category.slug).to eq('valid-slug') + expect(@category.reload.slug).to eq('valid-slug') + end + + it 'accepts and sanitize custom slug when the slug generation method is not english' do + SiteSetting.slug_generation_method = 'none' + xhr :put, :update_slug, category_id: @category.id, slug: ' another !_ slug @' + expect(response).to be_success + expect(@category.reload.slug).to eq('another-slug') + SiteSetting.slug_generation_method = 'ascii' end it 'rejects invalid custom slug' do