Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set size limit for slug #809

Merged
merged 3 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/friendly_id/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ module Base
# Configures the name of the column where FriendlyId will store the slug.
# Defaults to `:slug`.
#
# @option options [Integer] :slug_limit Available when using `:slugged`.
# Configures the limit of the slug. This option has no default value.
#
# @option options [Symbol] :slug_generator_class Available when using `:slugged`.
# Sets the class used to generate unique slugs. You should not specify this
# unless you're doing some extensive hacking on FriendlyId. Defaults to
Expand Down
4 changes: 4 additions & 0 deletions lib/friendly_id/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
#
# config.slug_column = 'slug'
#
# By default, slug has no size limit, but you can change it if you wish.
#
# config.slug_limit = 255
#
# When FriendlyId can not generate a unique ID from your base method, it appends
# a UUID, separated by a single dash. You can configure the character used as the
# separator. If you're upgrading from FriendlyId 4, you may wish to replace this
Expand Down
24 changes: 19 additions & 5 deletions lib/friendly_id/slugged.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ def self.included(model_class)
# @param [#to_s] value The value used as the basis of the slug.
# @return The candidate slug text, without a sequence.
def normalize_friendly_id(value)
value.to_s.parameterize
value = value.to_s.parameterize
value = value[0...friendly_id_config.slug_limit] if friendly_id_config.slug_limit
value
end

# Whether to generate a new slug.
Expand All @@ -300,7 +302,14 @@ def should_generate_new_friendly_id?
end

def resolve_friendly_id_conflict(candidates)
[candidates.first, SecureRandom.uuid].compact.join(friendly_id_config.sequence_separator)
candidate = candidates.first
uid = SecureRandom.uuid
if candidate && friendly_id_config.slug_limit
max_candidate_size = friendly_id_config.slug_limit - uid.size - friendly_id_config.sequence_separator.size
max_candidate_size = [max_candidate_size, 0].max
candidate = candidate[0...max_candidate_size]
end
[candidate, uid].compact.join(friendly_id_config.sequence_separator)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK just one last comment. I would use the 'extract method' refactor here:

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

def apply_slug_limit(candidate, uuid)
  return candidate unless candidate && friendly_id_config.slug_limit
  
  candidate[0...slug_limit(uuid)]
end
private :apply_slug_limit

def slug_limit(uuid)
  [
    friendly_id_config.slug_limit - uuid.size - friendly_id_config.sequence_separator.size,
    0
  ].max
end
private :max_candidate_size_for_uuid

I haven't tried the code, but would something along those lines work?
My rationale is that it just makes the original method easier to reason about rather than having to read the code a few times to figure out what's going on with the conditional. It also means others can use this functionality without rewriting it if they override resolve_friendly_id_conflict. 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may even be a simpler version waiting to be found, but I wanted to communicate my thoughts with Ruby code. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, looks much better 👍

end

# Sets the slug.
Expand Down Expand Up @@ -334,11 +343,11 @@ def unset_slug_if_invalid
end
private :unset_slug_if_invalid

# This module adds the `:slug_column`, and `:sequence_separator`, and
# `:slug_generator_class` configuration options to
# This module adds the `:slug_column`, and `:slug_limit`, and `:sequence_separator`,
# and `:slug_generator_class` configuration options to
# {FriendlyId::Configuration FriendlyId::Configuration}.
module Configuration
attr_writer :slug_column, :sequence_separator
attr_writer :slug_column, :slug_limit, :sequence_separator
attr_accessor :slug_generator_class

# Makes FriendlyId use the slug column for querying.
Expand All @@ -361,6 +370,11 @@ def sequence_separator
def slug_column
@slug_column ||= defaults[:slug_column]
end

# The limit that will be used for slug.
def slug_limit
@slug_limit ||= defaults[:slug_limit]
end
end
end
end
27 changes: 26 additions & 1 deletion test/slugged_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,31 @@ def self.name

end

class SlugLimitTest < TestCaseClass

include FriendlyId::Test

class Journalist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :slugged, :slug_limit => 40
end

def model_class
Journalist
end

test "should limit slug size" do
transaction do
m1 = model_class.create! :name => 'a' * 50
assert_equal m1.slug, 'a' * 40
m2 = model_class.create! :name => m1.name
m2.save!
# "aaa-<uid>"
assert_match(/\Aa{3}\-/, m2.slug)
end
end
end

class DefaultScopeTest < TestCaseClass

include FriendlyId::Test
Expand Down Expand Up @@ -424,4 +449,4 @@ class Novel < ActiveRecord::Base
assert_equal novel.id.to_s, novel.to_param
end
end
end
end