mirror of
https://github.com/danbee/slugtastic
synced 2025-03-04 08:49:05 +00:00
Slug generation now uses parameterize.
This commit is contained in:
parent
edaaa06930
commit
066d38cb9b
@ -1,3 +1,9 @@
|
||||
## v1.1.0
|
||||
|
||||
* Slugs are now generated with `parameterize`.
|
||||
* Spaces get converted to hyphens instead of underscores. Hyphens in the original string are collapsed.
|
||||
* Removed dependence on iconv.
|
||||
|
||||
## v1.0.0
|
||||
|
||||
* Added test for slugs with numbers.
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
slugtastic (0.2.1)
|
||||
slugtastic (1.1.0)
|
||||
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
|
||||
@ -2,12 +2,9 @@ require "slugtastic/version"
|
||||
require "slugtastic/model_additions"
|
||||
require "slugtastic/railtie" if defined? Rails
|
||||
|
||||
# TODO: iconv will be deprecated in the future.
|
||||
require 'iconv'
|
||||
|
||||
module Slugtastic
|
||||
def self.generate_slug(string)
|
||||
return if string.nil?
|
||||
Iconv.iconv("ASCII//TRANSLIT//IGNORE", "UTF-8", string).join.downcase.gsub(/ /, '_').gsub(/[^a-z0-9\-_]/, '')
|
||||
string.parameterize
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
module Slugtastic
|
||||
VERSION = "1.0.0"
|
||||
VERSION = "1.1.0"
|
||||
end
|
||||
|
||||
@ -10,15 +10,15 @@ end
|
||||
|
||||
describe Slugtastic::ModelAdditions do
|
||||
it "generates a slug from the title" do
|
||||
Model.create!(:title => "A Simple Title").slug.should eq "a_simple_title"
|
||||
Model.create!(:title => "A Simple Title").slug.should eq "a-simple-title"
|
||||
end
|
||||
|
||||
it "doesn't regenerate the slug if it already exists" do
|
||||
model = Model.create!(:title => "A Simple Title")
|
||||
model.slug.should eq "a_simple_title"
|
||||
model.slug.should eq "a-simple-title"
|
||||
|
||||
model.title = "A new title"
|
||||
model.save
|
||||
model.slug.should eq "a_simple_title"
|
||||
model.slug.should eq "a-simple-title"
|
||||
end
|
||||
end
|
||||
|
||||
@ -8,24 +8,24 @@ describe Slugtastic do
|
||||
end
|
||||
|
||||
it "generates a slug from a simple string" do
|
||||
Slugtastic.generate_slug("A simple string.").should eq "a_simple_string"
|
||||
Slugtastic.generate_slug("A simple string.").should eq "a-simple-string"
|
||||
end
|
||||
|
||||
it "generates a slug from a string with numbers" do
|
||||
Slugtastic.generate_slug("Slugtastic was built in 2012.").should eq "slugtastic_was_built_in_2012"
|
||||
Slugtastic.generate_slug("Slugtastic was built in 2012.").should eq "slugtastic-was-built-in-2012"
|
||||
end
|
||||
|
||||
it "handles strings with hypens in them" do
|
||||
Slugtastic.generate_slug("A string - with Hyphens").should eq "a_string_-_with_hyphens"
|
||||
Slugtastic.generate_slug("A string - with Hyphens").should eq "a-string-with-hyphens"
|
||||
end
|
||||
|
||||
it "handles strings with other characters in them" do
|
||||
Slugtastic.generate_slug("A string, with /All sorts!").should eq "a_string_with_all_sorts"
|
||||
Slugtastic.generate_slug("A string, with /All sorts!").should eq "a-string-with-all-sorts"
|
||||
end
|
||||
|
||||
it "handles basic transliteration" do
|
||||
Slugtastic.generate_slug("Un été À la maison.").should eq "un_ete_a_la_maison"
|
||||
Slugtastic.generate_slug("Ātri brūna lapsa").should eq "atri_bruna_lapsa"
|
||||
Slugtastic.generate_slug("Un été À la maison.").should eq "un-ete-a-la-maison"
|
||||
Slugtastic.generate_slug("Ātri brūna lapsa").should eq "atri-bruna-lapsa"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user