friendly_id 2.0.1 → 2.0.2

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 (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
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,13 @@
1
+ == 2.0.2 2009-02-09
2
+
3
+ * 2 major enhancements:
4
+ * Made FriendlyId depend only on ActiveRecord. It should now be possible to
5
+ use FriendlyId with Camping or any other codebase that uses AR.
6
+ * Overhauled creaky testing setup and switched to Shoulda.
7
+
8
+ * 1 minor enhancment:
9
+ * Made reserved words work for non-slugged models.
10
+
1
11
  == 2.0.1 2009-01-19
2
12
 
3
13
  * 1 minor enhancements:
@@ -14,32 +14,19 @@ lib/friendly_id.rb
14
14
  lib/friendly_id/helpers.rb
15
15
  lib/friendly_id/non_sluggable_class_methods.rb
16
16
  lib/friendly_id/non_sluggable_instance_methods.rb
17
- lib/friendly_id/shoulda_macros.rb
18
17
  lib/friendly_id/slug.rb
19
18
  lib/friendly_id/sluggable_class_methods.rb
20
19
  lib/friendly_id/sluggable_instance_methods.rb
21
20
  lib/friendly_id/version.rb
22
21
  lib/tasks/friendly_id.rake
23
22
  lib/tasks/friendly_id.rb
24
- test/database.yml
25
- test/fixtures/countries.yml
26
- test/fixtures/country.rb
27
- test/fixtures/people.yml
28
- test/fixtures/person.rb
29
- test/fixtures/post.rb
30
- test/fixtures/posts.yml
31
- test/fixtures/slugs.yml
32
- test/fixtures/user.rb
33
- test/fixtures/users.yml
23
+ test/models/country.rb
24
+ test/models/person.rb
25
+ test/models/post.rb
26
+ test/models/user.rb
34
27
  test/non_slugged_test.rb
35
- test/rails/2.x/app/controllers/application.rb
36
- test/rails/2.x/config/boot.rb
37
- test/rails/2.x/config/database.yml
38
- test/rails/2.x/config/environment.rb
39
- test/rails/2.x/config/environments/test.rb
40
- test/rails/2.x/config/routes.rb
41
28
  test/schema.rb
42
29
  test/scoped_model_test.rb
43
30
  test/slug_test.rb
44
- test/sluggable_test.rb
31
+ test/slugged_model_test.rb
45
32
  test/test_helper.rb
data/Rakefile CHANGED
@@ -5,18 +5,15 @@ $hoe = Hoe.new("friendly_id", FriendlyId::Version::STRING) do |p|
5
5
  p.rubyforge_name = "friendly-id"
6
6
  p.author = ['Norman Clarke', 'Adrian Mugnolo', 'Emilio Tagua']
7
7
  p.email = ['norman@randomba.org', 'adrian@randomba.org', 'miloops@gmail.com']
8
- p.summary = "A comprehensive slugging and pretty-URL plugin for Ruby on Rails."
9
- p.description = 'A comprehensive slugging and pretty-URL plugin for Ruby on Rails.'
10
- p.url = 'http://randomba.org'
11
- p.need_tar = true
12
- p.need_zip = true
8
+ p.summary = "A comprehensive slugging and pretty-URL plugin for ActiveRecord."
9
+ p.description = 'A comprehensive slugging and pretty-URL plugin for ActiveRecord.'
10
+ p.url = 'http://friendly-id.rubyforge.org/'
13
11
  p.test_globs = ['test/**/*_test.rb']
14
12
  p.extra_deps << ['unicode', '>= 0.1']
15
- p.extra_dev_deps = [
16
- ['newgem', ">= #{::Newgem::VERSION}"]
17
- ]
18
- p.rdoc_pattern = /^(lib|bin|ext)|txt|rdoc$/
19
- changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
13
+ p.extra_deps << ['activerecord', '>= 2.0.0']
14
+ p.extra_dev_deps << ['newgem', ">= #{::Newgem::VERSION}"]
15
+ p.extra_dev_deps << ['Shoulda', ">= 1.2.0"]
16
+ p.extra_dev_deps << ['sqlite3-ruby']
20
17
  p.remote_rdoc_dir = ""
21
18
  end
22
19
 
@@ -2,24 +2,24 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{friendly_id}
5
- s.version = "2.0.1"
5
+ s.version = "2.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Norman Clarke", "Adrian Mugnolo", "Emilio Tagua"]
9
- s.date = %q{2009-01-19}
10
- s.description = %q{A comprehensive slugging and pretty-URL plugin for Ruby on Rails.}
9
+ s.cert_chain = ["/Users/norman/.gem/gem-public_cert.pem"]
10
+ s.date = %q{2009-02-09}
11
+ s.description = %q{A comprehensive slugging and pretty-URL plugin for ActiveRecord.}
11
12
  s.email = ["norman@randomba.org", "adrian@randomba.org", "miloops@gmail.com"]
