rakutenusa-friendly_id 2.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/test/schema.rb ADDED
@@ -0,0 +1,47 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+
3
+ create_table "books", :force => true do |t|
4
+ t.column "title", "string"
5
+ t.column "type", "text"
6
+ end
7
+
8
+ create_table "things", :force => true do |t|
9
+ t.column "name", "string"
10
+ end
11
+
12
+ create_table "posts", :force => true do |t|
13
+ t.column "title", "string"
14
+ t.column "content", "text"
15
+ t.column "created_at", "datetime"
16
+ t.column "updated_at", "datetime"
17
+ end
18
+
19
+ create_table "users", :force => true do |t|
20
+ t.column "login", "string"
21
+ t.column "email", "string"
22
+ t.column "created_at", "datetime"
23
+ t.column "updated_at", "datetime"
24
+ end
25
+
26
+ create_table "people", :force => true do |t|
27
+ t.column "name", "string"
28
+ t.column "country_id", "integer"
29
+ end
30
+
31
+ create_table "countries", :force => true do |t|
32
+ t.column "name", "string"
33
+ end
34
+
35
+ create_table "slugs", :force => true do |t|
36
+ t.column "name", "string"
37
+ t.column "sluggable_id", "integer"
38
+ t.column "sequence", "integer", :null => false, :default => 1
39
+ t.column "sluggable_type", "string", :limit => 40
40
+ t.column "scope", "string", :limit => 40
41
+ t.column "created_at", "datetime"
42
+ end
43
+
44
+ add_index "slugs", ["sluggable_id"], :name => "index_slugs_on_sluggable_id"
45
+ add_index "slugs", ["name", "sluggable_type", "scope", "sequence"], :name => "index_slugs_on_n_s_s_and_s", :unique => true
46
+
47
+ end
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ScopedModelTest < Test::Unit::TestCase
4
+
5
+ context "A slugged model that uses a scope" do
6
+
7
+ setup do
8
+ Person.delete_all
9
+ Country.delete_all
10
+ Slug.delete_all
11
+ @usa = Country.create!(:name => "USA")
12
+ @canada = Country.create!(:name => "Canada")
13
+ @person = Person.create!(:name => "John Smith", :country => @usa)
14
+ @person2 = Person.create!(:name => "John Smith", :country => @canada)
15
+ end
16
+
17
+ should "find all scoped records without scope" do
18
+ assert_equal 2, Person.find(:all, @person.friendly_id).size
19
+ end
20
+
21
+ should "find a single scoped records with a scope" do
22
+ assert Person.find(@person.friendly_id, :scope => @person.country.to_param)
23
+ end
24
+
25
+ should "raise an error when finding a single scoped record with no scope" do
26
+ assert_raises ActiveRecord::RecordNotFound do
27
+ Person.find(@person.friendly_id)
28
+ end
29
+ end
30
+
31
+ should "append scope error info when missing scope causes a find to fail" do
32
+ begin
33
+ Person.find(@person.friendly_id)
34
+ fail "The find should not have succeeded"
35
+ rescue ActiveRecord::RecordNotFound => e
36
+ assert_match /expected scope/, e.message
37
+ end
38
+ end
39
+
40
+ should "append scope error info when the scope value causes a find to fail" do
41
+ begin
42
+ Person.find(@person.friendly_id, :scope => "badscope")
43
+ fail "The find should not have succeeded"
44
+ rescue ActiveRecord::RecordNotFound => e
45
+ assert_match /scope=badscope/, e.message
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
data/test/slug_test.rb ADDED
@@ -0,0 +1,106 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/test_helper'
4
+
5
+ class SlugTest < Test::Unit::TestCase
6
+
7
+ context "a slug" do
8
+
9
+ setup do
10
+ Slug.delete_all
11
+ Post.delete_all
12
+ end
13
+
14
+ should "indicate if it is the most recent slug" do
15
+ @post = Post.create!(:title => "test title", :content => "test content")
16
+ @post.title = "a new title"
17
+ @post.save!
18
+ assert @post.slugs.last.is_most_recent?
19
+ assert !@post.slugs.first.is_most_recent?
20
+ end
21
+
22
+ end
23
+
24
+ context "the Slug class" do
25
+
26
+ should "parse the slug name and sequence" do
27
+ assert_equal ["test", "2"], Slug::parse("test--2")
28
+ end
29
+
30
+ should "parse with a default sequence of 1" do
31
+ assert_equal ["test", "1"], Slug::parse("test")
32
+ end
33
+
34
+ should "should strip diacritics" do
35
+ assert_equal "acai", Slug::strip_diacritics("açaí")
36
+ end
37
+
38
+ should "strip diacritics correctly " do
39
+ input = "ÀÁÂÃÄÅÆÇÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"
40
+ output = Slug::strip_diacritics(input).split(//)
41
+ expected = "AAAAAAAECEEEIIIIDNOOOOOOUUUUYThssaaaaaaaeceeeeiiiidnoooooouuuuythy".split(//)
42
+ output.split.each_index do |i|
43
+ assert_equal output[i], expected[i]
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ context "the Slug class's to_friendly_id method" do
50
+
51
+ should "include the sequence if the sequence is greater than 1" do
52
+ slug = Slug.new(:name => "test", :sequence => 2)
53
+ assert_equal "test--2", slug.to_friendly_id
54
+ end
55
+
56
+ should "not include the sequence if the sequence is 1" do
57
+ slug = Slug.new(:name => "test", :sequence => 1)
58
+ assert_equal "test", slug.to_friendly_id
59
+ end
60
+
61
+ end
62
+
63
+ context "the Slug class's normalize method" do
64
+
65
+ should "should lowercase strings" do
66
+ assert_match /abc/, Slug::normalize("ABC")
67
+ end
68
+
69
+ should "should replace whitespace with dashes" do
70
+ assert_match /a-b/, Slug::normalize("a b")
71
+ end
72
+
73
+ should "should replace 2spaces with 1dash" do
74
+ assert_match /a-b/, Slug::normalize("a b")
75
+ end
76
+
77
+ should "should remove punctuation" do
78
+ assert_match /abc/, Slug::normalize('abc!@#$%^&*•¶§∞¢££¡¿()><?"":;][]\.,/')
79
+ end
80
+
81
+ should "should strip trailing space" do
82
+ assert_match /ab/, Slug::normalize("ab ")
83
+ end
84
+
85
+ should "should strip leading space" do
86
+ assert_match /ab/, Slug::normalize(" ab")
87
+ end
88
+
89
+ should "should strip trailing slashes" do
90
+ assert_match /ab/, Slug::normalize("ab-")
91
+ end
92
+
93
+ should "should strip leading slashes" do
94
+ assert_match /ab/, Slug::normalize("-ab")
95
+ end
96
+
97
+ should "should not modify valid name strings" do
98
+ assert_match /a-b-c-d/, Slug::normalize("a-b-c-d")
99
+ end
100
+
101
+ should "work with non roman chars" do
102
+ assert_equal "検-索", Slug::normalize("検 索")
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,263 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/test_helper'
4
+
5
+ class SluggedModelTest < Test::Unit::TestCase
6
+
7
+ context "A slugged model with default FriendlyId options" do
8
+
9
+ setup do
10
+ Post.friendly_id_options = FriendlyId::DEFAULT_FRIENDLY_ID_OPTIONS.merge(:column => :title, :use_slug => true)
11
+ Post.delete_all
12
+ Person.delete_all
13
+ Slug.delete_all
14
+ @post = Post.new :title => "Test post", :content => "Test content"
15
+ @post.save!
16
+ end
17
+
18
+ should "have friendly_id options" do
19
+ assert_not_nil Post.friendly_id_options
20
+ end
21
+
22
+ should "have a slug" do
23
+ assert_not_nil @post.slug
24
+ end
25
+
26
+ should "be findable by its friendly_id" do
27
+ assert Post.find(@post.friendly_id)
28
+ end
29
+
30
+ should "be findable by its regular id" do
31
+ assert Post.find(@post.id)
32
+ end
33
+
34
+ should "generate slug text" do
35
+ post = Post.new :title => "Test post", :content => "Test content"
36
+ assert_not_nil @post.slug_text
37
+ end
38
+
39
+ should "respect finder conditions" do
40
+ assert_raises ActiveRecord::RecordNotFound do
41
+ Post.find(@post.friendly_id, :conditions => "1 = 2")
42
+ end
43
+ end
44
+
45
+ should "raise an error if the friendly_id text is reserved" do
46
+ assert_raises(FriendlyId::SlugGenerationError) do
47
+ Post.create!(:title => "new")
48
+ end
49
+ end
50
+
51
+ should "raise an error if the friendly_id text is blank" do
52
+ assert_raises(FriendlyId::SlugGenerationError) do
53
+ Post.create(:title => "")
54
+ end
55
+ end
56
+
57
+ should "raise an error if the normalized friendly id becomes blank" do
58
+ assert_raises(FriendlyId::SlugGenerationError) do
59
+ post = Post.create!(:title => "-.-")
60
+ end
61
+ end
62
+
63
+ should "not make a new slug unless the friendly_id method value has changed" do
64
+ @post.content = "Changed content"
65
+ @post.save!
66
+ assert_equal 1, @post.slugs.size
67
+ end
68
+
69
+ should "make a new slug if the friendly_id method value has changed" do
70
+ @post.title = "Changed title"
71
+ @post.save!
72
+ assert_equal 2, @post.slugs.size
73
+ end
74
+
75
+ should "have a slug sequence of 1 by default" do
76
+ assert_equal 1, @post.slug.sequence
77
+ end
78
+
79
+ should "increment sequence for duplicate slug names" do
80
+ @post2 = Post.create! :title => @post.title, :content => "Test content for post2"
81
+ assert_equal 2, @post2.slug.sequence
82
+ end
83
+
84
+ should "have a friendly_id that terminates with -- and the slug sequence if the sequence is greater than 1" do
85
+ @post2 = Post.create! :title => @post.title, :content => "Test content for post2"
86
+ assert_match(/--2\z/, @post2.friendly_id)
87
+ end
88
+
89
+ should "not strip diacritics" do
90
+ @post = Post.new(:title => "¡Feliz año!")
91
+ assert_match(/#{'ñ'}/, @post.slug_text)
92
+ end
93
+
94
+ should "not convert to ASCII" do
95
+ @post = Post.new(:title => "katakana: ゲコゴサザシジ")
96
+ assert_equal "katakana-ゲコゴサザシジ", @post.slug_text
97
+ end
98
+
99
+ should "allow the same friendly_id across models" do
100
+ @person = Person.create!(:name => @post.title)
101
+ assert_equal @person.friendly_id, @post.friendly_id
102
+ end
103
+
104
+ should "truncate slug text longer than the max length" do
105
+ @post = Post.new(:title => "a" * (Post.friendly_id_options[:max_length] + 1))
106
+ assert_equal @post.slug_text.length, Post.friendly_id_options[:max_length]
107
+ end
108
+
109
+ should "be able to reuse an old friendly_id without incrementing the sequence" do
110
+ old_title = @post.title
111
+ old_friendly_id = @post.friendly_id
112
+ @post.title = "A changed title"
113
+ @post.save!
114
+ @post.title = old_title
115
+ @post.save!
116
+ assert_equal old_friendly_id, @post.friendly_id
117
+ end
118
+
119
+ should "allow eager loading of slugs" do
120
+ assert_nothing_raised do
121
+ Post.find(@post.friendly_id, :include => :slugs)
122
+ end
123
+ end
124
+
125
+ context "and configured to strip diacritics" do
126
+ setup do
127
+ Post.friendly_id_options = Post.friendly_id_options.merge(:strip_diacritics => true)
128
+ end
129
+
130
+ should "strip diacritics from Roman alphabet based characters" do
131
+ @post = Post.new(:title => "¡Feliz año!")
132
+ assert_no_match(/#{'ñ'}/, @post.slug_text)
133
+ end
134
+ end
135
+
136
+ context "and configured to convert to ASCII" do
137
+ setup do
138
+ Post.friendly_id_options = Post.friendly_id_options.merge(:strip_non_ascii => true)
139
+ end
140
+
141
+ should "strip non-ascii characters" do
142
+ @post = Post.new(:title => "katakana: ゲコゴサザシジ")
143
+ assert_equal "katakana", @post.slug_text
144
+ end
145
+ end
146
+
147
+ context "that doesn't have a slug" do
148
+
149
+ setup do
150
+ @post.slug.destroy
151
+ @post = Post.find(@post.id)
152
+ end
153
+
154
+ should "have a to_param method that returns the id cast to a string" do
155
+ assert_equal @post.id.to_s, @post.to_param
156
+ end
157
+
158
+ end
159
+
160
+ context "when found using its friendly_id" do
161
+ setup do
162
+ @post = Post.find(@post.friendly_id)
163
+ end
164
+
165
+ should "indicate that it was found using the friendly_id" do
166
+ assert @post.found_using_friendly_id?
167
+ end
168
+
169
+ should "not indicate that it has a better id" do
170
+ assert !@post.has_better_id?
171
+ end
172
+
173
+ should "not indicate that it was found using its numeric id" do
174
+ assert !@post.found_using_numeric_id?
175
+ end
176
+
177
+ should "have a finder slug" do
178
+ assert_not_nil @post.finder_slug
179
+ end
180
+
181
+ end
182
+
183
+ context "when found using its regular id" do
184
+ setup do
185
+ @post = Post.find(@post.id)
186
+ end
187
+
188
+ should "indicate that it was not found using the friendly id" do
189
+ assert !@post.found_using_friendly_id?
190
+ end
191
+
192
+ should "indicate that it has a better id" do
193
+ assert @post.has_better_id?
194
+ end
195
+
196
+ should "indicate that it was found using its numeric id" do
197
+ assert @post.found_using_numeric_id?
198
+ end
199
+
200
+ should "not have a finder slug" do
201
+ assert_nil @post.finder_slug
202
+ end
203
+
204
+ end
205
+
206
+ context "when found using an outdated friendly id" do
207
+ setup do
208
+ old_id = @post.friendly_id
209
+ @post.title = "Title changed"
210
+ @post.save!
211
+ @post = Post.find(old_id)
212
+ end
213
+
214
+ should "indicate that it was found using a friendly_id" do
215
+ assert @post.found_using_friendly_id?
216
+ end
217
+
218
+ should "indicate that it has a better id" do
219
+ assert @post.has_better_id?
220
+ end
221
+
222
+ should "not indicate that it was found using its numeric id" do
223
+ assert !@post.found_using_numeric_id?
224
+ end
225
+
226
+ should "should have a finder slug different from its default slug" do
227
+ assert_not_equal @post.slug, @post.finder_slug
228
+ end
229
+
230
+ end
231
+
232
+ context "when using an array as the find argument" do
233
+
234
+ setup do
235
+ @post2 = Post.create!(:title => "another post", :content => "more content")
236
+ end
237
+
238
+ should "return results" do
239
+ assert_equal 2, Post.find([@post.friendly_id, @post2.friendly_id]).size
240
+ end
241
+
242
+ should "indicate that the results were found using a friendly_id" do
243
+ @posts = Post.find [@post.friendly_id, @post2.friendly_id]
244
+ @posts.each { |p| assert p.found_using_friendly_id? }
245
+ end
246
+
247
+ should "raise an error when all records are not found" do
248
+ assert_raises(ActiveRecord::RecordNotFound) do
249
+ Post.find([@post.friendly_id, 'non-existant-slug-record'])
250
+ end
251
+ end
252
+
253
+ should "allow eager loading of slugs" do
254
+ assert_nothing_raised do
255
+ Post.find([@post.friendly_id, @post2.friendly_id], :include => :slugs)
256
+ end
257
+ end
258
+
259
+ end
260
+
261
+ end
262
+
263
+ end
data/test/sti_test.rb ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/test_helper'
4
+
5
+ class SluggedModelTest < Test::Unit::TestCase
6
+
7
+ context "A slugged model using single table inheritance" do
8
+
9
+ setup do
10
+ Novel.friendly_id_options = FriendlyId::DEFAULT_FRIENDLY_ID_OPTIONS.merge(:column => :title, :use_slug => true)
11
+ Novel.delete_all
12
+ Slug.delete_all
13
+ @novel = Novel.new :title => "Test novel"
14
+ @novel.save!
15
+ end
16
+
17
+ should "have a slug" do
18
+ assert_not_nil @novel.slug
19
+ end
20
+
21
+ context "found by its friendly id" do
22
+
23
+ setup do
24
+ @novel = Novel.find(@novel.friendly_id)
25
+ end
26
+
27
+ should "not indicate that it has a better id" do
28
+ assert !@novel.has_better_id?
29
+ end
30
+
31
+ end
32
+
33
+
34
+ context "found by its numeric id" do
35
+
36
+ setup do
37
+ @novel = Novel.find(@novel.id)
38
+ end
39
+
40
+ should "indicate that it has a better id" do
41
+ assert @novel.has_better_id?
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,36 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift(File.dirname(__FILE__))
3
+ $VERBOSE=false
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ # You can use "rake test AR_VERSION=2.0.5" to test against 2.0.5, for example.
8
+ # The default is to use the latest installed ActiveRecord.
9
+ if ENV["AR_VERSION"]
10
+ gem 'activerecord', "#{ENV["AR_VERSION"]}"
11
+ end
12
+ require 'active_record'
13
+
14
+ require 'friendly_id'
15
+ require 'models/post'
16
+ require 'models/person'
17
+ require 'models/user'
18
+ require 'models/country'
19
+ require 'models/book'
20
+ require 'models/novel'
21
+ require 'models/thing'
22
+
23
+ # Borrowed from ActiveSupport
24
+ def silence_stream(stream)
25
+ old_stream = stream.dup
26
+ stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
27
+ stream.sync = true
28
+ yield
29
+ ensure
30
+ stream.reopen(old_stream)
31
+ end
32
+
33
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
34
+ silence_stream(STDOUT) do
35
+ load(File.dirname(__FILE__) + "/schema.rb")
36
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rakutenusa-friendly_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.6
5
+ platform: ruby
6
+ authors:
7
+ - RakutenUSA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-29 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: dev@rakutenusa.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - History.txt
26
+ - Manifest.txt
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - generators/friendly_id/friendly_id_generator.rb
31
+ - generators/friendly_id/templates/create_slugs.rb
32
+ - generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb
33
+ - generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb
34
+ - lib/friendly_id.rb
35
+ - lib/friendly_id/helpers.rb
36
+ - lib/friendly_id/non_sluggable_class_methods.rb
37
+ - lib/friendly_id/non_sluggable_instance_methods.rb
38
+ - lib/friendly_id/slug.rb
39
+ - lib/friendly_id/sluggable_class_methods.rb
40
+ - lib/friendly_id/sluggable_instance_methods.rb
41
+ - lib/friendly_id/version.rb
42
+ - lib/tasks/friendly_id.rake
43
+ - lib/tasks/friendly_id.rb
44
+ - test/custom_slug_normalizer_test.rb
45
+ - test/models/book.rb
46
+ - test/models/country.rb
47
+ - test/models/novel.rb
48
+ - test/models/person.rb
49
+ - test/models/post.rb
50
+ - test/models/thing.rb
51
+ - test/models/user.rb
52
+ - test/non_slugged_test.rb
53
+ - test/schema.rb
54
+ - test/scoped_model_test.rb
55
+ - test/slug_test.rb
56
+ - test/slugged_model_test.rb
57
+ - test/sti_test.rb
58
+ - test/test_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/rakutenusa/friendly_id
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.2.0
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: TODO
85
+ test_files:
86
+ - test/custom_slug_normalizer_test.rb
87
+ - test/models/book.rb
88
+ - test/models/country.rb
89
+ - test/models/novel.rb
90
+ - test/models/person.rb
91
+ - test/models/post.rb
92
+ - test/models/thing.rb
93
+ - test/models/user.rb
94
+ - test/non_slugged_test.rb
95
+ - test/schema.rb
96
+ - test/scoped_model_test.rb
97
+ - test/slug_test.rb
98
+ - test/slugged_model_test.rb
99
+ - test/sti_test.rb
100
+ - test/test_helper.rb