31 lines
586 B
Ruby
31 lines
586 B
Ruby
module Tournaments
|
|
class GenerateSlug
|
|
def self.call(tournament)
|
|
new(tournament).call
|
|
end
|
|
|
|
def initialize(tournament)
|
|
@tournament = tournament
|
|
end
|
|
|
|
def call
|
|
base = @tournament.name.to_s.parameterize.presence || "torneo"
|
|
slug = base
|
|
n = 2
|
|
while conflict?(slug)
|
|
slug = "#{base}-#{n}"
|
|
n += 1
|
|
end
|
|
slug
|
|
end
|
|
|
|
private
|
|
|
|
def conflict?(slug)
|
|
scope = Tournament.where(slug: slug)
|
|
scope = scope.where.not(id: @tournament.id) if @tournament.persisted?
|
|
scope.exists?
|
|
end
|
|
end
|
|
end
|