Skip to content

Commit

Permalink
Added Ruby 1.9 compatibility. Removed dependency on ancient Unicode gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Clarke committed Mar 25, 2009
1 parent 2f296d0 commit 7bf51d8
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 37 deletions.
6 changes: 6 additions & 0 deletions History.txt
@@ -1,3 +1,9 @@
== 2.1.0 2009-03-25

2 major enhancements:
* Ruby 1.9 compatibility.
* Removed dependency on ancient Unicode gem.

== 2.0.4 2009-02-12

* 1 major enhancment:
Expand Down
6 changes: 0 additions & 6 deletions README.rdoc
Expand Up @@ -240,12 +240,6 @@ discouraged:

./script/plugin install git://github.com/norman/friendly_id.git

If you are installing as a plugin, make sure you have installed the unicode gem,
which FriendlyId depends on:

gem install unicode


== Setting it up

FriendlyId currently works with Rails 2.0.0 - 2.3.0. Here's how to set it up.
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -9,8 +9,8 @@ $hoe = Hoe.new("friendly_id", FriendlyId::Version::STRING) do |p|
p.description = 'A comprehensive slugging and pretty-URL plugin for ActiveRecord.'
p.url = 'http://friendly-id.rubyforge.org/'
p.test_globs = ['test/**/*_test.rb']
p.extra_deps << ['unicode', '>= 0.1']
p.extra_deps << ['activerecord', '>= 2.0.0']
p.extra_deps << ['activesupport', '>= 2.0.0']
p.extra_dev_deps << ['newgem', ">= #{::Newgem::VERSION}"]
p.extra_dev_deps << ['Shoulda', ">= 1.2.0"]
p.extra_dev_deps << ['sqlite3-ruby']
Expand Down
11 changes: 5 additions & 6 deletions friendly_id.gemspec
Expand Up @@ -2,11 +2,10 @@

Gem::Specification.new do |s|
s.name = %q{friendly_id}
s.version = "2.0.4"

s.version = "2.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Norman Clarke", "Adrian Mugnolo", "Emilio Tagua"]
s.date = %q{2009-02-12}
s.date = %q{2009-03-25}
s.description = %q{A comprehensive slugging and pretty-URL plugin for ActiveRecord.}
s.email = ["norman@randomba.org", "adrian@randomba.org", "miloops@gmail.com"]
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
Expand All @@ -25,23 +24,23 @@ Gem::Specification.new do |s|
s.specification_version = 2

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<unicode>, [">= 0.1"])
s.add_runtime_dependency(%q<activerecord>, [">= 2.0.0"])
s.add_runtime_dependency(%q<activesupport>, [">= 2.0.0"])
s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
s.add_development_dependency(%q<Shoulda>, [">= 1.2.0"])
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
else
s.add_dependency(%q<unicode>, [">= 0.1"])
s.add_dependency(%q<activerecord>, [">= 2.0.0"])
s.add_dependency(%q<activesupport>, [">= 2.0.0"])
s.add_dependency(%q<newgem>, [">= 1.2.3"])
s.add_dependency(%q<Shoulda>, [">= 1.2.0"])
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
s.add_dependency(%q<hoe>, [">= 1.8.0"])
end
else
s.add_dependency(%q<unicode>, [">= 0.1"])
s.add_dependency(%q<activerecord>, [">= 2.0.0"])
s.add_dependency(%q<activesupport>, [">= 2.0.0"])
s.add_dependency(%q<newgem>, [">= 1.2.3"])
s.add_dependency(%q<Shoulda>, [">= 1.2.0"])
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
Expand Down
51 changes: 36 additions & 15 deletions lib/friendly_id/slug.rb
@@ -1,9 +1,23 @@
require 'unicode'
#encoding: utf-8

# A Slug is a unique, human-friendly identifier for an ActiveRecord.
class Slug < ActiveRecord::Base

belongs_to :sluggable, :polymorphic => true
before_save :check_for_blank_name, :set_sequence


ASCII_APPROXIMATIONS = {
198 => "AE",
208 => "D",
216 => "O",
222 => "Th",
223 => "ss",
230 => "ae",
240 => "d",
248 => "o",
254 => "th"
}.freeze

class << self

