Sha256: 2612475c2a5624eb6741ab10d04a7597b9b97665dd05b7191e1fb3bc68e5355a

Contents?: true

Size: 864 Bytes

Versions: 104

Compression:

Stored size: 864 Bytes

Contents

class TagIdValidator < ActiveModel::Validator

  def validate(record)
    unless valid_tag_id?(record)
      record.errors[:tag_id] << "ID must be valid in a URL and have no more than one slash"
    end
  end

private

  def valid_tag_id?(tag)
    tag_contains_correct_characters(tag.tag_id) &&
    tag_doesnt_end_with_slash(tag.tag_id) &&
    tag_contains_correct_number_of_slashes(tag.tag_id, child?: tag_is_child_tag?(tag))
  end

  def tag_contains_correct_characters(tag_id)
    tag_id =~ %r{^[a-z0-9/-]+$}
  end

  def tag_doesnt_end_with_slash(tag_id)
    !tag_id.end_with?('/')
  end

  def tag_contains_correct_number_of_slashes(tag_id, options = {})
    if options[:child?]
      tag_id.count('/') <= 1
    else
      tag_id.count('/') == 0
    end
  end

  def tag_is_child_tag?(tag)
    tag.respond_to?(:parent_id) && tag.parent_id.present?
  end

end

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
govuk_content_models-41.1.1 app/validators/tag_id_validator.rb
govuk_content_models-41.1.0 app/validators/tag_id_validator.rb
govuk_content_models-41.0.0 app/validators/tag_id_validator.rb
govuk_content_models-40.0.0 app/validators/tag_id_validator.rb
govuk_content_models-39.0.0 app/validators/tag_id_validator.rb
govuk_content_models-38.0.0 app/validators/tag_id_validator.rb
govuk_content_models-37.0.0 app/validators/tag_id_validator.rb
govuk_content_models-36.0.0 app/validators/tag_id_validator.rb
govuk_content_models-35.0.1 app/validators/tag_id_validator.rb
govuk_content_models-35.0.0 app/validators/tag_id_validator.rb
govuk_content_models-34.0.0 app/validators/tag_id_validator.rb
govuk_content_models-33.0.0 app/validators/tag_id_validator.rb
govuk_content_models-32.3.1 app/validators/tag_id_validator.rb
govuk_content_models-32.3.0 app/validators/tag_id_validator.rb
govuk_content_models-32.2.0 app/validators/tag_id_validator.rb
govuk_content_models-32.1.0 app/validators/tag_id_validator.rb
govuk_content_models-32.0.0 app/validators/tag_id_validator.rb
govuk_content_models-31.4.0 app/validators/tag_id_validator.rb
govuk_content_models-31.3.0 app/validators/tag_id_validator.rb
govuk_content_models-31.2.2 app/validators/tag_id_validator.rb