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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 606af0800382602b2f88d11be3a839a3e8c23255
4
- data.tar.gz: a501dd78829d595fe897475bdf4a6c6d98caee9c
3
+ metadata.gz: 67c7eb649e6e0fa756ebcfdbae36f276d7bc6adc
4
+ data.tar.gz: '093984f88a0e674471260a9989673190d48c5b22'
5
5
  SHA512:
6
- metadata.gz: cb2afe89084b532541152c46056649ec6bfca0a2fe51493706f7f996ee233bae010a248a735fb55451e89b1918b29e60b5471e78c0272f29a5c5c410220eacc8
7
- data.tar.gz: 8a527f735585ce88c1627bc54f206745f69a826d27574f6d306ee4a29dff23421e82225b0b9e9751cdd5a3a9911f3dba755d10d7a9951758052a3bd869b26441
6
+ metadata.gz: 9039e1625fed0fcc1001b9b0b97cf51f6205aa103211e137ca4db834aa75f18c37a98a54e3030f42d8bb0f83489683d1fec8dce6cc2650d763ee4fb0b5e4c033
7
+ data.tar.gz: 120aa54db11b436a357122cfd0a53c39bba48c4724d89ce100e5a6dbf8dbec1db6955ebd3c2b118e8c0366505732b8c4e12100e44b40b9fc422904b30a48d29b
@@ -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
@@ -179,7 +179,7 @@ rake db:migrate
179
179
  ```
180
180
  ```ruby
181
181
  # edit app/models/user.rb
182
- class User < ActiveRecord::Base
182
+ class User < ApplicationRecord
183
183
  extend FriendlyId
184
184
  friendly_id :name, use: :slugged
185
185
  end
@@ -10,26 +10,35 @@ module FriendlyId
10
10
 
11
11
  def initialize(object, *array)
12
12
  @object = object
13
- @candidates = to_candidate_array(object, array.flatten(1))
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
- pre_candidates = @candidates.map do |candidate|
20
- @object.normalize_friendly_id(candidate.map(&:call).join(' '))
21
- end.select {|x| wanted?(x)}
17
+ return candidates unless block_given?
18
+ candidates.each{ |candidate| yield candidate }
19
+ end
22
20
 
23
- unless pre_candidates.all? {|x| reserved?(x)}
24
- pre_candidates.reject! {|x| reserved?(x)}
25
- end
21
+ private
26
22
 
27
- return pre_candidates unless block_given?
23
+ def candidates
24
+ @candidates ||= begin
25
+ candidates = normalize(@raw_candidates)
26
+ filter(candidates)
27
+ end
28
+ end
28
29
 
29
- pre_candidates.each {|x| yield x}
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
- private
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).next_slug
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
- @slug_column = scope.connection.quote_column_name(slug_column)
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
- if match = /#{slug}#{sequence_separator}(\d+)\z/.match(slug_conflicts.last)
38
- match[1].to_i
39
- end
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
@@ -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
- self.slug = diff.first
332
+ send "#{friendly_id_config.slug_column}=", diff.first
333
333
  end
334
334
  end
335
335
  private :unset_slug_if_invalid
@@ -1,3 +1,3 @@
1
1
  module FriendlyId
2
- VERSION = "5.2.0"
2
+ VERSION = "5.2.1"
3
3
  end
@@ -67,6 +67,9 @@ module FriendlyId
67
67
  # Used to test candidates
68
68
  add_column :cities, :code, :string, :limit => 3
69
69
 
70
+ # Used as a non-default slug_column
71
+ add_column :authors, :subdomain, :string
72
+
70
73
  @done = true
71
74
  end
72
75
 
@@ -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
@@ -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.0
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: 2016-12-01 00:00:00.000000000 Z
12
+ date: 2017-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord