Skip to content

Commit

Permalink
Use last candidate slug
Browse files Browse the repository at this point in the history
Add fix when used which History thanks to @kylefleming
  • Loading branch information
Gil Vandendriessche committed Oct 6, 2016
1 parent aff0564 commit 70f99e5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/friendly_id/sequentially_slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ def self.setup(model_class)
end

def resolve_friendly_id_conflict(candidate_slugs)
candidate = candidate_slugs.first
candidate = candidate_slugs.to_a.last
return if candidate.nil?
SequentialSlugCalculator.new(scope_for_slug_generator,
candidate,
friendly_id_config.slug_column,
friendly_id_config.sequence_separator).next_slug
friendly_id_config.sequence_separator,
self.class.base_class).next_slug
end

class SequentialSlugCalculator
attr_accessor :scope, :slug, :slug_column, :sequence_separator

def initialize(scope, slug, slug_column, sequence_separator)
def initialize(scope, slug, slug_column, sequence_separator, base_class)
@scope = scope
@slug = slug
@slug_column = scope.connection.quote_column_name(slug_column)
table_name = scope.connection.quote_table_name(base_class.arel_table.name)
@slug_column = "#{table_name}.#{scope.connection.quote_column_name(slug_column)}"
@sequence_separator = sequence_separator
end

Expand Down
28 changes: 28 additions & 0 deletions test/sequentially_slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,31 @@ def slug_candidates
assert_nil record.slug
end
end

class SequentiallySluggedTestWithHistory < TestCaseClass
include FriendlyId::Test
include FriendlyId::Test::Shared::Core

class Article < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => [:sequentially_slugged, :history]
end

def model_class
Article
end

test "should work with regeneration with history when slug already exists" do
transaction do
record1 = model_class.create! :name => "Test name"
record2 = model_class.create! :name => "Another test name"
assert_equal 'test-name', record1.slug
assert_equal 'another-test-name', record2.slug

record2.name = "Test name"
record2.slug = nil
record2.save!
assert_equal 'test-name-2', record2.slug
end
end
end

0 comments on commit 70f99e5

Please sign in to comment.