Class: Ace::Review::Atoms::FeedbackSlugGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/review/atoms/feedback_slug_generator.rb

Overview

Generates URL-safe slugs from feedback item titles.

Creates slugs suitable for use in filenames and URLs, handling edge cases like unicode, special characters, and length limits.

Examples:

Basic usage

FeedbackSlugGenerator.generate("Missing error handling")
#=> "missing-error-handling"

Long titles are truncated

FeedbackSlugGenerator.generate("A very long title that exceeds forty characters limit")
#=> "a-very-long-title-that-exceeds-forty" (truncated to 40 chars)

Unicode is transliterated or removed

FeedbackSlugGenerator.generate("Fix bug in caf\u00e9 module")
#=> "fix-bug-in-caf-module"

Constant Summary collapse

DEFAULT_MAX_LENGTH =
40

Class Method Summary collapse

Class Method Details

.generate(title, max_length: DEFAULT_MAX_LENGTH) ⇒ String

Generate a URL-safe slug from a title

Examples:

Basic title

FeedbackSlugGenerator.generate("Fix authentication bug")
#=> "fix-authentication-bug"

With special characters

FeedbackSlugGenerator.generate("Add try/catch block (urgent!)")
#=> "add-try-catch-block-urgent"

Empty or nil input

FeedbackSlugGenerator.generate(nil)
#=> ""
FeedbackSlugGenerator.generate("")
#=> ""

Parameters:

  • title (String)

    The title to convert to a slug

  • max_length (Integer) (defaults to: DEFAULT_MAX_LENGTH)

    Maximum slug length (default: 40)

Returns:

  • (String)

    URL-safe slug



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ace/review/atoms/feedback_slug_generator.rb', line 45

def self.generate(title, max_length: DEFAULT_MAX_LENGTH)
  return "" if title.nil? || title.empty?

  title
    .unicode_normalize(:nfkd)           # Decompose unicode (e.g., é -> e + combining accent)
    .encode("ASCII", undef: :replace, replace: "") # Strip non-ASCII
    .gsub(/[^a-zA-Z0-9\-_\s]/, "")      # Remove special chars (keep spaces for now)
    .gsub(/[\s_]+/, "-").squeeze("-")                     # Collapse consecutive hyphens
    .gsub(/\A-|-\z/, "")                 # Remove leading/trailing hyphens
    .downcase
    .slice(0, max_length)                # Truncate to max length
    .gsub(/-\z/, "")                     # Remove trailing hyphen after truncation
end