the_force 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/the_force/slugs.rb +21 -0
- data/lib/the_force.rb +1 -0
- data/test/unit/test_slugs.rb +35 -0
- metadata +7 -4
data/Rakefile
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# CRZ - given a name to slug-ize, and either a list or a table and column, make a unique slug
|
2
|
+
# - There's are inherent race conditions here, naturally.
|
3
|
+
# - Should go like "b" -> "b_1"
|
4
|
+
# e.g. TheForce.unique_slug("My Gallery", Gallery, :permalink)
|
5
|
+
# e.g. TheForce.unique_slug("My Gallery", ["My_Gallery", "MY GALLERY"])
|
6
|
+
# e.g. before_validate 'self.permaslug = TheForce.unique_slug self.title, Article, :permaslug'
|
7
|
+
# e.g. before_validate 'self.permaslug = TheForce.unique_slug self.heading, Section.find_all_by_article_id(self.article.id).map {|s| s.heading}'
|
8
|
+
module TheForce
|
9
|
+
def self.unique_slug(title, *others)
|
10
|
+
arr = others[0].find(:all).map { |e| e.send(others[1]) } if others.length == 2
|
11
|
+
arr = others.first if others.length == 1
|
12
|
+
|
13
|
+
base = title.strip.downcase.gsub(/\s+/, '_').gsub(/[^a-zA-Z0-9_]+/, '')
|
14
|
+
slug, suffix = base, "1"
|
15
|
+
while arr.include?(slug) do
|
16
|
+
suffix.succ!
|
17
|
+
slug = "#{base}_#{suffix}"
|
18
|
+
end
|
19
|
+
slug
|
20
|
+
end
|
21
|
+
end
|
data/lib/the_force.rb
CHANGED
@@ -3,6 +3,7 @@ require File.join(ROOT, 'ruby_version')
|
|
3
3
|
require File.join(ROOT, 'keep_trying')
|
4
4
|
require File.join(ROOT, 'thread_pool')
|
5
5
|
require File.join(ROOT, 'timer')
|
6
|
+
require File.join(ROOT, 'slugs')
|
6
7
|
#require File.join(ROOT, 'object_support')
|
7
8
|
require File.join(ROOT, 'remote_includes')
|
8
9
|
true
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'the_force/slugs'
|
2
|
+
|
3
|
+
class TestSlugs < Test::Unit::TestCase
|
4
|
+
def test_returns_original_slug_if_not_present_in_collection
|
5
|
+
assert_equal 'a', TheForce.unique_slug('a', [])
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_replaces_spaces_with_underscores
|
9
|
+
assert_equal 'a_b', TheForce.unique_slug('a b', [])
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_strips_before_changing
|
13
|
+
assert_equal 'a_b', TheForce.unique_slug(' a b ', [])
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_strips_before_comparing
|
17
|
+
assert_equal 'a_b_2', TheForce.unique_slug(' a b ', ['a_b'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_removes_non_alphanumerics
|
21
|
+
assert_equal 'ab', TheForce.unique_slug('a\'b%', [])
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_adds_numeric_suffix_of_2_if_present_in_collection
|
25
|
+
assert_equal 'a_2', TheForce.unique_slug('a', ['a'])
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_increments_numeric_suffix_until_not_present_in_collection
|
29
|
+
assert_equal 'a_4', TheForce.unique_slug('a', ['a', 'a_1', 'a_2', 'a_3'])
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_implicit_collect_column
|
33
|
+
assert_equal false, "PENDING -- TheForce.unique_slug('super title', Gallery, :permalink)"
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_force
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 6
|
10
|
+
version: 0.3.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Ziegler
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-24 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/the_force/rails_support.rb
|
41
41
|
- lib/the_force/remote_includes.rb
|
42
42
|
- lib/the_force/ruby_version.rb
|
43
|
+
- lib/the_force/slugs.rb
|
43
44
|
- lib/the_force/thread_pool.rb
|
44
45
|
- lib/the_force/timer.rb
|
45
46
|
- lib/the_force.rb
|
@@ -49,6 +50,7 @@ files:
|
|
49
50
|
- test/unit/test_object_support.rb
|
50
51
|
- test/unit/test_rails_support.rb
|
51
52
|
- test/unit/test_ruby_version.rb
|
53
|
+
- test/unit/test_slugs.rb
|
52
54
|
has_rdoc: true
|
53
55
|
homepage: http://www.symbolforce.com
|
54
56
|
licenses: []
|
@@ -91,3 +93,4 @@ test_files:
|
|
91
93
|
- test/unit/test_object_support.rb
|
92
94
|
- test/unit/test_rails_support.rb
|
93
95
|
- test/unit/test_ruby_version.rb
|
96
|
+
- test/unit/test_slugs.rb
|