mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 14:03:22 +08:00
FIX: Topic#title
were being parameterized when encoded slug is
enabled.
This commit is contained in:
parent
70aed105a6
commit
401fbdbfe7
21
lib/slug.rb
21
lib/slug.rb
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue
Block a user