discourse/lib/validators/topic_title_length_validator.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

25 lines
618 B
Ruby

# frozen_string_literal: true
class TopicTitleLengthValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
title_validator(record).validate_each(record, attribute, value)
end
private
def title_validator(record)
length_range =
if record.user.try(:admin?)
1..SiteSetting.max_topic_title_length
elsif record.private_message?
SiteSetting.private_message_title_length
else
SiteSetting.topic_title_length
end
ActiveModel::Validations::LengthValidator.new(attributes: :title, in: length_range, allow_blank: true)
end
end