Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/models/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/publify_core/string_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
10 changes: 0 additions & 10 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 45 additions & 4 deletions spec/publify_core/string_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <i>is</i> a <b>test</b>".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
Loading