12
13
  s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
13
- s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "friendly_id.gemspec", "generators/friendly_id/friendly_id_generator.rb", "generators/friendly_id/templates/create_slugs.rb", "generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb", "generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb", "init.rb", "lib/friendly_id.rb", "lib/friendly_id/helpers.rb", "lib/friendly_id/non_sluggable_class_methods.rb", "lib/friendly_id/non_sluggable_instance_methods.rb", "lib/friendly_id/shoulda_macros.rb", "lib/friendly_id/slug.rb", "lib/friendly_id/sluggable_class_methods.rb", "lib/friendly_id/sluggable_instance_methods.rb", "lib/friendly_id/version.rb", "lib/tasks/friendly_id.rake", "lib/tasks/friendly_id.rb", "test/database.yml", "test/fixtures/countries.yml", "test/fixtures/country.rb", "test/fixtures/people.yml", "test/fixtures/person.rb", "test/fixtures/post.rb", "test/fixtures/posts.yml", "test/fixtures/slugs.yml", "test/fixtures/user.rb", "test/fixtures/users.yml", "test/non_slugged_test.rb", "test/rails/2.x/app/controllers/application.rb", "test/rails/2.x/config/boot.rb", "test/rails/2.x/config/database.yml", "test/rails/2.x/config/environment.rb", "test/rails/2.x/config/environments/test.rb", "test/rails/2.x/config/routes.rb", "test/schema.rb", "test/scoped_model_test.rb", "test/slug_test.rb", "test/sluggable_test.rb", "test/test_helper.rb"]
14
+ s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "config/website.yml", "friendly_id.gemspec", "generators/friendly_id/friendly_id_generator.rb", "generators/friendly_id/templates/create_slugs.rb", "generators/friendly_id_20_upgrade/friendly_id_20_upgrade_generator.rb", "generators/friendly_id_20_upgrade/templates/upgrade_friendly_id_to_20.rb", "init.rb", "lib/friendly_id.rb", "lib/friendly_id/helpers.rb", "lib/friendly_id/non_sluggable_class_methods.rb", "lib/friendly_id/non_sluggable_instance_methods.rb", "lib/friendly_id/slug.rb", "lib/friendly_id/sluggable_class_methods.rb", "lib/friendly_id/sluggable_instance_methods.rb", "lib/friendly_id/version.rb", "lib/tasks/friendly_id.rake", "lib/tasks/friendly_id.rb", "test/models/country.rb", "test/models/person.rb", "test/models/post.rb", "test/models/user.rb", "test/non_slugged_test.rb", "test/schema.rb", "test/scoped_model_test.rb", "test/slug_test.rb", "test/slugged_model_test.rb", "test/test_helper.rb"]
14
15
  s.has_rdoc = true
