From be29354d2325e975fb98a3e9ce48a08ecf8c4ddd Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 12 Jun 2026 17:29:40 +0200 Subject: [PATCH 1/5] Move and improve specs for StringExt#to_url --- spec/models/article_spec.rb | 10 ------- spec/publify_core/string_ext_spec.rb | 41 ++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 57839f04..ec278893 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -132,16 +132,6 @@ expect(not_found).to be_nil end - it "test_strip_title" do - expect("Article-3".to_url).to eq "article-3" - expect("Article 3!?#".to_url).to eq "article-3" - expect("There is Sex in my Violence!".to_url).to eq "there-is-sex-in-my-violence" - expect("-article-".to_url).to eq "article" - expect("Lorem ipsum dolor sit amet, consectetaur adipisicing elit".to_url) - .to eq "lorem-ipsum-dolor-sit-amet-consectetaur-adipisicing-elit" - expect("My Cat's Best Friend".to_url).to eq "my-cats-best-friend" - end - describe "#set_permalink" do it "works for simple cases" do a = blog.articles.build(title: "Article 3!", state: :published) diff --git a/spec/publify_core/string_ext_spec.rb b/spec/publify_core/string_ext_spec.rb index 4e9b40c4..c8d72527 100644 --- a/spec/publify_core/string_ext_spec.rb +++ b/spec/publify_core/string_ext_spec.rb @@ -11,8 +11,45 @@ end describe "to_url" do - it "gives a proper space-less, trimmed URL" do - expect(" this is a sentence ".to_url).to eq("this-is-a-sentence") + it "downcases the string" do + expect("Article-3".to_url).to eq "article-3" + end + + it "strips trailing special characters" do + expect("Article 3!?#".to_url).to eq "article-3" + end + + it "replaces special characters with a dash" do + expect("foo@bar".to_url).to eq "foo-bar" + end + + it "handles more than two words" do + expect("There is Sex in my Violence!".to_url).to eq "there-is-sex-in-my-violence" + end + + it "strips leading and trailing dashes" do + expect("-article-".to_url).to eq "article" + end + + it "handles punctuation in the middle of th string" do + expect("Lorem ipsum dolor sit amet, consectetaur adipisicing elit".to_url) + .to eq "lorem-ipsum-dolor-sit-amet-consectetaur-adipisicing-elit" + end + + it "drops leading and trailing spaces" do + expect(" this is a sentence ".to_url).to eq("this-is-a-sentence") + end + + it "compresses multiple spaces to a single space" do + expect("this is a sentence".to_url).to eq("this-is-a-sentence") + end + + it "drops single quotes instead of replacing them with a dash" do + expect("My Cat's Best Friend".to_url).to eq "my-cats-best-friend" + end + + it "drops double quotes instead of replacing them with a dash" do + expect("bar\"baz".to_url).to eq "barbaz" end end end From 77384030a3539d68b6766a7ad9cd9c6e801dcdc1 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 12 Jun 2026 17:49:28 +0200 Subject: [PATCH 2/5] Improve specs for StringExt#to_permalink --- spec/publify_core/string_ext_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/publify_core/string_ext_spec.rb b/spec/publify_core/string_ext_spec.rb index c8d72527..4a0a474a 100644 --- a/spec/publify_core/string_ext_spec.rb +++ b/spec/publify_core/string_ext_spec.rb @@ -8,6 +8,11 @@ expect("L'été s'ra chaud, l'été s'ra chaud".to_permalink) .to eq("l-ete-s-ra-chaud-l-ete-s-ra-chaud") end + + it "strips html" do + expect("This is a test".to_permalink) + .to eq "this-is-a-test" + end end describe "to_url" do From 41c4f3ee6aeae4842a9805fe24a531c7ff7f4ee6 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 12 Jun 2026 17:50:52 +0200 Subject: [PATCH 3/5] Remove useless Redirect#to_url method --- app/models/redirect.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/redirect.rb b/app/models/redirect.rb index edd0f488..d59041ea 100644 --- a/app/models/redirect.rb +++ b/app/models/redirect.rb @@ -31,10 +31,6 @@ def shorten end end - def to_url - raise "Use #from_url" - end - def from_url File.join(blog.shortener_url, from_path) end From 1e2ed180cce4031e8fc67cdc086d1c6f2d5267e0 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 12 Jun 2026 18:23:44 +0200 Subject: [PATCH 4/5] Use StringExt#to_permalink exclusively --- app/models/tag.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index 25233393..71bf9a5b 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -19,11 +19,9 @@ def self.create_from_article!(article) tags = [] Tag.transaction do - tagwords = article.keywords.to_s.scan(/((['"]).*?\2|[.:[[:alnum:]]]+)/).map do |x| - x.first.tr("\"'", "") - end + tagwords = article.keywords.to_s.scan(/((['"]).*?\2|[.:[[:alnum:]]]+)/).map(&:first) tagwords.uniq.each do |tagword| - tagname = tagword.to_url + tagname = tagword.to_permalink tags << article.blog.tags.find_or_create_by(name: tagname) do |tag| tag.display_name = tagword end @@ -35,7 +33,7 @@ def self.create_from_article!(article) def ensure_naming_conventions self.display_name = name if display_name.blank? - self.name = display_name.to_url if display_name.present? + self.name = display_name.to_permalink if display_name.present? end def self.find_all_with_content_counters From 16ee52acae69bee616fe3c90a72a20a2d6c61d38 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Fri, 12 Jun 2026 18:41:29 +0200 Subject: [PATCH 5/5] Make StringExt#to_url an implementation detail of #to_permalink With this change, #to_url can no longer be used. --- lib/publify_core/string_ext.rb | 10 ++++++---- spec/publify_core/string_ext_spec.rb | 25 ++++++++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/publify_core/string_ext.rb b/lib/publify_core/string_ext.rb index e5042c7f..bccb2c1a 100644 --- a/lib/publify_core/string_ext.rb +++ b/lib/publify_core/string_ext.rb @@ -21,6 +21,12 @@ def to_permalink string.gsub(/<[^>]*>/, "").to_url end + def to_title(item, settings, params) + TitleBuilder.new(self).build(item, settings, params) + end + + protected + # Returns a-string-with-dashes when passed 'a string with dashes'. # All special chars are stripped in the process def to_url @@ -30,10 +36,6 @@ def to_url s = s.gsub(/\P{Word}/, " ") s.strip.tr_s(" ", "-").tr(" ", "-").sub(/^$/, "-") end - - def to_title(item, settings, params) - TitleBuilder.new(self).build(item, settings, params) - end end end diff --git a/spec/publify_core/string_ext_spec.rb b/spec/publify_core/string_ext_spec.rb index 4a0a474a..51493b1c 100644 --- a/spec/publify_core/string_ext_spec.rb +++ b/spec/publify_core/string_ext_spec.rb @@ -13,48 +13,47 @@ expect("This is a test".to_permalink) .to eq "this-is-a-test" end - end - describe "to_url" do it "downcases the string" do - expect("Article-3".to_url).to eq "article-3" + expect("Article-3".to_permalink).to eq "article-3" end it "strips trailing special characters" do - expect("Article 3!?#".to_url).to eq "article-3" + expect("Article 3!?#".to_permalink).to eq "article-3" end it "replaces special characters with a dash" do - expect("foo@bar".to_url).to eq "foo-bar" + expect("foo@bar".to_permalink).to eq "foo-bar" end it "handles more than two words" do - expect("There is Sex in my Violence!".to_url).to eq "there-is-sex-in-my-violence" + expect("There is Sex in my Violence!".to_permalink) + .to eq "there-is-sex-in-my-violence" end it "strips leading and trailing dashes" do - expect("-article-".to_url).to eq "article" + expect("-article-".to_permalink).to eq "article" end it "handles punctuation in the middle of th string" do - expect("Lorem ipsum dolor sit amet, consectetaur adipisicing elit".to_url) + expect("Lorem ipsum dolor sit amet, consectetaur adipisicing elit".to_permalink) .to eq "lorem-ipsum-dolor-sit-amet-consectetaur-adipisicing-elit" end it "drops leading and trailing spaces" do - expect(" this is a sentence ".to_url).to eq("this-is-a-sentence") + expect(" this is a sentence ".to_permalink).to eq("this-is-a-sentence") end it "compresses multiple spaces to a single space" do - expect("this is a sentence".to_url).to eq("this-is-a-sentence") + expect("this is a sentence".to_permalink).to eq("this-is-a-sentence") end - it "drops single quotes instead of replacing them with a dash" do - expect("My Cat's Best Friend".to_url).to eq "my-cats-best-friend" + it "replaces single quotes with a dash" do + expect("My Cat's Best Friend".to_permalink).to eq "my-cat-s-best-friend" end it "drops double quotes instead of replacing them with a dash" do - expect("bar\"baz".to_url).to eq "barbaz" + expect("bar\"baz".to_permalink).to eq "barbaz" end end end