friendly_id 5.2.0 → 5.2.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 +7 -0
- data/README.md +1 -1
- data/lib/friendly_id/candidates.rb +21 -12
- data/lib/friendly_id/sequentially_slugged.rb +11 -6
- data/lib/friendly_id/slugged.rb +2 -2
- data/lib/friendly_id/version.rb +1 -1
- data/test/schema.rb +3 -0
- data/test/sequentially_slugged_test.rb +28 -0
- data/test/slugged_test.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67c7eb649e6e0fa756ebcfdbae36f276d7bc6adc
|
4
|
+
data.tar.gz: '093984f88a0e674471260a9989673190d48c5b22'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9039e1625fed0fcc1001b9b0b97cf51f6205aa103211e137ca4db834aa75f18c37a98a54e3030f42d8bb0f83489683d1fec8dce6cc2650d763ee4fb0b5e4c033
|
7
|
+
data.tar.gz: 120aa54db11b436a357122cfd0a53c39bba48c4724d89ce100e5a6dbf8dbec1db6955ebd3c2b118e8c0366505732b8c4e12100e44b40b9fc422904b30a48d29b
|
data/Changelog.md
CHANGED
@@ -3,6 +3,13 @@
|
|
3
3
|
We would like to think our many [contributors](https://github.com/norman/friendly_id/graphs/contributors) for
|
4
4
|
suggestions, ideas and improvements to FriendlyId.
|
5
5
|
|
6
|
+
## 5.2.1 (2017-04-09)
|
7
|
+
|
8
|
+
* Change ActiveRecord::Base to ApplicationRecord ([#782](https://github.com/norman/friendly_id/pull/782)).
|
9
|
+
* Refactor `Candidates#each` method. ([#773](https://github.com/norman/friendly_id/pull/773)).
|
10
|
+
* Assign to configured slug column, not 'slug' when validation fails. ([#779](https://github.com/norman/friendly_id/pull/779)).
|
11
|
+
* Fix sequential slugs when using History. ([#774](https://github.com/norman/friendly_id/pull/774)).
|
12
|
+
|
6
13
|
## 5.2.0 (2016-12-01)
|
7
14
|
|
8
15
|
* Add sequential slug module for FriendlyId 4.x-style sequential slugs. ([#644](https://github.com/norman/friendly_id/pull/644)).
|
data/README.md
CHANGED
@@ -10,26 +10,35 @@ module FriendlyId
|
|
10
10
|
|
11
11
|
def initialize(object, *array)
|
12
12
|
@object = object
|
13
|
-
@
|
13
|
+
@raw_candidates = to_candidate_array(object, array.flatten(1))
|
14
14
|
end
|
15
15
|
|
16
|
-
# Visits each candidate, calls it, passes it to `normalize_friendly_id` and
|
17
|
-
# yields any wanted and unreserved slug candidates.
|
18
16
|
def each(*args, &block)
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
return candidates unless block_given?
|
18
|
+
candidates.each{ |candidate| yield candidate }
|
19
|
+
end
|
22
20
|
|
23
|
-
|
24
|
-
pre_candidates.reject! {|x| reserved?(x)}
|
25
|
-
end
|
21
|
+
private
|
26
22
|
|
27
|
-
|
23
|
+
def candidates
|
24
|
+
@candidates ||= begin
|
25
|
+
candidates = normalize(@raw_candidates)
|
26
|
+
filter(candidates)
|
27
|
+
end
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
+
def normalize(candidates)
|
31
|
+
candidates.map do |candidate|
|
32
|
+
@object.normalize_friendly_id(candidate.map(&:call).join(' '))
|
33
|
+
end.select {|x| wanted?(x)}
|
30
34
|
end
|
31
35
|
|
32
|
-
|
36
|
+
def filter(candidates)
|
37
|
+
unless candidates.all? {|x| reserved?(x)}
|
38
|
+
candidates.reject! {|x| reserved?(x)}
|
39
|
+
end
|
40
|
+
candidates
|
41
|
+
end
|
33
42
|
|
34
43
|
def to_candidate_array(object, array)
|
35
44
|
array.map do |candidate|
|
@@ -10,16 +10,18 @@ module FriendlyId
|
|
10
10
|
SequentialSlugCalculator.new(scope_for_slug_generator,
|
11
11
|
candidate,
|
12
12
|
friendly_id_config.slug_column,
|
13
|
-
friendly_id_config.sequence_separator
|
13
|
+
friendly_id_config.sequence_separator,
|
14
|
+
self.class.base_class).next_slug
|
14
15
|
end
|
15
16
|
|
16
17
|
class SequentialSlugCalculator
|
17
18
|
attr_accessor :scope, :slug, :slug_column, :sequence_separator
|
18
19
|
|
19
|
-
def initialize(scope, slug, slug_column, sequence_separator)
|
20
|
+
def initialize(scope, slug, slug_column, sequence_separator, base_class)
|
20
21
|
@scope = scope
|
21
22
|
@slug = slug
|
22
|
-
|
23
|
+
table_name = scope.connection.quote_table_name(base_class.arel_table.name)
|
24
|
+
@slug_column = "#{table_name}.#{scope.connection.quote_column_name(slug_column)}"
|
23
25
|
@sequence_separator = sequence_separator
|
24
26
|
end
|
25
27
|
|
@@ -34,9 +36,12 @@ module FriendlyId
|
|
34
36
|
end
|
35
37
|
|
36
38
|
def last_sequence_number
|
37
|
-
|
38
|
-
|
39
|
-
|
39
|
+
regexp = /#{slug}#{sequence_separator}(\d+)\z/
|
40
|
+
# Reject slug_conflicts that doesn't come from the first_candidate
|
41
|
+
# Map all sequence numbers and take the maximum
|
42
|
+
slug_conflicts.reject{ |slug_conflict| !regexp.match(slug_conflict) }.map do |slug_conflict|
|
43
|
+
regexp.match(slug_conflict)[1].to_i
|
44
|
+
end.max
|
40
45
|
end
|
41
46
|
|
42
47
|
def slug_conflicts
|
data/lib/friendly_id/slugged.rb
CHANGED
@@ -327,9 +327,9 @@ Github issue](https://github.com/norman/friendly_id/issues/185) for discussion.
|
|
327
327
|
private :slug_generator
|
328
328
|
|
329
329
|
def unset_slug_if_invalid
|
330
|
-
if errors.present? && attribute_changed?(friendly_id_config.query_field)
|
330
|
+
if errors.present? && attribute_changed?(friendly_id_config.query_field.to_s)
|
331
331
|
diff = changes[friendly_id_config.query_field]
|
332
|
-
|
332
|
+
send "#{friendly_id_config.slug_column}=", diff.first
|
333
333
|
end
|
334
334
|
end
|
335
335
|
private :unset_slug_if_invalid
|
data/lib/friendly_id/version.rb
CHANGED
data/test/schema.rb
CHANGED
@@ -110,3 +110,31 @@ class SequentiallySluggedTest < TestCaseClass
|
|
110
110
|
assert_nil record.slug
|
111
111
|
end
|
112
112
|
end
|
113
|
+
|
114
|
+
class SequentiallySluggedTestWithHistory < TestCaseClass
|
115
|
+
include FriendlyId::Test
|
116
|
+
include FriendlyId::Test::Shared::Core
|
117
|
+
|
118
|
+
class Article < ActiveRecord::Base
|
119
|
+
extend FriendlyId
|
120
|
+
friendly_id :name, :use => [:sequentially_slugged, :history]
|
121
|
+
end
|
122
|
+
|
123
|
+
def model_class
|
124
|
+
Article
|
125
|
+
end
|
126
|
+
|
127
|
+
test "should work with regeneration with history when slug already exists" do
|
128
|
+
transaction do
|
129
|
+
record1 = model_class.create! :name => "Test name"
|
130
|
+
record2 = model_class.create! :name => "Another test name"
|
131
|
+
assert_equal 'test-name', record1.slug
|
132
|
+
assert_equal 'another-test-name', record2.slug
|
133
|
+
|
134
|
+
record2.name = "Test name"
|
135
|
+
record2.slug = nil
|
136
|
+
record2.save!
|
137
|
+
assert_equal 'test-name-2', record2.slug
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
data/test/slugged_test.rb
CHANGED
@@ -110,6 +110,26 @@ class SluggedTest < TestCaseClass
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
|
+
test "should not set slug on create if unrelated validations fail with custom slug_column" do
|
114
|
+
klass = Class.new(ActiveRecord::Base) do
|
115
|
+
self.table_name = 'authors'
|
116
|
+
extend FriendlyId
|
117
|
+
validates_presence_of :active
|
118
|
+
friendly_id :name, :use => :slugged, :slug_column => :subdomain
|
119
|
+
|
120
|
+
def self.name
|
121
|
+
"Author"
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
transaction do
|
126
|
+
instance = klass.new :name => 'foo'
|
127
|
+
refute instance.save
|
128
|
+
refute instance.valid?
|
129
|
+
assert_nil instance.subdomain
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
113
133
|
test "should not update slug on save if unrelated validations fail" do
|
114
134
|
klass = Class.new model_class do
|
115
135
|
validates_presence_of :active
|
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.2.
|
4
|
+
version: 5.2.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: 2017-04-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|