Class: TypedEAV::Field::Url

Inherits:
ValidatedString show all
Defined in:
app/models/typed_eav/field/url.rb

Overview

URL-typed field. Inherits string_value storage shape, the min_length / max_length / pattern store_accessor, the max_gte_min_length guard, and the validate_pattern_syntax guard from Field::ValidatedString. Adds the URL_FORMAT regex (http/https only) and layers the format check onto validate_typed_value via super.

Latent-bug fix (per ADR-0004): max_gte_min_length (previously only on Text) now applies here — a Url field configured with max_length < min_length fails at field-save.

Constant Summary collapse

URL_FORMAT =
/\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/

Constants inherited from Base

Base::RESERVED_NAMES

Constants included from TypedStorage

TypedStorage::DEFAULT_OPERATORS_BY_COLUMN, TypedStorage::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods inherited from Base

#allowed_option_values, #array_field?, #backfill_default!, #clear_option_cache!, #default_value, #default_value=, #display_name, export_schema, #field_type_name, import_schema, #insert_at, #move_higher, #move_lower, #move_to_bottom, #move_to_top, #optionable?

Methods included from TypedStorage

#after_snapshot, #apply_default, #before_snapshot, #read_value, #value_changed?, #write_value

Instance Method Details

#cast(raw) ⇒ Object



24
25
26
# File 'app/models/typed_eav/field/url.rb', line 24

def cast(raw)
  [raw&.to_s&.strip, false]
end

#url_format_valid?(val) ⇒ Boolean

Returns:



28
29
30
# File 'app/models/typed_eav/field/url.rb', line 28

def url_format_valid?(val)
  URL_FORMAT.match?(val)
end

#validate_typed_value(record, val) ⇒ Object



32
33
34
35
# File 'app/models/typed_eav/field/url.rb', line 32

def validate_typed_value(record, val)
  super
  record.errors.add(:value, "is not a valid URL") unless url_format_valid?(val)
end