friendly_id 5.0.5 → 5.1.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +1 -5
- data/README.md +2 -0
- data/lib/friendly_id/candidates.rb +18 -5
- data/lib/friendly_id/slugged.rb +2 -2
- data/lib/friendly_id/version.rb +1 -1
- data/test/candidates_test.rb +117 -0
- data/test/reserved_test.rb +26 -1
- data/test/schema.rb +5 -2
- data/test/shared.rb +0 -2
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f6c2b8454c78ef4f095d0bdc7f1541c59a2a975
|
4
|
+
data.tar.gz: bdd5bc1adf9f8aeb697587994b488bb863323b62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5dfeb116b0f1a3dc381fb4ca22c6280f32a3989c3e0b93658b362512ea1670ba2dfc97f27689d8eb2c419da9b07386f1b9ff3297916a2809ef643ba725523a81
|
7
|
+
data.tar.gz: 89d1b972eaa26c8cacae62df4e709a4cba87aebfd3fc19c9a1bd7c3c4c97e179486289294c54209ff63b660db1543d12cba8ed0f304f279c7d6a30f7e2faad3f
|
data/Changelog.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
We would like to think our many {file:Contributors contributors} for
|
4
4
|
suggestions, ideas and improvements to FriendlyId.
|
5
5
|
|
6
|
-
## 5.1.0 (
|
6
|
+
## 5.1.0 (NOT RELEASED YET)
|
7
7
|
|
8
8
|
* FriendlyId will no longer allow blank strings as slugs ([#571](https://github.com/norman/friendly_id/pull/571)).
|
9
9
|
* FriendlyId will now try to use the first non-reserved candidate as its
|
@@ -11,10 +11,6 @@ suggestions, ideas and improvements to FriendlyId.
|
|
11
11
|
* Fix order dependence bug between history and scoped modules ([#588](https://github.com/norman/friendly_id/pull/588)).
|
12
12
|
* Fix "friendly" finds on Rails 4.2 ([#607](https://github.com/norman/friendly_id/issues/607)).
|
13
13
|
|
14
|
-
## 5.0.5 (2015-01-15)
|
15
|
-
|
16
|
-
* Backported bugfixes and Rails 4.2 support from 5.1.0.
|
17
|
-
|
18
14
|
## 5.0.4 (2014-05-29)
|
19
15
|
|
20
16
|
* Bug fix for call to removed `primary` method on Edge Rails. ([#557](https://github.com/norman/friendly_id/pull/557)).
|
data/README.md
CHANGED
@@ -38,6 +38,8 @@ versioning, i18n, scoped slugs, reserved words, and custom slug generators.
|
|
38
38
|
|
39
39
|
### What Changed in Version 5.1
|
40
40
|
|
41
|
+
_NOTE: 5.1 is the latest beta version and will be released soon._
|
42
|
+
|
41
43
|
5.1 is a bugfix release, but bumps the minor version because some applications may be dependent
|
42
44
|
on the previously buggy behavior. The changes include:
|
43
45
|
|
@@ -14,12 +14,16 @@ module FriendlyId
|
|
14
14
|
end
|
15
15
|
|
16
16
|
# Visits each candidate, calls it, passes it to `normalize_friendly_id` and
|
17
|
-
# yields
|
17
|
+
# yields any wanted and unreserved slug candidates.
|
18
18
|
def each(*args, &block)
|
19
|
-
@candidates.map do |candidate|
|
20
|
-
|
21
|
-
|
19
|
+
pre_candidates = @candidates.map do |candidate|
|
20
|
+
@object.normalize_friendly_id(candidate.map(&:call).join(' '))
|
21
|
+
end.select {|x| wanted?(x)}
|
22
|
+
|
23
|
+
unless pre_candidates.all? {|x| reserved?(x)}
|
24
|
+
pre_candidates.reject! {|x| reserved?(x)}
|
22
25
|
end
|
26
|
+
pre_candidates.each {|x| yield x}
|
23
27
|
end
|
24
28
|
|
25
29
|
private
|
@@ -44,7 +48,16 @@ module FriendlyId
|
|
44
48
|
end
|
45
49
|
|
46
50
|
def wanted?(slug)
|
47
|
-
|
51
|
+
slug.present?
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def reserved?(slug)
|
57
|
+
config = @object.friendly_id_config
|
58
|
+
return false unless config.uses? ::FriendlyId::Reserved
|
59
|
+
return false unless config.reserved_words
|
60
|
+
config.reserved_words.include?(slug)
|
48
61
|
end
|
49
62
|
end
|
50
63
|
end
|
data/lib/friendly_id/slugged.rb
CHANGED
@@ -251,9 +251,9 @@ Github issue](https://github.com/norman/friendly_id/issues/185) for discussion.
|
|
251
251
|
# Process the given value to make it suitable for use as a slug.
|
252
252
|
#
|
253
253
|
# This method is not intended to be invoked directly; FriendlyId uses it
|
254
|
-
#
|
254
|
+
# internaly to process strings into slugs.
|
255
255
|
#
|
256
|
-
# However, if FriendlyId's default slug generation doesn't
|
256
|
+
# However, if FriendlyId's default slug generation doesn't suite your needs,
|
257
257
|
# you can override this method in your model class to control exactly how
|
258
258
|
# slugs are generated.
|
259
259
|
#
|
data/lib/friendly_id/version.rb
CHANGED
@@ -0,0 +1,117 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class CandidatesTest < Minitest::Test
|
4
|
+
|
5
|
+
include FriendlyId::Test
|
6
|
+
|
7
|
+
class City < ActiveRecord::Base
|
8
|
+
extend FriendlyId
|
9
|
+
friendly_id :slug_candidates, use: :slugged
|
10
|
+
alias_attribute :slug_candidates, :name
|
11
|
+
end
|
12
|
+
|
13
|
+
def model_class
|
14
|
+
City
|
15
|
+
end
|
16
|
+
|
17
|
+
def with_instances_of(klass = model_class, &block)
|
18
|
+
transaction do
|
19
|
+
city1 = klass.create! :name => "New York", :code => "JFK"
|
20
|
+
city2 = klass.create! :name => "New York", :code => "EWR"
|
21
|
+
yield city1, city2
|
22
|
+
end
|
23
|
+
end
|
24
|
+
alias_method :with_instances, :with_instances_of
|
25
|
+
|
26
|
+
test "resolves conflict with candidate" do
|
27
|
+
with_instances do |city1, city2|
|
28
|
+
assert_equal "new-york", city1.slug
|
29
|
+
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city2.slug)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
test "accepts candidate as symbol" do
|
34
|
+
klass = Class.new model_class do
|
35
|
+
def slug_candidates
|
36
|
+
:name
|
37
|
+
end
|
38
|
+
end
|
39
|
+
with_instances_of klass do |_, city|
|
40
|
+
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
test "accepts multiple candidates" do
|
45
|
+
klass = Class.new model_class do
|
46
|
+
def slug_candidates
|
47
|
+
[name, code]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
with_instances_of klass do |_, city|
|
51
|
+
assert_equal "ewr", city.slug
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
test "ignores blank candidate" do
|
56
|
+
klass = Class.new model_class do
|
57
|
+
def slug_candidates
|
58
|
+
[name, ""]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
with_instances_of klass do |_, city|
|
62
|
+
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
test "ignores nil candidate" do
|
67
|
+
klass = Class.new model_class do
|
68
|
+
def slug_candidates
|
69
|
+
[name, nil]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
with_instances_of klass do |_, city|
|
73
|
+
assert_match(/\Anew-york-([a-z0-9]+\-){4}[a-z0-9]+\z/, city.slug)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
test "accepts candidate with nested array" do
|
78
|
+
klass = Class.new model_class do
|
79
|
+
def slug_candidates
|
80
|
+
[name, [name, code]]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
with_instances_of klass do |_, city|
|
84
|
+
assert_equal "new-york-ewr", city.slug
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
test "accepts candidate with lambda" do
|
89
|
+
klass = Class.new City do
|
90
|
+
def slug_candidates
|
91
|
+
[name, [name, ->{ rand 1000 }]]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
with_instances_of klass do |_, city|
|
95
|
+
assert_match(/\Anew-york-\d{,3}\z/, city.friendly_id)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
test "accepts candidate with object" do
|
100
|
+
klass = Class.new City do
|
101
|
+
class Airport
|
102
|
+
def initialize(code)
|
103
|
+
@code = code
|
104
|
+
end
|
105
|
+
attr_reader :code
|
106
|
+
alias_method :to_s, :code
|
107
|
+
end
|
108
|
+
def slug_candidates
|
109
|
+
[name, [name, Airport.new(code)]]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
with_instances_of klass do |_, city|
|
113
|
+
assert_equal "new-york-ewr", city.friendly_id
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
data/test/reserved_test.rb
CHANGED
@@ -6,13 +6,17 @@ class ReservedTest < Minitest::Test
|
|
6
6
|
|
7
7
|
class Journalist < ActiveRecord::Base
|
8
8
|
extend FriendlyId
|
9
|
-
friendly_id :
|
9
|
+
friendly_id :slug_candidates, :use => [:slugged, :reserved], :reserved_words => %w(new edit)
|
10
10
|
|
11
11
|
after_validation :move_friendly_id_error_to_name
|
12
12
|
|
13
13
|
def move_friendly_id_error_to_name
|
14
14
|
errors.add :name, *errors.delete(:friendly_id) if errors[:friendly_id].present?
|
15
15
|
end
|
16
|
+
|
17
|
+
def slug_candidates
|
18
|
+
name
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
def model_class
|
@@ -37,4 +41,25 @@ class ReservedTest < Minitest::Test
|
|
37
41
|
end
|
38
42
|
end
|
39
43
|
|
44
|
+
test "should reject reserved candidates" do
|
45
|
+
transaction do
|
46
|
+
record = model_class.new(:name => 'new')
|
47
|
+
def record.slug_candidates
|
48
|
+
[:name, "foo"]
|
49
|
+
end
|
50
|
+
record.save!
|
51
|
+
assert_equal "foo", record.friendly_id
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
test "should be invalid if all candidates are reserved" do
|
56
|
+
transaction do
|
57
|
+
record = model_class.new(:name => 'new')
|
58
|
+
def record.slug_candidates
|
59
|
+
["edit", "new"]
|
60
|
+
end
|
61
|
+
assert_raises(ActiveRecord::RecordInvalid) {record.save!}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
40
65
|
end
|
data/test/schema.rb
CHANGED
@@ -64,13 +64,16 @@ module FriendlyId
|
|
64
64
|
# Used to test :scoped and :history together
|
65
65
|
add_column :restaurants, :city_id, :integer
|
66
66
|
|
67
|
+
# Used to test candidates
|
68
|
+
add_column :cities, :code, :string, :limit => 3
|
69
|
+
|
67
70
|
@done = true
|
68
71
|
end
|
69
72
|
|
70
73
|
private
|
71
74
|
|
72
75
|
def slugged_tables
|
73
|
-
%w[journalists articles novelists novels manuals]
|
76
|
+
%w[journalists articles novelists novels manuals cities]
|
74
77
|
end
|
75
78
|
|
76
79
|
def tables_with_uuid_primary_key
|
@@ -82,7 +85,7 @@ module FriendlyId
|
|
82
85
|
end
|
83
86
|
|
84
87
|
def simple_tables
|
85
|
-
%w[authors books publishers
|
88
|
+
%w[authors books publishers]
|
86
89
|
end
|
87
90
|
|
88
91
|
def tables
|
data/test/shared.rb
CHANGED
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: 5.0.
|
4
|
+
version: 5.1.0.beta.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Norman Clarke
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -184,6 +184,7 @@ files:
|
|
184
184
|
- lib/friendly_id/version.rb
|
185
185
|
- lib/generators/friendly_id_generator.rb
|
186
186
|
- test/base_test.rb
|
187
|
+
- test/candidates_test.rb
|
187
188
|
- test/configuration_test.rb
|
188
189
|
- test/core_test.rb
|
189
190
|
- test/databases.yml
|
@@ -214,9 +215,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
214
215
|
version: 1.9.3
|
215
216
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
216
217
|
requirements:
|
217
|
-
- - "
|
218
|
+
- - ">"
|
218
219
|
- !ruby/object:Gem::Version
|
219
|
-
version:
|
220
|
+
version: 1.3.1
|
220
221
|
requirements: []
|
221
222
|
rubyforge_project: friendly_id
|
222
223
|
rubygems_version: 2.4.4
|