friendly_id 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.txt +10 -0
  3. data/Manifest.txt +5 -18
  4. data/Rakefile +7 -10
  5. data/friendly_id.gemspec +17 -8
  6. data/lib/friendly_id.rb +29 -13
  7. data/lib/friendly_id/non_sluggable_class_methods.rb +4 -4
  8. data/lib/friendly_id/non_sluggable_instance_methods.rb +8 -0
  9. data/lib/friendly_id/slug.rb +14 -12
  10. data/lib/friendly_id/sluggable_class_methods.rb +13 -2
  11. data/lib/friendly_id/sluggable_instance_methods.rb +8 -5
  12. data/lib/friendly_id/version.rb +1 -1
  13. data/test/{fixtures → models}/country.rb +0 -0
  14. data/test/{fixtures → models}/person.rb +0 -0
  15. data/test/models/post.rb +3 -0
  16. data/test/{fixtures → models}/user.rb +0 -0
  17. data/test/non_slugged_test.rb +65 -60
  18. data/test/schema.rb +18 -18
  19. data/test/scoped_model_test.rb +43 -13
  20. data/test/slug_test.rb +93 -74
  21. data/test/slugged_model_test.rb +263 -0
  22. data/test/test_helper.rb +27 -29
  23. metadata +41 -24
  24. metadata.gz.sig +1 -2
  25. data/lib/friendly_id/shoulda_macros.rb +0 -36
  26. data/test/database.yml +0 -3
  27. data/test/fixtures/countries.yml +0 -4
  28. data/test/fixtures/people.yml +0 -7
  29. data/test/fixtures/post.rb +0 -3
  30. data/test/fixtures/posts.yml +0 -23
  31. data/test/fixtures/slugs.yml +0 -53
  32. data/test/fixtures/users.yml +0 -7
  33. data/test/rails/2.x/app/controllers/application.rb +0 -0
  34. data/test/rails/2.x/config/boot.rb +0 -109
  35. data/test/rails/2.x/config/database.yml +0 -3
  36. data/test/rails/2.x/config/environment.rb +0 -7
  37. data/test/rails/2.x/config/environments/test.rb +0 -6
  38. data/test/rails/2.x/config/routes.rb +0 -0
  39. data/test/sluggable_test.rb +0 -185
@@ -1,35 +1,35 @@
1
- ActiveRecord::Schema.define(:version => 3) do
1
+ ActiveRecord::Schema.define(:version => 1) do
2
2
 
3
3
  create_table "posts", :force => true do |t|
4
- t.string "name"
5
- t.text "content"
6
- t.datetime "created_at"
7
- t.datetime "updated_at"
4
+ t.column "title", "string"
5
+ t.column "content", "text"
6
+ t.column "created_at", "datetime"
7
+ t.column "updated_at", "datetime"
8
8
  end
9
9
 
10
10
  create_table "users", :force => true do |t|
11
- t.string "login"
12
- t.string "email"
13
- t.datetime "created_at"
14
- t.datetime "updated_at"
11
+ t.column "login", "string"
12
+ t.column "email", "string"
13
+ t.column "created_at", "datetime"
14
+ t.column "updated_at", "datetime"
15
15
  end
16
16
 
17
17
  create_table "people", :force => true do |t|
18
- t.string "name"
19
- t.integer "country_id"
18
+ t.column "name", "string"
19
+ t.column "country_id", "integer"
20
20
  end
21
21
 
22
22
  create_table "countries", :force => true do |t|
23
- t.string "name"
23
+ t.column "name", "string"
24
24
  end
25
25
 
26
26
  create_table "slugs", :force => true do |t|
27
- t.string "name"
28
- t.integer "sluggable_id"
29
- t.integer "sequence", :null => false, :default => 1
30
- t.string "sluggable_type", :limit => 40
31
- t.string "scope", :limit => 40
32
- t.datetime "created_at"
27
+ t.column "name", "string"
28
+ t.column "sluggable_id", "integer"
29
+ t.column "sequence", "integer", :null => false, :default => 1
30
+ t.column "sluggable_type", "string", :limit => 40
31
+ t.column "scope", "string", :limit => 40
32
+ t.column "created_at", "datetime"
33
33
  end
34
34
 
35
35
  add_index "slugs", ["sluggable_id"], :name => "index_slugs_on_sluggable_id"
@@ -2,20 +2,50 @@ require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class ScopedModelTest < Test::Unit::TestCase
4
4
 
