slugtastic 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +4 -0
- data/lib/slugtastic/model_additions.rb +8 -0
- data/lib/slugtastic/version.rb +1 -1
- data/lib/slugtastic.rb +5 -1
- data/spec/slugtastic_spec.rb +9 -0
- metadata +1 -1
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
module Slugtastic
|
2
2
|
module ModelAdditions
|
3
3
|
|
4
|
+
# To generate a slug from another value, call <tt>has_slug</tt> in any
|
5
|
+
# ActiveRecord model and pass in the name of the slug attribute.
|
6
|
+
# By default the slug will be generated from the <tt>title</tt> attribute, but
|
7
|
+
# you can specify by adding <tt>:from => {attribute}</tt>.
|
8
|
+
#
|
9
|
+
# class Article < ActiveRecord::Base
|
10
|
+
# has_slug :slug, :from => :title
|
11
|
+
# end
|
4
12
|
def has_slug name, options = { :from => :title }
|
5
13
|
before_validation do |record|
|
6
14
|
send("#{name}=", Slugtastic.generate_slug(send(options[:from]))) if send(name).nil? or send(name).blank?
|
data/lib/slugtastic/version.rb
CHANGED
data/lib/slugtastic.rb
CHANGED
@@ -2,8 +2,12 @@ require "slugtastic/version"
|
|
2
2
|
require "slugtastic/model_additions"
|
3
3
|
require "slugtastic/railtie" if defined? Rails
|
4
4
|
|
5
|
+
# TODO: iconv will be deprecated in the future.
|
6
|
+
require 'iconv'
|
7
|
+
|
5
8
|
module Slugtastic
|
6
9
|
def self.generate_slug(string)
|
7
|
-
|
10
|
+
return if string.nil?
|
11
|
+
Iconv.iconv("ASCII//TRANSLIT//IGNORE", "UTF-8", string).join.downcase.gsub(/ /, '_').gsub(/[^a-z0-9\-_]/, '')
|
8
12
|
end
|
9
13
|
end
|
data/spec/slugtastic_spec.rb
CHANGED
@@ -3,6 +3,10 @@ require 'spec_helper'
|
|
3
3
|
|
4
4
|
describe Slugtastic do
|
5
5
|
describe ".generate_slug" do
|
6
|
+
it "returns empty if the input string is empty" do
|
7
|
+
Slugtastic.generate_slug("").should eq ""
|
8
|
+
end
|
9
|
+
|
6
10
|
it "generates a slug from a simple string" do
|
7
11
|
Slugtastic.generate_slug("A simple string.").should eq "a_simple_string"
|
8
12
|
end
|
@@ -14,5 +18,10 @@ describe Slugtastic do
|
|
14
18
|
it "handles strings with other characters in them" do
|
15
19
|
Slugtastic.generate_slug("A string, with /All sorts!").should eq "a_string_with_all_sorts"
|
16
20
|
end
|
21
|
+
|
22
|
+
it "handles basic transliteration" do
|
23
|
+
Slugtastic.generate_slug("Un été À la maison.").should eq "un_ete_a_la_maison"
|
24
|
+
Slugtastic.generate_slug("Ātri brūna lapsa").should eq "atri_bruna_lapsa"
|
25
|
+
end
|
17
26
|
end
|
18
27
|
end
|