friendly_id 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -6,6 +6,14 @@ suggestions, ideas and improvements to FriendlyId.
6
6
  * Table of Contents
7
7
  {:toc}
8
8
 
9
+ ## 3.0.2 (2010-04-09)
10
+
11
+ * Fixed finding non-slugged models by an array of ids.
12
+ * Added backported `tidy_bytes` implementation from [utf8_utils](http://github.com/norman/utf8_utils)
13
+ * Removed dependency on Rubygems 1.3.6; this blocked deploy on Heroku (thanks Steven Noble)
14
+ * Replaced File.dirname calls with File.expand_path, which should allow compatibility with Ruby 1.9.2
15
+ * Cleanups and some improvements to tests.
16
+
9
17
  ## 3.0.1 (2010-03-30)
10
18
 
11
19
  * Fixed bad path in Rails 2.x generator.
@@ -15,7 +23,7 @@ suggestions, ideas and improvements to FriendlyId.
15
23
  * Rails 3 support.
16
24
  * Removed features deprecated in FriendlyId 2.3.
17
25
  * Fixed searching by numeric friendly_id in non-slugged models.
18
- * Added :allow_nil config option (Andre Duffeck and Norman Clarke)
26
+ * Added `:allow_nil` config option (Andre Duffeck and Norman Clarke)
19
27
 
20
28
  ## 2.3.4 (2010-03-22)
21
29
 
data/Guide.md CHANGED
@@ -517,7 +517,7 @@ any time.
517
517
  FriendlyId uses [Bundler](http://github.com/carlhuda/bundler) to manage its gem
518
518
  dependencies. To run the tests, first make sure you have bundler installed.
519
519
  Then, copy Gemfile.default to Gemfile. If you wish to test against different gem
520
- versions than the ones specifed in the Gemfile (for example, to test with
520
+ versions than the ones specified in the Gemfile (for example, to test with
521
521
  Postgres or MySQL rather than SQLite3, or to test against different versions of
522
522
  ActiveRecord), then simply modify the Gemfile to suit your dependencies.
523
523
 
@@ -535,19 +535,25 @@ enabled. But if it is, then your patches would be very welcome!
535
535
 
536
536
  ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
537
537
  activerecord (2.3.5)
538
- friendly_id (2.3.0)
538
+ friendly_id (2.3.2)
539
539
  sqlite3 3.6.19 in-memory database
540
+ | DEFAULT | NO_SLUG | SLUG | CACHED_SLUG |
540
541
  ------------------------------------------------------------------------------------------------
541
- find model using id x10000 | 2.948 | 0 |
542
- find model using array of ids x10000 | 6.020 | 0 |
543
- find unslugged model using friendly id x10000 | 4.474 | 52% |
544
- find unslugged model using array of friendly ids x10000 | 6.498 | 7% |
545
- find slugged model using friendly id x10000 | 7.536 | 155% |
546
- find slugged model using array of friendly ids x10000 | 18.020 | 200% |
547
- find cached slugged model using friendly id x10000 | 4.791 | 63% |
548
- find cached slugged model using array of friendly ids x10000 | 7.275 | 21% |
549
- find model using id, then to_param x10000 | 2.974 | 0 |
550
- find unslugged model using friendly id, then to_param x10000 | 4.608 | 55% |
551
- find slugged model using friendly id, then to_param x10000 | 12.589 | 323% |
552
- find cached slugged model using friendly id, then to_param x10000 | 5.037 | 69% |
542
+ find model by id x1000 | 0.274 | 0.426 | 0.780 | 0.489 |
543
+ find model using array of ids x1000 | 0.517 | 0.525 | 1.692 | 0.623 |
544
+ find model using id, then to_param x1000 | 0.279 | 0.431 | 1.253 | 0.498 |
545
+ ================================================================================================
546
+ Total | 1.071 | 1.382 | 3.725 | 1.610 |
547
+
548
+ ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
549
+ activerecord (3.0.0.beta1)
550
+ friendly_id (2.3.2)
551
+ sqlite3 3.6.19 in-memory database
552
+
553
+ | DEFAULT | NO_SLUG | SLUG | CACHED_SLUG |
553
554
  ------------------------------------------------------------------------------------------------
555
+ find model by id x1000 | 0.557 | 1.135 | 6.491 | 1.398 |
556
+ find model using array of ids x1000 | 0.862 | 0.882 | 6.152 | 1.919 |
557
+ find model using id, then to_param x1000 | 0.658 | 2.200 | 8.398 | 1.539 |
558
+ ================================================================================================
559
+ Total | 2.077 | 4.217 | 21.041 | 4.856 |
data/Rakefile CHANGED
@@ -4,8 +4,9 @@ require "rake/gempackagetask"
4
4
  require "rake/rdoctask"
5
5
  require "rake/clean"
6
6
 
7
- CLEAN << "pkg" << "doc" << "coverage" << ".yardoc"
7
+ task :default => :test
8
8
 
9
+ CLEAN << "pkg" << "doc" << "coverage" << ".yardoc"
9
10
  Rake::GemPackageTask.new(eval(File.read("friendly_id.gemspec"))) { |pkg| }
10
11
  Rake::RDocTask.new do |r|
11
12
  r.rdoc_dir = "doc"
@@ -32,10 +33,7 @@ rescue LoadError
32
33
  end
33
34
 
34
35
 
35
- task :test do
36
- Rake::Task["test:friendly_id"].invoke
37
- Rake::Task["test:ar"].invoke
38
- end
36
+ Rake::TestTask.new(:test) { |t| t.pattern = "test/**/*_test.rb" }
39
37
 
40
38
  namespace :test do
41
39
  task :rails do
@@ -45,7 +43,7 @@ namespace :test do
45
43
  end
46
44
  Rake::TestTask.new(:friendly_id) { |t| t.pattern = "test/*_test.rb" }
47
45
  Rake::TestTask.new(:ar) { |t| t.pattern = "test/active_record_adapter/*_test.rb" }
48
-
46
+
49
47
  namespace :rails do
50
48
  task :plugin do
51
49
  rm_rf "fid"
@@ -65,4 +63,4 @@ task :pushdocs do
65
63
  sh "git push origin gh-pages"
66
64
  sh "git checkout #{branch}"
67
65
  sh "git stash apply"
68
- end
66
+ end
data/extras/bench.rb CHANGED
@@ -1,59 +1,36 @@
1
- #!/usr/bin/env ruby -KU
2
- require File.dirname(__FILE__) + '/extras'
1
+ require File.expand_path('../extras', __FILE__)
3
2
  require 'rbench'
4
- FACTOR = 100
3
+ FACTOR = 10
4
+
5
5
  RBench.run(TIMES) do
6
6
 
7
7
  column :times
8
- column :ar
8
+ column :default
9
+ column :no_slug
10
+ column :slug
11
+ column :cached_slug
9
12
 
10
- report 'find model using id', (TIMES * FACTOR).ceil do
11
- ar { User.find(get_id) }
13
+ report 'find model by id', (TIMES * FACTOR).ceil do
14
+ default { User.find(get_id) }
15
+ no_slug { User.find(USERS.rand) }
16
+ slug { Post.find(POSTS.rand) }
17
+ cached_slug { District.find(DISTRICTS.rand) }
12
18
  end
13
19
 
14
20
  report 'find model using array of ids', (TIMES * FACTOR).ceil do
15
- ar { User.find([get_id, get_id]) }
16
- end
17
-
18
- report 'find unslugged model using friendly id', (TIMES * FACTOR).ceil do
19
- ar { User.find(USERS.rand) }
20
- end
21
-
22
- report 'find unslugged model using array of friendly ids', (TIMES * FACTOR).ceil do
23
- ar { User.find([USERS.rand, USERS.rand]) }
24
- end
25
-
26
- report 'find slugged model using friendly id', (TIMES * FACTOR).ceil do
27
- ar { Post.find(POSTS.rand) }
28
- end
29
-
30
- report 'find slugged model using array of friendly ids', (TIMES * FACTOR).ceil do
31
- ar { Post.find([POSTS.rand, POSTS.rand]) }
32
- end
33
-
34
- report 'find cached slugged model using friendly id', (TIMES * FACTOR).ceil do
35
- ar { District.find(DISTRICTS.rand) }
36
- end
37
-
38
- report 'find cached slugged model using array of friendly ids', (TIMES * FACTOR).ceil do
39
- ar { District.find([DISTRICTS.rand, DISTRICTS.rand]) }
21
+ default { User.find(get_id(2)) }
22
+ no_slug { User.find(USERS.rand(2)) }
23
+ slug { Post.find(POSTS.rand(2)) }
24
+ cached_slug { District.find(DISTRICTS.rand(2)) }
40
25
  end
41
26
 
42
27
  report 'find model using id, then to_param', (TIMES * FACTOR).ceil do
43
- ar { User.find(get_id).to_param }
44
- end
45
-
46
- report 'find unslugged model using friendly id, then to_param', (TIMES * FACTOR).ceil do
47
- ar { User.find(USERS.rand).to_param }
28
+ default { User.find(get_id).to_param }
29
+ no_slug { User.find(USERS.rand).to_param }
30
+ slug { Post.find(POSTS.rand).to_param }
31
+ cached_slug { District.find(DISTRICTS.rand).to_param }
48
32
  end
49
33
 
50
- report 'find slugged model using friendly id, then to_param', (TIMES * FACTOR).ceil do
51
- ar { Post.find(POSTS.rand).to_param }
52
- end
53
-
54
- report 'find cached slugged model using friendly id, then to_param', (TIMES * FACTOR).ceil do
55
- ar { District.find(DISTRICTS.rand).to_param }
56
- end
57
-
58
34
  summary 'Total'
35
+
59
36
  end
data/extras/extras.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby -KU
2
2
  require File.dirname(__FILE__) + '/../test/test_helper'
3
- require File.dirname(__FILE__) + '/../test/active_record_adapter/test_helper'
3
+ require File.dirname(__FILE__) + '/../test/active_record_adapter/ar_test_helper'
4
4
  require 'ffaker'
5
5
 
6
6
  TIMES = (ENV['N'] || 100).to_i
@@ -11,7 +11,7 @@ USERS = []
11
11
  User.delete_all
12
12
  Post.delete_all
13
13
  District.delete_all
14
- (Slug or Slug).delete_all
14
+ Slug.delete_all
15
15
 
16
16
  100.times do
17
17
  name = Faker::Name.name
@@ -20,12 +20,19 @@ District.delete_all
20
20
  DISTRICTS << (District.create! :name => name).friendly_id
21
21
  end
22
22
 
23
- def get_id
24
- rand(99) + 1
23
+ def get_id(returns = 1)
24
+ (1..100).to_a.rand(returns)
25
25
  end
26
26
 
27
27
  class Array
28
- def rand
29
- self[Kernel.rand(length)]
28
+ def rand(returns = 1)
29
+ @return = []
30
+ returns.times do
31
+ until @return.length == returns do
32
+ val = self[Kernel.rand(length)]
33
+ @return << val unless @return.include? val
34
+ end
35
+ end
36
+ return returns == 1 ? @return.first : @return
30
37
  end
31
38
  end
@@ -33,10 +33,6 @@ module FriendlyId
33
33
  @custom_cache_column = cache_column
34
34
  end
35
35
 
36
- def cache_finders?
37
- !! cache_column
38
- end
39
-
40
36
  def child_scopes
41
37
  @child_scopes ||= associated_friendly_classes.select { |klass| klass.friendly_id_config.scopes_over?(configured_class) }
42
38
  end
@@ -24,7 +24,7 @@ module FriendlyId
24
24
  include SimpleFinder
25
25
 
26
26
  def find
27
- @results = model_class.scoped(:conditions => conditions).scoped(options).all(options)
27
+ @results = model_class.scoped(:conditions => conditions).all(options).uniq
28
28
  raise(::ActiveRecord::RecordNotFound, error_message) if @results.size != expected_size
29
29
  friendly_results.each { |result| result.friendly_id_status.name = result.to_param }
30
30
  @results
@@ -62,10 +62,6 @@ module FriendlyId
62
62
  slugs.detect {|slug| result.id == slug.sluggable_id}
63
63
  end
64
64
 
65
- def unfriendly_ids?
66
- ! unfriendly_ids.empty?
67
- end
68
-
69
65
  end
70
66
 
71
67
  # Performs a find a single friendly_id using the cached_slug column,
@@ -86,10 +86,6 @@ module FriendlyId
86
86
  yield self if block_given?
87
87
  end
88
88
 
89
- def forbid_nil?
90
- !allow_nil?
91
- end
92
-
93
89
  def reserved_words=(*words)
94
90
  @reserved_words = words.flatten.uniq
95
91
  end
@@ -59,6 +59,42 @@ module FriendlyId
59
59
  :spanish => Hash[209, [78, 110], 241, [110, 110]]
60
60
  }
61
61
 
62
+ # CP-1252 decimal byte => UTF-8 approximation as an array of bytes
63
+ CP1252 = {
64
+ 128 => [226, 130, 172],
65
+ 129 => nil,
66
+ 130 => [226, 128, 154],
67
+ 131 => [198, 146],
68
+ 132 => [226, 128, 158],
69
+ 133 => [226, 128, 166],
70
+ 134 => [226, 128, 160],
71
+ 135 => [226, 128, 161],
72
+ 136 => [203, 134],
73
+ 137 => [226, 128, 176],
74
+ 138 => [197, 160],
75
+ 139 => [226, 128, 185],
76
+ 140 => [197, 146],
77
+ 141 => nil,
78
+ 142 => [197, 189],
79
+ 143 => nil,
80
+ 144 => nil,
81
+ 145 => [226, 128, 152],
82
+ 146 => [226, 128, 153],
83
+ 147 => [226, 128, 156],
84
+ 148 => [226, 128, 157],
85
+ 149 => [226, 128, 162],
86
+ 150 => [226, 128, 147],
87
+ 151 => [226, 128, 148],
88
+ 152 => [203, 156],
89
+ 153 => [226, 132, 162],
90
+ 154 => [197, 161],
91
+ 155 => [226, 128, 186],
92
+ 156 => [197, 147],
93
+ 157 => nil,
94
+ 158 => [197, 190],
95
+ 159 => [197, 184]
96
+ }
97
+
62
98
  cattr_accessor :approximations
63
99
  self.approximations = []
64
100
 
@@ -110,6 +146,7 @@ module FriendlyId
110
146
  # @param string [String] The string to use as the basis of the SlugString.
111
147
  def initialize(string)
112
148
  super string.to_s
149
+ tidy_bytes!
113
150
  end
114
151
 
115
152
  # Approximate an ASCII string. This works only for Western strings using
@@ -218,10 +255,61 @@ module FriendlyId
218
255
  with_dashes!
219
256
  end
220
257
 
221
- # Truncate the string to +max+ length.
222
- # @return String
223
- def truncate!(max)
224
- @wrapped_string = self[0...max].to_s if length > max
258
+ # Attempt to replace invalid UTF-8 bytes with valid ones. This method
259
+ # naively assumes if you have invalid UTF8 bytes, they are either Windows
260
+ # CP-1252 or ISO8859-1. In practice this isn't a bad assumption, but may not
261
+ # always work.
262
+ #
263
+ # Passing +true+ will forcibly tidy all bytes, assuming that the string's
264
+ # encoding is CP-1252 or ISO-8859-1.
265
+ def tidy_bytes!(force = false)
266
+
267
+ if force
268
+ @wrapped_string = @wrapped_string.unpack("C*").map do |b|
269
+ tidy_byte(b)
270
+ end.flatten.compact.pack("C*").unpack("U*").pack("U*")
271
+ end
272
+
273
+ bytes = @wrapped_string.unpack("C*")
274
+ conts_expected = 0
275
+ last_lead = 0
276
+
277
+ bytes.each_index do |i|
278
+
279
+ byte = bytes[i]
280
+ is_ascii = byte < 128
281
+ is_cont = byte > 127 && byte < 192
282
+ is_lead = byte > 191 && byte < 245
283
+ is_unused = byte > 240
284
+ is_restricted = byte > 244
285
+
286
+ # Impossible or highly unlikely byte? Clean it.
287
+ if is_unused || is_restricted
288
+ bytes[i] = tidy_byte(byte)
289
+ elsif is_cont
290
+ # Not expecting contination byte? Clean up. Otherwise, now expect one less.
291
+ conts_expected == 0 ? bytes[i] = tidy_byte(byte) : conts_expected -= 1
292
+ else
293
+ if conts_expected > 0
294
+ # Expected continuation, but got ASCII or leading? Clean backwards up to
295
+ # the leading byte.
296
+ (1..(i - last_lead)).each {|j| bytes[i - j] = tidy_byte(bytes[i - j])}
297
+ conts_expected = 0
298
+ end
299
+ if is_lead
300
+ # Final byte is leading? Clean it.
301
+ if i == bytes.length - 1
302
+ bytes[i] = tidy_byte(bytes.last)
303
+ else
304
+ # Valid leading byte? Expect continuations determined by position of
305
+ # first zero bit, with max of 3.
306
+ conts_expected = byte < 224 ? 1 : byte < 240 ? 2 : 3
307
+ last_lead = i
308
+ end
309
+ end
310
+ end
311
+ end
312
+ @wrapped_string = bytes.empty? ? "" : bytes.flatten.compact.pack("C*").unpack("U*").pack("U*")
225
313
  end
226
314
 
227
315
  # Delete any non-ascii characters.
@@ -235,6 +323,12 @@ module FriendlyId
235
323
  end
236
324
  end
237
325
 
326
+ # Truncate the string to +max+ length.
327
+ # @return String
328
+ def truncate!(max)
329
+ @wrapped_string = self[0...max].to_s if length > max
330
+ end
331
+
238
332
  # Upper-cases the string. Note that this works for Unicode strings,
239
333
  # though your milage may vary with Greek and Turkic strings.
240
334
  # @return String
@@ -261,8 +355,8 @@ module FriendlyId
261
355
  @wrapped_string = @wrapped_string.gsub(/[\s\-]+/u, "-")
262
356
  end
263
357
 
264
- %w[approximate_ascii clean downcase word_chars normalize normalize_for to_ascii
265
- truncate upcase with_dashes].each do |method|
358
+ %w[approximate_ascii clean downcase word_chars normalize normalize_for tidy_bytes
359
+ to_ascii truncate upcase with_dashes].each do |method|
266
360
  class_eval(<<-EOM)
267
361
  def #{method}(*args)
268
362
  send_to_new_instance(:#{method}!, *args)
@@ -289,5 +383,9 @@ module FriendlyId
289
383
  string
290
384
  end
291
385
 
386
+ def tidy_byte(byte)
387
+ byte < 160 ? CP1252[byte] : byte < 192 ? [194, byte] : [195, byte - 64]
388
+ end
389
+
292
390
  end
293
391
  end
@@ -198,6 +198,7 @@ module FriendlyId
198
198
 
199
199
  # Tests for FriendlyId::Status.
200
200
  module Status
201
+
201
202
  test "should default to not friendly" do
202
203
  assert !status.friendly?
203
204
  end
@@ -205,6 +206,7 @@ module FriendlyId
205
206
  test "should default to numeric" do
206
207
  assert status.numeric?
207
208
  end
209
+
208
210
  end
209
211
 
210
212
  # Tests for FriendlyId::Status for a model that uses slugs.
@@ -2,7 +2,7 @@ module FriendlyId
2
2
  module Version
3
3
  MAJOR = 3
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
  BUILD = nil
7
7
  STRING = [MAJOR, MINOR, TINY, BUILD].compact.join('.')
8
8
  end
@@ -1,16 +1,17 @@
1
- require File.dirname(__FILE__) + "/../test_helper"
1
+ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  require "active_record"
4
4
  require "active_support"
5
5
 
6
- require File.dirname(__FILE__) + "/../../lib/friendly_id/active_record.rb"
7
- require File.dirname(__FILE__) + "/../../generators/friendly_id/templates/create_slugs"
8
- require File.dirname(__FILE__) + "/support/models"
9
- require File.dirname(__FILE__) + '/core'
10
- require File.dirname(__FILE__) + '/slugged'
6
+ require File.expand_path("../../../lib/friendly_id/active_record", __FILE__)
7
+ require File.expand_path("../../../generators/friendly_id/templates/create_slugs", __FILE__)
8
+ require File.expand_path("../support/models", __FILE__)
9
+ require File.expand_path('../core', __FILE__)
10
+ require File.expand_path('../slugged', __FILE__)
11
+
12
+ local_db_settings = File.expand_path("../support/database.yml", __FILE__)
13
+ default_db_settings = File.expand_path("../support/database.sqlite3.yml", __FILE__)
11
14
 
12
- local_db_settings = File.dirname(__FILE__) + "/support/database.yml"
13
- default_db_settings = File.dirname(__FILE__) + "/support/database.sqlite3.yml"
14
15
  db_settings = File.exists?(local_db_settings) ? local_db_settings : default_db_settings
15
16
  ActiveRecord::Base.establish_connection(YAML::load(File.open(db_settings)))
16
17
 
@@ -32,7 +33,6 @@ end
32
33
 
33
34
  # A model that specifies a custom cached slug column
34
35
  class City < ActiveRecord::Base
35
- attr_accessible :name
36
36
  has_friendly_id :name, :use_slug => true, :cache_column => "my_slug"
37
37
  end
38
38
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('../ar_test_helper', __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
 
@@ -44,26 +44,31 @@ module FriendlyId
44
44
 
45
45
  test "instances should be findable by an array of friendly_ids" do
46
46
  second = klass.create!(:name => "second_instance")
47
+ third = klass.create!(:name => "third_instance")
47
48
  assert_equal 2, klass.find([instance.friendly_id, second.friendly_id]).size
48
49
  end
49
50
 
50
51
  test "instances should be findable by an array of numeric ids" do
51
52
  second = klass.create!(:name => "second_instance")
53
+ third = klass.create!(:name => "third_instance")
52
54
  assert_equal 2, klass.find([instance.id.to_i, second.id.to_i]).size
53
55
  end
54
56
 
55
57
  test "instances should be findable by an array of numeric ids as strings" do
56
58
  second = klass.create!(:name => "second_instance")
59
+ third = klass.create!(:name => "third_instance")
57
60
  assert_equal 2, klass.find([instance.id.to_s, second.id.to_s]).size
58
61
  end
59
62
 
60
63
  test "instances should be findable by an array of instances" do
61
64
  second = klass.create!(:name => "second_instance")
65
+ third = klass.create!(:name => "third_instance")
62
66
  assert_equal 2, klass.find([instance, second]).size
63
67
  end
64
68
 
65
69
  test "instances should be findable by an array of mixed types" do
66
70
  second = klass.create!(:name => "second_instance")
71
+ third = klass.create!(:name => "third_instance")
67
72
  assert_equal 2, klass.find([instance.friendly_id, second]).size
68
73
  end
69
74
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('../ar_test_helper', __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -21,11 +21,12 @@ module FriendlyId
21
21
 
22
22
  class StatusTest < ::Test::Unit::TestCase
23
23
 
24
- include FriendlyId::Test::Generic
25
- include FriendlyId::Test::Simple
26
- include FriendlyId::Test::ActiveRecordAdapter::Core
27
24
  include SimpleTest
28
25
 
26
+ def setup
27
+ User.delete_all
28
+ end
29
+
29
30
  test "should default to not friendly" do
30
31
  assert !status.friendly?
31
32
  end
@@ -51,8 +52,21 @@ module FriendlyId
51
52
  end
52
53
 
53
54
  class BasicTest < ::Test::Unit::TestCase
54
- include Core
55
+ include FriendlyId::Test::Generic
56
+ include FriendlyId::Test::Simple
57
+ include FriendlyId::Test::ActiveRecordAdapter::Core
55
58
  include SimpleTest
59
+
60
+ test "status should be friendly when found using friendly id" do
61
+ record = klass.send(find_method, instance.friendly_id)
62
+ assert record.friendly_id_status.friendly?
63
+ end
64
+
65
+ test "status should not be friendly when found using numeric id" do
66
+ record = klass.send(find_method, instance.id)
67
+ assert !record.friendly_id_status.friendly?
68
+ end
69
+
56
70
  end
57
71
 
58
72
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
- require File.dirname(__FILE__) + '/test_helper'
2
+ require File.expand_path("../ar_test_helper", __FILE__)
3
3
 
4
4
  module FriendlyId
5
5
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path("../ar_test_helper", __FILE__)
2
2
 
3
3
 
4
4
  module FriendlyId
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper'
1
+ require File.expand_path('../ar_test_helper', __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
  module Test
@@ -20,8 +20,9 @@ class CreateSupportModels < ActiveRecord::Migration
20
20
  t.string :name
21
21
  t.string :my_slug
22
22
  t.integer :population
23
- t.index :my_slug, :unique => true
24
23
  end
24
+ add_index :cities, :my_slug, :unique => true
25
+
25
26
 
26
27
  create_table :countries do |t|
27
28
  t.string :name
@@ -31,8 +32,8 @@ class CreateSupportModels < ActiveRecord::Migration
31
32
  t.string :name
32
33
  t.string :note
33
34
  t.string :cached_slug
34
- t.index :cached_slug, :unique => true
35
35
  end
36
+ add_index :districts, :cached_slug, :unique => true
36
37
 
37
38
  create_table :events do |t|
38
39
  t.string :name
@@ -67,8 +68,8 @@ class CreateSupportModels < ActiveRecord::Migration
67
68
 
68
69
  create_table :users do |t|
69
70
  t.string :name
70
- t.index :name, :unique => true
71
71
  end
72
+ add_index :users, :name, :unique => true
72
73
 
73
74
  end
74
75
 
@@ -1,5 +1,5 @@
1
- require(File.dirname(__FILE__) + '/test_helper')
2
- require File.dirname(__FILE__) + '/../../lib/friendly_id/active_record_adapter/tasks'
1
+ require File.expand_path('../ar_test_helper', __FILE__)
2
+ require File.expand_path('../../../lib/friendly_id/active_record_adapter/tasks', __FILE__)
3
3
 
4
4
  class TasksTest < Test::Unit::TestCase
5
5
 
@@ -1,4 +1,4 @@
1
- require(File.dirname(__FILE__) + "/test_helper")
1
+ require File.expand_path('../test_helper', __FILE__)
2
2
 
3
3
  module FriendlyId
4
4
 
@@ -73,8 +73,14 @@ module FriendlyId
73
73
  assert_equal "検-索", SlugString.new("検 索").with_dashes
74
74
  end
75
75
 
76
- test "to_ascii should work with invalid UTF-8 strings" do
77
- assert_equal "abc", SlugString.new("\x93abc").to_ascii.to_s
76
+ test "should work with invalid UTF-8 strings" do
77
+ %w[approximate_ascii clean downcase word_chars normalize to_ascii upcase with_dashes].each do |method|
78
+ string = SlugString.new("\x93abc")
79
+ assert_nothing_raised do
80
+ method == "truncate" ? string.send(method, 32) : string.send(method)
81
+ end
82
+ end
83
+
78
84
  end
79
85
 
80
86
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $KCODE = "UTF8" if RUBY_VERSION < "1.9"
2
2
  $VERBOSE = false
3
3
  begin
4
- require File.join(File.dirname(__FILE__), '../.bundle/environment')
4
+ require File.expand_path('../../.bundle/environment', __FILE__)
5
5
  rescue LoadError
6
6
  # Fall back on doing an unlocked resolve at runtime.
7
7
  require "rubygems"
@@ -11,5 +11,5 @@ end
11
11
  require "test/unit"
12
12
  require "mocha"
13
13
  require "active_support"
14
- require File.dirname(__FILE__) + "/../lib/friendly_id"
15
- require File.dirname(__FILE__) + "/../lib/friendly_id/test"
14
+ require File.expand_path("../../lib/friendly_id", __FILE__)
15
+ require File.expand_path("../../lib/friendly_id/test", __FILE__)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 3
7
7
  - 0
8
- - 1
9
- version: 3.0.1
8
+ - 2
9
+ version: 3.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Norman Clarke
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-03-30 00:00:00 -03:00
19
+ date: 2010-04-09 00:00:00 -03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -85,6 +85,7 @@ files:
85
85
  - rails/init.rb
86
86
  - generators/friendly_id/friendly_id_generator.rb
87
87
  - generators/friendly_id/templates/create_slugs.rb
88
+ - test/active_record_adapter/ar_test_helper.rb
88
89
  - test/active_record_adapter/basic_slugged_model_test.rb
89
90
  - test/active_record_adapter/cached_slug_test.rb
90
91
  - test/active_record_adapter/core.rb
@@ -102,7 +103,6 @@ files:
102
103
  - test/active_record_adapter/support/database.sqlite3.yml
103
104
  - test/active_record_adapter/support/models.rb
104
105
  - test/active_record_adapter/tasks_test.rb
105
- - test/active_record_adapter/test_helper.rb
106
106
  - test/friendly_id_test.rb
107
107
  - test/slug_string_test.rb
108
108
  - test/test_helper.rb
@@ -133,10 +133,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  - - ">="
134
134
  - !ruby/object:Gem::Version
135
135
  segments:
136
- - 1
137
- - 3
138
- - 6
139
- version: 1.3.6
136
+ - 0
137
+ version: "0"
140
138
  requirements: []
141
139
 
142
140
  rubyforge_project: friendly-id