5
- fixtures :people, :countries, :slugs
6
-
7
- def test_should_find_scoped_records_without_scope
8
- assert_equal 2, Person.find(:all, "john-smith").size
9
- end
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
10
48
 
11
- def test_should_find_scoped_records_with_scope
12
- assert_equal people(:john_smith), Person.find("john-smith", :scope => "argentina")
13
- assert_equal people(:john_smith2), Person.find("john-smith", :scope => "usa")
14
- end
15
-
16
- def test_should_create_scoped_records_with_scope
17
- person = Person.create!(:name => "Joe Schmoe", :country => countries(:usa))
18
- assert_equal "usa", person.slug.scope
19
49
  end
20
50
 
21
51
  end
@@ -1,87 +1,106 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.dirname(__FILE__) + '/test_helper'
2
4
 
3
5
  class SlugTest < Test::Unit::TestCase
4
6
 
5
- fixtures :posts, :slugs
6
-
7
- def test_should_indicate_if_it_is_the_most_recent
8
- assert slugs(:two_new).is_most_recent?
9
- assert !slugs(:two_old).is_most_recent?
10
- end
11
-
12
- def test_parse_should_return_slug_name_and_sequence
13
- assert_equal ["test", "2"], Slug::parse("test--2")
14
- end
15
-
16
- def test_parse_should_return_a_default_sequnce_of_1
17
- assert_equal ["test", "1"], Slug::parse("test")
18
- end
19
-
20
- def test_strip_diacritics_should_strip_diacritics
21
- assert_equal "acai", Slug::strip_diacritics("açaí")
22
- end
23
-
24
- def test_to_friendly_id_should_include_sequence_if_its_greater_than_1
25
- slug = Slug.new(:name => "test", :sequence => 2)
26
- assert_equal "test--2", slug.to_friendly_id
27
- end
28
-
29
- def test_to_friendly_id_should_include_sequence_if_its_than_1
30
- slug = Slug.new(:name => "test", :sequence => 1)
31
- assert_equal "test", slug.to_friendly_id
32
- end
33
-
34
- def test_normalize_should_lowercase_strings
35
- assert_match /abc/, Slug::normalize("ABC")
36
- end
37
-
38
- def test_normalize_should_replace_whitespace_with_dashes
39
- assert_match /a-b/, Slug::normalize("a b")
40
- end
41
-
42
- def test_normalize_should_replace_2spaces_with_1dash
43
- assert_match /a-b/, Slug::normalize("a b")
44
- end
45
-
46
- def test_normalize_should_remove_punctuation
47
- assert_match /abc/, Slug::normalize('abc!@#$%^&*•¶§∞¢££¡¿()><?"":;][]\.,/')
48
- end
49
-
50
- def test_normalize_should_strip_trailing_space
51
- assert_match /ab/, Slug::normalize("ab ")
52
- end
53
-
54
- def test_normalize_should_strip_leading_space
55
- assert_match /ab/, Slug::normalize(" ab")
56
- end
57
-
58
- def test_normalize_should_strip_trailing_slashes
59
- assert_match /ab/, Slug::normalize("ab-")
60
- end
7
+ context "a slug" do
8
+
9
+ setup do
10
+ Slug.delete_all
11
+ Post.delete_all
12
+ end
61
13
 
62
- def test_normalize_should_strip_leading_slashes
63
- assert_match /ab/, Slug::normalize("-ab")
64
- end
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
65
21
 
66
- def test_normalize_should_not_modify_valid_name_strings
67
- assert_match /a-b-c-d/, Slug::normalize("a-b-c-d")
68
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
69
33
 
70
- # These strings are taken from various international Google homepages. I
71
- # would be most grateful if a fluent speaker of any language that uses a
72
- # writing system other than the Roman alphabet could help me make some
73
- # better tests to ensure this is working correctly.
74
- def test_normalize_works_with_non_roman_chars
75
- assert_equal "検-索", Slug::normalize("検 索")
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
+
76
47
  end
77
-
78
- def test_strip_diactics_correctly_strips_diacritics
79
- input = "ÀÁÂÃÄÅÆÇÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"
80
- output = Slug::strip_diacritics(input).split(//)
81
- expected = "AAAAAAAECEEEIIIIDNOOOOOOUUUUYThssaaaaaaaeceeeeiiiidnoooooouuuuythy".split(//)
82
- output.split.each_index do |i|
83
- assert_equal output[i], expected[i]
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
84
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
+
85
61
  end
86
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
87
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