rsl-stringex 1.0.2 → 1.0.3
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/Rakefile +2 -0
- data/lib/lucky_sneaks/acts_as_url.rb +5 -2
- data/stringex.gemspec +2 -2
- data/test/acts_as_url_test.rb +15 -0
- data/test/string_extensions_test.rb +3 -1
- data/test/unidecoder_test.rb +3 -1
- metadata +2 -2
data/Rakefile
CHANGED
@@ -30,6 +30,7 @@ module LuckySneaks
|
|
30
30
|
cattr_accessor :scope_for_url
|
31
31
|
cattr_accessor :url_attribute # The attribute on the DB
|
32
32
|
cattr_accessor :only_when_blank
|
33
|
+
cattr_accessor :duplicate_count_separator
|
33
34
|
|
34
35
|
if options[:sync_url]
|
35
36
|
before_validation :ensure_unique_url
|
@@ -41,6 +42,7 @@ module LuckySneaks
|
|
41
42
|
self.scope_for_url = options[:scope]
|
42
43
|
self.url_attribute = options[:url_attribute] || "url"
|
43
44
|
self.only_when_blank = options[:only_when_blank] || false
|
45
|
+
self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
|
44
46
|
end
|
45
47
|
|
46
48
|
# Initialize the url fields for the records that need it. Designed for people who add
|
@@ -61,6 +63,7 @@ module LuckySneaks
|
|
61
63
|
private
|
62
64
|
def ensure_unique_url
|
63
65
|
url_attribute = self.class.url_attribute
|
66
|
+
separator = self.class.duplicate_count_separator
|
64
67
|
base_url = self.send(url_attribute)
|
65
68
|
base_url = self.send(self.class.attribute_to_urlify).to_s.to_url if base_url.blank? || !self.only_when_blank
|
66
69
|
conditions = ["#{url_attribute} LIKE ?", base_url+'%']
|
@@ -76,10 +79,10 @@ module LuckySneaks
|
|
76
79
|
write_attribute url_attribute, base_url
|
77
80
|
if url_owners.any?{|owner| owner.send(url_attribute) == base_url}
|
78
81
|
n = 1
|
79
|
-
while url_owners.any?{|owner| owner.send(url_attribute) == "#{base_url}
|
82
|
+
while url_owners.any?{|owner| owner.send(url_attribute) == "#{base_url}#{separator}#{n}"}
|
80
83
|
n = n.succ
|
81
84
|
end
|
82
|
-
write_attribute url_attribute, "#{base_url}
|
85
|
+
write_attribute url_attribute, "#{base_url}#{separator}#{n}"
|
83
86
|
end
|
84
87
|
end
|
85
88
|
end
|
data/stringex.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{stringex}
|
5
|
-
s.version = "1.0.
|
5
|
+
s.version = "1.0.3"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Russell Norris"]
|
9
|
-
s.date = %q{2009-08-
|
9
|
+
s.date = %q{2009-08-20}
|
10
10
|
s.description = %q{Some [hopefully] useful extensions to Ruby’s String class. Stringex is made up of three libraries: ActsAsUrl [permalink solution with better character translation], Unidecoder [Unicode to Ascii transliteration], and StringExtensions [miscellaneous helper methods for the String class].}
|
11
11
|
s.email = %q{rsl@luckysneaks.com}
|
12
12
|
s.extra_rdoc_files = [
|
data/test/acts_as_url_test.rb
CHANGED
@@ -40,6 +40,10 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
40
40
|
create_table :blankuments, :force => true do |t|
|
41
41
|
t.string :title, :url, :other
|
42
42
|
end
|
43
|
+
|
44
|
+
create_table :duplicateuments, :force => true do |t|
|
45
|
+
t.string :title, :url, :other
|
46
|
+
end
|
43
47
|
end
|
44
48
|
ActiveRecord::Migration.verbose = true
|
45
49
|
|
@@ -71,6 +75,10 @@ class Blankument < ActiveRecord::Base
|
|
71
75
|
acts_as_url :title, :only_when_blank => true
|
72
76
|
end
|
73
77
|
|
78
|
+
class Duplicateument < ActiveRecord::Base
|
79
|
+
acts_as_url :title, :duplicate_count_separator => "---"
|
80
|
+
end
|
81
|
+
|
74
82
|
class ActsAsUrlTest < Test::Unit::TestCase
|
75
83
|
def test_should_create_url
|
76
84
|
@doc = Document.create(:title => "Let's Make a Test Title, <em>Okay</em>?")
|
@@ -188,4 +196,11 @@ class ActsAsUrlTest < Test::Unit::TestCase
|
|
188
196
|
@doc = Procument.create!(:title => "Title String")
|
189
197
|
assert_equal "title-string-got-massaged", @doc.url
|
190
198
|
end
|
199
|
+
|
200
|
+
def test_should_create_unique_with_custom_duplicate_count_separator
|
201
|
+
@doc = Duplicateument.create!(:title => "Unique")
|
202
|
+
@other_doc = Duplicateument.create!(:title => "Unique")
|
203
|
+
assert_equal "unique", @doc.url
|
204
|
+
assert_equal "unique---1", @other_doc.url
|
205
|
+
end
|
191
206
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
1
3
|
require "test/unit"
|
2
4
|
|
3
5
|
$: << File.join(File.dirname(__FILE__), '../lib')
|
@@ -155,4 +157,4 @@ class StringExtensionsTest < Test::Unit::TestCase
|
|
155
157
|
|
156
158
|
assert_equal "now-with-hyphens", "----now---------with-hyphens--------".collapse("-")
|
157
159
|
end
|
158
|
-
end
|
160
|
+
end
|
data/test/unidecoder_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rsl-stringex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Russell Norris
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-20 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|