stringex 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +8 -0
- data/Rakefile +1 -1
- data/lib/lucky_sneaks/acts_as_url.rb +12 -1
- data/lib/lucky_sneaks/string_extensions.rb +2 -1
- data/lib/lucky_sneaks/unidecoder_data/x00.yml +4 -4
- data/lib/lucky_sneaks/unidecoder_data/x02.yml +4 -4
- data/lib/lucky_sneaks/unidecoder_data/x22.yml +1 -1
- data/stringex.gemspec +2 -2
- data/test/acts_as_url_test.rb +16 -0
- data/test/string_extensions_test.rb +2 -1
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -6,6 +6,7 @@ Some [hopefully] useful extensions to Ruby's String class. It is made up of thre
|
|
6
6
|
|
7
7
|
This library is designed to create URI-friendly representations of an attribute, for use in generating urls from your attributes. Basic usage is just calling the method:
|
8
8
|
|
9
|
+
# Inside your model
|
9
10
|
acts_as_url :title
|
10
11
|
|
11
12
|
which will populate the <tt>url</tt> attribute on the object with the converted contents of the <tt>title</tt> attribute. This behavior can be customized by adding the following options to the arguments of the <tt>acts_as_url</tt> method:
|
@@ -20,6 +21,7 @@ which will populate the <tt>url</tt> attribute on the object with the converted
|
|
20
21
|
|
21
22
|
In order to use the generated url attribute, you will probably want to override <tt>to_param</tt> like so, in your Model:
|
22
23
|
|
24
|
+
# Inside your model
|
23
25
|
def to_param
|
24
26
|
url # or whatever you set :url_attribute to
|
25
27
|
end
|
@@ -69,6 +71,12 @@ You probably won't ever need to run Unidecoder by itself. StringExtensions adds
|
|
69
71
|
|
70
72
|
A collection of extensions on Ruby's String class. Please see the documentation for string_extensions module for more information. There's not much to explain about them really.
|
71
73
|
|
74
|
+
== Note to users of Devise
|
75
|
+
|
76
|
+
You'll need to add a <tt>:find_by => :url</tt> to your <tt>load_and_authorize_resource</tt>. Here's an example:
|
77
|
+
|
78
|
+
load_and_authorize_resource :class => "Whatever", :message => "Not authorized", :find_by => :url
|
79
|
+
|
72
80
|
== Thanks & Acknowledgements
|
73
81
|
|
74
82
|
If it's not obvious, some of the code for ActsAsUrl is based on Rick Olsen's permalink_fu[http://svn.techno-weenie.net/projects/plugins/permalink_fu/] plugin. Unidecoder is a Ruby port of Sean Burke's Text::Unidecode[http://interglacial.com/~sburke/tpj/as_html/tpj22.html] module for Perl. And, finally, the bulk of strip_html_tags[link:classes/LuckySneaks/StringExtensions.html#M000005] in StringExtensions was stolen from Tobias Lütke's Regex in Typo[http://typosphere.org/].
|
data/Rakefile
CHANGED
@@ -34,7 +34,7 @@ module LuckySneaks
|
|
34
34
|
cattr_accessor :duplicate_count_separator
|
35
35
|
|
36
36
|
if options[:sync_url]
|
37
|
-
before_validation
|
37
|
+
before_validation(:ensure_unique_url)
|
38
38
|
else
|
39
39
|
if defined?(ActiveModel::Callbacks)
|
40
40
|
before_validation(:ensure_unique_url, :on => :create)
|
@@ -48,6 +48,16 @@ module LuckySneaks
|
|
48
48
|
self.url_attribute = options[:url_attribute] || "url"
|
49
49
|
self.only_when_blank = options[:only_when_blank] || false
|
50
50
|
self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
|
51
|
+
|
52
|
+
class_eval <<-"END"
|
53
|
+
def #{url_attribute}
|
54
|
+
if !new_record? && errors[attribute_to_urlify].present?
|
55
|
+
self.class.find(id).send(url_attribute)
|
56
|
+
else
|
57
|
+
read_attribute(url_attribute)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
END
|
51
61
|
end
|
52
62
|
|
53
63
|
# Initialize the url fields for the records that need it. Designed for people who add
|
@@ -66,6 +76,7 @@ module LuckySneaks
|
|
66
76
|
end
|
67
77
|
|
68
78
|
private
|
79
|
+
|
69
80
|
def ensure_unique_url
|
70
81
|
url_attribute = self.class.url_attribute
|
71
82
|
separator = self.class.duplicate_count_separator
|
@@ -154,7 +154,8 @@ module LuckySneaks
|
|
154
154
|
/\s*\*\s*/ => "star",
|
155
155
|
/\s*%\s*/ => "percent",
|
156
156
|
/\s*(\\|\/)\s*/ => "slash",
|
157
|
-
/(\s*=\s*)/ => " equals "
|
157
|
+
/(\s*=\s*)/ => " equals ",
|
158
|
+
/\s*\+\s*/ => "plus"
|
158
159
|
}.each do |found, replaced|
|
159
160
|
replaced = " #{replaced} " unless replaced =~ /\\1/
|
160
161
|
dummy.gsub!(found, replaced)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
---
|
2
|
-
- "\
|
2
|
+
- "\x00"
|
3
3
|
- "\x01"
|
4
4
|
- "\x02"
|
5
5
|
- "\x03"
|
@@ -44,7 +44,7 @@
|
|
44
44
|
- '*'
|
45
45
|
- +
|
46
46
|
- ','
|
47
|
-
- -
|
47
|
+
- '-'
|
48
48
|
- .
|
49
49
|
- /
|
50
50
|
- 0
|
@@ -60,7 +60,7 @@
|
|
60
60
|
- ':'
|
61
61
|
- ;
|
62
62
|
- <
|
63
|
-
- =
|
63
|
+
- '='
|
64
64
|
- '>'
|
65
65
|
- '?'
|
66
66
|
- '@'
|
@@ -174,7 +174,7 @@
|
|
174
174
|
- '!'
|
175
175
|
- ''
|
176
176
|
- (r)
|
177
|
-
- -
|
177
|
+
- '-'
|
178
178
|
- deg
|
179
179
|
- +-
|
180
180
|
- 2
|
@@ -186,7 +186,7 @@
|
|
186
186
|
- y
|
187
187
|
- "'"
|
188
188
|
- '"'
|
189
|
-
- `
|
189
|
+
- '`'
|
190
190
|
- "'"
|
191
191
|
- '`'
|
192
192
|
- '`'
|
@@ -200,7 +200,7 @@
|
|
200
200
|
- '^'
|
201
201
|
- V
|
202
202
|
- "'"
|
203
|
-
- -
|
203
|
+
- '-'
|
204
204
|
- /
|
205
205
|
- '`'
|
206
206
|
- ','
|
@@ -214,7 +214,7 @@
|
|
214
214
|
- '^'
|
215
215
|
- V
|
216
216
|
- +
|
217
|
-
- -
|
217
|
+
- '-'
|
218
218
|
- V
|
219
219
|
- .
|
220
220
|
- '@'
|
@@ -236,7 +236,7 @@
|
|
236
236
|
- ''
|
237
237
|
- ''
|
238
238
|
- V
|
239
|
-
- =
|
239
|
+
- '='
|
240
240
|
- '"'
|
241
241
|
- '[?]'
|
242
242
|
- '[?]'
|
data/stringex.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{stringex}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Russell Norris"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-08-08}
|
13
13
|
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].}
|
14
14
|
s.email = %q{rsl@luckysneaks.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/acts_as_url_test.rb
CHANGED
@@ -45,6 +45,10 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
45
45
|
create_table :duplicateuments, :force => true do |t|
|
46
46
|
t.string :title, :url, :other
|
47
47
|
end
|
48
|
+
|
49
|
+
create_table :validatuments, :force => true do |t|
|
50
|
+
t.string :title, :url, :other
|
51
|
+
end
|
48
52
|
end
|
49
53
|
ActiveRecord::Migration.verbose = true
|
50
54
|
|
@@ -80,6 +84,11 @@ class Duplicateument < ActiveRecord::Base
|
|
80
84
|
acts_as_url :title, :duplicate_count_separator => "---"
|
81
85
|
end
|
82
86
|
|
87
|
+
class Validatument < ActiveRecord::Base
|
88
|
+
acts_as_url :title, :sync_url => true
|
89
|
+
validates_presence_of :title
|
90
|
+
end
|
91
|
+
|
83
92
|
class ActsAsUrlTest < Test::Unit::TestCase
|
84
93
|
def test_should_create_url
|
85
94
|
@doc = Document.create(:title => "Let's Make a Test Title, <em>Okay</em>?")
|
@@ -204,4 +213,11 @@ class ActsAsUrlTest < Test::Unit::TestCase
|
|
204
213
|
assert_equal "unique", @doc.url
|
205
214
|
assert_equal "unique---1", @other_doc.url
|
206
215
|
end
|
216
|
+
|
217
|
+
def test_should_only_update_url_if_model_is_valid
|
218
|
+
@doc = Validatument.create!(:title => "Initial")
|
219
|
+
@doc.title = nil
|
220
|
+
assert !@doc.valid?
|
221
|
+
assert_equal "initial", @doc.url
|
222
|
+
end
|
207
223
|
end
|
@@ -127,7 +127,8 @@ class StringExtensionsTest < Test::Unit::TestCase
|
|
127
127
|
"100% of yr love" => "100 percent of yr love",
|
128
128
|
"Kisses are $3.25 each" => "Kisses are 3 dollars 25 cents each",
|
129
129
|
"That CD is £3.25 plus tax" => "That CD is 3 pounds 25 pence plus tax",
|
130
|
-
"This CD is ¥1000 instead" => "This CD is 1000 yen instead"
|
130
|
+
"This CD is ¥1000 instead" => "This CD is 1000 yen instead",
|
131
|
+
"Food+Drink" => "Food plus Drink"
|
131
132
|
}.each do |misc, plain|
|
132
133
|
assert_equal plain, misc.convert_misc_characters
|
133
134
|
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 1.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Russell Norris
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-08 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|