slug 0.5.7 → 4.1.1

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/test/test_slug.rb DELETED
@@ -1,246 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
-
3
- class TestSlug < Test::Unit::TestCase
4
-
5
- def setup
6
- Article.delete_all
7
- Person.delete_all
8
- end
9
-
10
- should "base slug on specified source column" do
11
- article = Article.create!(:headline => 'Test Headline')
12
- assert_equal 'test-headline', article.slug
13
- end
14
-
15
- should "base slug on specified source column, even if it is defined as a method rather than database attribute" do
16
- article = Event.create!(:title => 'Test Event', :location => 'Portland')
17
- assert_equal 'test-event-portland', article.slug
18
- end
19
-
20
- context "slug column" do
21
- should "save slug to 'slug' column by default" do
22
- article = Article.create!(:headline => 'Test Headline')
23
- assert_equal 'test-headline', article.slug
24
- end
25
-
26
- should "save slug to :column specified in options" do
27
- person = Person.create!(:name => 'Test Person')
28
- assert_equal 'test-person', person.web_slug
29
- end
30
- end
31
-
32
- context "column validations" do
33
- should "raise ArgumentError if an invalid source column is passed" do
34
- Company.slug(:invalid_source_column)
35
- assert_raises(ArgumentError) { Company.create! }
36
- end
37
-
38
- should "raise an ArgumentError if an invalid slug column is passed" do
39
- Company.slug(:name, :column => :bad_slug_column)
40
- assert_raises(ArgumentError) { Company.create! }
41
- end
42
- end
43
-
44
- should "set validation error if source column is empty" do
45
- article = Article.create
46
- assert !article.valid?
47
- require 'ruby-debug'
48
- assert article.errors.on(:slug)
49
- end
50
-
51
- should "set validation error if normalization makes source value empty" do
52
- article = Article.create(:headline => '$$$')
53
- assert !article.valid?
54
- assert article.errors.on(:slug)
55
- end
56
-
57
- should "not update the slug even if the source column changes" do
58
- article = Article.create!(:headline => 'Test Headline')
59
- article.update_attributes!(:headline => 'New Headline')
60
- assert_equal 'test-headline', article.slug
61
- end
62
-
63
- should "validate slug format on save" do
64
- article = Article.create!(:headline => 'Test Headline')
65
- article.slug = 'A BAD $LUG.'
66
-
67
- assert !article.valid?
68
- assert article.errors[:slug].present?
69
- end
70
-
71
- should "validate uniqueness of slug by default" do
72
- article1 = Article.create!(:headline => 'Test Headline')
73
- article2 = Article.create!(:headline => 'Test Headline')
74
- article2.slug = 'test-headline'
75
-
76
- assert !article2.valid?
77
- assert article2.errors[:slug].present?
78
- end
79
-
80
- should "use validate_uniquness_if proc to decide whether uniqueness validation applies" do
81
- article1 = Post.create!(:headline => 'Test Headline')
82
- article2 = Post.new
83
- article2.slug = 'test-headline'
84
-
85
- assert article2.valid?
86
- end
87
-
88
- should "not overwrite slug value on create if it was already specified" do
89
- a = Article.create!(:headline => 'Test Headline', :slug => 'slug1')
90
- assert_equal 'slug1', a.slug
91
- end
92
-
93
- context "resetting a slug" do
94
-
95
- setup do
96
- @article = Article.create(:headline => 'test headline')
97
- @original_slug = @article.slug
98
- end
99
-
100
- should "maintain the same slug if slug column hasn't changed" do
101
- @article.reset_slug
102
- assert_equal @original_slug, @article.slug
103
- end
104
-
105
- should "change slug if slug column has updated" do
106
- @article.headline = "donkey"
107
- @article.reset_slug
108
- assert_not_equal @original_slug, @article.slug
109
- end
110
-
111
- should "maintain sequence" do
112
- @existing_article = Article.create!(:headline => 'world cup')
113
- @article.headline = "world cup"
114
- @article.reset_slug
115
- assert_equal 'world-cup-1', @article.slug
116
- end
117
-
118
- end
119
-
120
-
121
- context "slug normalization" do
122
- setup do
123
- @article = Article.new
124
- end
125
-
126
- should "should lowercase strings" do
127
- @article.headline = 'AbC'
128
- @article.save!
129
- assert_equal "abc", @article.slug
130
- end
131
-
132
- should "should replace whitespace with dashes" do
133
- @article.headline = 'a b'
134
- @article.save!
135
- assert_equal 'a-b', @article.slug
136
- end
137
-
138
- should "should replace 2spaces with 1dash" do
139
- @article.headline = 'a b'
140
- @article.save!
141
- assert_equal 'a-b', @article.slug
142
- end
143
-
144
- should "should remove punctuation" do
145
- @article.headline = 'abc!@#$%^&*•¶§∞¢££¡¿()><?""\':;][]\.,/'
146
- @article.save!
147
- assert_match 'abc', @article.slug
148
- end
149
-
150
- should "should strip trailing space" do
151
- @article.headline = 'ab '
152
- @article.save!
153
- assert_equal 'ab', @article.slug
154
- end
155
-
156
- should "should strip leading space" do
157
- @article.headline = ' ab'
158
- @article.save!
159
- assert_equal 'ab', @article.slug
160
- end
161
-
162
- should "should strip trailing dashes" do
163
- @article.headline = 'ab-'
164
- @article.save!
165
- assert_match 'ab', @article.slug
166
- end
167
-
168
- should "should strip leading dashes" do
169
- @article.headline = '-ab'
170
- @article.save!
171
- assert_match 'ab', @article.slug
172
- end
173
-
174
- should "remove double-dashes" do
175
- @article.headline = 'a--b--c'
176
- @article.save!
177
- assert_match 'a-b-c', @article.slug
178
- end
179
-
180
- should "should not modify valid slug strings" do
181
- @article.headline = 'a-b-c-d'
182
- @article.save!
183
- assert_match 'a-b-c-d', @article.slug
184
- end
185
-
186
- should "not insert dashes for periods in acronyms, regardless of where they appear in string" do
187
- @article.headline = "N.Y.P.D. vs. N.S.A. vs. F.B.I."
188
- @article.save!
189
- assert_match 'nypd-vs-nsa-vs-fbi', @article.slug
190
- end
191
-
192
- should "not insert dashes for apostrophes" do
193
- @article.headline = "Thomas Jefferson's Papers"
194
- @article.save!
195
- assert_match 'thomas-jeffersons-papers', @article.slug
196
- end
197
-
198
- should "preserve numbers in slug" do
199
- @article.headline = "2010 Election"
200
- @article.save!
201
- assert_match '2010-election', @article.slug
202
- end
203
- end
204
-
205
- context "diacritics handling" do
206
- setup do
207
- @article = Article.new
208
- end
209
-
210
- should "should strip diacritics" do
211
- @article.headline = "açaí"
212
- @article.save!
213
- assert_equal "acai", @article.slug
214
- end
215
-
216
- should "strip diacritics correctly " do
217
- @article.headline = "ÀÁÂÃÄÅÆÇÈÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"
218
- @article.save!
219
- expected = "aaaaaaaeceeeiiiidnoooooouuuuythssaaaaaaaeceeeeiiiidnoooooouuuuythy".split(//)
220
- output = @article.slug.split(//)
221
- output.each_index do |i|
222
- assert_equal expected[i], output[i]
223
- end
224
- end
225
- end
226
-
227
- context "sequence handling" do
228
- should "not add a sequence if saving first instance of slug" do
229
- article = Article.create!(:headline => 'Test Headline')
230
- assert_equal 'test-headline', article.slug
231
- end
232
-
233
- should "assign a -1 suffix to the second instance of the slug" do
234
- article_1 = Article.create!(:headline => 'Test Headline')
235
- article_2 = Article.create!(:headline => 'Test Headline')
236
- assert_equal 'test-headline-1', article_2.slug
237
- end
238
-
239
- should "assign a -12 suffix to the thirteenth instance of the slug" do
240
- 12.times { |i| Article.create!(:headline => 'Test Headline') }
241
- article_13 = Article.create!(:headline => 'Test Headline')
242
- assert_equal 'test-headline-12', article_13.slug
243
- end
244
- end
245
-
246
- end