15
- s.homepage = %q{http://randomba.org}
16
+ s.homepage = %q{http://friendly-id.rubyforge.org/}
16
17
  s.rdoc_options = ["--main", "README.rdoc"]
17
18
  s.require_paths = ["lib"]
18
19
  s.rubyforge_project = %q{friendly-id}
19
20
  s.rubygems_version = %q{1.3.1}
20
- s.signing_key = %q{/Users/norman/.gem/gem-private_key.pem}
21
- s.summary = %q{A comprehensive slugging and pretty-URL plugin for Ruby on Rails.}
22
- s.test_files = ["test/non_slugged_test.rb", "test/scoped_model_test.rb", "test/slug_test.rb", "test/sluggable_test.rb"]
21
+ s.summary = %q{A comprehensive slugging and pretty-URL plugin for ActiveRecord.}
22
+ s.test_files = ["test/non_slugged_test.rb", "test/scoped_model_test.rb", "test/slug_test.rb", "test/slugged_model_test.rb"]
23
23
 
24
24
  if s.respond_to? :specification_version then
25
25
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -27,16 +27,25 @@ Gem::Specification.new do |s|
27
27
 
28
28
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
29
29
  s.add_runtime_dependency(%q<unicode>, [">= 0.1"])
30
+ s.add_runtime_dependency(%q<active_record>, [">= 2.0.0"])
30
31
  s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
32
+ s.add_development_dependency(%q<Shoulda>, [">= 1.2.0"])
33
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
31
34
  s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
32
35
  else
33
36
  s.add_dependency(%q<unicode>, [">= 0.1"])
37
+ s.add_dependency(%q<active_record>, [">= 2.0.0"])
34
38
  s.add_dependency(%q<newgem>, [">= 1.2.3"])
39
+ s.add_dependency(%q<Shoulda>, [">= 1.2.0"])
40
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
35
41
  s.add_dependency(%q<hoe>, [">= 1.8.0"])
36
42
  end
37
43
  else
38
44
  s.add_dependency(%q<unicode>, [">= 0.1"])
45
+ s.add_dependency(%q<active_record>, [">= 2.0.0"])
39
46
  s.add_dependency(%q<newgem>, [">= 1.2.3"])
47
+ s.add_dependency(%q<Shoulda>, [">= 1.2.0"])
48
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
40
49
  s.add_dependency(%q<hoe>, [">= 1.8.0"])
41
50
  end
42
51
  end
@@ -1,21 +1,35 @@
1
- require 'unicode'
2
1
  require 'friendly_id/helpers'
3
2
  require 'friendly_id/slug'
4
- require 'friendly_id/shoulda_macros'
5
-
6
3
 
7
4
  # FriendlyId is a comprehensize Rails plugin/gem for slugging and permalinks.
8
5
  module FriendlyId
9
6
 
7
+ # Default options for has_friendly_id.
8
+ DEFAULT_FRIENDLY_ID_OPTIONS = {
9
+ :max_length => 255,
10
+ :method => nil,
11
+ :reserved => ["new", "index"],
12
+ :reserved_message => 'can not be "%s"',
13
+ :scope => nil,
14
+ :strip_diacritics => false,
15
+ :strip_non_ascii => false,
16
+ :use_slug => false }.freeze
17
+
18
+ # Valid keys for has_friendly_id options.
19
+ VALID_FRIENDLY_ID_KEYS = [
20
+ :max_length,
21
+ :reserved,
22
+ :reserved_message,
23
+ :scope,
24
+ :strip_diacritics,
25
+ :strip_non_ascii,
26
+ :use_slug ].freeze
27
+
10
28
  # This error is raised when it's not possible to generate a unique slug.
11
29
  class SlugGenerationError < StandardError ; end
12
30
 
13
31
  module ClassMethods
14
32
 
15
- # Default options for friendly_id.
16
- DEFAULT_FRIENDLY_ID_OPTIONS = {:method => nil, :use_slug => false, :max_length => 255, :reserved => [], :strip_diacritics => false, :scope => nil}.freeze
17
- VALID_FRIENDLY_ID_KEYS = [:use_slug, :max_length, :reserved, :strip_diacritics, :scope].freeze
18
-
19
33
  # Set up an ActiveRecord model to use a friendly_id.
20
34
  #
21
35
  # The column argument can be one of your model's columns, or a method
@@ -25,15 +39,17 @@ module FriendlyId
25
39
  # * <tt>:use_slug</tt> - Defaults to false. Use slugs when you want to use a non-unique text field for friendly ids.
26
40
  # * <tt>:max_length</tt> - Defaults to 255. The maximum allowed length for a slug.
27
41
  # * <tt>:strip_diacritics</tt> - Defaults to false. If true, it will remove accents, umlauts, etc. from western characters.
28
- # * <tt>:reseved</tt> - Array of words that are reserved and can't be used as slugs. If such a word is used, it will be treated the same as if that slug was already taken (numeric extension will be appended). Defaults to [].
42
+ # * <tt>:strip_non_ascii</tt> - Defaults to false. If true, it will all non-ascii ([^a-z0-9]) characters.
43
+ # * <tt>:reseved</tt> - Array of words that are reserved and can't be used as friendly_id's. For sluggable models, if such a word is used, it will be treated the same as if that slug was already taken (numeric extension will be appended). Defaults to ["new", "index"].
44
+ # * <tt>:reseved_message</tt> - The validation message that will be shown when a reserved word is used as a frindly_id. Defaults to '"%s" is reserved'.
29
45
  def has_friendly_id(column, options = {})
30
46
  options.assert_valid_keys VALID_FRIENDLY_ID_KEYS
31
47
  options = DEFAULT_FRIENDLY_ID_OPTIONS.merge(options).merge(:column => column)
32
48
  write_inheritable_attribute :friendly_id_options, options
33
- class_inheritable_reader :friendly_id_options
49
+ class_inheritable_accessor :friendly_id_options
34
50
 
35
51
  if options[:use_slug]
36
- has_many :slugs, :order => 'id DESC', :as => :sluggable, :dependent => :destroy, :readonly => true
52
+ has_many :slugs, :order => 'id DESC', :as => :sluggable, :dependent => :destroy
37
53
  require 'friendly_id/sluggable_class_methods'
38
54
  require 'friendly_id/sluggable_instance_methods'
39
55
  extend SluggableClassMethods
@@ -44,9 +60,10 @@ module FriendlyId
44
60
  require 'friendly_id/non_sluggable_instance_methods'
45
61
  extend NonSluggableClassMethods
46
62
  include NonSluggableInstanceMethods
63
+ validate_on_create :validate_friendly_id
47
64
  end
48
65
  end
49
-
66
+
50
67
  end
51
68
 
52
69
  class << self
@@ -55,7 +72,6 @@ module FriendlyId
55
72
  def enable
56
73
  return if ActiveRecord::Base.methods.include? 'has_friendly_id'
57
74
  ActiveRecord::Base.class_eval { extend FriendlyId::ClassMethods }
58
- Test::Unit::TestCase.class_eval { include FriendlyId::ShouldaMacros }
59
75
  end
60
76
 
61
77
  end
@@ -64,4 +80,4 @@ end
64
80
 
65
81
  if defined?(ActiveRecord)
66
82
  FriendlyId::enable
67
- end
83
+ end
@@ -21,17 +21,17 @@ module FriendlyId::NonSluggableClassMethods
21
21
  end
22
22
 
23
23
  def find_some_with_friendly(ids_and_names, options) #:nodoc:#
24
-
24
+
25
25
  results = with_scope :find => options do
26
- find :all, :conditions => ["#{ quoted_table_name }.#{ primary_key } IN (?) OR #{friendly_id_options[:column].to_s} IN (?)",
26
+ find :all, :conditions => ["#{quoted_table_name}.#{primary_key} IN (?) OR #{friendly_id_options[:column].to_s} IN (?)",
27
27
  ids_and_names, ids_and_names]
28
28
  end
29
-
29
+
30
30
  expected = expected_size(ids_and_names, options)
31
31
  if results.size != expected
32
32
  raise ActiveRecord::RecordNotFound, "Couldn't find all #{ name.pluralize } with IDs (#{ ids_and_names * ', ' }) AND #{ sanitize_sql options[:conditions] } (found #{ results.size } results, but was looking for #{ expected })"
33
33
  end
34
-
34
+
35
35
  results.each {|r| r.send(:found_using_friendly_id=, true) if ids_and_names.include?(r.friendly_id)}
36
36
 
37
37
  results
@@ -25,6 +25,14 @@ module FriendlyId::NonSluggableInstanceMethods
25
25
  end
26
26
 
27
27
  private
28
+
29
+ def validate_friendly_id
30
+ if self.class.friendly_id_options[:reserved].include? friendly_id
31
+ self.errors.add(self.class.friendly_id_options[:column],
32
+ self.class.friendly_id_options[:reserved_message] % friendly_id)
33
+ return false
34
+ end
35
+ end
28
36
 
29
37
  def found_using_friendly_id=(value) #:nodoc#
30
38
  @found_using_friendly_id = value
@@ -1,3 +1,4 @@
1
+ require 'unicode'
1
2
  # A Slug is a unique, human-friendly identifier for an ActiveRecord.
2
3
  class Slug < ActiveRecord::Base
3
4
 
@@ -23,12 +24,12 @@ class Slug < ActiveRecord::Base
23
24
  # terror in Europe unlike anything ever seen before or after. I'm not
24
25
  # taking any chances.
25
26
  def normalize(slug_text)
26
- return "" if slug_text.blank?
27
- slug_text.
27
+ return "" if slug_text.nil? || slug_text == ""
28
+ Unicode::normalize_KC(slug_text).
28
29
  send(chars_func).
29
30
  # For some reason Spanish ¡ and ¿ are not detected as non-word
30
31
  # characters. Bug in Ruby?
31
- normalize.gsub(/[\W|¡|¿]/u, ' ').
32
+ gsub(/[\W|¡|¿]/u, ' ').
32
33
  strip.
33
34
  gsub(/\s+/u, '-').
34
35
  gsub(/-\z/u, '').
@@ -42,20 +43,21 @@ class Slug < ActiveRecord::Base
42
43
  return name, sequence
43
44
  end
44
45
 
45
- # Remove diacritics (accents, umlauts, etc.) from the string.
46
+ # Remove diacritics (accents, umlauts, etc.) from the string. Borrowed
47
+ # from "The Ruby Way."
46
48
  def strip_diacritics(string)
47
- require 'unicode'
48
- Unicode::normalize_KD(string).unpack('U*').select { |cp|
49
- cp < 0x300 || cp > 0x036F
50
- }.pack('U*')
49
+ Unicode::normalize_KD(string).unpack('U*').select { |u| u < 0x300 || u > 0x036F }.pack('U*')
50
+ end
51
+
52
+ # Remove non-ascii characters from the string.
53
+ def strip_non_ascii(string)
54
+ strip_diacritics(string).gsub(/[^a-z0-9]+/i, ' ')
51
55
  end
52
56
 
53
57
  private
54
58
 
55
59
  def chars_func
56
- Rails.version =~ /2.2.[\d]*/ ? :mb_chars : :chars
57
- rescue NoMethodError
58
- :chars
60
+ "".respond_to?(:mb_chars) ? :mb_chars : :chars
59
61
  end
60
62
 
61
63
  end
@@ -73,7 +75,7 @@ class Slug < ActiveRecord::Base
73
75
 
74
76
  # Raise a FriendlyId::SlugGenerationError if the slug name is blank.
75
77
  def check_for_blank_name #:nodoc:#
76
- if name.blank?
78
+ if name == "" || name.nil?
77
79
  raise FriendlyId::SlugGenerationError.new("The slug text is blank.")
78
80
  end
79
81
  end
@@ -1,5 +1,5 @@
1
1
  module FriendlyId::SluggableClassMethods
2
-
2
+
3
3
  include FriendlyId::Helpers
4
4
 
5
5
  def self.extended(base) #:nodoc:#
@@ -38,6 +38,17 @@ module FriendlyId::SluggableClassMethods
38
38
  end
39
39
 
40
40
  result
41
+ rescue ActiveRecord::RecordNotFound => e
42
+
43
+ if friendly_id_options[:scope]
44
+ if !scope
45
+ e.message << "; expected scope but got none"
46
+ else
47
+ e.message << " and scope=#{scope}"
48
+ end
49
+ end
50
+
51
+ raise e
41
52
 
42
53
  end
43
54
 
@@ -87,7 +98,7 @@ module FriendlyId::SluggableClassMethods
87
98
  ids = []
88
99
  ids_and_names.each do |id_or_name|
89
100
  name, sequence = Slug.parse id_or_name
90
- slug = Slug.find(:first, :readonly => true, :conditions => {
101
+ slug = Slug.find(:first, :conditions => {
91
102
  :name => name,
92
103
  :scope => scope,
93
104
  :sequence => sequence,
@@ -6,7 +6,7 @@ module FriendlyId::SluggableInstanceMethods
6
6
  attr_accessor :finder_slug_name
7
7
 
8
8
  def finder_slug
9
- @finder_slug ||= init_finder_slug
9
+ @finder_slug ||= init_finder_slug or nil
10
10
  end
11
11
 
12
12
  # Was the record found using one of its friendly ids?
@@ -57,10 +57,13 @@ module FriendlyId::SluggableInstanceMethods
57
57
  def slug_text
58
58
  base = send friendly_id_options[:column]
59
59
  if self.friendly_id_options[:strip_diacritics]
60
- base = Slug::normalize(Slug::strip_diacritics(base))
61
- else
62
- base = Slug::normalize(base)
60
+ base = Slug::strip_diacritics(base)
63
61
  end
62
+ if self.friendly_id_options[:strip_non_ascii]
63
+ base = Slug::strip_non_ascii(base)
64
+ end
65
+ base = Slug::normalize(base)
66
+
64
67
  if base.length > friendly_id_options[:max_length]
65
68
  base = base[0...friendly_id_options[:max_length]]
66
69
  end
@@ -82,7 +85,7 @@ module FriendlyId::SluggableInstanceMethods
82
85
  def init_finder_slug
83
86
  return false if !@finder_slug_name
84
87
  name, sequence = Slug.parse(@finder_slug_name)
85
- slug = Slug.find(:first, :conditions => {:sluggable_id => id, :name => name, :sequence => sequence, :sluggable_type => self.class.name })
88
+ slug = Slug.find(:first, :conditions => {:sluggable_id => id, :name => name, :sequence => sequence, :sluggable_type => self.class.base_class.name })
86
89
  finder_slug = slug
87
90
  end
88
91
 
@@ -2,7 +2,7 @@ module FriendlyId #:nodoc:
2
2
  module Version #:nodoc:
3
3
  MAJOR = 2
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 2
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+ has_friendly_id :title, :use_slug => true
3
+ end
File without changes
@@ -2,84 +2,89 @@ require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class NonSluggedTest < Test::Unit::TestCase
4
4
 
5
- fixtures :users
5
+ context "A non-slugged model with default FriendlyId options" do
6
6
 
7
- def setup
8
- end
7
+ setup do
8
+ User.delete_all
9
+ @user = User.create!(:login => "joe", :email => "joe@example.org")
10
+ end
9
11
 
10
- def test_should_find_user_using_friendly_id
11
- assert User.find(users(:joe).friendly_id)
12
- end
12
+ should "have friendly_id options" do
13
+ assert_not_nil User.friendly_id_options
14
+ end
13
15
 
14
- def test_should_find_users_using_friendly_id
15
- assert User.find([users(:joe).friendly_id])
16
- end
16
+ should "not have a slug" do
17
+ assert !@user.respond_to?(:slug)
18
+ end
17
19
 
18
- def test_to_param_should_return_a_string
19
- assert_equal String, users(:joe).to_param.class
20
- end
20
+ should "be findable by its friendly_id" do
21
+ assert User.find(@user.friendly_id)
22
+ end
21
23
 
22
- def test_should_not_find_users_using_non_existent_friendly_ids
23
- assert_raises ActiveRecord::RecordNotFound do
24
- User.find(['bad', 'bad2'])
24
+ should "be findable by its regular id" do
25
+ assert User.find(@user.id)
25
26
  end
26
- end
27
-
28
- def test_finding_by_array_with_friendly_and_non_friendly_id_for_same_record_raises_error
29
- assert_raises ActiveRecord::RecordNotFound do
30
- User.find([users(:joe).id, "joe"]).size
27
+
28
+ should "respect finder conditions" do
29
+ assert_raises ActiveRecord::RecordNotFound do
30
+ User.find(@user.friendly_id, :conditions => "1 = 2")
31
+ end
31
32
  end
32
- end
33
33
 
34
- def test_finding_with_mixed_array_should_indicate_whether_found_by_numeric_or_friendly
35
- @users = User.find([users(:jane).id, "joe"], :order => "login ASC")
36
- assert @users[0].found_using_numeric_id?
37
- assert @users[1].found_using_friendly_id?
38
- end
34
+ should "indicate if it was found by its friendly id" do
35
+ @user = User.find(@user.friendly_id)
36
+ assert @user.found_using_friendly_id?
37
+ end
39
38
 
40
- def test_finder_options_are_not_ignored
41
- assert_raises ActiveRecord::RecordNotFound do
42
- User.find(users(:joe).friendly_id, :conditions => "1 = 2")
39
+ should "indicate if it was found by its numeric id" do
40
+ @user = User.find(@user.id)
41
+ assert @user.found_using_numeric_id?
43
42
  end
44
- assert_raises ActiveRecord::RecordNotFound do
45
- User.find([users(:joe).friendly_id], :conditions => "1 = 2")
43
+
44
+ should "indicate if it has a better id" do
45
+ @user = User.find(@user.id)
46
+ assert @user.has_better_id?
46
47
  end
47
- end
48
48
 
49
- def test_user_should_have_friendly_id_options
50
- assert_not_nil User.friendly_id_options
51
- end
49
+ should "not validate if the friendly_id text is reserved" do
50
+ @user = User.new(:login => "new", :email => "test@example.org")
51
+ assert !@user.valid?
52
+ end
52
53
 
53
- def test_user_should_not_be_found_using_friendly_id_unless_it_really_was
54
- assert !User.find(users(:joe).id).found_using_friendly_id?
55
- end
54
+ should "have always string for a friendly_id" do
55
+ assert_equal String, @user.to_param.class
56
+ end
56
57
 
57
- def test_users_should_not_be_found_using_friendly_id_unless_they_really_were
58
- @users = User.find([users(:jane).id])
59
- assert @users[0].found_using_numeric_id?
60
- end
58
+ context "when using an array as the find argument" do
61
59
 
62
- def test_user_should_be_considered_found_by_numeric_id_as_default
63
- @user = User.new
64
- assert @user.found_using_numeric_id?
65
- end
60
+ setup do
61
+ @user2 = User.create(:login => "jane", :email => "jane@example.org")
62
+ end
66
63
 
67
- def test_user_should_indicate_if_it_was_found_using_numeric_id
68
- @user = User.find(users(:joe).id)
69
- assert @user.found_using_numeric_id?
70
- assert !@user.found_using_friendly_id?
71
- end
64
+ should "return results" do
65
+ assert_equal 2, User.find([@user.friendly_id, @user2.friendly_id]).size
66
+ end
72
67
 
73
- def test_user_should_indicate_if_it_was_found_using_friendly_id
74
- @user = User.find(users(:joe).friendly_id)
75
- assert !@user.found_using_numeric_id?
76
- assert @user.found_using_friendly_id?
77
- end
68
+ should "not allow mixed friendly and non-friendly ids for the same record" do
69
+ assert_raises ActiveRecord::RecordNotFound do
70
+ User.find([@user.id, @user.friendly_id]).size
71
+ end
72
+ end
73
+
74
+ should "raise an error when all records are not found" do
75
+ assert_raises ActiveRecord::RecordNotFound do
76
+ User.find(['bad', 'bad2'])
77
+ end
78
+ end
79
+
80
+ should "indicate if the results were found using a friendly_id" do
81
+ @users = User.find([@user.id, @user2.friendly_id], :order => "login ASC")
82
+ assert @users[0].found_using_friendly_id?
83
+ assert @users[1].found_using_numeric_id?
84
+ end
85
+
86
+ end
78
87
 
79
- def test_should_indicate_there_is_a_better_id_if_found_by_numeric_id
80
- @user = User.find(users(:joe).id)
81
- assert @user.found_using_numeric_id?
82
- assert @user.has_better_id?
83
88
  end
84
89
 
85
90
  end