2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-04 16:58:25 -05:00
|
|
|
class TopicTitleLengthValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2013-06-29 16:57:10 -05:00
|
|
|
title_validator(record).validate_each(record, attribute, value)
|
2013-06-04 16:58:25 -05:00
|
|
|
end
|
2013-06-29 16:57:10 -05:00
|
|
|
|
|
|
|
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
|
2013-06-04 16:58:25 -05:00
|
|
|
end
|