slug 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 5
3
- :patch: 2
3
+ :patch: 3
4
4
  :major: 0
data/lib/slug/slug.rb CHANGED
@@ -20,8 +20,9 @@ module Slug
20
20
 
21
21
  self.slug_column = opts.has_key?(:column) ? opts[:column] : :slug
22
22
 
23
- validates_presence_of self.slug_column, :message => "#{self.slug_column} cannot be blank. Is #{self.slug_source} sluggable?"
24
- validates_uniqueness_of self.slug_column
23
+ validates_presence_of self.slug_column, :message => "#{self.slug_column} cannot be blank. Is #{self.slug_source} sluggable?"
24
+ validates_uniqueness_of self.slug_column
25
+ validates_format_of self.slug_column, :with => /^[a-z0-9-]+/, :message => "#{self.slug_column} contains invalid characters. Only downcase letters, numbers, and '-' are allowed."
25
26
  before_validation_on_create :set_slug
26
27
  end
27
28
  end
@@ -54,7 +55,7 @@ module Slug
54
55
  raise ArgumentError, "Source column '#{self.slug_source}' does not exist!" if !self.respond_to?(self.slug_source)
55
56
  raise ArgumentError, "Slug column '#{self.slug_column}' does not exist!" if !self.respond_to?("#{self.slug_column}=")
56
57
  end
57
-
58
+
58
59
  # Takes the slug, downcases it and replaces non-word characters with a -.
59
60
  # Feel free to override this method if you'd like different slug formatting.
60
61
  def normalize_slug
@@ -62,9 +63,10 @@ module Slug
62
63
  s = ActiveSupport::Multibyte.proxy_class.new(self[self.slug_column]).normalize(:kc)
63
64
  s.downcase!
64
65
  s.strip!
65
- s.gsub!(/[\W]/u, ' ') # Remove non-word characters
66
- s.gsub!(/\s+/u, '-') # Convert whitespaces to dashes
67
- s.gsub!(/-\z/u, '') # Remove trailing dashes
66
+ s.gsub!(/[^\w\s-]/u, '') # Remove non-word characters
67
+ s.gsub!(/\s+/u, '-') # Convert whitespaces to dashes
68
+ s.gsub!(/-\z/u, '') # Remove trailing dashes
69
+ s.gsub!(/-+/, '-') # get rid of double-dashes
68
70
  self[self.slug_column] = s.to_s
69
71
  end
70
72
 
@@ -83,7 +85,6 @@ module Slug
83
85
  a
84
86
  end
85
87
  s = s.pack('U*')
86
- s.gsub!(/[^a-z0-9]+/i, ' ')
87
88
  self[self.slug_column] = s.to_s
88
89
  end
89
90
 
data/slug.gemspec CHANGED
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{slug}
8
- s.version = "0.5.2"
8
+ s.version = "0.5.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Koski"]
12
- s.date = %q{2009-12-07}
12
+ s.date = %q{2010-03-29}
13
13
  s.description = %q{Simple, straightforward slugs for your ActiveRecord models.}
14
14
  s.email = %q{ben.koski@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -59,4 +59,3 @@ Gem::Specification.new do |s|
59
59
  s.add_dependency(%q<activesupport>, [">= 0"])
60
60
  end
61
61
  end
62
-
data/test/test_slug.rb CHANGED
@@ -45,15 +45,12 @@ class TestSlug < Test::Unit::TestCase
45
45
  article = Article.create
46
46
  assert !article.valid?
47
47
  require 'ruby-debug'
48
- debugger if article.errors.count > 1
49
- assert_equal 1, article.errors.count
50
48
  assert article.errors.on(:slug)
51
49
  end
52
50
 
53
51
  should "set validation error if normalization makes source value empty" do
54
- article = Article.create(:headline => '---')
52
+ article = Article.create(:headline => '$$$')
55
53
  assert !article.valid?
56
- assert_equal 1, article.errors.count
57
54
  assert article.errors.on(:slug)
58
55
  end
59
56
 
@@ -63,6 +60,14 @@ class TestSlug < Test::Unit::TestCase
63
60
  assert_equal 'test-headline', article.slug
64
61
  end
65
62
 
63
+ should "validate slug format on save" do
64
+ article = Article.create!(:headline => 'Test Headline')
65
+ article.slug = 'A BAD $LUG.'
66
+
67
+ assert !article.valid?
68
+ assert article.errors[:slug].present?
69
+ end
70
+
66
71
  context "slug normalization" do
67
72
  setup do
68
73
  @article = Article.new
@@ -127,6 +132,24 @@ class TestSlug < Test::Unit::TestCase
127
132
  @article.save!
128
133
  assert_match 'a-b-c-d', @article.slug
129
134
  end
135
+
136
+ should "not insert dashes for periods in acronyms, regardless of where they appear in string" do
137
+ @article.headline = "N.Y.P.D. vs. N.S.A. vs. F.B.I."
138
+ @article.save!
139
+ assert_match 'nypd-vs-nsa-vs-fbi', @article.slug
140
+ end
141
+
142
+ should "not insert dashes for apostrophes" do
143
+ @article.headline = "Thomas Jefferson's Papers"
144
+ @article.save!
145
+ assert_match 'thomas-jeffersons-papers', @article.slug
146
+ end
147
+
148
+ should "preserve numbers in slug" do
149
+ @article.headline = "2010 Election"
150
+ @article.save!
151
+ assert_match '2010-election', @article.slug
152
+ end
130
153
  end
131
154
 
132
155
  context "diacritics handling" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Koski
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-07 00:00:00 -05:00
12
+ date: 2010-03-29 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency