class ComponentValidator

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.

Public Instance Methods

check_validity!() click to toggle source

Validates the arguiments passed to the validator.

# 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) click to toggle source

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

# 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