FIX: Topic#title were being parameterized when encoded slug is

enabled.
This commit is contained in:
Guo Xiang Tan 2017-10-27 11:02:12 +08:00
parent 70aed105a6
commit 401fbdbfe7
2 changed files with 24 additions and 17 deletions

View File

@ -25,11 +25,12 @@ module Slug
private
def self.prettify_slug(slug, max_length: MAX_LENGTH)
slug.tr!("_", "-")
slug = slug.squeeze('-') # squeeze continuous dashes to prettify slug
slug.gsub!(/\A-+|-+\z/, '') # remove possible trailing and preceding dashes
slug.truncate(max_length, omission: '')
def self.prettify_slug(slug, max_length:)
slug
.tr("_", "-")
.truncate(max_length, omission: '')
.squeeze('-') # squeeze continuous dashes to prettify slug
.gsub(/\A-+|-+\z/, '') # remove possible trailing and preceding dashes
end
def self.ascii_generator(string)
@ -40,11 +41,11 @@ module Slug
# This generator will sanitize almost all special characters,
# including reserved characters from RFC3986.
# See also URI::REGEXP::PATTERN.
string.strip!
string.gsub!(/\s+/, '-')
string.gsub!(CHAR_FILTER_REGEXP, '')
string.downcase! if downcase
string
string = string.strip
.gsub(/\s+/, '-')
.gsub(CHAR_FILTER_REGEXP, '')
downcase ? string.downcase : string
end
def self.none_generator(string)

View File

@ -123,21 +123,25 @@ describe Topic do
context 'slug' do
let(:title) { "hello world topic" }
let(:slug) { "hello-world-topic" }
let!(:expected_title) { title.dup }
let!(:expected_slug) { slug.dup }
let(:topic) { Fabricate.build(:topic, title: title) }
context 'encoded generator' do
before { SiteSetting.slug_generation_method = 'encoded' }
after { SiteSetting.slug_generation_method = 'ascii' }
it "returns a Slug for a title" do
Slug.expects(:for).with(title).returns(slug)
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
expect(topic.title).to eq(expected_title)
expect(topic.slug).to eq(expected_slug)
end
context 'for cjk characters' do
let(:title) { "熱帶風暴畫眉" }
let(:slug) { "熱帶風暴畫眉" }
let!(:expected_title) { title.dup }
it "returns encoded Slug for a title" do
Slug.expects(:for).with(title).returns(slug)
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
expect(topic.title).to eq(expected_title)
expect(topic.slug).to eq(expected_title)
end
end
@ -153,7 +157,7 @@ describe Topic do
context 'none generator' do
before { SiteSetting.slug_generation_method = 'none' }
after { SiteSetting.slug_generation_method = 'ascii' }
let(:title) { "熱帶風暴畫眉" }
let(:slug) { "topic" }
@ -165,6 +169,7 @@ describe Topic do
context '#ascii_generator' do
before { SiteSetting.slug_generation_method = 'ascii' }
it "returns a Slug for a title" do
Slug.expects(:for).with(title).returns(slug)
expect(Fabricate.build(:topic, title: title).slug).to eq(slug)
@ -173,6 +178,7 @@ describe Topic do
context 'for cjk characters' do
let(:title) { "熱帶風暴畫眉" }
let(:slug) { 'topic' }
it "returns 'topic' when the slug is empty (say, non-latin characters)" do
Slug.expects(:for).with(title).returns("topic")
expect(Fabricate.build(:topic, title: title).slug).to eq("topic")