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
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
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/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..51493b1c 100644
--- a/spec/publify_core/string_ext_spec.rb
+++ b/spec/publify_core/string_ext_spec.rb
@@ -8,11 +8,52 @@
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
- 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 "strips html" do
+ expect("This is a test".to_permalink)
+ .to eq "this-is-a-test"
+ end
+
+ it "downcases the string" do
+ expect("Article-3".to_permalink).to eq "article-3"
+ end
+
+ it "strips trailing special characters" do
+ expect("Article 3!?#".to_permalink).to eq "article-3"
+ end
+
+ it "replaces special characters with a dash" do
+ 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_permalink)
+ .to eq "there-is-sex-in-my-violence"
+ end
+
+ it "strips leading and trailing dashes" do
+ 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_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_permalink).to eq("this-is-a-sentence")
+ end
+
+ it "compresses multiple spaces to a single space" do
+ expect("this is a sentence".to_permalink).to eq("this-is-a-sentence")
+ end
+
+ 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_permalink).to eq "barbaz"
end
end
end