Expand All @@ -13,20 +27,14 @@ class << self
#
# slug.normalize('This... is an example!') # => "this-is-an-example"
#
# Note that Rails 2.2.x offers a parameterize method for this. It's not
# used here because it assumes you want to strip away accented characters,
# and this may not always be your desire.
#
# At the time of writing, it also handles several characters incorrectly,
# for instance replacing Icelandic's "thorn" character with "y" rather
# than "d." This might be pedantic, but I don't want to piss off the
# Vikings. The last time anyone pissed them off, they uleashed a wave of
# terror in Europe unlike anything ever seen before or after. I'm not
# taking any chances.
# Note that the Unicode handling in ActiveSupport may fail to process some
# characters from Polish, Icelandic and other languages. If your
# application uses these languages, check {out this
# article}[http://link-coming-soon.com] for information on how to get
# better urls in your application.
def normalize(slug_text)
return "" if slug_text.nil? || slug_text == ""
Unicode::normalize_KC(slug_text).
send(chars_func).
ActiveSupport::Multibyte.proxy_class.new(slug_text).normalize(:kc).
# For some reason Spanish ¡ and ¿ are not detected as non-word
# characters. Bug in Ruby?
gsub(/[\W|¡|¿]/u, ' ').
Expand All @@ -36,6 +44,10 @@ def normalize(slug_text)
downcase.
to_s
end

def postnormalize(string)
string.gs
end

def parse(friendly_id)
name, sequence = friendly_id.split('--')
Expand All @@ -46,8 +58,17 @@ def parse(friendly_id)
# Remove diacritics (accents, umlauts, etc.) from the string. Borrowed
# from "The Ruby Way."
def strip_diacritics(string)
Unicode::normalize_KD(string).unpack('U*').select { |u| u < 0x300 || u > 0x036F }.pack('U*')
ActiveSupport::Multibyte.proxy_class.new(string).normalize(:kd).unpack('U*').inject([]) { |a, u|
if ASCII_APPROXIMATIONS[u]
a += ASCII_APPROXIMATIONS[u].unpack('U*')
elsif (u < 0x300 || u > 0x036F)
a << u
end
a
}.pack('U*')
end



# Remove non-ascii characters from the string.
def strip_non_ascii(string)
Expand Down Expand Up @@ -75,7 +96,7 @@ def to_friendly_id

# Raise a FriendlyId::SlugGenerationError if the slug name is blank.
def check_for_blank_name #:nodoc:#
if name == "" || name.nil?
if name.blank?
raise FriendlyId::SlugGenerationError.new("The slug text is blank.")
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/friendly_id/version.rb
@@ -1,8 +1,8 @@
module FriendlyId #:nodoc:
module Version #:nodoc:
MAJOR = 2
MINOR = 0
TINY = 4
MINOR = 1
TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.')
end
end
4 changes: 2 additions & 2 deletions test/slug_test.rb
Expand Up @@ -39,8 +39,8 @@ class SlugTest < Test::Unit::TestCase
input = "ÀÁÂÃÄÅÆÇÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"
output = Slug::strip_diacritics(input).split(//)
expected = "AAAAAAAECEEEIIIIDNOOOOOOUUUUYThssaaaaaaaeceeeeiiiidnoooooouuuuythy".split(//)
output.split.each_index do |i|
assert_equal output[i], expected[i]
output.each_index do |i|
assert_equal expected[i], output[i]
end
end

Expand Down
8 changes: 4 additions & 4 deletions test/sti_test.rb
Expand Up @@ -17,9 +17,9 @@ class SluggedModelTest < Test::Unit::TestCase
should "have a slug" do
assert_not_nil @novel.slug
end

context "found by its friendly id" do

setup do
@novel = Novel.find(@novel.friendly_id)
end
Expand All @@ -29,10 +29,10 @@ class SluggedModelTest < Test::Unit::TestCase
end

end


context "found by its numeric id" do

setup do
@novel = Novel.find(@novel.id)
end
Expand Down
5 changes: 4 additions & 1 deletion test/test_helper.rb
@@ -1,15 +1,18 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
$:.unshift(File.dirname(__FILE__))
$VERBOSE=false
$KCODE = 'UTF8' if RUBY_VERSION < '1.9'
$VERBOSE = false
require 'rubygems'
require 'test/unit'
require 'shoulda'
# You can use "rake test AR_VERSION=2.0.5" to test against 2.0.5, for example.
# The default is to use the latest installed ActiveRecord.
if ENV["AR_VERSION"]
gem 'activerecord', "#{ENV["AR_VERSION"]}"
gem 'activesupport', "#{ENV["AR_VERSION"]}"
end
require 'active_record'
require 'active_support'

require 'friendly_id'
require 'models/post'
Expand Down

0 comments on commit 7bf51d8

Please sign in to comment.