Class: ComponentValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/decidim/component_validator.rb

Overview

A custom validator to make sure components are correctly assigned to a model.

Adds a presence validation and checks that the manifest is the correct one.

Instance Method Summary collapse

Instance Method Details

#check_validity!Object

Validates the arguiments passed to the validator.

Raises:

  • (ArgumentError)


8
9
10
# File 'lib/decidim/component_validator.rb', line 8

def check_validity!
  raise ArgumentError, "You must include a `manifest` option with the name of the manifest to validate when validating a component" if options[:manifest].blank?
end

#validate_each(record, attribute, component) ⇒ Object

The actual validator method. It is called when ActiveRecord iterates over all the validators.



14
15
16
17
18
19
20
21
# File 'lib/decidim/component_validator.rb', line 14

def validate_each(record, attribute, component)
  unless component
    record.errors[attribute] << :blank
    return
  end

  record.errors[attribute] << :invalid if component.manifest_name.to_s != options[:manifest].to_s
end