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 +3 -2
- data/gemfiles/Gemfile.rails-3.0.rb +2 -1
- data/gemfiles/Gemfile.rails-3.1.rb +1 -0
- data/lib/friendly_id.rb +1 -1
- data/lib/friendly_id/finder_methods.rb +2 -0
- data/lib/friendly_id/history.rb +8 -4
- data/lib/friendly_id/object_utils.rb +13 -5
- data/lib/generators/friendly_id_generator.rb +2 -11
- data/test/generator_test.rb +22 -0
- data/test/history_test.rb +11 -0
- data/test/shared.rb +2 -0
- metadata +34 -22
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 "
|
19
|
-
s.add_development_dependency "
|
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"
|
data/lib/friendly_id.rb
CHANGED
@@ -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)
|
data/lib/friendly_id/history.rb
CHANGED
@@ -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,
|
63
|
-
|
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,
|
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?
|
22
|
-
#
|
23
|
-
#
|
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
|
-
|
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
|
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.
|
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-
|
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: &
|
27
|
+
requirement: &70173663524880 !ruby/object:Gem::Requirement
|
17
28
|
none: false
|
18
29
|
requirements:
|
19
30
|
- - ~>
|
20
31
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
32
|
+
version: 3.1.0
|
22
33
|
type: :development
|
23
34
|
prerelease: false
|
24
|
-
version_requirements: *
|
35
|
+
version_requirements: *70173663524880
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: sqlite3
|
27
|
-
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:
|
43
|
+
version: 1.3.4
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70173663524340
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: minitest
|
38
|
-
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: *
|
57
|
+
version_requirements: *70173663523820
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: mocha
|
49
|
-
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: *
|
68
|
+
version_requirements: *70173663523340
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: ffaker
|
60
|
-
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: *
|
79
|
+
version_requirements: *70173663522820
|
69
80
|
- !ruby/object:Gem::Dependency
|
70
81
|
name: maruku
|
71
|
-
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: *
|
90
|
+
version_requirements: *70173663522180
|
80
91
|
- !ruby/object:Gem::Dependency
|
81
92
|
name: yard
|
82
|
-
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: *
|
101
|
+
version_requirements: *70173663521720
|
91
102
|
- !ruby/object:Gem::Dependency
|
92
103
|
name: i18n
|
93
|
-
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: *
|
112
|
+
version_requirements: *70173663521180
|
102
113
|
- !ruby/object:Gem::Dependency
|
103
114
|
name: simplecov
|
104
|
-
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: *
|
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
|