Class: Spree::Validators

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/validators.rb

Overview

Registry of extra validator classes run against a model on top of its own validations. Addresses are the one model that ships with it, because address rules are regional and business-specific — phone formats, VAT numbers, address verification — and a host needs to add and remove them without reopening the model.

Spree.validators.addresses.register(MyStore::PostBoxValidator)
Spree.validators.addresses.unregister(Spree::Addresses::PhoneValidator)

Register from config.to_prepare (not an initializer) when the validator is your own class: the set holds classes, so a reloadable constant registered once at boot would go stale on the next reload.

For rules that veto a whole flow rather than shape a record, use a workflow :validate hook instead (see Spree::Hooks).

Defined Under Namespace

Classes: Set

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Validators

Returns a new instance of Validators.



54
55
56
# File 'lib/spree/validators.rb', line 54

def initialize
  @sets = Hash.new { |sets, name| sets[name] = Set.new }
end

Instance Method Details

#[](name) ⇒ Object



58
59
60
# File 'lib/spree/validators.rb', line 58

def [](name)
  @sets[name.to_sym]
end

#[]=(name, entries) ⇒ Object



62
63
64
# File 'lib/spree/validators.rb', line 62

def []=(name, entries)
  @sets[name.to_sym] = Set.new(entries)
end

#addresses ⇒ Object



66
67
68
# File 'lib/spree/validators.rb', line 66

def addresses
  self[:addresses]
end

#addresses=(entries) ⇒ Object



70
71
72
# File 'lib/spree/validators.rb', line 70

def addresses=(entries)
  self[:addresses] = entries
end