Module: PostValidations
- Extended by:
- ActiveSupport::Concern
- Included in:
- Post, SuggestedEdit
- Defined in:
- app/models/concerns/post_validations.rb
Overview
Validations for posts which are shared between posts and suggested edits.
Instance Method Summary collapse
- #maximum_tag_length ⇒ Object
- #maximum_tags ⇒ Object
- #maximum_title_length ⇒ Object
- #no_spaces_in_tags ⇒ Object
- #required_tags? ⇒ Boolean
- #stripped_minimum_body ⇒ Object
- #stripped_minimum_title ⇒ Object
- #tags_in_tag_set ⇒ Object
Instance Method Details
#maximum_tag_length ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'app/models/concerns/post_validations.rb', line 24 def maximum_tag_length .each do |tag| max_len = SiteSetting['MaxTagLength'] if tag.length > max_len errors.add(:tags, "can't be more than #{max_len} characters long each") end end end |
#maximum_tags ⇒ Object
16 17 18 19 20 21 22 |
# File 'app/models/concerns/post_validations.rb', line 16 def if .length > 5 errors.add(:base, "Post can't have more than 5 tags.") elsif .empty? errors.add(:base, 'Post must have at least one tag.') end end |
#maximum_title_length ⇒ Object
62 63 64 65 66 67 |
# File 'app/models/concerns/post_validations.rb', line 62 def maximum_title_length max_title_len = SiteSetting['MaxTitleLength'] if title.length > [(max_title_len || 255), 255].min errors.add(:title, "can't be more than #{max_title_len} characters") end end |
#no_spaces_in_tags ⇒ Object
33 34 35 36 37 38 39 |
# File 'app/models/concerns/post_validations.rb', line 33 def .each do |tag| if tag.include?(' ') || tag.include?('_') errors.add(:tags, 'may not include spaces or underscores - use hyphens for multiple-word tags') end end end |
#required_tags? ⇒ Boolean
76 77 78 79 80 81 82 83 |
# File 'app/models/concerns/post_validations.rb', line 76 def required = category&.required_tag_ids return unless required.present? && !required.empty? unless tag_ids.any? { |t| required.include? t } errors.add(:tags, "must contain at least one required tag (#{category..pluck(:name).join(', ')})") end end |
#stripped_minimum_body ⇒ Object
41 42 43 44 45 46 |
# File 'app/models/concerns/post_validations.rb', line 41 def stripped_minimum_body min_body = category.nil? ? 30 : category.min_body_length if (body_markdown&.gsub(/(?:^[\s\t\u2000-\u200F]+|[\s\t\u2000-\u200F]+$)/, '')&.length || 0) < min_body errors.add(:body, "must be more than #{min_body} non-whitespace characters long") end end |
#stripped_minimum_title ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/models/concerns/post_validations.rb', line 48 def stripped_minimum_title min_title = if ['HelpDoc', 'PolicyDoc'].include?(post_type.name) 1 elsif category.nil? 15 else category.min_title_length end if (title&.gsub(/(?:^[\s\t\u2000-\u200F]+|[\s\t\u2000-\u200F]+$)/, '')&.length || 0) < min_title errors.add(:title, "must be more than #{min_title} non-whitespace characters long") end end |
#tags_in_tag_set ⇒ Object
69 70 71 72 73 74 |
# File 'app/models/concerns/post_validations.rb', line 69 def tag_set = category.tag_set unless .all? { |t| t.tag_set_id == tag_set.id } errors.add(:base, "Not all of this question's tags are in the correct tag set.") end end |