friendly_id 4.0.0.beta11 → 4.0.0.beta12

Sign up to get free protection for your applications and to get access to all the features.
data/friendly_id.gemspec CHANGED
@@ -15,8 +15,9 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- {test}/*`.split("\n")
16
16
  s.require_paths = ["lib"]
17
17
 
18
- s.add_development_dependency "activerecord", "~> 3.1"
19
- s.add_development_dependency "sqlite3", "~> 1.3"
18
+ s.add_development_dependency "railties", "~> 3.1.0"
19
+ s.add_development_dependency "activerecord", "~> 3.1.0"
20
+ s.add_development_dependency "sqlite3", "~> 1.3.4"
20
21
  s.add_development_dependency "minitest", "~> 2.4.0"
21
22
  s.add_development_dependency "mocha", "~> 0.9.12"
22
23
  s.add_development_dependency "ffaker", "~> 1.8.0"
@@ -12,7 +12,8 @@ platform :ruby do
12
12
  gem "sqlite3", "~> 1.3.4"
13
13
  end
14
14
 
15
- gem "activerecord", "3.0.10"
15
+ gem "activerecord", "~> 3.0.10"
16
16
  gem "minitest", "~> 2.4.0"
17
17
  gem "mocha", "~> 0.9.12"
18
+ gem "railties", "~> 3.0.10"
18
19
  gem "rake"
@@ -16,4 +16,5 @@ end
16
16
  gem "activerecord", "~> 3.1.0"
17
17
  gem "minitest", "~> 2.4.0"
18
18
  gem "mocha", "~> 0.9.12"
19
+ gem "railties", "~> 3.1.0"
19
20
  gem "rake"
data/lib/friendly_id.rb CHANGED
@@ -81,7 +81,7 @@ In general, use slugs by default unless you know for sure you don't need them.
81
81
  module FriendlyId
82
82
 
83
83
  # The current version.
84
- VERSION = "4.0.0.beta11"
84
+ VERSION = "4.0.0.beta12"
85
85
 
86
86
  @mutex = Mutex.new
87
87
 
@@ -23,6 +23,8 @@ module FriendlyId
23
23
  # @example
24
24
  # person = Person.exists?(123)
25
25
  # person = Person.exists?("joe")
26
+ # person = Person.exists?({:name => 'joe'})
27
+ # person = Person.exists?(['name = ?', 'joe'])
26
28
  #
27
29
  # @see FriendlyId::ObjectUtils
28
30
  def exists?(id = nil)
@@ -59,8 +59,9 @@ method.
59
59
  model_class.instance_eval do
60
60
  raise "FriendlyId::History is incompatible with FriendlyId::Scoped" if self < Scoped
61
61
  @friendly_id_config.use :slugged
62
- has_many :slugs, :as => :sluggable, :dependent => :destroy, :class_name => Slug.to_s
63
- before_save :build_slug, :if => lambda {|r| r.should_generate_new_friendly_id?}
62
+ has_many :slugs, :as => :sluggable, :dependent => :destroy,
63
+ :class_name => Slug.to_s, :order => "#{Slug.quoted_table_name}.id DESC"
64
+ before_save :build_slug
64
65
  relation_class.send :include, FinderMethods
65
66
  end
66
67
  end
@@ -68,6 +69,9 @@ method.
68
69
  private
69
70
 
70
71
  def build_slug
72
+ return unless should_generate_new_friendly_id?
73
+ # Allow reversion back to a previously used slug
74
+ slugs.where(:slug => friendly_id).delete_all
71
75
  slugs.build :slug => friendly_id
72
76
  end
73
77
 
@@ -76,7 +80,7 @@ method.
76
80
 
77
81
  # Search for a record in the slugs table using the specified slug.
78
82
  def find_one(id)
79
- return super if id.unfriendly_id?
83
+ return super(id) if id.unfriendly_id?
80
84
  where(@klass.friendly_id_config.query_field => id).first or
81
85
  with_old_friendly_id(id) {|x| find_one_without_friendly_id(x)} or
82
86
  find_one_without_friendly_id(id)
@@ -84,7 +88,7 @@ method.
84
88
 
85
89
  # Search for a record in the slugs table using the specified slug.
86
90
  def exists?(id = nil)
87
- return super if id.unfriendly_id?
91
+ return super(id) if id.unfriendly_id?
88
92
  exists_without_friendly_id?(@klass.friendly_id_config.query_field => id) or
89
93
  with_old_friendly_id(id) {|x| exists_without_friendly_id?(x)} or
90
94
  exists_without_friendly_id?(id)
@@ -12,17 +12,25 @@ module FriendlyId
12
12
  # else nil.
13
13
  #
14
14
  # An object is considired "definitely unfriendly" if its class is or
15
- # inherits from Numeric, Symbol or ActiveRecord::Base.
15
+ # inherits from ActiveRecord::Base, Array, Hash, NilClass, Numeric, or
16
+ # Symbol.
16
17
  #
17
18
  # An object is considered "definitely friendly" if it responds to +to_i+,
18
19
  # and its value when cast to an integer and then back to a string is
19
20
  # different from its value when merely cast to a string:
20
21
  #
21
- # 123.friendly_id? #=> false
22
- # "123".friendly_id? #=> nil
23
- # "abc123".friendly_id? #=> true
22
+ # 123.friendly_id? #=> false
23
+ # :id.friendly_id? #=> false
24
+ # {:name => 'joe'}.friendly_id? #=> false
25
+ # ['name = ?', 'joe'].friendly_id? #=> false
26
+ # nil.friendly_id? #=> false
27
+ # "123".friendly_id? #=> nil
28
+ # "abc123".friendly_id? #=> true
24
29
  def friendly_id?
25
- if [Numeric, Symbol, ActiveRecord::Base].detect {|klass| self.class < klass}
30
+ unfriendly_classes = [ActiveRecord::Base, Array, Hash, NilClass, Numeric, Symbol]
31
+ # Considered unfriendly if this is an instance of an unfriendly class or
32
+ # one of its descendants.
33
+ if unfriendly_classes.detect {|klass| self.class <= klass}
26
34
  false
27
35
  elsif respond_to?(:to_i) && to_i.to_s != to_s
28
36
  true
@@ -1,10 +1,11 @@
1
1
  require 'rails/generators'
2
- require 'rails/generators/migration'
2
+ require "rails/generators/active_record"
3
3
 
4
4
  # This generator adds a migration for the {FriendlyId::History
5
5
  # FriendlyId::History} addon.
6
6
  class FriendlyIdGenerator < Rails::Generators::Base
7
7
  include Rails::Generators::Migration
8
+ extend ActiveRecord::Generators::Migration
8
9
 
9
10
  source_root File.expand_path('../../friendly_id', __FILE__)
10
11
 
@@ -13,14 +14,4 @@ class FriendlyIdGenerator < Rails::Generators::Base
13
14
  migration_template 'migration.rb', 'db/migrate/create_friendly_id_slugs.rb'
14
15
  end
15
16
 
16
- # TODO: use the module provided with Rails, no need to do this
17
- # any more
18
- # Taken from ActiveRecord's migration generator
19
- def self.next_migration_number(dirname) #:nodoc:
20
- if ActiveRecord::Base.timestamped_migrations
21
- Time.now.utc.strftime("%Y%m%d%H%M%S")
22
- else
23
- "%.3d" % (current_migration_number(dirname) + 1)
24
- end
25
- end
26
17
  end
@@ -0,0 +1,22 @@
1
+ require File.expand_path("../helper", __FILE__)
2
+
3
+ require "test/unit"
4
+ require "rails/generators"
5
+ require "generators/friendly_id_generator"
6
+
7
+ class FriendlyIdGeneratorTest < Rails::Generators::TestCase
8
+
9
+ tests FriendlyIdGenerator
10
+ destination File.expand_path("../../tmp", __FILE__)
11
+
12
+ setup :prepare_destination
13
+
14
+ test "should generate a migration" do
15
+ begin
16
+ run_generator
17
+ assert_migration "db/migrate/create_friendly_id_slugs"
18
+ ensure
19
+ FileUtils.rm_rf self.destination_root
20
+ end
21
+ end
22
+ end
data/test/history_test.rb CHANGED
@@ -66,4 +66,15 @@ class HistoryTest < MiniTest::Unit::TestCase
66
66
  end
67
67
  end
68
68
 
69
+ test "should handle renames" do
70
+ with_instance_of(model_class) do |record|
71
+ record.name = 'x'
72
+ assert record.save
73
+ record.name = 'y'
74
+ assert record.save
75
+ record.name = 'x'
76
+ assert record.save
77
+ end
78
+ end
79
+
69
80
  end
data/test/shared.rb CHANGED
@@ -70,6 +70,8 @@ module FriendlyId
70
70
  with_instance_of(model_class) do |record|
71
71
  assert model_class.exists? record.id
72
72
  assert model_class.exists? record.friendly_id
73
+ assert model_class.exists?({:id => record.id})
74
+ assert model_class.exists?(['id = ?', record.id])
73
75
  assert !model_class.exists?(record.friendly_id + "-hello")
74
76
  assert !model_class.exists?(0)
75
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta11
4
+ version: 4.0.0.beta12
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,44 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-07 00:00:00.000000000 Z
12
+ date: 2011-09-13 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: &70173663525760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70173663525760
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: activerecord
16
- requirement: &70177920323920 !ruby/object:Gem::Requirement
27
+ requirement: &70173663524880 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ~>
20
31
  - !ruby/object:Gem::Version
21
- version: '3.1'
32
+ version: 3.1.0
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *70177920323920
35
+ version_requirements: *70173663524880
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: sqlite3
27
- requirement: &70177920323260 !ruby/object:Gem::Requirement
38
+ requirement: &70173663524340 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
31
42
  - !ruby/object:Gem::Version
32
- version: '1.3'
43
+ version: 1.3.4
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70177920323260
46
+ version_requirements: *70173663524340
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: minitest
38
- requirement: &70177920322440 !ruby/object:Gem::Requirement
49
+ requirement: &70173663523820 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 2.4.0
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70177920322440
57
+ version_requirements: *70173663523820
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: mocha
49
- requirement: &70177920321640 !ruby/object:Gem::Requirement
60
+ requirement: &70173663523340 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: 0.9.12
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70177920321640
68
+ version_requirements: *70173663523340
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: ffaker
60
- requirement: &70177920321060 !ruby/object:Gem::Requirement
71
+ requirement: &70173663522820 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ~>
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: 1.8.0
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *70177920321060
79
+ version_requirements: *70173663522820
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: maruku
71
- requirement: &70177920499500 !ruby/object:Gem::Requirement
82
+ requirement: &70173663522180 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ~>
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: 0.6.0
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70177920499500
90
+ version_requirements: *70173663522180
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: yard
82
- requirement: &70177920498640 !ruby/object:Gem::Requirement
93
+ requirement: &70173663521720 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ~>
@@ -87,10 +98,10 @@ dependencies:
87
98
  version: 0.7.2
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *70177920498640
101
+ version_requirements: *70173663521720
91
102
  - !ruby/object:Gem::Dependency
92
103
  name: i18n
93
- requirement: &70177920497740 !ruby/object:Gem::Requirement
104
+ requirement: &70173663521180 !ruby/object:Gem::Requirement
94
105
  none: false
95
106
  requirements:
96
107
  - - ~>
@@ -98,10 +109,10 @@ dependencies:
98
109
  version: 0.6.0
99
110
  type: :development
100
111
  prerelease: false
101
- version_requirements: *70177920497740
112
+ version_requirements: *70173663521180
102
113
  - !ruby/object:Gem::Dependency
103
114
  name: simplecov
104
- requirement: &70177920496960 !ruby/object:Gem::Requirement
115
+ requirement: &70173663520740 !ruby/object:Gem::Requirement
105
116
  none: false
106
117
  requirements:
107
118
  - - ! '>='
@@ -109,7 +120,7 @@ dependencies:
109
120
  version: '0'
110
121
  type: :development
111
122
  prerelease: false
112
- version_requirements: *70177920496960
123
+ version_requirements: *70173663520740
113
124
  description: ! 'FriendlyId is the "Swiss Army bulldozer" of slugging and permalink
114
125
  plugins for
115
126
 
@@ -159,6 +170,7 @@ files:
159
170
  - test/configuration_test.rb
160
171
  - test/core_test.rb
161
172
  - test/databases.yml
173
+ - test/generator_test.rb
162
174
  - test/helper.rb
163
175
  - test/history_test.rb
164
176
  - test/i18n_test.rb