Merge commit '425d77f8124a50fc033e8fb3bdf7b89a6a25f4fa' into glitch-soc/merge-upstream

Conflicts:
- `.rubocop_todo.yml`:
  Upstream regenerated this file, glitch-soc had a specific ignore.
- `README.md`:
  Upstream updated its README, but glitch-soc has a completely different one.
  Kept glitch-soc's README
This commit is contained in:
Claire
2023-08-11 22:15:41 +02:00
130 changed files with 1818 additions and 1188 deletions

View File

@@ -4,18 +4,20 @@ class LanguageValidator < ActiveModel::EachValidator
include LanguagesHelper
def validate_each(record, attribute, value)
record.errors.add(attribute, :invalid) unless valid?(value)
@value = value
record.errors.add(attribute, :invalid) unless valid_locale_value?
end
private
def valid?(str)
if str.nil?
def valid_locale_value?
if @value.nil?
true
elsif str.is_a?(Array)
str.all? { |x| valid_locale?(x) }
elsif @value.is_a?(Array)
@value.all? { |x| valid_locale?(x) }
else
valid_locale?(str)
valid_locale?(@value)
end
end
end

View File

@@ -45,7 +45,7 @@ class StatusLengthValidator < ActiveModel::Validator
def rewrite_entities(str, entities)
entities.sort_by! { |entity| entity[:indices].first }
result = ''.dup
result = +''
last_index = entities.reduce(0) do |index, entity|
result << str[index...entity[:indices].first]

View File

@@ -1,16 +1,31 @@
# frozen_string_literal: true
class URLValidator < ActiveModel::EachValidator
VALID_SCHEMES = %w(http https).freeze
def validate_each(record, attribute, value)
record.errors.add(attribute, :invalid) unless compliant?(value)
@value = value
record.errors.add(attribute, :invalid) unless compliant_url?
end
private
def compliant?(url)
parsed_url = Addressable::URI.parse(url)
parsed_url && %w(http https).include?(parsed_url.scheme) && parsed_url.host
def compliant_url?
parsed_url.present? && valid_url_scheme? && valid_url_host?
end
def parsed_url
Addressable::URI.parse(@value)
rescue Addressable::URI::InvalidURIError
false
end
def valid_url_scheme?
VALID_SCHEMES.include?(parsed_url.scheme)
end
def valid_url_host?
parsed_url.host.present?
end
end