slug 0.5.4 → 0.5.5
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/VERSION.yml +2 -2
- data/lib/slug/slug.rb +14 -8
- data/slug.gemspec +2 -2
- data/test/models.rb +4 -0
- data/test/schema.rb +5 -0
- data/test/test_slug.rb +23 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/slug/slug.rb
CHANGED
@@ -8,10 +8,13 @@ module Slug
|
|
8
8
|
#
|
9
9
|
# Options:
|
10
10
|
# * <tt>:column</tt> - the column the slug will be saved to (defaults to <tt>:slug</tt>)
|
11
|
+
# * <tt>:validates_uniquness_if</tt> - proc to determine whether uniqueness validation runs, same format as validates_uniquness_of :if
|
11
12
|
#
|
12
|
-
# Slug will take care of validating presence and uniqueness of slug.
|
13
|
-
|
14
|
-
#
|
13
|
+
# Slug will take care of validating presence and uniqueness of slug.
|
14
|
+
|
15
|
+
# Before create, Slug will generate and assign the slug if it wasn't explicitly set.
|
16
|
+
# Note that subsequent changes to the source column will have no effect on the slug.
|
17
|
+
# If you'd like to update the slug later on, call <tt>@model.set_slug</tt>
|
15
18
|
def slug source, opts={}
|
16
19
|
class_inheritable_accessor :slug_source, :slug_column
|
17
20
|
include InstanceMethods
|
@@ -19,9 +22,12 @@ module Slug
|
|
19
22
|
self.slug_source = source
|
20
23
|
|
21
24
|
self.slug_column = opts.has_key?(:column) ? opts[:column] : :slug
|
25
|
+
|
26
|
+
uniqueness_opts = {}
|
27
|
+
uniqueness_opts.merge!(:if => opts[:validates_uniqueness_if]) if opts[:validates_uniqueness_if].present?
|
22
28
|
|
23
29
|
validates_presence_of self.slug_column, :message => "cannot be blank. Is #{self.slug_source} sluggable?"
|
24
|
-
validates_uniqueness_of self.slug_column
|
30
|
+
validates_uniqueness_of self.slug_column, uniqueness_opts
|
25
31
|
validates_format_of self.slug_column, :with => /^[a-z0-9-]+$/, :message => "contains invalid characters. Only downcase letters, numbers, and '-' are allowed."
|
26
32
|
before_validation_on_create :set_slug
|
27
33
|
end
|
@@ -32,7 +38,7 @@ module Slug
|
|
32
38
|
# Sets the slug. Called before create.
|
33
39
|
def set_slug
|
34
40
|
validate_slug_columns
|
35
|
-
self[self.slug_column] = self.send(self.slug_source)
|
41
|
+
self[self.slug_column] = self.send(self.slug_source) if self[self.slug_column].blank?
|
36
42
|
|
37
43
|
strip_diacritics_from_slug
|
38
44
|
normalize_slug
|
@@ -63,9 +69,9 @@ module Slug
|
|
63
69
|
s = ActiveSupport::Multibyte.proxy_class.new(self[self.slug_column]).normalize(:kc)
|
64
70
|
s.downcase!
|
65
71
|
s.strip!
|
66
|
-
s.gsub!(/[^\w\s-]
|
67
|
-
s.gsub!(/\s
|
68
|
-
s.gsub!(/-\z
|
72
|
+
s.gsub!(/[^\w\s-]/, '') # Remove non-word characters
|
73
|
+
s.gsub!(/\s+/, '-') # Convert whitespaces to dashes
|
74
|
+
s.gsub!(/-\z/, '') # Remove trailing dashes
|
69
75
|
s.gsub!(/-+/, '-') # get rid of double-dashes
|
70
76
|
self[self.slug_column] = s.to_s
|
71
77
|
end
|
data/slug.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{slug}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.5"
|
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{2010-
|
12
|
+
s.date = %q{2010-04-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 = [
|
data/test/models.rb
CHANGED
@@ -13,6 +13,10 @@ class Company < ActiveRecord::Base
|
|
13
13
|
slug :name
|
14
14
|
end
|
15
15
|
|
16
|
+
class Post < ActiveRecord::Base
|
17
|
+
slug :headline, :validates_uniqueness_if => Proc.new { false }
|
18
|
+
end
|
19
|
+
|
16
20
|
# Used to test slugs based on methods rather than database attributes
|
17
21
|
class Event < ActiveRecord::Base
|
18
22
|
slug :title_for_slug
|
data/test/schema.rb
CHANGED
@@ -16,6 +16,11 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
16
16
|
t.column "slug", "string"
|
17
17
|
end
|
18
18
|
|
19
|
+
create_table "posts", :force => true do |t|
|
20
|
+
t.column "headline", "string"
|
21
|
+
t.column "slug", "string"
|
22
|
+
end
|
23
|
+
|
19
24
|
create_table "events", :force => true do |t|
|
20
25
|
t.column "title", "string"
|
21
26
|
t.column "location", "string"
|
data/test/test_slug.rb
CHANGED
@@ -67,7 +67,30 @@ class TestSlug < Test::Unit::TestCase
|
|
67
67
|
assert !article.valid?
|
68
68
|
assert article.errors[:slug].present?
|
69
69
|
end
|
70
|
+
|
71
|
+
should "validate uniqueness of slug by default" do
|
72
|
+
article1 = Article.create!(:headline => 'Test Headline')
|
73
|
+
article2 = Article.create!(:headline => 'Test Headline')
|
74
|
+
article2.slug = 'test-headline'
|
75
|
+
|
76
|
+
assert !article2.valid?
|
77
|
+
assert article2.errors[:slug].present?
|
78
|
+
end
|
70
79
|
|
80
|
+
should "use validate_uniquness_if proc to decide whether uniqueness validation applies" do
|
81
|
+
article1 = Post.create!(:headline => 'Test Headline')
|
82
|
+
article2 = Post.new
|
83
|
+
article2.slug = 'test-headline'
|
84
|
+
|
85
|
+
assert article2.valid?
|
86
|
+
end
|
87
|
+
|
88
|
+
should "not overwrite slug value on create if it was already specified" do
|
89
|
+
a = Article.create!(:headline => 'Test Headline', :slug => 'slug1')
|
90
|
+
assert_equal 'slug1', a.slug
|
91
|
+
end
|
92
|
+
|
93
|
+
|
71
94
|
context "slug normalization" do
|
72
95
|
setup do
|
73
96
|
@article = Article.new
|
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.
|
4
|
+
version: 0.5.5
|
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: 2010-
|
12
|
+
date: 2010-04-29 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|