dougcole-friendly_id 2.0.5 → 2.0.6

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.
Files changed (43) hide show
  1. data/History.txt +20 -0
  2. data/Manifest.txt +10 -18
  3. data/README.rdoc +34 -5
  4. data/Rakefile +8 -0
  5. data/VERSION.yml +1 -1
  6. data/lib/friendly_id.rb +33 -13
  7. data/lib/friendly_id/non_sluggable_class_methods.rb +3 -3
  8. data/lib/friendly_id/non_sluggable_instance_methods.rb +9 -1
  9. data/lib/friendly_id/slug.rb +14 -12
  10. data/lib/friendly_id/sluggable_class_methods.rb +14 -3
  11. data/lib/friendly_id/sluggable_instance_methods.rb +11 -4
  12. data/lib/friendly_id/version.rb +1 -1
  13. data/test/custom_slug_normalizer_test.rb +35 -0
  14. data/test/models/book.rb +2 -0
  15. data/test/{fixtures → models}/country.rb +0 -0
  16. data/test/models/novel.rb +3 -0
  17. data/test/{fixtures → models}/person.rb +0 -0
  18. data/test/models/post.rb +3 -0
  19. data/test/models/thing.rb +6 -0
  20. data/test/{fixtures → models}/user.rb +0 -0
  21. data/test/non_slugged_test.rb +71 -60
  22. data/test/schema.rb +29 -20
  23. data/test/scoped_model_test.rb +43 -13
  24. data/test/slug_test.rb +93 -74
  25. data/test/slugged_model_test.rb +263 -0
  26. data/test/sti_test.rb +48 -0
  27. data/test/test_helper.rb +30 -29
  28. metadata +15 -20
  29. data/lib/friendly_id/shoulda_macros.rb +0 -36
  30. data/test/database.yml +0 -3
  31. data/test/fixtures/countries.yml +0 -4
  32. data/test/fixtures/people.yml +0 -7
  33. data/test/fixtures/post.rb +0 -3
  34. data/test/fixtures/posts.yml +0 -23
  35. data/test/fixtures/slugs.yml +0 -53
  36. data/test/fixtures/users.yml +0 -7
  37. data/test/rails/2.x/app/controllers/application.rb +0 -0
  38. data/test/rails/2.x/config/boot.rb +0 -109
  39. data/test/rails/2.x/config/database.yml +0 -3
  40. data/test/rails/2.x/config/environment.rb +0 -7
  41. data/test/rails/2.x/config/environments/test.rb +0 -6
  42. data/test/rails/2.x/config/routes.rb +0 -0
  43. data/test/sluggable_test.rb +0 -185
@@ -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
data/test/test_helper.rb CHANGED
@@ -1,35 +1,36 @@
1
1
  $:.unshift(File.dirname(__FILE__) + '/../lib')
2
- $VERBOSE = false
3
-
4
- ENV['RAILS_ENV'] = 'test'
5
- require File.dirname(__FILE__) + '/rails/2.x/config/environment.rb'
6
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
7
-
2
+ $:.unshift(File.dirname(__FILE__))
3
+ $VERBOSE=false
4
+ require 'rubygems'
8
5
  require 'test/unit'
9
- require 'active_record/fixtures'
10
- require 'action_controller/test_process'
11
- require 'sqlite3'
12
- require 'friendly_id/slug'
13
-
14
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
15
- ActiveRecord::Base.establish_connection
16
-
17
- silence_stream(STDOUT) do
18
- load(File.dirname(__FILE__) + "/schema.rb")
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"]}"
19
11
  end
12
+ require 'active_record'
20
13
 
21
- Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
22
- $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
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'
23
22
 
24
- class Test::Unit::TestCase #:nodoc:
25
- include ActionController::TestProcess
26
- def create_fixtures(*table_names)
27
- if block_given?
28
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
29
- else
30
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
31
- end
32
- end
33
- self.use_transactional_fixtures = true
34
- self.use_instantiated_fixtures = false
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)
35
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 CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dougcole-friendly_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norman Clarke
@@ -11,11 +11,12 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-02-05 00:00:00 -08:00
14
+ date: 2009-02-17 00:00:00 -08:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: schacon-git
19
+ type: :runtime
19
20
  version_requirement:
20
21
  version_requirements: !ruby/object:Gem::Requirement
21
22
  requirements:
@@ -25,6 +26,7 @@ dependencies:
25
26
  version:
26
27
  - !ruby/object:Gem::Dependency
27
28
  name: unicode
29
+ type: :runtime
28
30
  version_requirement:
29
31
  version_requirements: !ruby/object:Gem::Requirement
30
32
  requirements:
@@ -67,42 +69,35 @@ files:
67
69
  - lib/friendly_id/non_sluggable_instance_methods.rb
68
70
  - lib/friendly_id/slug.rb
69
71
  - lib/friendly_id/non_sluggable_class_methods.rb
70
- - lib/friendly_id/shoulda_macros.rb
71
72
  - lib/friendly_id/helpers.rb
72
73
  - lib/friendly_id/sluggable_class_methods.rb
73
74
  - lib/friendly_id/sluggable_instance_methods.rb
74
75
  - lib/friendly_id/version.rb
75
76
  - lib/friendly_id.rb
77
+ - test/sti_test.rb
76
78
  - test/slug_test.rb
79
+ - test/slugged_model_test.rb
77
80
  - test/scoped_model_test.rb
78
81
  - test/test_helper.rb
79
82
  - test/fixtures
80
- - test/fixtures/posts.yml
81
- - test/fixtures/user.rb
82
- - test/fixtures/people.yml
83
- - test/fixtures/person.rb
84
- - test/fixtures/countries.yml
85
- - test/fixtures/slugs.yml
86
- - test/fixtures/users.yml
87
- - test/fixtures/country.rb
88
- - test/fixtures/post.rb
89
- - test/database.yml
90
- - test/sluggable_test.rb
83
+ - test/models
84
+ - test/models/user.rb
85
+ - test/models/book.rb
86
+ - test/models/person.rb
87
+ - test/models/novel.rb
88
+ - test/models/country.rb
89
+ - test/models/post.rb
90
+ - test/models/thing.rb
91
+ - test/custom_slug_normalizer_test.rb
91
92
  - test/schema.rb
92
93
  - test/rails
93
94
  - test/rails/2.x
94
95
  - test/rails/2.x/app
95
96
  - test/rails/2.x/app/controllers
96
- - test/rails/2.x/app/controllers/application.rb
97
97
  - test/rails/2.x/log
98
98
  - test/rails/2.x/log/test.log
99
99
  - test/rails/2.x/config
100
- - test/rails/2.x/config/boot.rb
101
100
  - test/rails/2.x/config/environments
102
- - test/rails/2.x/config/environments/test.rb
103
- - test/rails/2.x/config/routes.rb
104
- - test/rails/2.x/config/database.yml
105
- - test/rails/2.x/config/environment.rb
106
101
  - test/non_slugged_test.rb
107
102
  - init.rb
108
103
  has_rdoc: true