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

View File

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