Class: SlugValidator::InstanceValidator

Inherits:
Struct
  • Object
show all
Defined in:
app/validators/slug_validator.rb

Instance Method Summary collapse

Instance Method Details

#ends_with?(expected_suffix) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/validators/slug_validator.rb', line 24

def ends_with?(expected_suffix)
  value.to_s.end_with?(expected_suffix)
end

#of_kind?(expected_kind) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/validators/slug_validator.rb', line 28

def of_kind?(expected_kind)
  record.respond_to?(:kind) && [*expected_kind].include?(record.kind)
end

#starts_with?(expected_prefix) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/validators/slug_validator.rb', line 20

def starts_with?(expected_prefix)
  value.to_s.start_with?(expected_prefix)
end

#url_after_first_slashObject



32
33
34
# File 'app/validators/slug_validator.rb', line 32

def url_after_first_slash
  value.to_s.split('/', 2)[1]
end

#url_after_first_slash_is_valid_slug!Object



36
37
38
39
40
# File 'app/validators/slug_validator.rb', line 36

def url_after_first_slash_is_valid_slug!
  if !valid_slug?(url_after_first_slash)
    record.errors[attribute] << "must be usable in a url"
  end
end

#url_partsObject



42
43
44
# File 'app/validators/slug_validator.rb', line 42

def url_parts
  value.to_s.split("/")
end

#valid_slug?(url_part) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'app/validators/slug_validator.rb', line 46

def valid_slug?(url_part)
  # Regex taken from ActiveSupport::Inflector.parameterize
  # We don't want to use this method because it also does a number of cosmetic tidy-ups
  # which lead to false-positives (eg merging consecutive '-'s)
  ! url_part.to_s.match(/[^a-z0-9\-_]/)
end