Skip to content

Commit

Permalink
Merge pull request norman#571 from xymbol/fix-blank-slugs
Browse files Browse the repository at this point in the history
Fix blank slugs
  • Loading branch information
norman committed Jul 2, 2014
2 parents fdbe416 + 194a3e4 commit dd4af0f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
15 changes: 10 additions & 5 deletions lib/friendly_id/candidates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ def initialize(object, *array)
@candidates = to_candidate_array(object, array.flatten(1))
end


# Visits each slug candidate, calls it, passes it to `normalize_friendly_id` and yields the result.
# Visits each candidate, calls it, passes it to `normalize_friendly_id` and
# yields the wanted slug candidates.
def each(*args, &block)
@candidates.each(*args) do |candidate|
yield @object.normalize_friendly_id(candidate.map(&:call).join(' '))
@candidates.map do |candidate|
slug = @object.normalize_friendly_id(candidate.map(&:call).join(' '))
yield slug if wanted?(slug)
end
end

Expand All @@ -41,5 +42,9 @@ def to_candidate_array(object, array)
end
end
end

def wanted?(slug)
!slug.blank?
end
end
end
end
10 changes: 3 additions & 7 deletions lib/friendly_id/slug_generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FriendlyId
# The default slug generator offers functionality to check slug strings for
# uniqueness and, if necessary, appends a sequence to guarantee it.
# The default slug generator offers functionality to check slug candidates for
# availability.
class SlugGenerator

def initialize(scope)
Expand All @@ -11,12 +11,8 @@ def available?(slug)
!@scope.exists_by_friendly_id?(slug)
end

def add(slug)
slug
end

def generate(candidates)
candidates.each {|c| return add c if available?(c)}
candidates.each {|c| return c if available?(c)}
nil
end

Expand Down
2 changes: 1 addition & 1 deletion lib/friendly_id/slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def should_generate_new_friendly_id?
end

def resolve_friendly_id_conflict(candidates)
candidates.first + friendly_id_config.sequence_separator + SecureRandom.uuid
[candidates.first, SecureRandom.uuid].compact.join(friendly_id_config.sequence_separator)
end

# Sets the slug.
Expand Down
16 changes: 11 additions & 5 deletions test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ def self.name
end

test "should not update matching slug" do
with_instance_of(model_class) do |record|
class << record
def should_generate_new_friendly_id?
name_changed?
end
klass = Class.new model_class do
def should_generate_new_friendly_id?
name_changed?
end
end
with_instance_of klass do |record|
old_id = record.friendly_id
record.name += " "
record.save!
Expand Down Expand Up @@ -202,6 +202,12 @@ def self.name
end
end

test "should sequence blank slugs without a separator" do
with_instance_of model_class, :name => "" do |record|
assert_match(/\A([a-z0-9]+\-){4}[a-z0-9]+\z/, record.slug)
end
end

end

class DefaultScopeTest < MiniTest::Unit::TestCase
Expand Down

0 comments on commit dd4af0f

Please sign in to comment.